/// <summary>
        /// Generate the daily returns plot using the python libraries.
        /// </summary>
        public override string Render()
        {
            var backtestReturns = ResultsUtil.EquityPoints(_backtest);
            var liveReturns     = ResultsUtil.EquityPoints(_live);

            var backtestSeries = new Series <DateTime, double>(backtestReturns.Keys, backtestReturns.Values);
            var liveSeries     = new Series <DateTime, double>(liveReturns.Keys, liveReturns.Values);

            // The following two operations are equivalent to the Pandas `DataFrame.resample(...)` method
            var backtestResampled = backtestSeries.PercentChange().ResampleEquivalence(date => date.Date, s => s.Sum()) * 100;
            var liveResampled     = liveSeries.PercentChange().ResampleEquivalence(date => date.Date, s => s.Sum()) * 100;

            var base64 = "";

            using (Py.GIL())
            {
                var backtestList = new PyList();
                backtestList.Append(backtestResampled.Keys.ToList().ToPython());
                backtestList.Append(backtestResampled.Values.ToList().ToPython());

                var liveList = new PyList();
                liveList.Append(liveResampled.Keys.ToList().ToPython());
                liveList.Append(liveResampled.Values.ToList().ToPython());

                base64 = Charting.GetDailyReturns(backtestList, liveList);

                backtestList.Dispose();
                liveList.Dispose();
            }

            return(base64);
        }
Exemple #2
0
        /// <summary>
        /// Try to get the length of arguments of a method
        /// </summary>
        /// <param name="pyObject">Object representing a method</param>
        /// <param name="length">Lenght of arguments</param>
        /// <returns>True if pyObject is a method</returns>
        private static bool TryGetArgLength(PyObject pyObject, out long length)
        {
            using (Py.GIL())
            {
                var inspect = lazyInspect.Value;
                if (inspect.isfunction(pyObject))
                {
                    var args   = inspect.getargspec(pyObject).args as PyObject;
                    var pyList = new PyList(args);
                    length = pyList.Length();
                    pyList.Dispose();
                    args.Dispose();
                    return(true);
                }

                if (inspect.ismethod(pyObject))
                {
                    var args   = inspect.getargspec(pyObject).args as PyObject;
                    var pyList = new PyList(args);
                    length = pyList.Length() - 1;
                    pyList.Dispose();
                    args.Dispose();
                    return(true);
                }
            }
            length = 0;
            return(false);
        }
Exemple #3
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);
        }
Exemple #4
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);
            }
        }
Exemple #5
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();
                }
            }
        }
Exemple #6
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);
        }