Пример #1
0
        public Playfield readJsonFile()
        {
            if (sources == null)
            {
                string       mydocpath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                StreamReader stream    = File.OpenText(mydocpath + @"\picked_states.txt");
                sources = (JArray)JToken.ReadFrom(new JsonTextReader(stream));
            }

            Console.WriteLine("index = " + index);

            if (index < sources.Count)
            {
                var state = sources[index];

                var  ownJson   = state["own"];
                var  enemyJson = state["enemy"];
                bool own       = (bool)state["turn"];

                PlayerKeyInfo p1KeyInfo       = ownJson.ToObject <PlayerKeyInfo>();
                PlayerKeyInfo p2KeyInfo       = enemyJson.ToObject <PlayerKeyInfo>();
                Playfield     parsedPlayfield = new Playfield(1000, own, p1KeyInfo, p2KeyInfo);
                index++;
                return(parsedPlayfield);
            }

            return(null);
        }
Пример #2
0
        //public dynamic getPlayfieldFeature(Playfield p, bool own)
        //{
        //    PyList featureList = new PyList();

        //    Player mPlayer, ePlayer;

        //    if (own)
        //    {
        //        mPlayer = p.playerFirst;
        //        ePlayer = p.playerSecond;
        //    }
        //    else
        //    {
        //        mPlayer = p.playerSecond;
        //        ePlayer = p.playerFirst;
        //    }

        //    int hpboarder = 10;
        //    if (mPlayer.ownHeroName == HeroEnum.warlock && ePlayer.ownHeroName != HeroEnum.mage) hpboarder = 6;
        //    int aggroboarder = 11;

        //    if (p.isOwnTurn != own)
        //    {
        //        PyInt num = new PyInt(-p.lastTurnEvaluatePenality);
        //        featureList.Append(new PyInt(-p.lastTurnEvaluatePenality));

        //    }
        //    else
        //    {
        //        featureList.Append(new PyInt(-p.evaluatePenality));
        //    }

        //    featureList.Append(new PyInt(mPlayer.owncards.Count * 5));

        //    featureList.Append(new PyInt(mPlayer.ownMaxMana));
        //    featureList.Append(new PyInt(-ePlayer.ownMaxMana));

        //    featureList.Append(new PyInt(mPlayer.ownMaxMana * 20 - ePlayer.ownMaxMana * 20));

        //    featureList.Append(new PyInt(-2 * ePlayer.spellpower));

        //    int hpFeature = mPlayer.ownHero.Hp + mPlayer.ownHero.armor > hpboarder ? 1 : 0;

        //    if (mPlayer.ownHero.Hp + mPlayer.ownHero.armor > hpboarder)
        //    {
        //        featureList.Append(new PyInt(mPlayer.ownHero.Hp + mPlayer.ownHero.armor));
        //    }
        //    else
        //    {
        //        featureList.Append(new PyInt(-2 * (hpboarder + 1 - mPlayer.ownHero.Hp - mPlayer.ownHero.armor) * (hpboarder + 1 - mPlayer.ownHero.Hp - mPlayer.ownHero.armor)));
        //    }


        //    if (ePlayer.ownHero.Hp + ePlayer.ownHero.armor > aggroboarder)
        //    {
        //        featureList.Append(new PyInt(-ePlayer.ownHero.Hp - ePlayer.ownHero.armor));
        //    }
        //    else
        //    {
        //        featureList.Append(new PyInt(3 * (aggroboarder + 1 - ePlayer.ownHero.Hp - ePlayer.ownHero.armor)));
        //    }

        //    if (ePlayer.ownHero.armor + ePlayer.ownHero.Hp <= 10) //RR should be updated by lethal check
        //    {
        //        featureList.Append(new PyInt(mPlayer.owncarddraw * 100));
        //    }
        //    else
        //    {
        //        featureList.Append(new PyInt(0));
        //    }

        //    if (mPlayer.ownMaxMana < 4)
        //    {
        //        featureList.Append(new PyInt(mPlayer.owncarddraw * 2));
        //    }
        //    else
        //    {
        //        featureList.Append(new PyInt(mPlayer.owncarddraw * 5));
        //    }

        //    //retval += p.owncarddraw * 5;
        //    featureList.Append(new PyInt(-ePlayer.owncarddraw * 15));

        //    //int owntaunt = 0;
        //    int readycount = 0;
        //    int ownMinionsCount = 0;
        //    int ownMinionVal = 0;
        //    foreach (Minion m in mPlayer.ownMinions)
        //    {
        //        ownMinionVal += 5;
        //        ownMinionVal += m.Hp * 2;
        //        ownMinionVal += m.Angr * 2;
        //        ownMinionVal += m.handcard.card.rarity;
        //        if (!m.playedThisTurn && m.windfury) ownMinionVal += m.Angr;
        //        if (m.divineshild) ownMinionVal += 1;
        //        if (m.stealth) ownMinionVal += 1;
        //        if (m.handcard.card.isSpecialMinion)
        //        {
        //            ownMinionVal += 1;
        //            if (!m.taunt && m.stealth) ownMinionVal += 20;
        //        }
        //        else
        //        {
        //            if (m.Angr <= 2 && m.Hp <= 2 && !m.divineshild) ownMinionVal -= 5;
        //        }
        //        //if (!m.taunt && m.stealth && penman.specialMinions.ContainsKey(m.name)) retval += 20;
        //        //if (m.poisonous) retval += 1;
        //        if (m.divineshild && m.taunt) ownMinionVal += 4;
        //        //if (m.taunt && m.handcard.card.name == CardDB.cardName.frog) owntaunt++;
        //        //if (m.handcard.card.isToken && m.Angr <= 2 && m.Hp <= 2) ownMinionVal -= 5;
        //        //if (!penman.specialMinions.ContainsKey(m.name) && m.Angr <= 2 && m.Hp <= 2) ownMinionVal -= 5;
        //        if (m.handcard.card.name == CardDB.cardName.direwolfalpha || m.handcard.card.name == CardDB.cardName.flametonguetotem || m.handcard.card.name == CardDB.cardName.stormwindchampion || m.handcard.card.name == CardDB.cardName.raidleader) ownMinionVal += 10;
        //        if (m.handcard.card.name == CardDB.cardName.bloodmagethalnos) ownMinionVal += 10;
        //        if (m.handcard.card.name == CardDB.cardName.nerubianegg)
        //        {
        //            if (m.Angr >= 1) ownMinionVal += 2;
        //            if ((!m.taunt && m.Angr == 0) && (m.divineshild || m.maxHp > 2)) ownMinionVal -= 10;
        //        }
        //        if (m.Ready) readycount++;
        //        if (m.Hp <= 4 && (m.Angr > 2 || m.Hp > 3)) ownMinionsCount++;
        //    }
        //    featureList.Append(new PyInt(ownMinionVal));

        //    /*if (ePlayer.ownMinions.Count >= 0)
        //    {
        //        int anz = ePlayer.ownMinions.Count;
        //        if (owntaunt == 0) retval -= 10 * anz;
        //        retval += owntaunt * 10 - 11 * anz;
        //    }*/


        //    bool useAbili = false;
        //    bool usecoin = false;
        //    List<Action> actions;

        //    if (p.isOwnTurn != own)
        //    {
        //        actions = mPlayer.lastTurnActions;
        //    }
        //    else
        //    {
        //        actions = mPlayer.playactions;
        //    }

        //    int actionVal = 0;
        //    foreach (Action a in actions)
        //    {
        //        if (a.actionType == actionEnum.attackWithHero && ePlayer.ownHero.Hp <= 15) actionVal++; //attackfacehp = 15
        //        if (a.actionType == actionEnum.useHeroPower) useAbili = true;
        //        if (mPlayer.ownHeroName == HeroEnum.warrior && a.actionType == actionEnum.attackWithHero && useAbili) actionVal -= 1;
        //        //if (a.actionType == actionEnum.useHeroPower && a.card.card.name == CardDB.cardName.lesserheal && (!a.target.own)) actionVal -= 5;
        //        if (a.actionType != actionEnum.playcard) continue;
        //        if ((a.card.card.name == CardDB.cardName.thecoin || a.card.card.name == CardDB.cardName.innervate)) usecoin = true;
        //        //save spell for all classes: (except for rouge if he has no combo)
        //        if (a.target == null) continue;
        //        if (mPlayer.ownHeroName != HeroEnum.thief && a.card.card.type == CardDB.cardtype.SPELL && (!a.target.own && a.target.isHero) && a.card.card.name != CardDB.cardName.shieldblock) actionVal -= 11;
        //        if (mPlayer.ownHeroName == HeroEnum.thief && a.card.card.type == CardDB.cardtype.SPELL && (a.target.isHero && !a.target.own)) actionVal -= 11;
        //    }
        //    if (usecoin && useAbili && mPlayer.ownMaxMana <= 2) actionVal -= 40;
        //    if (usecoin) actionVal -= 5 * mPlayer.manaTurnEnd;
        //    if (mPlayer.manaTurnEnd >= 2 && !useAbili)
        //    {
        //        actionVal -= 10;
        //        if (mPlayer.ownHeroName == HeroEnum.thief && (mPlayer.ownWeaponDurability >= 2 || mPlayer.ownWeaponAttack >= 2)) actionVal += 10;
        //    }
        //    //if (usecoin && p.mana >= 1) actionVal -= 20;
        //    featureList.Append(new PyInt(actionVal));

        //    int handMobFeature = 0;
        //    int mobsInHand = 0;
        //    int bigMobsInHand = 0;
        //    foreach (Handmanager.Handcard hc in mPlayer.owncards)
        //    {
        //        if (hc.card.type == CardDB.cardtype.MOB)
        //        {
        //            mobsInHand++;
        //            if (hc.card.Attack >= 3) bigMobsInHand++;
        //        }
        //    }

        //    if (ownMinionsCount - ePlayer.ownMinions.Count >= 4 && bigMobsInHand >= 1)
        //    {
        //        handMobFeature += bigMobsInHand * 25;
        //    }
        //    featureList.Append(new PyInt(handMobFeature));

        //    int enemyMinionFeature = 0;
        //    //bool hasTank = false;
        //    foreach (Minion m in ePlayer.ownMinions)
        //    {
        //        enemyMinionFeature += bh.getEnemyMinionValue(m, p);
        //        //hasTank = hasTank || m.taunt;
        //    }
        //    featureList.Append(new PyInt(-enemyMinionFeature));

        //    int otherVal = 0;
        //    otherVal -= ePlayer.ownSecretsIDList.Count;
        //    otherVal -= p.lostDamage;//damage which was to high (like killing a 2/1 with an 3/3 -> => lostdamage =2
        //    otherVal -= p.lostWeaponDamage;

        //    //if (p.ownMinions.Count == 0) retval -= 20;
        //    //if (ePlayer.ownMinions.Count == 0) retval += 20;
        //    //if (ePlayer.ownHero.Hp <= 0) otherVal = 10000;
        //    //soulfire etc
        //    int deletecardsAtLast = 0;
        //    foreach (Action a in actions)
        //    {
        //        if (a.actionType != actionEnum.playcard) continue;
        //        if (a.card.card.name == CardDB.cardName.soulfire || a.card.card.name == CardDB.cardName.doomguard || a.card.card.name == CardDB.cardName.succubus) deletecardsAtLast = 1;
        //        if (deletecardsAtLast == 1 && !(a.card.card.name == CardDB.cardName.soulfire || a.card.card.name == CardDB.cardName.doomguard || a.card.card.name == CardDB.cardName.succubus)) otherVal -= 20;
        //    }
        //    if (ePlayer.ownHero.Hp >= 1 && p.guessingHeroHP <= 0)
        //    {
        //        if (p.turnCounter < 2) otherVal += mPlayer.owncarddraw * 100;
        //        otherVal -= 1000;
        //    }
        //    //if (mPlayer.ownHero.Hp <= 0) retval = -10000;
        //    featureList.Append(new PyInt(otherVal));

        //    return featureList;
        //}


        public string getStateString(PlayerKeyInfo ownkeyInfo, PlayerKeyInfo enemykeyInfo)
        {
            string heroString = "own:" + ownkeyInfo.heroInfo.entity + ", hp:" + ownkeyInfo.heroInfo.Hp + ", mana:" + ownkeyInfo.maxMana + "\n";

            heroString += "enemy:" + enemykeyInfo.heroInfo.entity + ", hp:" + enemykeyInfo.heroInfo.Hp + ", mana:" + enemykeyInfo.maxMana + "\n";
            string minionString = "own minion======:\n";

            foreach (PlayerKeyInfo.MinionKeyInfo mk in ownkeyInfo.minionJsonList)
            {
                minionString += mk.name + ": " + mk.Hp + "\n";
            }

            minionString += "enemy minion======:\n";
            foreach (PlayerKeyInfo.MinionKeyInfo mk in enemykeyInfo.minionJsonList)
            {
                minionString += mk.name + ": " + mk.Hp + "\n";
            }

            string cardString = "own card======:\n";

            foreach (PlayerKeyInfo.CardKeyInfo mk in ownkeyInfo.handcardJsonList)
            {
                cardString += mk.cardName + "\n";
            }

            cardString += "enemy card======:\n";
            foreach (PlayerKeyInfo.CardKeyInfo mk in enemykeyInfo.handcardJsonList)
            {
                cardString += mk.cardName + "\n";
            }

            return(heroString + minionString + cardString);
        }
