Exemple #1
0
        public void EncodeNormalFeatureH5(string fileName)
        {
            dynamic      h5py = Py.Import("h5py");
            StreamReader file = new StreamReader(fileName);
            string       line = null;

            PyList[] features = new PyList[9];
            for (int i = 0; i < 9; i++)
            {
                features[i] = new PyList();
            }
            PyList resultList = new PyList();
            int    count      = 0;

            string[] featureNames = null;
            while ((line = file.ReadLine()) != null)
            {
                GameRecord          gameRecord = JsonConvert.DeserializeObject <GameRecord>(line);
                List <StateKeyInfo> playSec    = new List <StateKeyInfo>();
                foreach (StateKeyInfo stKeyInfo in gameRecord.playSec)
                {
                    int          result          = (stKeyInfo.attackPlayer.turn == gameRecord.result) ? 1 : 0;
                    StateFeature normalFeature   = Featurization.normalFeaturization(stKeyInfo);
                    Feature      playableFeature = normalFeature.featrueArray[5];
                    PyList[]     featureList     = normalFeature.getPyFeatureData();
                    if (featureNames == null)
                    {
                        featureNames = normalFeature.getFeatureNames();
                    }
                    for (int i = 0; i < 9; i++)
                    {
                        PythonUtils.AppendRecycle(features[i], featureList[i]);
                    }
                    PythonUtils.AppendRecycle(resultList, FeatureConst.Instance.pyIntMap[result]);
                }
            }
            string  outFileName = fileName + "norm.hdf5";
            dynamic outFile     = h5py.File(outFileName, "w");

            for (int i = 0; i < 9; i++)
            {
                outFile.create_dataset(featureNames[i], Py.kw("data", features[i]));
            }
            outFile.close();
        }
Exemple #2
0
        public void PerformanceTest(string fileName)
        {
            dynamic      np   = Py.Import("numpy");
            StreamReader file = new StreamReader(fileName);
            string       line = null;

            PyList[] features = new PyList[9];
            for (int i = 0; i < 9; i++)
            {
                features[i] = new PyList();
            }
            //PyTuple tp1 = new PyTuple(new PyObject[] { FeatureConst.Instance.pyIntMap[1], FeatureConst.Instance.pyIntMap[26) });
            //PyTuple tp2 = new PyTuple(new PyObject[] { FeatureConst.Instance.pyIntMap[1), FeatureConst.Instance.pyIntMap[19 * 17 * 5)});
            //PyTuple tp3 = new PyTuple(new PyObject[] { FeatureConst.Instance.pyIntMap[1), FeatureConst.Instance.pyIntMap[9*46)});
            //PyTuple tp4 = new PyTuple(new PyObject[] { FeatureConst.Instance.pyIntMap[1), FeatureConst.Instance.pyIntMap[46) });
            while ((line = file.ReadLine()) != null)
            {
                GameRecord          gameRecord = JsonConvert.DeserializeObject <GameRecord>(line);
                List <StateKeyInfo> playSec    = new List <StateKeyInfo>();
                foreach (StateKeyInfo stKeyInfo in gameRecord.playSec)
                {
                    PyList        resultList = new PyList();
                    PlayerKeyInfo p1Info     = stKeyInfo.attackPlayer;
                    PlayerKeyInfo p2Info     = stKeyInfo.defensePlayer;

                    bool isOwnTurn = p1Info.turn == 0 ? true : false;

                    Playfield tempPf = null;
                    if (isOwnTurn)
                    {
                        tempPf = new Playfield(stKeyInfo.nextEntity, isOwnTurn, p1Info, p2Info);
                    }
                    else
                    {
                        tempPf = new Playfield(stKeyInfo.nextEntity, isOwnTurn, p2Info, p1Info);
                    }

                    var watch = System.Diagnostics.Stopwatch.StartNew();
                    for (int j = 0; j < 10000; j++)
                    {
                        StateFeature interFeature = Featurization.interactionFeaturization(tempPf);
                        Movegenerator.Instance.getMoveList(tempPf, false, true, true, 0.0);
                    }
                    watch.Stop();
                    var elapsedMs = watch.ElapsedMilliseconds;
                    Helpfunctions.Instance.logg("ElapsedMilliseconds for 1000 Featurization" + elapsedMs);

                    dynamic encode = null;
                    watch = System.Diagnostics.Stopwatch.StartNew();
                    for (int j = 0; j < 10000; j++)
                    {
                        encode = DNNEval.Instance.parsePlayfieldCNNAction(tempPf, tempPf, tempPf.isOwnTurn);
                    }
                    watch.Stop();
                    elapsedMs = watch.ElapsedMilliseconds;
                    Helpfunctions.Instance.logg("ElapsedMilliseconds for 1000 encoding" + elapsedMs);

                    //dynamic global_ft = np.zeros(tp1, Py.kw("dtype", "float32"));
                    //dynamic board_ft = np.zeros(tp2, Py.kw("dtype", "float32"));
                    //dynamic hand_ft = np.zeros(tp3, Py.kw("dtype", "float32"));
                    //dynamic play_ft = np.zeros(tp4, Py.kw("dtype", "float32"));
                    dynamic ft = new PyList();
                    //PythonUtils.AppendRecycle(ft, global_ft);
                    //PythonUtils.AppendRecycle(ft, board_ft);
                    //PythonUtils.AppendRecycle(ft, hand_ft);
                    //PythonUtils.AppendRecycle(ft, play_ft);
                    encode = new PyList();
                    PyList ftidx = new PyList();
                    ftidx.Append(FeatureConst.Instance.pyIntMap[1]);

                    encode.Append(ftidx);
                    encode.Append(ft);

                    watch = System.Diagnostics.Stopwatch.StartNew();
                    for (int j = 0; j < 10000; j++)
                    {
                        DNNEval.Instance.PredictTest(encode);
                    }
                    watch.Stop();
                    elapsedMs = watch.ElapsedMilliseconds;
                    Helpfunctions.Instance.logg("ElapsedMilliseconds for 1000 NNEval" + elapsedMs);
                    resultList.Dispose();
                }
            }
        }
