public static PyObject ToPython(this JointTrajectory obj)
        {
            var jointSet = PyConvert.ToPyObject(obj.JointSet);

            using (Py.GIL())
            {
                var points = new PyList();
                foreach (var p in obj)
                {
                    points.Append(PyConvert.ToPyObject(p));
                }
                return(pyXamlaMotionTypes.JointTrajectory(jointSet, points));
            }
        }
        public static PyObject ToPython(this CollisionObject obj)
        {
            var frame = PyConvert.ToPyObject(obj.Frame);

            using (Py.GIL())
            {
                var primitives = new PyList();
                foreach (var p in obj.Primitives)
                {
                    primitives.Append(PyConvert.ToPyObject(p));
                }
                return(pyXamlaMotionTypes.CollisionObject(primitives, frame));
            }
        }
        /// <summary>
        /// Get fundamental data from given symbols
        /// </summary>
        /// <param name="pyObject">The symbols to retrieve fundamental data for</param>
        /// <param name="selector">Selects a value from the Fundamental data to filter the request output</param>
        /// <param name="start">The start date of selected data</param>
        /// <param name="end">The end date of selected data</param>
        /// <returns></returns>
        public PyObject GetFundamental(PyObject tickers, string selector, DateTime? start = null, DateTime? end = null)
        {
            if (string.IsNullOrWhiteSpace(selector))
            {
                return "Invalid selector. Cannot be None, empty or consist only of white-space characters".ToPython();
            }

            using (Py.GIL())
            {
                // If tickers are not a PyList, we create one
                if (!PyList.IsListType(tickers))
                {
                    var tmp = new PyList();
                    tmp.Append(tickers);
                    tickers = tmp;
                }

                var list = new List<Tuple<Symbol, DateTime, object>>();

                foreach (var ticker in tickers)
                {
                    var symbol = QuantConnect.Symbol.Create(ticker.ToString(), SecurityType.Equity, Market.USA);
                    var dir = new DirectoryInfo(Path.Combine(Globals.DataFolder, "equity", symbol.ID.Market, "fundamental", "fine", symbol.Value.ToLower()));
                    if (!dir.Exists) continue;

                    var config = new SubscriptionDataConfig(typeof(FineFundamental), symbol, Resolution.Daily, TimeZones.NewYork, TimeZones.NewYork, false, false, false);

                    foreach (var fileName in dir.EnumerateFiles())
                    {
                        var date = DateTime.ParseExact(fileName.Name.Substring(0, 8), DateFormat.EightCharacter, CultureInfo.InvariantCulture);
                        if (date < start || date > end) continue;

                        var factory = new TextSubscriptionDataSourceReader(_dataCacheProvider, config, date, false);
                        var source = new SubscriptionDataSource(fileName.FullName, SubscriptionTransportMedium.LocalFile);
                        var value = factory.Read(source).Select(x => GetPropertyValue(x, selector)).First();

                        list.Add(Tuple.Create(symbol, date, value));
                    }
                }

                var data = new PyDict();
                foreach (var item in list.GroupBy(x => x.Item1))
                {
                    var index = item.Select(x => x.Item2);
                    data.SetItem(item.Key, _pandas.Series(item.Select(x => x.Item3).ToList(), index));
                }

                return _pandas.DataFrame(data);
            }
        }
        /// <summary>
        /// Generate the leverage utilization plot using the python libraries.
        /// </summary>
        public override string Render()
        {
            var backtestSeries = Metrics.LeverageUtilization(_backtestPortfolios).FillMissing(Direction.Forward);
            var liveSeries     = Metrics.LeverageUtilization(_livePortfolios).FillMissing(Direction.Forward);

            var base64 = "";

            using (Py.GIL())
            {
                var backtestList = new PyList();
                var liveList     = new PyList();

                backtestList.Append(backtestSeries.Keys.ToList().ToPython());
                backtestList.Append(backtestSeries.Values.ToList().ToPython());

                liveList.Append(liveSeries.Keys.ToList().ToPython());
                liveList.Append(liveSeries.Values.ToList().ToPython());

                base64 = Charting.GetLeverage(backtestList, liveList);
            }

            return(base64);
        }
        public static PyObject ToPython(this JointPath obj)
        {
            var jointSet = PyConvert.ToPyObject(obj.JointSet);

            using (Py.GIL())
            {
                var jointValues = new PyList();
                foreach (var v in obj)
                {
                    jointValues.Append(PyConvert.ToPyObject(v));
                }
                return(pyXamlaMotionTypes.JointPath(jointSet, jointValues));
            }
        }
