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