Ejemplo n.º 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();
        }
Ejemplo n.º 2
0
        public void GetCardPolicy(Playfield pf)
        {
            string msg = Featurization.FeaturizationToStringFlatten(pf);

            if (policyDict.ContainsKey(msg))
            {
                Array.Copy(policyDict[msg], cachedProb, cachedProb.Length);
            }
            else
            {
                string   cardProb     = ZMQMessager.Instance.Send(msg);
                string[] probStrArray = cardProb.Split();
                for (int i = 0; i < probStrArray.Length; i++)
                {
                    cachedProb[i] = Double.Parse(probStrArray[i]);
                }
                policyDict[msg] = new double[cachedProb.Length];
                Array.Copy(cachedProb, policyDict[msg], cachedProb.Length);
            }
        }
Ejemplo n.º 3
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);
            }
        }
Ejemplo n.º 4
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();
                }
            }
        }
Ejemplo n.º 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();
        }