Exemple #6
0
        public float predict(List <int> endTurnFeature, dynamic hand_feature)
        {
            PyList feature_list = new PyList();
            PyList end_feature  = new PyList();

            foreach (int f in endTurnFeature)
            {
                end_feature.Append(new PyInt(f));
            }
            feature_list.Append(np.array(end_feature).reshape(1, -1));
            feature_list.Append(hand_feature.reshape(1, -1));
            //dynamic result = evaluator.model.predict_proba(feature_list, Py.kw("verbose", 0))[0];
            dynamic result = evaluator.model.predict(feature_list, Py.kw("verbose", 0))[0];

            return(result);
        }
Exemple #7
0
        public DocBin(string[] attrs, bool storeUserData)
        {
            using (Py.GIL())
            {
                var pyAttrs = new PyList();
                if (attrs != null)
                {
                    foreach (var att in attrs)
                    {
                        var pyAtt = new PyString(att);
                        pyAttrs.Append(pyAtt);
                    }
                }

                var     pyStoreUserDate = new PyInt(storeUserData ? 1 : 0);
                dynamic spacy           = Py.Import("spacy");
                _pyDocBin = spacy.tokens.DocBin.__call__(pyAttrs, pyStoreUserDate);
            }
        }
Exemple #8
0
 /// <summary>
 /// Returns a view object that displays a list of dictionary's (Symbol, value) tuple pairs.
 /// </summary>
 /// <returns>Returns a view object that displays a list of a given dictionary's (Symbol, value) tuple pair.</returns>
 public PyList items()
 {
     using (Py.GIL())
     {
         var pyList = new PyList();
         foreach (var key in GetKeys)
         {
             using (var pyKey = key.ToPython())
             {
                 using (var pyValue = this[key].ToPython())
                 {
                     using (var pyObject = new PyTuple(new PyObject[] { pyKey, pyValue }))
                     {
                         pyList.Append(pyObject);
                     }
                 }
             }
         }
         return(pyList);
     }
 }
Exemple #9
0
        public override void FillIdxArr(PyList idxArr, int offset)
        {
            PyList Dim1 = new PyList();
            PyList Dim2 = new PyList();
            PyList Dim3 = new PyList();

            for (int i = 0; i < data.Length; i++)
            {
                for (int j = 0; j < data[i].Length; j++)
                {
                    //npArray[data[i][j] + offset][i].itemset(j, 1);
                    Dim1.Append(FeatureConst.Instance.pyIntMap[data[i][j] + offset]);
                    Dim2.Append(FeatureConst.Instance.pyIntMap[i]);
                    Dim3.Append(FeatureConst.Instance.pyIntMap[j]);
                }
            }

            PythonUtils.AppendRecycle(idxArr, Dim1);
            PythonUtils.AppendRecycle(idxArr, Dim2);
            PythonUtils.AppendRecycle(idxArr, Dim3);
        }
Exemple #10
0
        public PyList getHandFeature(Playfield p, bool own)
        {
            Player mPlayer, ePlayer;

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

            int[] featureArray = new int[cardArray.Length * 2];
            foreach (Handmanager.Handcard hc in mPlayer.owncards)
            {
                int idx = cardIdxDict[hc.card.name];
                featureArray[idx] = featureArray[idx] + 1;
            }

            foreach (Handmanager.Handcard hc in ePlayer.owncards)
            {
                int idx = cardIdxDict[hc.card.name] + 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);
        }
Exemple #11
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();
                }
            }
        }
        /// <summary>
        /// The generated output string to be injected
        /// </summary>
        public override string Render()
        {
            var backtestPoints          = ResultsUtil.EquityPoints(_backtest);
            var backtestBenchmarkPoints = ResultsUtil.BenchmarkPoints(_backtest);

            var backtestSeries          = new Series <DateTime, double>(backtestPoints.Keys, backtestPoints.Values);
            var backtestBenchmarkSeries = new Series <DateTime, double>(backtestBenchmarkPoints.Keys, backtestBenchmarkPoints.Values);

            var html = new List <string>();

            foreach (var crisisEvent in Crisis.Events)
            {
                using (Py.GIL())
                {
                    var crisis = crisisEvent.Value;
                    var data   = new PyList();
                    var frame  = Frame.CreateEmpty <DateTime, string>();

                    // The two following operations are equivalent to Pandas' `df.resample("D").sum()`
                    frame["Backtest"]  = backtestSeries.ResampleEquivalence(date => date.Date, s => s.LastValue());
                    frame["Benchmark"] = backtestBenchmarkSeries.ResampleEquivalence(date => date.Date, s => s.LastValue());

                    var crisisFrame = frame.Where(kvp => kvp.Key >= crisis.Start && kvp.Key <= crisis.End);
                    crisisFrame = crisisFrame.Join("BacktestPercent", crisisFrame["Backtest"].CumulativeReturns());
                    crisisFrame = crisisFrame.Join("BenchmarkPercent", crisisFrame["Benchmark"].CumulativeReturns());

                    // Pad out all missing values to start from 0 for nice plots
                    crisisFrame = crisisFrame.FillMissing(Direction.Forward).FillMissing(0.0);

                    data.Append(crisisFrame.RowKeys.ToList().ToPython());
                    data.Append(crisisFrame["BacktestPercent"].Values.ToList().ToPython());
                    data.Append(crisisFrame["BenchmarkPercent"].Values.ToList().ToPython());

                    var base64 = (string)Charting.GetCrisisEventsPlots(data, crisis.Name.Replace("/", "").Replace(".", "").Replace(" ", ""));

                    if (base64 == _emptyChart)
                    {
                        continue;
                    }

                    if (!crisisFrame.IsEmpty)
                    {
                        var contents = _template.Replace(ReportKey.CrisisTitle, crisis.ToString(crisisFrame.GetRowKeyAt(0), crisisFrame.GetRowKeyAt(crisisFrame.RowCount - 1)));
                        contents = contents.Replace(ReportKey.CrisisContents, base64);

                        html.Add(contents);
                    }
                }
            }

            if (Key == ReportKey.CrisisPageStyle)
            {
                if (html.Count == 0)
                {
                    return("display: none;");
                }

                return(string.Empty);
            }

            return(string.Join("\n", html));
        }