Пример #3
0
        public PyList getHandFeature(PlayerKeyInfo ownInfo, PlayerKeyInfo enemyInfo)
        {
            int[] featureArray = new int[cardArray.Length * 2];
            foreach (PlayerKeyInfo.CardKeyInfo hc in ownInfo.handcardJsonList)
            {
                int idx = cardIdxDict[CardDB.Instance.cardNamestringToEnum(hc.cardName)];
                featureArray[idx] = featureArray[idx] + 1;
            }

            foreach (PlayerKeyInfo.CardKeyInfo hc in enemyInfo.handcardJsonList)
            {
                int idx = cardIdxDict[CardDB.Instance.cardNamestringToEnum(hc.cardName)] + cardArray.Length;
                featureArray[idx] = featureArray[idx] + 1;
            }

            PyList hand_feature = new PyList();

            foreach (int ft in featureArray)
            {
                PyInt num = new PyInt(ft);
                hand_feature.Append(num);
                num.Dispose();
            }

            return(hand_feature);
        }
Пример #4
0
 public StateKeyInfo(int nextEntity, PlayerKeyInfo ap, PlayerKeyInfo dp, bool isRandom, double stateVal)
 {
     this.nextEntity    = nextEntity;
     this.turnSt        = 0;
     this.attackPlayer  = ap;
     this.defensePlayer = dp;
     this.isRandom      = isRandom;
     this.stateValue    = stateVal;
 }
