예제 #1
0
        public int getBestCandidate(List <Playfield> candidateStates, bool playerSide)
        {
            int    ret         = 0;
            PyList handList    = new PyList();
            PyList endTurnList = new PyList();

            foreach (Playfield p in candidateStates)
            {
                PyList pyHandFt = getHandFeature(p, playerSide);
                PythonUtils.AppendRecycle(handList, pyHandFt);

                List <int> endTurnFeat   = bh.getPlayfieldFeature(p, playerSide);
                PyList     pyEndTurnFeat = new PyList();
                foreach (int ft in endTurnFeat)
                {
                    PythonUtils.AppendRecycle(pyEndTurnFeat, new PyInt(ft));
                }
                PythonUtils.AppendRecycle(endTurnList, pyEndTurnFeat);
            }
            PyList feature_list = new PyList();

            PythonUtils.AppendRecycle(feature_list, endTurnList);
            PythonUtils.AppendRecycle(feature_list, handList);

            dynamic result = evaluator.advance(feature_list, Py.kw("verbose", 0));

            ret = result.AsManagedObject(typeof(int));
            result.Dispose();
            feature_list.Dispose();
            return(ret);
        }
예제 #2
0
        public override void FillIdxArr(PyList idxArr, int offset)
        {
            PyList Dim1 = new PyList();
            PyList Dim2 = new PyList();

            for (int i = 0; i < data.Length; i++)
            {
                Dim1.Append(FeatureConst.Instance.pyIntMap[data[i] + offset]);
                Dim2.Append(FeatureConst.Instance.pyIntMap[i]);
            }
            PythonUtils.AppendRecycle(idxArr, Dim1);
            PythonUtils.AppendRecycle(idxArr, Dim2);
        }
예제 #3
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();
        }
예제 #4
0
        public override void FillIdxArr(PyList idxArr, int offset)
        {
            PyList Dim1 = new PyList();
            PyList Dim2 = new PyList();
            PyList Dim3 = new PyList();

            for (int i = 0; i < data.Length; i++)
            {
                for (int j = 0; j < data[i].Length; j++)
                {
                    //npArray[data[i][j] + offset][i].itemset(j, 1);
                    Dim1.Append(FeatureConst.Instance.pyIntMap[data[i][j] + offset]);
                    Dim2.Append(FeatureConst.Instance.pyIntMap[i]);
                    Dim3.Append(FeatureConst.Instance.pyIntMap[j]);
                }
            }

            PythonUtils.AppendRecycle(idxArr, Dim1);
            PythonUtils.AppendRecycle(idxArr, Dim2);
            PythonUtils.AppendRecycle(idxArr, Dim3);
        }
예제 #5
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();
        }
예제 #6
0
        public PyList[] getPyFeatureData()
        {
            PyList[] ret = new PyList[9];

            PyList pyGlobal = new PyList();

            foreach (int f in featrueArray[0].data)
            {
                pyGlobal.Append(FeatureConst.Instance.pyIntMap[f]);
            }

            PyList pyOwnMinionHp = new PyList();

            foreach (int f in featrueArray[1].data)
            {
                pyOwnMinionHp.Append(FeatureConst.Instance.pyIntMap[f]);
            }

            PyList pyOwnMinion = new PyList();

            foreach (int f in featrueArray[2].data)
            {
                pyOwnMinion.Append(FeatureConst.Instance.pyIntMap[f]);
            }

            PyList pyOwnCard = new PyList();

            foreach (int f in featrueArray[3].data)
            {
                pyOwnCard.Append(FeatureConst.Instance.pyIntMap[f]);
            }

            PyList pyOwnPlayed = new PyList();

            foreach (int f in featrueArray[4].data)
            {
                pyOwnPlayed.Append(FeatureConst.Instance.pyIntMap[f]);
            }

            PyList pyOwnPlayable = new PyList();

            foreach (int[] action in featrueArray[5].data)
            {
                PyList row = new PyList();
                foreach (int f in action)
                {
                    row.Append(FeatureConst.Instance.pyIntMap[f]);
                }
                PythonUtils.AppendRecycle(pyOwnPlayable, row);
            }

            PyList pyEnemyMinionHp = new PyList();

            foreach (int f in featrueArray[6].data)
            {
                pyEnemyMinionHp.Append(FeatureConst.Instance.pyIntMap[f]);
            }

            PyList pyEnemyMinion = new PyList();

            foreach (int f in featrueArray[7].data)
            {
                pyEnemyMinion.Append(FeatureConst.Instance.pyIntMap[f]);
            }

            PyList pyEnemyCard = new PyList();

            foreach (int f in featrueArray[8].data)
            {
                pyEnemyCard.Append(FeatureConst.Instance.pyIntMap[f]);
            }

            ret[0] = pyGlobal;
            ret[1] = pyOwnMinionHp;
            ret[2] = pyOwnMinion;
            ret[3] = pyOwnCard;
            ret[4] = pyOwnPlayed;
            ret[5] = pyOwnPlayable;
            ret[6] = pyEnemyMinionHp;
            ret[7] = pyEnemyMinion;
            ret[8] = pyEnemyCard;

            return(ret);
        }