Exemple #13
0
        public PyList[] getPyFeatureData()
        {
            PyList[] ret = new PyList[9];

            PyList pyGlobal = new PyList();

            foreach (int f in featrueArray[0].data)
            {
                pyGlobal.Append(FeatureConst.Instance.pyIntMap[f]);
            }

            PyList pyOwnMinionHp = new PyList();

            foreach (int f in featrueArray[1].data)
            {
                pyOwnMinionHp.Append(FeatureConst.Instance.pyIntMap[f]);
            }

            PyList pyOwnMinion = new PyList();

            foreach (int f in featrueArray[2].data)
            {
                pyOwnMinion.Append(FeatureConst.Instance.pyIntMap[f]);
            }

            PyList pyOwnCard = new PyList();

            foreach (int f in featrueArray[3].data)
            {
                pyOwnCard.Append(FeatureConst.Instance.pyIntMap[f]);
            }

            PyList pyOwnPlayed = new PyList();

            foreach (int f in featrueArray[4].data)
            {
                pyOwnPlayed.Append(FeatureConst.Instance.pyIntMap[f]);
            }

            PyList pyOwnPlayable = new PyList();

            foreach (int[] action in featrueArray[5].data)
            {
                PyList row = new PyList();
                foreach (int f in action)
                {
                    row.Append(FeatureConst.Instance.pyIntMap[f]);
                }
                PythonUtils.AppendRecycle(pyOwnPlayable, row);
            }

            PyList pyEnemyMinionHp = new PyList();

            foreach (int f in featrueArray[6].data)
            {
                pyEnemyMinionHp.Append(FeatureConst.Instance.pyIntMap[f]);
            }

            PyList pyEnemyMinion = new PyList();

            foreach (int f in featrueArray[7].data)
            {
                pyEnemyMinion.Append(FeatureConst.Instance.pyIntMap[f]);
            }

            PyList pyEnemyCard = new PyList();

            foreach (int f in featrueArray[8].data)
            {
                pyEnemyCard.Append(FeatureConst.Instance.pyIntMap[f]);
            }

            ret[0] = pyGlobal;
            ret[1] = pyOwnMinionHp;
            ret[2] = pyOwnMinion;
            ret[3] = pyOwnCard;
            ret[4] = pyOwnPlayed;
            ret[5] = pyOwnPlayable;
            ret[6] = pyEnemyMinionHp;
            ret[7] = pyEnemyMinion;
            ret[8] = pyEnemyCard;

            return(ret);
        }