Пример #5
0
        public void replay(GameRecord gameRecord)
        {
            //GameRecord gameRecord = JsonConvert.DeserializeObject<GameRecord>(line);
            int count = 0;

            foreach (StateKeyInfo stKeyInfo in gameRecord.playSec)
            {
                count++;
                PlayerKeyInfo p1Info = stKeyInfo.attackPlayer;
                PlayerKeyInfo p2Info = stKeyInfo.defensePlayer;

                if (count == 14)
                {
                    int debug = 1;
                }

                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);
                }

                Helpfunctions.Instance.logg("=====================");
                //tempPf.printBoard();
                foreach (PlayerKeyInfo.ActionKeyInfo actionKeyInfo in p1Info.playedActionJsonList)
                {
                    Player mPlayer = tempPf.getCurrentPlayer(true);
                    Player ePlayer = tempPf.getCurrentPlayer(false);
                    //Console.WriteLine("Action: " + actionKeyInfo.ToString());
                    //tempPf.printBoard();
                    //tempPf.printBoard();
                    Action action = CreateActionFromInfo(tempPf, actionKeyInfo);
                    if (action != null)
                    {
                        tempPf.getNextEntity();
                        tempPf.doAction(action);
                        //action.print();
                    }
                    else
                    {
                        //tempPf.printBoard();
                        foreach (var cardJson in p1Info.playedCardJsonList)
                        {
                            Console.WriteLine(cardJson.cardName + ", " + cardJson.entity);
                        }
                    }
                }
            }
        }