예제 #7
0
        public static void NumpyFeaturization(Playfield pf, dynamic globalIdxArr, dynamic boardIdxArr, dynamic handIdxArr, dynamic playIdxArr)
        {
            Player mPlayer   = pf.getCurrentPlayer(true);
            Player ePlayer   = pf.getCurrentPlayer(false);
            int    ownMana   = mPlayer.ownMaxMana;
            int    enemyMana = ePlayer.ownMaxMana;
            int    ownHp     = mPlayer.ownHero.getHeroHpValue();
            int    enemyHp   = ePlayer.ownHero.getHeroHpValue();
            int    glbIdx    = 0;

            glbIdx += ownMana - 1;
            //globalIdxArr.itemset(glbIdx, 1);
            globalIdxArr.Append(FeatureConst.Instance.pyIntMap[glbIdx]);
            glbIdx = 10;
            if (!pf.isOwnTurn)
            {
                globalIdxArr.Append(FeatureConst.Instance.pyIntMap[glbIdx]);
            }
            int heroIdx = FeatureConst.Instance.heroHpDict[ownHp + enemyHp * 10];

            glbIdx += heroIdx;
            globalIdxArr.Append(FeatureConst.Instance.pyIntMap[glbIdx]);

            BinaryFeature boardFeature = encodeBoard(pf);

            boardFeature.FillIdxArr(boardIdxArr, 0);

            BinaryFeature handFeature = encodeHand(pf);
            //BinaryFeature DeckFeature = encodeDeck(pf);

            PyList dim1 = new PyList();
            PyList dim2 = new PyList();

            for (int i = 0; i < handFeature.data.Length; i++)
            {
                dim1.Append(FeatureConst.Instance.pyIntMap[handFeature.data[i]]);
                dim2.Append(FeatureConst.Instance.pyIntMap[i]);
            }

            //for (int i = 0; i < DeckFeature.data.Length; i++)
            //{
            //    dim1.Append(FeatureConst.Instance.pyIntMap[DeckFeature.data[i] + 9]);
            //    dim2.Append(FeatureConst.Instance.pyIntMap[i]);
            //}

            PythonUtils.AppendRecycle(handIdxArr, dim1);
            PythonUtils.AppendRecycle(handIdxArr, dim2);

            BinaryFeature playableFeature = encodePlay(pf);

            dim1 = new PyList();
            dim2 = new PyList();

            for (int i = 0; i < playableFeature.data.Length; i++)
            {
                if (playableFeature.data[i] > 0)
                {
                    dim1.Append(FeatureConst.Instance.pyIntMap[0]);
                    dim2.Append(FeatureConst.Instance.pyIntMap[i]);
                }
            }

            BinaryFeature playedFeature = encodePlayed(pf);

            for (int i = 0; i < playedFeature.data.Length; i++)
            {
                if (playedFeature.data[i] > 0)
                {
                    dim1.Append(FeatureConst.Instance.pyIntMap[1]);
                    dim2.Append(FeatureConst.Instance.pyIntMap[i]);
                }
            }
            PythonUtils.AppendRecycle(playIdxArr, dim1);
            PythonUtils.AppendRecycle(playIdxArr, dim2);
        }