Exemple #14
0
        public static void NumpyFeaturization(Playfield pf, dynamic globalIdxArr, dynamic boardIdxArr, dynamic handIdxArr, dynamic playIdxArr)
        {
            Player mPlayer   = pf.getCurrentPlayer(true);
            Player ePlayer   = pf.getCurrentPlayer(false);
            int    ownMana   = mPlayer.ownMaxMana;
            int    enemyMana = ePlayer.ownMaxMana;
            int    ownHp     = mPlayer.ownHero.getHeroHpValue();
            int    enemyHp   = ePlayer.ownHero.getHeroHpValue();
            int    glbIdx    = 0;

            glbIdx += ownMana - 1;
            //globalIdxArr.itemset(glbIdx, 1);
            globalIdxArr.Append(FeatureConst.Instance.pyIntMap[glbIdx]);
            glbIdx = 10;
            if (!pf.isOwnTurn)
            {
                globalIdxArr.Append(FeatureConst.Instance.pyIntMap[glbIdx]);
            }
            int heroIdx = FeatureConst.Instance.heroHpDict[ownHp + enemyHp * 10];

            glbIdx += heroIdx;
            globalIdxArr.Append(FeatureConst.Instance.pyIntMap[glbIdx]);

            BinaryFeature boardFeature = encodeBoard(pf);

            boardFeature.FillIdxArr(boardIdxArr, 0);

            BinaryFeature handFeature = encodeHand(pf);
            //BinaryFeature DeckFeature = encodeDeck(pf);

            PyList dim1 = new PyList();
            PyList dim2 = new PyList();

            for (int i = 0; i < handFeature.data.Length; i++)
            {
                dim1.Append(FeatureConst.Instance.pyIntMap[handFeature.data[i]]);
                dim2.Append(FeatureConst.Instance.pyIntMap[i]);
            }

            //for (int i = 0; i < DeckFeature.data.Length; i++)
            //{
            //    dim1.Append(FeatureConst.Instance.pyIntMap[DeckFeature.data[i] + 9]);
            //    dim2.Append(FeatureConst.Instance.pyIntMap[i]);
            //}

            PythonUtils.AppendRecycle(handIdxArr, dim1);
            PythonUtils.AppendRecycle(handIdxArr, dim2);

            BinaryFeature playableFeature = encodePlay(pf);

            dim1 = new PyList();
            dim2 = new PyList();

            for (int i = 0; i < playableFeature.data.Length; i++)
            {
                if (playableFeature.data[i] > 0)
                {
                    dim1.Append(FeatureConst.Instance.pyIntMap[0]);
                    dim2.Append(FeatureConst.Instance.pyIntMap[i]);
                }
            }

            BinaryFeature playedFeature = encodePlayed(pf);

            for (int i = 0; i < playedFeature.data.Length; i++)
            {
                if (playedFeature.data[i] > 0)
                {
                    dim1.Append(FeatureConst.Instance.pyIntMap[1]);
                    dim2.Append(FeatureConst.Instance.pyIntMap[i]);
                }
            }
            PythonUtils.AppendRecycle(playIdxArr, dim1);
            PythonUtils.AppendRecycle(playIdxArr, dim2);
        }
        /// <summary>
        /// Generate the cumulative return of the backtest, benchmark, and live
        /// strategy using the ReportCharts.py python library
        /// </summary>
        public override string Render()
        {
            var backtestReturns = ResultsUtil.EquityPoints(_backtest);
            var benchmark       = ResultsUtil.BenchmarkPoints(_backtest);
            var liveReturns     = ResultsUtil.EquityPoints(_live);
            var liveBenchmark   = ResultsUtil.BenchmarkPoints(_live);

            var backtestTime     = backtestReturns.Keys.ToList();
            var backtestStrategy = backtestReturns.Values.ToList();
            var benchmarkTime    = benchmark.Keys.ToList();
            var benchmarkPoints  = benchmark.Values.ToList();

            var liveTime              = liveReturns.Keys.ToList();
            var liveStrategy          = liveReturns.Values.ToList();
            var liveBenchmarkTime     = liveBenchmark.Keys.ToList();
            var liveBenchmarkStrategy = liveBenchmark.Values.ToList();

            var base64 = "";

            using (Py.GIL())
            {
                var backtestList = new PyList();
                var liveList     = new PyList();

                var backtestSeries          = new Series <DateTime, double>(backtestTime, backtestStrategy);
                var liveSeries              = new Series <DateTime, double>(liveTime, liveStrategy);
                var backtestBenchmarkSeries = new Series <DateTime, double>(benchmarkTime, benchmarkPoints);
                var liveBenchmarkSeries     = new Series <DateTime, double>(liveBenchmarkTime, liveBenchmarkStrategy);

                // Equivalent in python using pandas for the following operations is:
                // --------------------------------------------------
                // >>> # note: [...] denotes the data we're passing in
                // >>> df = pd.Series([...], index=time)
                // >>> df_live = pd.Series([...], index=live_time)
                // >>> df_live = df_live.mul(df.iloc[-1] / df_live.iloc[0]).fillna(method='ffill').dropna()
                // >>> df_final = pd.concat([df, df_live], axis=0)
                // >>> df_cumulative_returns = ((df_final.pct_change().dropna() + 1).cumprod() - 1)
                // --------------------------------------------------
                //
                // We multiply the final value of the backtest and benchmark to have a continuous graph showing the performance out of sample
                // as a continuation of the cumulative returns graph. Otherwise, we start plotting from 0% and not the last value of the backtest data

                var backtestLastValue          = backtestSeries.ValueCount == 0 ? 0 : backtestSeries.LastValue();
                var backtestBenchmarkLastValue = backtestBenchmarkSeries.ValueCount == 0 ? 0 : backtestBenchmarkSeries.LastValue();

                var liveContinuousEquity      = liveSeries;
                var liveBenchContinuousEquity = liveBenchmarkSeries;

                if (liveSeries.ValueCount != 0)
                {
                    liveContinuousEquity = (liveSeries * (backtestLastValue / liveSeries.FirstValue()))
                                           .FillMissing(Direction.Forward)
                                           .DropMissing();
                }
                if (liveBenchmarkSeries.ValueCount != 0)
                {
                    liveBenchContinuousEquity = (liveBenchmarkSeries * (backtestBenchmarkLastValue / liveBenchmarkSeries.FirstValue()))
                                                .FillMissing(Direction.Forward)
                                                .DropMissing();
                }

                var liveStart      = liveContinuousEquity.ValueCount == 0 ? DateTime.MaxValue : liveContinuousEquity.DropMissing().FirstKey();
                var liveBenchStart = liveBenchContinuousEquity.ValueCount == 0 ? DateTime.MaxValue : liveBenchContinuousEquity.DropMissing().FirstKey();

                var finalEquity      = backtestSeries.Where(kvp => kvp.Key < liveStart).Observations.ToList();
                var finalBenchEquity = backtestBenchmarkSeries.Where(kvp => kvp.Key < liveBenchStart).Observations.ToList();

                finalEquity.AddRange(liveContinuousEquity.Observations);
                finalBenchEquity.AddRange(liveBenchContinuousEquity.Observations);

                var finalSeries = (new Series <DateTime, double>(finalEquity).CumulativeReturns() * 100)
                                  .FillMissing(Direction.Forward)
                                  .DropMissing();

                var finalBenchSeries = (new Series <DateTime, double>(finalBenchEquity).CumulativeReturns() * 100)
                                       .FillMissing(Direction.Forward)
                                       .DropMissing();

                var backtestCumulativePercent          = finalSeries.Where(kvp => kvp.Key < liveStart);
                var backtestBenchmarkCumulativePercent = finalBenchSeries.Where(kvp => kvp.Key < liveBenchStart);

                var liveCumulativePercent          = finalSeries.Where(kvp => kvp.Key >= liveStart);
                var liveBenchmarkCumulativePercent = finalBenchSeries.Where(kvp => kvp.Key >= liveBenchStart);

                backtestList.Append(backtestCumulativePercent.Keys.ToList().ToPython());
                backtestList.Append(backtestCumulativePercent.Values.ToList().ToPython());
                backtestList.Append(backtestBenchmarkCumulativePercent.Keys.ToList().ToPython());
                backtestList.Append(backtestBenchmarkCumulativePercent.Values.ToList().ToPython());

                liveList.Append(liveCumulativePercent.Keys.ToList().ToPython());
                liveList.Append(liveCumulativePercent.Values.ToList().ToPython());
                liveList.Append(liveBenchmarkCumulativePercent.Keys.ToList().ToPython());
                liveList.Append(liveBenchmarkCumulativePercent.Values.ToList().ToPython());

                base64 = Charting.GetCumulativeReturns(backtestList, liveList);
            }

            return(base64);
        }