Пример #6
0
        public void Replay(string fileName)
        {
            using (Py.GIL())
            {
                PythonEngine.Initialize();
                dynamic np       = Py.Import("numpy");
                dynamic py_utils = Py.Import("simple_dqn.py_utils");
                dynamic h5py     = Py.Import("h5py");
                dynamic gc       = Py.Import("gc");
                dynamic encoder  = Py.Import("simple_dqn.encoder");

                dynamic featureEncoder = encoder.FeatureEncoder();

                StreamReader file       = new StreamReader(fileName);
                string       line       = null;
                int          count      = 0;
                int          stateCount = 0;

                while ((line = file.ReadLine()) != null)
                {
                    if (count % 500 == 0)
                    {
                        Console.WriteLine("Read " + count + " lines.");
                        GC.Collect();
                        gc.collect();
                        //if (count > 0) break;
                    }

                    GameRecord gameRecord = JsonConvert.DeserializeObject <GameRecord>(line);
                    foreach (StateKeyInfo stKeyInfo in gameRecord.playSec)
                    {
                        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);
                        }

                        PyList targetIdxArr = new PyList();

                        string ftString = Featurization.FeaturizationToStringFlatten(tempPf);
                        featureEncoder.fill_ft_str(ftString);

                        foreach (PlayerKeyInfo.ActionKeyInfo actionKeyInfo in p1Info.playedActionJsonList)
                        {
                            Action action = CreateActionFromInfo(tempPf, actionKeyInfo);
                            tempPf.getNextEntity();
                            tempPf.doAction(action);
                        }

                        Featurization.NumpyHLTarget(tempPf, targetIdxArr);
                        featureEncoder.fill_target(targetIdxArr);
                        targetIdxArr.Dispose();

                        stateCount++;
                    }
                    count++;
                }

                PyString outFileName = new PyString(fileName + "HL.hdf5");
                featureEncoder.write_h5(outFileName);
            }
        }
