コード例 #1
0
        public void ExecuteBest(DateTime nowDate, List<CandidateClassifier> minScoreInfos)
        {
            OutputRealDealSummary(nowDate);

            float totalCost = 0;
            int totalDeal = 0;

            if (minScoreInfos != null)
            {
                List<DealInfo> candidateDeals = new List<DealInfo>();
                foreach (var minScoreInfo in minScoreInfos)
                {
                    minScoreInfo.WekaData.GenerateData(false, true);
                    weka.core.Instances minTestInstances = minScoreInfo.WekaData.CurrentTestInstances;
                    weka.core.Instances minTestInstancesNew = minScoreInfo.WekaData.CurrentTestInstancesNew;

                    if (minTestInstances.numInstances() > 0)
                    {
                        MyEvaluation eval = new MyEvaluation(minScoreInfo.CostMatrix);
                        eval.evaluateModel(minScoreInfo.CurrentTestRet, minScoreInfo.CurrentClassValue);

                        float vol;
                        // vol = (float)minScoreInfo.MoneyManagement.GetVolume(null);
                        vol = 0.1F;
                        //vol = (float)Math.Round(minScore / -20000.0, 1);
                        //WekaUtils.DebugAssert(vol > 0);

                        int tp = (int)eval.numTruePositives(1);
                        int fp = (int)eval.numFalsePositives(1);

                        double minScore = minScoreInfo.Deals.NowScore;
                        //WekaUtils.Instance.WriteLog(string.Format("Best Classifier: N={0},TC={1},TP={2},FP={3},TD={4},TV={5},TTP={6},TFP={7},",
                        //        minScoreInfo.Name, minScoreInfo.Deals.NowScore.ToString(Parameters.DoubleFormatString),
                        //        minScoreInfo.Deals.NowTp, minScoreInfo.Deals.NowFp, minScoreInfo.Deals.NowDeal,
                        //        minScoreInfo.Deals.TotalVolume.ToString("N2"), tp, fp),
                        //    true, ConsoleColor.DarkGreen);

                        // Exclude
                        //if (TestParameters.EnableExcludeClassifier)
                        //{
                        //if (minScoreInfo.ExcludeClassifier == null)
                        //{
                        //    string modelFileName4Exclude = GetExcludeModelFileName(minScoreInfo.Name);
                        //    minScoreInfo.ExcludeClassifier = WekaUtils.TryLoadClassifier(modelFileName4Exclude);
                        //}
                        //}

                        for (int i = 0; i < minScoreInfo.CurrentTestRet.Length; i++)
                        {
                            if (minScoreInfo.CurrentTestRet[i] == 2)
                            {
                                if (minScoreInfo.ExcludeClassifier != null)
                                {
                                    double cv2 = minScoreInfo.ExcludeClassifier.classifyInstance(minTestInstancesNew.instance(i));
                                    if (cv2 != 2)
                                        continue;
                                }
                                candidateDeals.Add(new DealInfo(WekaUtils.GetDateValueFromInstances(minTestInstances, 0, i),
                                    (float)WekaUtils.GetValueFromInstance(minTestInstances, "mainClose", i),
                                    minScoreInfo.DealType,
                                    (float)vol,
                                    (float)(minScoreInfo.CurrentClassValue[i] == 2 ? -minScoreInfo.Tp : minScoreInfo.Sl),
                                    WekaUtils.GetDateValueFromInstances(minTestInstances, 1, i)));
                            }
                        }
                        float nowCost = (float)eval.totalCost();
                        int nowDeal = tp + fp;
                        totalCost += nowCost * vol;
                        totalDeal += nowDeal;

                        //float diff = Math.Abs(totalCost - realDealsInfo.TotalCost);
                        //WekaUtils.DebugAssert(diff < 5);
                        //WekaUtils.DebugAssert(Math.Abs(totalDeal - (realDealsInfo.NowDeal + realDealsInfo.CurrentDeal)) == 0);
                        //if (diff > 0.5)
                        //    totalCost = realDealsInfo.TotalCost;

                        IterateClassifierInfos((k, i, j, h) =>
                        {
                            if (m_classifierInfoIdxs[k, i, j, h] == minScoreInfo)
                            {
                                m_totalScores[k, i, j, h] += nowCost;
                                m_totalDeals[k, i, j, h] += nowDeal;
                                return false;
                            }
                            else
                            {
                                return true;
                            }
                        });
                    }
                }

                int selectCount = 50;
                for (int i = 0; i < selectCount; ++i)
                {
                    if (candidateDeals.Count == 0)
                        break;

                    int selectedDealIdx = (int)Math.Round(m_randomGenerator.NextDouble() * candidateDeals.Count);
                    if (selectedDealIdx == candidateDeals.Count)
                        selectedDealIdx = candidateDeals.Count - 1;

                    m_realDealsInfo.AddDeal(candidateDeals[selectedDealIdx]);
                    candidateDeals.RemoveAt(selectedDealIdx);
                }

            }

            //if (m_enableDetailLogLevel2)
            //{
            //    IterateClassifierInfos((k, i, j, h) =>
            //        {
            //            if (h == m_currentTestHour && m_totalScores[k, i, j, h] != 0)
            //            {
            //                WekaUtils.Instance.WriteLog(string.Format("Predict score for {0}: TC={1}, TD={2}", m_classifierInfoIdxs[k, i, j, h].Name, m_totalScores[k, i, j, h].ToString(Parameters.DoubleFormatString), m_totalDeals[k, i, j, h]));
            //            }
            //        });

            //}
            if (!string.IsNullOrEmpty(currentSummary))
            {
                WekaUtils.Instance.WriteLog(currentSummary, true, ConsoleColor.Red);
                System.Console.Title = nowDate.ToString(Parameters.DateTimeFormat) + ":" + currentSummary;
            }

            //if (m_enableDetailLogLevel2)
            //{
            //    if (TestParameters.EnablePerhourTrain)
            //    {
            //        double[] totalCostPerHour = new double[m_classifierInfoIdxs.GetLength(3)];
            //        int[] totalDealPerHour = new int[m_classifierInfoIdxs.GetLength(3)];
            //        IterateClassifierInfos((k, i, j, h) =>
            //        {
            //            totalCostPerHour[h] += m_totalScores[k, i, j, h];
            //            totalDealPerHour[h] += m_totalDeals[k, i, j, h];
            //        });
            //        for (int i = 0; i < totalCostPerHour.Length; ++i)
            //        {
            //            if (totalCostPerHour[i] == 0)
            //                continue;

            //            WekaUtils.Instance.WriteLog(string.Format("Predict score Per hour of {0}: TC={1}, TD={2}", i, totalCostPerHour[i].ToString(Parameters.DoubleFormatString), totalDealPerHour[i]));
            //        }
            //    }

            //if ((m_testTimeStart.Month == 4 || m_testTimeStart.Month == 3) && m_testTimeStart.Day == 20)
            //{
            //    foreach (var cls in m_classifierInfos)
            //    {
            //        WekaUtils.Instance.WriteLog(cls.Value.Deals.PrintAll());
            //    }
            //}
            //if (m_currentTestHour == 0)
            //{
            //    var cls = m_classifierInfoIdxs[0, 0, 0, 0];
            //    WekaUtils.Instance.WriteLog((cls.Classifier as RandomClassifier).GetCountInfo());
            //}
            //}

            //if (m_saveDataFile)
            //{
            //    var files = System.IO.Directory.GetFiles(m_baseDir, "*.arff");
            //    if (files.Length > 100)
            //    {
            //        foreach (string fileName in files)
            //        {
            //            try
            //            {
            //                System.IO.File.Delete(fileName);
            //            }
            //            catch (Exception)
            //            {
            //            }
            //        }
            //    }
            //}
        }