Exemple #3
0
        public void EncodeInteractionFeature(string fileName)
        {
            dynamic      h5py = Py.Import("h5py");
            StreamReader file = new StreamReader(fileName);

            PyList[] features = new PyList[6];
            for (int i = 0; i < 6; i++)
            {
                features[i] = new PyList();
            }
            PyList targetList = new PyList();
            string line       = null;

            string[] featureNames = null;
            int      count        = 0;

            while ((line = file.ReadLine()) != null)
            {
                GameRecord          gameRecord = JsonConvert.DeserializeObject <GameRecord>(line);
                List <StateKeyInfo> playSec    = new List <StateKeyInfo>();

                int lastEntity = 1000;
                foreach (StateKeyInfo stKeyInfo in gameRecord.playSec)
                {
                    int           result = (stKeyInfo.attackPlayer.turn == gameRecord.result) ? 1 : 0;
                    PlayerKeyInfo p1Info = stKeyInfo.attackPlayer;
                    PlayerKeyInfo p2Info = stKeyInfo.defensePlayer;

                    bool isOwnTurn = p1Info.turn == 0 ? true : false;

                    Playfield tempPf = null;
                    if (isOwnTurn)
                    {
                        tempPf = new Playfield(lastEntity, isOwnTurn, p1Info, p2Info);
                    }
                    else
                    {
                        tempPf = new Playfield(lastEntity, isOwnTurn, p2Info, p1Info);
                    }

                    Player       mPlayer      = tempPf.getCurrentPlayer(true);
                    Player       ePlayer      = tempPf.getCurrentPlayer(false);
                    StateFeature interFeature = Featurization.interactionFeaturization(tempPf);

                    foreach (PlayerKeyInfo.ActionKeyInfo actionKeyInfo in p1Info.playedActionJsonList)
                    {
                        Action action = CreateActionFromInfo(tempPf, actionKeyInfo);
                        int    target = 0;
                        if (action.actionType == actionEnum.playcard)
                        {
                            target = FeatureConst.Instance.cardIdxDict[action.card.card.name];
                        }
                        else if (action.actionType == actionEnum.useHeroPower)
                        {
                            target = FeatureConst.Instance.cardIdxDict[CardDB.cardName.fireblast];
                        }
                        tempPf.getNextEntity();
                        tempPf.doAction(action);
                        PyList[] featureList = interFeature.getPyData();
                        if (featureNames == null)
                        {
                            featureNames = interFeature.getFeatureNames();
                        }
                        for (int i = 0; i < interFeature.numFeatures; i++)
                        {
                            PythonUtils.AppendRecycle(features[i], featureList[i]);
                        }
                        PythonUtils.AppendRecycle(targetList, FeatureConst.Instance.pyIntMap[target]);
                        interFeature = Featurization.interactionFeaturization(tempPf);
                    }
                    lastEntity = tempPf.getNextEntity() + 1;
                }
                count++;
                if (count % 1000 == 0)
                {
                    Console.WriteLine(count);
                }
            }
            string  outFileName = fileName + "inter.hdf5";
            dynamic outFile     = h5py.File(outFileName, "w");

            for (int i = 0; i < features.Length; i++)
            {
                outFile.create_dataset(featureNames[i], Py.kw("data", features[i]));
            }
            outFile.create_dataset("Target", Py.kw("data", targetList));
            outFile.close();
        }