Пример #7
0
        public void Encode(string fileName)
        {
            StreamReader file  = new StreamReader(fileName);
            string       line  = null;
            int          count = 0;

            while ((line = file.ReadLine()) != null)
            {
                if (count % 100 == 0)
                {
                    Console.WriteLine("Read " + count + " lines.");
                }

                GameRecord          gameRecord = JsonConvert.DeserializeObject <GameRecord>(line);
                List <StateKeyInfo> playSec    = new List <StateKeyInfo>();

                int lastEntity = 1000;
                foreach (StateKeyInfo stKeyInfo in gameRecord.playSec)
                {
                    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);
                    }

                    stKeyInfo.nextEntity = tempPf.nextEntity;
                    stKeyInfo.turnSt     = 1;
                    Player mPlayer = tempPf.getCurrentPlayer(true);
                    Player ePlayer = tempPf.getCurrentPlayer(false);

                    int length = stKeyInfo.attackPlayer.handcardJsonList.Count;
                    stKeyInfo.attackPlayer.handcardJsonList.Clear();

                    foreach (Handmanager.Handcard hc in mPlayer.owncards)
                    {
                        PlayerKeyInfo.CardKeyInfo hcInfo = new PlayerKeyInfo.CardKeyInfo(hc, true, tempPf);
                        stKeyInfo.attackPlayer.handcardJsonList.Add(hcInfo);
                    }

                    length = stKeyInfo.defensePlayer.handcardJsonList.Count;
                    stKeyInfo.defensePlayer.handcardJsonList.Clear();

                    foreach (Handmanager.Handcard hc in ePlayer.owncards)
                    {
                        PlayerKeyInfo.CardKeyInfo hcInfo = new PlayerKeyInfo.CardKeyInfo(hc, true, tempPf);
                        stKeyInfo.defensePlayer.handcardJsonList.Add(hcInfo);
                    }

                    //hero power
                    stKeyInfo.attackPlayer.canPlayHeroPower = new List <int>();
                    bool canPlayHeroPower = mPlayer.ownAbilityReady && mPlayer.mana >= 2;
                    if (canPlayHeroPower)
                    {
                        stKeyInfo.attackPlayer.canPlayHeroPower.Add(1);
                    }
                    else
                    {
                        stKeyInfo.attackPlayer.canPlayHeroPower.Add(0);
                    }

                    foreach (PlayerKeyInfo.ActionKeyInfo actionKeyInfo in p1Info.playedActionJsonList)
                    {
                        Action action = CreateActionFromInfo(tempPf, actionKeyInfo);

                        tempPf.getNextEntity();
                        tempPf.doAction(action);

                        PlayerKeyInfo ownkeyInfo, enemykeyInfo;
                        if (isOwnTurn)
                        {
                            ownkeyInfo   = new PlayerKeyInfo(tempPf.playerFirst, tempPf.homeDeck, true, tempPf);
                            enemykeyInfo = new PlayerKeyInfo(tempPf.playerSecond, tempPf.awayDeck, false, tempPf);
                        }
                        else
                        {
                            ownkeyInfo   = new PlayerKeyInfo(tempPf.playerSecond, tempPf.awayDeck, false, tempPf);
                            enemykeyInfo = new PlayerKeyInfo(tempPf.playerFirst, tempPf.homeDeck, true, tempPf);
                        }
                        StateKeyInfo interState = new StateKeyInfo(tempPf.nextEntity, ownkeyInfo, enemykeyInfo,
                                                                   false, 0.0);
                        //playSec.Add(interState);
                        if (action.own != null)
                        {
                            actionKeyInfo.ownName = action.own.name.ToString();
                        }
                        if (action.target != null)
                        {
                            actionKeyInfo.targetName = action.target.name.ToString();
                        }
                        if (action.actionType == actionEnum.playcard || action.actionType == actionEnum.useHeroPower)
                        {
                            if (stKeyInfo.attackPlayer.handcardChange == null)
                            {
                                stKeyInfo.attackPlayer.handcardChange = new List <List <PlayerKeyInfo.CardKeyInfo> >();
                            }
                            stKeyInfo.attackPlayer.handcardChange.Add(interState.attackPlayer.handcardJsonList);

                            canPlayHeroPower = mPlayer.ownAbilityReady && mPlayer.mana >= 2;
                            if (action.actionType == actionEnum.useHeroPower)
                            {
                                Debug.Assert(canPlayHeroPower == false);
                                actionKeyInfo.cardName = "fireblast";
                            }
                            else
                            {
                                actionKeyInfo.cardName = action.card.card.name.ToString();
                            }

                            if (canPlayHeroPower)
                            {
                                stKeyInfo.attackPlayer.canPlayHeroPower.Add(1);
                            }
                            else
                            {
                                stKeyInfo.attackPlayer.canPlayHeroPower.Add(0);
                            }
                        }
                    }

                    playSec.Add(stKeyInfo);
                    lastEntity = tempPf.getNextEntity() + 1;
                    //Console.WriteLine("===================");
                    //Console.WriteLine("End turn Next Entity:" + lastEntity);
                    //Console.WriteLine("===================");
                }
                gameRecord.playSec = playSec;
                count++;
                Helpfunctions.Instance.WriteResultToFileAbs(fileName + ".2.txt", JsonConvert.SerializeObject(gameRecord));
            }
        }