コード例 #2
0
        public void ExecuteBest(DateTime nowDate, List <CandidateClassifier> minScoreInfos)
        {
            OutputRealDealSummary(nowDate);

            float totalCost = 0;
            int   totalDeal = 0;

            if (minScoreInfos != null)
            {
                List <DealInfo> candidateDeals = new List <DealInfo>();
                foreach (var minScoreInfo in minScoreInfos)
                {
                    minScoreInfo.WekaData.GenerateData(false, true);
                    weka.core.Instances minTestInstances    = minScoreInfo.WekaData.CurrentTestInstances;
                    weka.core.Instances minTestInstancesNew = minScoreInfo.WekaData.CurrentTestInstancesNew;

                    if (minTestInstances.numInstances() > 0)
                    {
                        MyEvaluation eval = new MyEvaluation(minScoreInfo.CostMatrix);
                        eval.evaluateModel(minScoreInfo.CurrentTestRet, minScoreInfo.CurrentClassValue);

                        float vol;
                        // vol = (float)minScoreInfo.MoneyManagement.GetVolume(null);
                        vol = 0.1F;
                        //vol = (float)Math.Round(minScore / -20000.0, 1);
                        //WekaUtils.DebugAssert(vol > 0);

                        int tp = (int)eval.numTruePositives(1);
                        int fp = (int)eval.numFalsePositives(1);

                        double minScore = minScoreInfo.Deals.NowScore;
                        //WekaUtils.Instance.WriteLog(string.Format("Best Classifier: N={0},TC={1},TP={2},FP={3},TD={4},TV={5},TTP={6},TFP={7},",
                        //        minScoreInfo.Name, minScoreInfo.Deals.NowScore.ToString(Parameters.DoubleFormatString),
                        //        minScoreInfo.Deals.NowTp, minScoreInfo.Deals.NowFp, minScoreInfo.Deals.NowDeal,
                        //        minScoreInfo.Deals.TotalVolume.ToString("N2"), tp, fp),
                        //    true, ConsoleColor.DarkGreen);

                        // Exclude
                        //if (TestParameters.EnableExcludeClassifier)
                        //{
                        //if (minScoreInfo.ExcludeClassifier == null)
                        //{
                        //    string modelFileName4Exclude = GetExcludeModelFileName(minScoreInfo.Name);
                        //    minScoreInfo.ExcludeClassifier = WekaUtils.TryLoadClassifier(modelFileName4Exclude);
                        //}
                        //}

                        for (int i = 0; i < minScoreInfo.CurrentTestRet.Length; i++)
                        {
                            if (minScoreInfo.CurrentTestRet[i] == 2)
                            {
                                if (minScoreInfo.ExcludeClassifier != null)
                                {
                                    double cv2 = minScoreInfo.ExcludeClassifier.classifyInstance(minTestInstancesNew.instance(i));
                                    if (cv2 != 2)
                                    {
                                        continue;
                                    }
                                }
                                candidateDeals.Add(new DealInfo(WekaUtils.GetDateValueFromInstances(minTestInstances, 0, i),
                                                                (float)WekaUtils.GetValueFromInstance(minTestInstances, "mainClose", i),
                                                                minScoreInfo.DealType,
                                                                (float)vol,
                                                                (float)(minScoreInfo.CurrentClassValue[i] == 2 ? -minScoreInfo.Tp : minScoreInfo.Sl),
                                                                WekaUtils.GetDateValueFromInstances(minTestInstances, 1, i)));
                            }
                        }
                        float nowCost = (float)eval.totalCost();
                        int   nowDeal = tp + fp;
                        totalCost += nowCost * vol;
                        totalDeal += nowDeal;

                        //float diff = Math.Abs(totalCost - realDealsInfo.TotalCost);
                        //WekaUtils.DebugAssert(diff < 5);
                        //WekaUtils.DebugAssert(Math.Abs(totalDeal - (realDealsInfo.NowDeal + realDealsInfo.CurrentDeal)) == 0);
                        //if (diff > 0.5)
                        //    totalCost = realDealsInfo.TotalCost;

                        IterateClassifierInfos((k, i, j, h) =>
                        {
                            if (m_classifierInfoIdxs[k, i, j, h] == minScoreInfo)
                            {
                                m_totalScores[k, i, j, h] += nowCost;
                                m_totalDeals[k, i, j, h]  += nowDeal;
                                return(false);
                            }
                            else
                            {
                                return(true);
                            }
                        });
                    }
                }


                int selectCount = 50;
                for (int i = 0; i < selectCount; ++i)
                {
                    if (candidateDeals.Count == 0)
                    {
                        break;
                    }

                    int selectedDealIdx = (int)Math.Round(m_randomGenerator.NextDouble() * candidateDeals.Count);
                    if (selectedDealIdx == candidateDeals.Count)
                    {
                        selectedDealIdx = candidateDeals.Count - 1;
                    }

                    m_realDealsInfo.AddDeal(candidateDeals[selectedDealIdx]);
                    candidateDeals.RemoveAt(selectedDealIdx);
                }
            }

            //if (m_enableDetailLogLevel2)
            //{
            //    IterateClassifierInfos((k, i, j, h) =>
            //        {
            //            if (h == m_currentTestHour && m_totalScores[k, i, j, h] != 0)
            //            {
            //                WekaUtils.Instance.WriteLog(string.Format("Predict score for {0}: TC={1}, TD={2}", m_classifierInfoIdxs[k, i, j, h].Name, m_totalScores[k, i, j, h].ToString(Parameters.DoubleFormatString), m_totalDeals[k, i, j, h]));
            //            }
            //        });

            //}
            if (!string.IsNullOrEmpty(currentSummary))
            {
                WekaUtils.Instance.WriteLog(currentSummary, true, ConsoleColor.Red);
                System.Console.Title = nowDate.ToString(Parameters.DateTimeFormat) + ":" + currentSummary;
            }

            //if (m_enableDetailLogLevel2)
            //{
            //    if (TestParameters.EnablePerhourTrain)
            //    {
            //        double[] totalCostPerHour = new double[m_classifierInfoIdxs.GetLength(3)];
            //        int[] totalDealPerHour = new int[m_classifierInfoIdxs.GetLength(3)];
            //        IterateClassifierInfos((k, i, j, h) =>
            //        {
            //            totalCostPerHour[h] += m_totalScores[k, i, j, h];
            //            totalDealPerHour[h] += m_totalDeals[k, i, j, h];
            //        });
            //        for (int i = 0; i < totalCostPerHour.Length; ++i)
            //        {
            //            if (totalCostPerHour[i] == 0)
            //                continue;

            //            WekaUtils.Instance.WriteLog(string.Format("Predict score Per hour of {0}: TC={1}, TD={2}", i, totalCostPerHour[i].ToString(Parameters.DoubleFormatString), totalDealPerHour[i]));
            //        }
            //    }

            //if ((m_testTimeStart.Month == 4 || m_testTimeStart.Month == 3) && m_testTimeStart.Day == 20)
            //{
            //    foreach (var cls in m_classifierInfos)
            //    {
            //        WekaUtils.Instance.WriteLog(cls.Value.Deals.PrintAll());
            //    }
            //}
            //if (m_currentTestHour == 0)
            //{
            //    var cls = m_classifierInfoIdxs[0, 0, 0, 0];
            //    WekaUtils.Instance.WriteLog((cls.Classifier as RandomClassifier).GetCountInfo());
            //}
            //}

            //if (m_saveDataFile)
            //{
            //    var files = System.IO.Directory.GetFiles(m_baseDir, "*.arff");
            //    if (files.Length > 100)
            //    {
            //        foreach (string fileName in files)
            //        {
            //            try
            //            {
            //                System.IO.File.Delete(fileName);
            //            }
            //            catch (Exception)
            //            {
            //            }
            //        }
            //    }
            //}
        }