Exemple #16
0
 public static void AppendRecycle(PyList list, PyObject obj)
 {
     list.Append(obj);
     obj.Dispose();
 }
Exemple #17
0
        public static PyObject ToPyObject(object obj)
        {
            if (obj == null)
            {
                return(obj.ToPython());
            }
            var type = obj.GetType();

            if (type.IsPrimitive)
            {
                return(obj.ToPython());
            }
            else if (obj is string)
            {
                return(obj.ToPython());
            }
            else if (obj is ITuple t)
            {
                var items = TypeHelpers.GetTupleValues(obj).Select(ToPyObject).ToArray();
                return(new PyTuple(items));
            }
            else if (obj is IDictionary d)
            {
                // create dictionary
                var dict = new PyDict();
                foreach (var key in d.Keys)
                {
                    var value = d[key];
                    var pyKey = ToPyObject(key);
                    dict[pyKey] = ToPyObject(value);
                }
                return(dict);
            }
            else if (obj is A a)
            {
                return(NumpyHelper.CreateNdArray(a));
            }
            else if (obj is IList l)
            {
                // create list
                var list = new PyList();
                foreach (var x in l)
                {
                    list.Append(ToPyObject(x));
                }
                return(list);
            }
            else if (obj is JointSet s)
            {
                return(s.ToPython());
            }
            else if (obj is JointValues v)
            {
                return(v.ToPython());
            }
            else if (obj is Pose p)
            {
                return(p.ToPython());
            }
            else if (obj is TimeSpan timeSpan)
            {
                return(timeSpan.ToPython());
            }
            else if (obj is JointTrajectoryPoint jPoint)
            {
                return(jPoint.ToPython());
            }
            else if (obj is JointTrajectory jTrajectory)
            {
                return(jTrajectory.ToPython());
            }
            else if (obj is JointStates states)
            {
                return(states.ToPython());
            }
            else if (obj is JointPath jPath)
            {
                return(jPath.ToPython());
            }
            else if (obj is CartesianPath cPath)
            {
                return(cPath.ToPython());
            }
            else if (obj is PlanParameters parameters)
            {
                return(parameters.ToPython());
            }
            else if (obj is CollisionPrimitive primitive)
            {
                return(primitive.ToPython());
            }
            else if (obj is CollisionObject collisionObject)
            {
                return(collisionObject.ToPython());
            }

            throw new Exception("Object conversion not supported");
        }
        /// <summary>
        /// Generate the cumulative return of the backtest, benchmark, and live
        /// strategy using the ReportCharts.py python library
        /// </summary>
        public override string Render()
        {
            var backtestReturns = ResultsUtil.EquityPoints(_backtest);
            var benchmark       = ResultsUtil.BenchmarkPoints(_backtest);
            var liveReturns     = ResultsUtil.EquityPoints(_live);
            var liveBenchmark   = ResultsUtil.BenchmarkPoints(_live);

            var backtestTime     = backtestReturns.Keys.ToList();
            var backtestStrategy = backtestReturns.Values.ToList();
            var benchmarkTime    = benchmark.Keys.ToList();
            var benchmarkPoints  = benchmark.Values.ToList();

            var liveTime              = liveReturns.Keys.ToList();
            var liveStrategy          = liveReturns.Values.ToList();
            var liveBenchmarkTime     = liveBenchmark.Keys.ToList();
            var liveBenchmarkStrategy = liveBenchmark.Values.ToList();

            var base64 = "";

            using (Py.GIL())
            {
                var backtestList = new PyList();
                var liveList     = new PyList();

                var backtestSeries          = new Series <DateTime, double>(backtestTime, backtestStrategy);
                var liveSeries              = new Series <DateTime, double>(liveTime, liveStrategy);
                var backtestBenchmarkSeries = new Series <DateTime, double>(benchmarkTime, benchmarkPoints);
                var liveBenchmarkSeries     = new Series <DateTime, double>(liveBenchmarkTime, liveBenchmarkStrategy);

                // Equivalent in python using pandas for the following operations is:
                //
                // df.pct_change().cumsum().mul(100)
                var backtestCumulativePercent          = (backtestSeries.CumulativeReturns() * 100).FillMissing(Direction.Forward).DropMissing();
                var backtestBenchmarkCumulativePercent = (backtestBenchmarkSeries.CumulativeReturns() * 100).FillMissing(Direction.Forward).DropMissing();

                // Equivalent in python using pandas for the following operations is:
                // --------------------------------------------------
                // # note: [...] denotes the data we're passing in
                // bt = pd.Series([...], index=time)
                // df.pct_change().replace([np.inf, -np.inf], np.nan).dropna().cumsum().mul(100).add(bt.iloc[-1])
                // --------------------------------------------------
                //
                // We add the final value of the backtest and benchmark to have a continuous graph showing the performance out of sample
                // as a continuation of the cumulative returns graph. Otherwise, we start plotting from 0% and not the last value of the backtest data

                var backtestLastValue          = backtestCumulativePercent.IsEmpty ? 0 : backtestCumulativePercent.LastValue();
                var backtestBenchmarkLastValue = backtestBenchmarkCumulativePercent.IsEmpty ? 0 : backtestBenchmarkCumulativePercent.LastValue();

                var liveCumulativePercent          = (liveSeries.CumulativeReturns() * 100).FillMissing(Direction.Forward).DropMissing() + backtestLastValue;
                var liveBenchmarkCumulativePercent = (liveBenchmarkSeries.CumulativeReturns() * 100).FillMissing(Direction.Forward).DropMissing() + backtestBenchmarkLastValue;

                backtestList.Append(backtestCumulativePercent.Keys.ToList().ToPython());
                backtestList.Append(backtestCumulativePercent.Values.ToList().ToPython());
                backtestList.Append(backtestBenchmarkCumulativePercent.Keys.ToList().ToPython());
                backtestList.Append(backtestBenchmarkCumulativePercent.Values.ToList().ToPython());

                liveList.Append(liveCumulativePercent.Keys.ToList().ToPython());
                liveList.Append(liveCumulativePercent.Values.ToList().ToPython());
                liveList.Append(liveBenchmarkCumulativePercent.Keys.ToList().ToPython());
                liveList.Append(liveBenchmarkCumulativePercent.Values.ToList().ToPython());

                base64 = Charting.GetCumulativeReturns(backtestList, liveList);
            }

            return(base64);
        }