Пример #8
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();
                }
            }
        }
Пример #9
0
        public void EncodeModified(string fileName)
        {
            StreamReader file  = new StreamReader(fileName);
            string       line  = null;
            int          count = 0;

            while ((line = file.ReadLine()) != null)
            {
                //Helpfunctions.Instance.logg(line);
                if (count % 1000 == 0)
                {
                    Console.WriteLine("Finished " + count + " lines.");
                }

                GameRecord gameRecord = JsonConvert.DeserializeObject <GameRecord>(line);
                int        lastEntity = 1000;
                foreach (StateKeyInfo stKeyInfo in gameRecord.playSec)
                {
                    PlayerKeyInfo p1Info = stKeyInfo.attackPlayer;
                    PlayerKeyInfo p2Info = stKeyInfo.defensePlayer;

                    bool isOwnTurn = p1Info.turn == 0 ? true : false;
                    lastEntity = stKeyInfo.nextEntity;
                    Playfield tempPf = null;
                    if (isOwnTurn)
                    {
                        tempPf = new Playfield(lastEntity, isOwnTurn, p1Info, p2Info);
                    }
                    else
                    {
                        tempPf = new Playfield(lastEntity, isOwnTurn, p2Info, p1Info);
                    }

                    stKeyInfo.turnSt = 1;
                    Player mPlayer = tempPf.getCurrentPlayer(true);
                    Player ePlayer = tempPf.getCurrentPlayer(false);

                    int length = stKeyInfo.attackPlayer.handcardJsonList.Count;
                    stKeyInfo.attackPlayer.handcardJsonList.Clear();

                    foreach (Handmanager.Handcard hc in mPlayer.owncards)
                    {
                        PlayerKeyInfo.CardKeyInfo hcInfo = new PlayerKeyInfo.CardKeyInfo(hc, true, tempPf);
                        stKeyInfo.attackPlayer.handcardJsonList.Add(hcInfo);
                    }

                    length = stKeyInfo.defensePlayer.handcardJsonList.Count;
                    stKeyInfo.defensePlayer.handcardJsonList.Clear();

                    foreach (Handmanager.Handcard hc in ePlayer.owncards)
                    {
                        PlayerKeyInfo.CardKeyInfo hcInfo = new PlayerKeyInfo.CardKeyInfo(hc, true, tempPf);
                        stKeyInfo.defensePlayer.handcardJsonList.Add(hcInfo);
                    }

                    LinkedList <Action> actionSeq = searchValidSeq(tempPf, p1Info);
                    stKeyInfo.attackPlayer.canPlayHeroPower     = new List <int>();
                    stKeyInfo.attackPlayer.playedActionJsonList = new List <PlayerKeyInfo.ActionKeyInfo>();
                    stKeyInfo.attackPlayer.handcardChange       = new List <List <PlayerKeyInfo.CardKeyInfo> >();

                    foreach (Action action in actionSeq)
                    {
                        if (action.actionType == actionEnum.useHeroPower || action.actionType == actionEnum.playcard)
                        {
                            PlayerKeyInfo ownkeyInfo, enemykeyInfo;
                            if (isOwnTurn)
                            {
                                ownkeyInfo   = new PlayerKeyInfo(tempPf.playerFirst, tempPf.homeDeck, true, tempPf);
                                enemykeyInfo = new PlayerKeyInfo(tempPf.playerSecond, tempPf.awayDeck, false, tempPf);
                            }
                            else
                            {
                                ownkeyInfo   = new PlayerKeyInfo(tempPf.playerSecond, tempPf.awayDeck, false, tempPf);
                                enemykeyInfo = new PlayerKeyInfo(tempPf.playerFirst, tempPf.homeDeck, true, tempPf);
                            }
                            StateKeyInfo interState = new StateKeyInfo(tempPf.nextEntity, ownkeyInfo, enemykeyInfo, false, 0.0);
                            stKeyInfo.attackPlayer.handcardChange.Add(interState.attackPlayer.handcardJsonList);
                            bool canPlayHeroPower = mPlayer.ownAbilityReady && mPlayer.mana >= 2;
                            if (canPlayHeroPower)
                            {
                                stKeyInfo.attackPlayer.canPlayHeroPower.Add(1);
                            }
                            else
                            {
                                stKeyInfo.attackPlayer.canPlayHeroPower.Add(0);
                            }
                        }

                        Debug.Assert(stKeyInfo.attackPlayer.canPlayHeroPower.Count == stKeyInfo.attackPlayer.handcardChange.Count);

                        tempPf.getNextEntity();
                        tempPf.doAction(action);
                        stKeyInfo.attackPlayer.playedActionJsonList.Add(new PlayerKeyInfo.ActionKeyInfo(action));
                        if (debug)
                        {
                            action.print();
                        }
                    }
                    lastEntity = tempPf.getNextEntity() + 1;
                    //Console.WriteLine("===================");
                    //Console.WriteLine("End turn Next Entity:" + lastEntity);
                    //Console.WriteLine("===================");
                }
                count++;
                Helpfunctions.Instance.WriteResultToFileAbs(fileName + ".3.txt", JsonConvert.SerializeObject(gameRecord));
            }
        }