예제 #8
0
        public dynamic parsePlayfieldCNN(Playfield p, bool own)
        {
            Player             mPlayer, ePlayer;
            List <CardDB.Card> mDeck, eDeck;

            if (own)
            {
                mPlayer = p.playerFirst;
                ePlayer = p.playerSecond;
                mDeck   = p.homeDeck;
                eDeck   = p.awayDeck;
            }
            else
            {
                ePlayer = p.playerFirst;
                mPlayer = p.playerSecond;
                eDeck   = p.homeDeck;
                mDeck   = p.awayDeck;
            }

            PyInt ownMana   = FeatureConst.Instance.pyIntMap[mPlayer.ownMaxMana];
            PyInt ownHp     = FeatureConst.Instance.pyIntMap[mPlayer.ownHero.Hp + mPlayer.ownHero.armor];
            PyInt enemyMana = FeatureConst.Instance.pyIntMap[ePlayer.ownMaxMana];
            PyInt enemyHp   = FeatureConst.Instance.pyIntMap[ePlayer.ownHero.Hp + ePlayer.ownHero.armor];

            PyList hero_feature = new PyList(new PyObject[] { ownMana, ownHp, enemyMana, enemyHp });

            ownMana.Dispose();
            ownHp.Dispose();
            enemyMana.Dispose();
            enemyHp.Dispose();

            PyList own_hand_list = new PyList();

            foreach (Handmanager.Handcard hc in mPlayer.owncards)
            {
                PyDict hand_card = new PyDict();
                hand_card["cardName"] = new PyString(hc.card.name.ToString());
                PythonUtils.AppendRecycle(own_hand_list, hand_card);
            }

            PyList enemy_hand_list = new PyList();

            foreach (Handmanager.Handcard hc in ePlayer.owncards)
            {
                PyDict hand_card = new PyDict();
                hand_card["cardName"] = new PyString(hc.card.name.ToString());
                PythonUtils.AppendRecycle(enemy_hand_list, hand_card);
            }

            PyList own_minion_list = new PyList();

            foreach (Minion m in mPlayer.ownMinions)
            {
                PyDict minion = new PyDict();
                minion["Hp"]   = FeatureConst.Instance.pyIntMap[m.Hp];
                minion["Angr"] = FeatureConst.Instance.pyIntMap[m.Angr];
                minion["name"] = new PyString(m.name.ToString());
                PythonUtils.AppendRecycle(own_minion_list, minion);
            }

            PyList enemy_minion_list = new PyList();

            foreach (Minion m in ePlayer.ownMinions)
            {
                PyDict minion = new PyDict();
                minion["Hp"]   = FeatureConst.Instance.pyIntMap[m.Hp];
                minion["Angr"] = FeatureConst.Instance.pyIntMap[m.Angr];
                minion["name"] = new PyString(m.name.ToString());
                PythonUtils.AppendRecycle(enemy_minion_list, minion);
            }

            PyList own_deck_list = new PyList();

            foreach (CardDB.Card c in mDeck)
            {
                PyString card_name = new PyString(c.name.ToString());
                PythonUtils.AppendRecycle(own_deck_list, card_name);
            }

            PyList enemy_deck_list = new PyList();

            foreach (CardDB.Card c in eDeck)
            {
                PyString card_name = new PyString(c.name.ToString());
                PythonUtils.AppendRecycle(enemy_deck_list, card_name);
            }

            dynamic own_play_list = new PyList();

            foreach (Action action in mPlayer.playactions)
            {
                if (action.actionType == actionEnum.playcard)
                {
                    PyString card_name = new PyString(action.card.card.name.ToString());
                    PythonUtils.AppendRecycle(own_play_list, card_name);
                }
            }

            dynamic res_list = board_encoder.encode_for_cnn_phase(hero_feature, own_hand_list, own_minion_list,
                                                                  enemy_hand_list, enemy_minion_list, own_deck_list, enemy_deck_list, own_play_list);

            hero_feature.Dispose();
            own_hand_list.Dispose();
            own_minion_list.Dispose();
            enemy_hand_list.Dispose();
            enemy_minion_list.Dispose();
            own_deck_list.Dispose();
            enemy_deck_list.Dispose();
            own_play_list.Dispose();

            return(res_list);
        }