Exemple #4
0
        //4:0; 3:0; 2:0; 4:1; 3:1; 4:2; 2:1, 3:2; 4:3; 1:0
        //4:4; 3:3, 2:2, 1:1, 0.0
        //reverse line 1

        public static StateFeature normalFeaturization(StateKeyInfo stKeyInfo)
        {
            PlayerKeyInfo mPlayer = stKeyInfo.attackPlayer;
            PlayerKeyInfo ePlayer = stKeyInfo.defensePlayer;

            StateFeature ret = new StateFeature(9);

            ret.featrueArray[0]         = new Feature("Global", new int[4]); //ownhp, enemyhp, ownmana, enemymana;
            ret.featrueArray[0].data[0] = mPlayer.heroInfo.Hp;
            ret.featrueArray[0].data[1] = mPlayer.maxMana;
            ret.featrueArray[0].data[2] = ePlayer.heroInfo.Hp;
            ret.featrueArray[0].data[3] = ePlayer.maxMana;

            ret.featrueArray[1] = new Feature("OwnMinionHp", new int[FeatureConst.Instance.minionCardArray.Length]);
            ret.featrueArray[2] = new Feature("OwnMinion", new int[FeatureConst.Instance.minionCardArray.Length]);

            foreach (PlayerKeyInfo.MinionKeyInfo m in mPlayer.minionJsonList)
            {
                int idx = FeatureConst.Instance.cardStrIdxDict[m.name];
                ret.featrueArray[1].data[idx] = Math.Max(ret.featrueArray[1].data[idx], m.Hp);
                ret.featrueArray[2].data[idx]++;
            }

            ret.featrueArray[3] = new Feature("OwnCardFeature", new int[FeatureConst.Instance.cardArray.Length]);
            foreach (PlayerKeyInfo.CardKeyInfo hc in mPlayer.handcardJsonList)
            {
                int idx = FeatureConst.Instance.cardStrIdxDict[hc.cardName];
                ret.featrueArray[3].data[idx]++;
            }

            ret.featrueArray[4] = new Feature("OwnPlayedFeature", new int[10]);
            ret.featrueArray[5] = new Feature("OwnPlayableFeature", new int[10][]);

            for (int i = 0; i < 10; i++)
            {
                ret.featrueArray[5].data[i] = new int[FeatureConst.Instance.cardArray.Length];
            }
            List <List <PlayerKeyInfo.CardKeyInfo> > playableList = new List <List <PlayerKeyInfo.CardKeyInfo> >();

            playableList.Add(mPlayer.handcardJsonList);
            if (mPlayer.handcardChange != null)
            {
                playableList.AddRange(mPlayer.handcardChange);
            }
            int playedCount     = 0;
            int playedCardCount = 0;

            foreach (PlayerKeyInfo.ActionKeyInfo actionKeyInfo in mPlayer.playedActionJsonList)
            {
                if (actionKeyInfo.actionType == actionEnum.playcard || actionKeyInfo.actionType == actionEnum.useHeroPower)
                {
                    List <PlayerKeyInfo.CardKeyInfo> playable = playableList[playedCount];
                    foreach (PlayerKeyInfo.CardKeyInfo hc in playable)
                    {
                        if (hc.playable)
                        {
                            ret.featrueArray[5].data[playedCount][FeatureConst.Instance.cardStrIdxDict[hc.cardName]] = 1;
                        }
                        if (mPlayer.canPlayHeroPower[playedCount] == 1)
                        {
                            ret.featrueArray[5].data[playedCount][FeatureConst.Instance.cardStrIdxDict["fireblast"]] = 1;
                        }
                    }
                    int playedActionIdx;
                    if (actionKeyInfo.actionType == actionEnum.playcard)
                    {
                        playedActionIdx = FeatureConst.Instance.cardStrIdxDict[mPlayer.playedCardJsonList[playedCardCount].cardName];
                        playedCardCount++;
                    }
                    else
                    {
                        playedActionIdx = FeatureConst.Instance.cardStrIdxDict["fireblast"];
                    }
                    ret.featrueArray[4].data[playedCount] = playedActionIdx;
                    playedCount++;
                }
            }

            ret.featrueArray[6] = new Feature("EnemyMinionHp", new int[FeatureConst.Instance.minionCardArray.Length]);
            ret.featrueArray[7] = new Feature("EnemyMinion", new int[FeatureConst.Instance.minionCardArray.Length]);

            foreach (PlayerKeyInfo.MinionKeyInfo m in ePlayer.minionJsonList)
            {
                int idx = FeatureConst.Instance.cardStrIdxDict[m.name];
                ret.featrueArray[6].data[idx]++;
                ret.featrueArray[7].data[idx] = Math.Max(ret.featrueArray[7].data[idx], m.Hp);
            }

            ret.featrueArray[8] = new Feature("EnemyCardFeature", new int[FeatureConst.Instance.cardArray.Length]);

            foreach (PlayerKeyInfo.CardKeyInfo hc in ePlayer.handcardJsonList)
            {
                int idx = FeatureConst.Instance.cardStrIdxDict[hc.cardName];
                ret.featrueArray[8].data[idx]++;
            }

            return(ret);
        }