Пример #10
0
        public LinkedList <Action> searchValidSeq(Playfield pf, PlayerKeyInfo p1Info)
        {
            //this.debugCounter++;
            //Console.WriteLine(this.debugCounter);
            //if (this.debugCounter == 11)
            //{
            //    this.debug = true;
            //}
            Playfield  tempPf        = new Playfield(pf);
            Player     mPlayer       = tempPf.getCurrentPlayer(true);
            Player     ePlayer       = tempPf.getCurrentPlayer(false);
            List <int> pcActionId    = new List <int>();
            List <int> nonPcActionId = new List <int>();

            for (int i = 0; i < p1Info.playedActionJsonList.Count; i++)
            {
                PlayerKeyInfo.ActionKeyInfo keyInfo = p1Info.playedActionJsonList[i];
                if (keyInfo.actionType == actionEnum.useHeroPower || keyInfo.actionType == actionEnum.playcard)
                {
                    pcActionId.Add(i);
                }
                else
                {
                    nonPcActionId.Add(i);
                }
            }

            Node root = new Node(new Playfield(pf), null, 0, 0, null);
            Node cur  = root;

            while (cur.pcAction + cur.nonPCaction < p1Info.playedActionJsonList.Count)
            {
                if (this.debug)
                {
                    Console.WriteLine("pc:" + cur.pcAction + "/" + pcActionId.Count + "| nonPc:" + cur.nonPCaction + "/" + nonPcActionId.Count);
                }
                Node nextNode = null;
                if (cur.pcAction < pcActionId.Count && !cur.triedLeft)
                {
                    //try left
                    cur.triedLeft = true;
                    nextNode      = tryDoAction(cur, p1Info.playedActionJsonList[pcActionId[cur.pcAction]]);
                }
                else if (cur.nonPCaction < nonPcActionId.Count && !cur.triedRight)
                {
                    cur.triedRight = true;
                    nextNode       = tryDoAction(cur, p1Info.playedActionJsonList[nonPcActionId[cur.nonPCaction]]);
                }
                else
                {
                    nextNode = cur.par;
                }
                if (nextNode != null)
                {
                    cur = nextNode;
                }
            }

            LinkedList <Action> ret = new LinkedList <Action>();

            while (cur.action != null)
            {
                ret.AddFirst(cur.action);
                cur = cur.par;
            }
            return(ret);
        }
Пример #11
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();
        }
Пример #12
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);
        }