Ejemplo n.º 1
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.º 2
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();
        }