Exemple #19
0
        private void DQN(object sender, RoutedEventArgs e)
        {
            bool side     = true; //own side
            int  numGames = 5000;

            using (Py.GIL())
            {
                dynamic dqn   = Py.Import("simple_dqn.dqn");
                dynamic Agent = dqn.Agent;

                Console.WriteLine("Done importing");
                PyInt   width      = new PyInt(458);
                dynamic state_size = new PyTuple(new PyObject[] { width });
                dynamic agent      = Agent(Py.kw("state_size", state_size), Py.kw("discount", 0.95));
                Console.WriteLine(agent.discount);

                for (int i = 0; i < numGames; i++)
                {
                    PyFloat epsilon = new PyFloat(Math.Max(0.1, 1 - (double)(i + 1) * 4 / numGames));
                    agent.epsilon = epsilon;
                    if (!isInit)
                    {
                        Init(true, i);
                    }
                    Playfield state          = GameManager.Instance.mPlayfield;
                    int       result         = state.getGameResult();
                    float     total_cost     = 0.0f;
                    float     total_reward   = 0.0f;
                    float     reward         = 0.0f;
                    float     cost           = 0.0f;
                    int       num_move       = 0;
                    int       end_turn_count = 0;

                    agent.new_episode();
                    GreedyPlayer greedyPlayer = (GreedyPlayer)GameManager.Instance.playerFirst;

                    while (result == -1)
                    {
                        Macro moveTodo = null;
                        reward = 0.0f;
                        cost   = 0.0f;

                        if (state.isOwnTurn)
                        {
                            List <Macro> macroMoveList = greedyPlayer.getMacros(state);

                            macroMoveList.Add(null);

                            dynamic          cur_state_vector  = DNNEval.Instance.parsePlayfield(state, side);
                            List <Playfield> stateList         = new List <Playfield>();
                            PyList           state_vector_list = new PyList();
                            foreach (Macro macro in macroMoveList)
                            {
                                Playfield nextState = new Playfield(state);
                                if (macro != null)
                                {
                                    nextState.doMacroAction(macro);
                                    stateList.Add(nextState);
                                    reward = nextState.observerMoveReward();
                                }
                                else
                                {
                                    nextState.endTurn(false, false);
                                    nextState.drawTurnStartCard();
                                    //simulateEnemyTurn(nextState, false, true);
                                }
                                dynamic state_vector = DNNEval.Instance.parsePlayfield(nextState, side);
                                state_vector_list.Append(state_vector);
                            }
                            int action_index = agent.act(cur_state_vector, state_vector_list);
                            Helpfunctions.Instance.logg("Length:" + state_vector_list.Length());

                            moveTodo = macroMoveList[action_index];

                            if (moveTodo != null)
                            {
                                Helpfunctions.Instance.logg("Player 1 ##########Move##########");
                                //moveTodo.print();
                                state.doMacroAction(moveTodo);
                                reward = state.observerMoveReward();

                                num_move += 1;
                                cost      = agent.observe(reward);
                                Helpfunctions.Instance.logg("action_index = " + action_index + "/" + (macroMoveList.Count - 1) + "reward = " + reward);
                            }
                            else
                            {
                                if (state.playerFirst.mana == state.playerFirst.ownMaxMana)
                                {
                                    end_turn_count++;
                                }
                                Helpfunctions.Instance.logg("Player 1 ##########Move##########");
                                Helpfunctions.Instance.logg("Player 1 end turn");
                                state.endTurn(false, false);
                                state.drawTurnStartCard();

                                dynamic end_state_vector = DNNEval.Instance.parsePlayfield(state, side);
                                agent.update_transition(end_state_vector);
                                reward = 0.0f;

                                num_move += 1;
                                cost      = agent.observe(reward);
                                Helpfunctions.Instance.logg("action_index = " + action_index + "/" + (macroMoveList.Count - 1) + "reward = " + reward);

                                total_reward += simulateEnemyTurn(state, false, false, agent);
                                Helpfunctions.Instance.logg("Player 2 end turn");
                            }

                            //state.printBoard();

                            total_cost   += cost;
                            total_reward += reward;
                            result        = state.getGameResult();
                        }
                        else
                        {
                            int debug = 1;
                        }
                    }

                    if (result == 0)
                    {
                        int debug = 1;
                    }

                    isInit = false;
                    Helpfunctions.Instance.logg("last reward: " + reward);
                    Helpfunctions.Instance.logg("total reward: " + total_reward);
                    Helpfunctions.Instance.logg("avg cost: " + total_cost / num_move);
                    Helpfunctions.Instance.WriteResultToFile(@"\learning_log.txt", "No." + i + " total/avg cost: " + total_reward + ", " + total_cost / num_move + ", end turn: " + (float)end_turn_count / num_move);
                }
            }


            //for e in xrange(num_episodes):
            //    observation = env.reset()
            //    done = False
            //    agent.new_episode()
            //    total_cost = 0.0
            //    total_reward = 0.0
            //    frame = 0
            //    while not done:
            //        frame += 1
            //        #env.render()
            //        action, values = agent.act(observation)
            //        #action = env.action_space.sample()
            //        observation, reward, done, info = env.step(action)
            //        print reward
            //        total_cost += agent.observe(reward)
            //        total_reward += reward
            //    print "total reward", total_reward
            //    print "mean cost", total_cost/frame
        }