Exemple #5
0
        public static StateFeature interactionFeaturization(Playfield pf)
        {
            Player mPlayer = pf.getCurrentPlayer(true);
            Player ePlayer = pf.getCurrentPlayer(false);

            StateFeature ret = new StateFeature(6);
            int          globalFeatureSize = 6;

            ret.featrueArray[0]         = new Feature("Global", new int[globalFeatureSize]); //ownhp, enemyhp, ownmana, enemymana;
            ret.featrueArray[0].data[0] = mPlayer.ownHero.Hp;
            ret.featrueArray[0].data[1] = mPlayer.ownMaxMana;
            ret.featrueArray[0].data[2] = ePlayer.ownHero.Hp;
            ret.featrueArray[0].data[3] = ePlayer.ownMaxMana;

            int enemyBoardHp   = 0;
            int ownBoardAttack = 0;

            foreach (Minion m in ePlayer.ownMinions)
            {
                enemyBoardHp += m.Hp;
            }

            foreach (Minion m in mPlayer.ownMinions)
            {
                ownBoardAttack += m.Angr;
            }
            int atkAdv = enemyBoardHp <= ownBoardAttack ? 1 : 0;
            int numAdv = mPlayer.ownMinions.Count >= ePlayer.ownMinions.Count ? 1 : 0;

            ret.featrueArray[0].data[4] = atkAdv;
            ret.featrueArray[0].data[5] = numAdv;

            ret.featrueArray[1] = new Feature("OwnCardFeature", new int[FeatureConst.Instance.cardArray.Length]);
            ret.featrueArray[2] = new Feature("OwnPlayableFeature", new int[FeatureConst.Instance.cardArray.Length]);
            ret.featrueArray[3] = new Feature("OwnPlayedFeature", new int[FeatureConst.Instance.cardArray.Length]);
            ret.featrueArray[4] = new Feature("EffectFeature", new int[FeatureConst.Instance.cardArray.Length]);
            ret.featrueArray[5] = new Feature("CanPlayAfter", new int[FeatureConst.Instance.cardArray.Length]);

            double totalEffect = 0;

            foreach (Minion m in ePlayer.ownMinions)
            {
                totalEffect += 2 * m.getDefMinionValue();
            }

            if (mPlayer.ownAbilityReady && mPlayer.mana >= 2)
            {
                int idx = FeatureConst.Instance.cardIdxDict[CardDB.cardName.fireblast];
                ret.featrueArray[2].data[idx] = 1;

                double effect       = getHeroPowerMinionInteraction(pf, ePlayer.ownMinions);
                int    scaledEffect = (effect != 0) ? calcScaledEffect(effect, totalEffect) : 0;
                ret.featrueArray[4].data[idx] = scaledEffect;
            }

            foreach (Handmanager.Handcard hc in mPlayer.owncards)
            {
                int idx = FeatureConst.Instance.cardIdxDict[hc.card.name];
                ret.featrueArray[1].data[idx]++;
                if (ret.featrueArray[1].data[idx] > 6)
                {
                    int debug = 1;
                }
                if (hc.canplayCard(pf, pf.isOwnTurn))
                {
                    ret.featrueArray[2].data[idx] = 1;

                    if (hc.card.type == CardDB.cardtype.SPELL)
                    {
                        double effect       = getSpellMinionInteraction(pf, hc, ePlayer.ownMinions);
                        int    scaledEffect = (effect != 0) ? calcScaledEffect(effect, totalEffect) : 0;
                        ret.featrueArray[4].data[idx] = scaledEffect;
                    }
                    else
                    {
                        double effect       = getNonspellMinionInteraction(pf, hc, ePlayer.ownMinions);
                        int    scaledEffect = (effect != 0) ? calcScaledEffect(effect, totalEffect) : 0;
                        ret.featrueArray[4].data[idx] = scaledEffect;
                    }
                }
            }

            foreach (Handmanager.Handcard hc in mPlayer.owncards)
            {
                int idx = FeatureConst.Instance.cardIdxDict[hc.card.name];
                if (ret.featrueArray[2].data[idx] == 1)
                {
                    //mana after play
                    int temp          = mPlayer.mana;
                    int manaAfterPlay = mPlayer.mana - hc.getManaCost(pf, pf.isOwnTurn);
                    mPlayer.mana = manaAfterPlay;
                    int canPlayAfter = 0;
                    foreach (Handmanager.Handcard hc1 in mPlayer.owncards)
                    {
                        if (hc1.canplayCard(pf, pf.isOwnTurn))
                        {
                            int idx1 = FeatureConst.Instance.cardIdxDict[hc1.card.name];
                            canPlayAfter = Math.Max(canPlayAfter, ret.featrueArray[4].data[idx1]);
                        }
                    }
                    mPlayer.mana = temp;
                    ret.featrueArray[5].data[idx] = canPlayAfter;
                }
            }

            foreach (Action action in mPlayer.playactions)
            {
                if (action.actionType == actionEnum.playcard)
                {
                    int idx = FeatureConst.Instance.cardIdxDict[action.card.card.name];
                    ret.featrueArray[3].data[idx]++;
                }
                else if (action.actionType == actionEnum.useHeroPower)
                {
                    int idx = FeatureConst.Instance.cardIdxDict[CardDB.cardName.fireblast];
                    ret.featrueArray[3].data[idx]++;
                }
            }

            return(ret);
        }