Exemple #20
0
        private void DQNTurn(object sender, RoutedEventArgs e)
        {
            bool side     = true; //own side
            int  numGames = 500;

            using (Py.GIL())
            {
                dynamic dqn   = Py.Import("simple_dqn.dqn");
                dynamic Agent = dqn.Agent;

                Console.WriteLine("Done importing");
                PyInt   width      = new PyInt(458);
                dynamic state_size = new PyTuple(new PyObject[] { width });
                dynamic agent      = Agent(Py.kw("state_size", state_size), Py.kw("discount", 0.95));
                Console.WriteLine(agent.discount);

                for (int i = 0; i < numGames; i++)
                {
                    if (!isInit)
                    {
                        Init(true, i);
                    }
                    Playfield state          = GameManager.Instance.mPlayfield;
                    int       result         = state.getGameResult();
                    float     total_cost     = 0.0f;
                    float     total_reward   = 0.0f;
                    float     reward         = 0.0f;
                    float     cost           = 0.0f;
                    int       num_move       = 0;
                    int       end_turn_count = 0;

                    agent.new_episode();

                    while (result == -1)
                    {
                        Action moveTodo = null;
                        reward = 0.0f;
                        cost   = 0.0f;

                        if (state.isOwnTurn)
                        {
                            List <Action> moveList = Movegenerator.Instance.getMoveList(state, false, true, true, 0.0);
                            moveList.Add(null);

                            dynamic          cur_state_vector  = DNNEval.Instance.parsePlayfield(state, side);
                            List <Playfield> stateList         = new List <Playfield>();
                            PyList           state_vector_list = new PyList();
                            foreach (Action action in moveList)
                            {
                                Playfield nextState = new Playfield(state);
                                if (action != null)
                                {
                                    nextState.doAction(action);
                                    stateList.Add(nextState);
                                    reward = nextState.observerMoveReward();
                                }
                                else
                                {
                                    nextState.endTurn(false, false);
                                    nextState.drawTurnStartCard();
                                    //simulateEnemyTurn(nextState, false, true, agent);
                                }
                                dynamic state_vector = DNNEval.Instance.parsePlayfield(nextState, side);
                                state_vector_list.Append(state_vector);
                            }
                            int action_index = agent.act(cur_state_vector, state_vector_list);
                            Helpfunctions.Instance.logg("Length:" + state_vector_list.Length());

                            moveTodo = moveList[action_index];

                            if (moveTodo != null)
                            {
                                Helpfunctions.Instance.logg("Player 1 ##########Move##########");
                                moveTodo.print();
                                state.doAction(moveTodo);
                                reward = state.observerMoveReward();
                            }
                            else
                            {
                                if (state.playerFirst.mana == state.playerFirst.ownMaxMana)
                                {
                                    end_turn_count++;
                                }
                                Helpfunctions.Instance.logg("Player 1 ##########Move##########");
                                Helpfunctions.Instance.logg("Player 1 end turn");
                                state.endTurn(false, false);
                                state.drawTurnStartCard();
                                reward        = 0.0f;
                                total_reward += simulateEnemyTurn(state, false, false, agent);
                                Helpfunctions.Instance.logg("Player 2 end turn");
                            }

                            //state.printBoard();

                            num_move += 1;

                            cost = agent.observe(reward);
                            Helpfunctions.Instance.logg("reward = " + reward);
                            total_cost   += cost;
                            total_reward += reward;
                            result        = state.getGameResult();
                        }
                        else
                        {
                            int debug = 1;
                        }
                    }

                    if (result == 0)
                    {
                        int debug = 1;
                    }

                    isInit = false;
                    Helpfunctions.Instance.logg("last reward: " + reward);
                    Helpfunctions.Instance.logg("total reward: " + total_reward);
                    Helpfunctions.Instance.logg("avg cost: " + total_cost / num_move);
                    Helpfunctions.Instance.WriteResultToFile(@"\learning_log.txt", "No." + i + " total/avg cost: " + total_reward + ", " + total_cost / num_move + ", end turn: " + (float)end_turn_count / num_move);
                }
            }
        }