Example #1
0
        public string Predict(List <double> marks)
        {
            try
            {
                if (marks.Count() < 10)
                {
                    return("غير معلوم");
                }

                if (Model == null)
                {
                    if (!File.Exists("randomforest.xml"))
                    {
                        Learn("data.txt", 500, 266, 10);
                    }
                    Model = ClassificationForestModel.Load(() => new StreamReader(@"randomforest.xml"));
                }

                var lastTenMarks = marks.Skip(marks.Count - 10);

                F64Matrix observations = new F64Matrix(1, lastTenMarks.Count());
                for (var j = 0; j < lastTenMarks.Count(); j++)
                {
                    observations.At(0, j, lastTenMarks.ElementAt(j));
                }
                var v = Model.Predict(observations).First();
                Console.WriteLine("predict " + v);
                return(GetClass(v));
            }
            catch (Exception e)
            {
                Console.Error.WriteLine(e.Message);
                return("غير معلوم");
            }
        }
Example #2
0
        /// <summary>
        /// Simulates the predictions of the gray box random forest.
        /// </summary>
        /// <param name="testData">The test data.</param>
        /// <param name="grayBoxRandomForest">The gray box random forest.</param>
        /// <param name="currentGeneration">The current generation.</param>
        private void SimulatePredictionsOfGrayBoxRandomForest(
            List <DataRecord <TResult> > testData,
            ClassificationForestModel grayBoxRandomForest,
            int currentGeneration)
        {
            for (var currentTimePoint = TimeSpan.Zero;
                 currentTimePoint < this._configuration.CpuTimeout;
                 currentTimePoint += this._configuration.DataRecordUpdateInterval)
            {
                if (currentTimePoint < this._simulationConfiguration.GrayBoxStartTimePoint)
                {
                    continue;
                }

                var currentTestData =
                    GrayBoxSimulation <TTargetAlgorithm, TInstance, TResult> .GetCurrentTestData(testData, currentTimePoint);

                if (!currentTestData.Any())
                {
                    continue;
                }

                var predictions = this.GetPredictionsOfGrayBoxRandomForest(grayBoxRandomForest, currentTestData);
                this.UpdatePredictionDictionaryAndDataDictionary(predictions, currentTestData, currentTimePoint);

                var currentPredictionScores = this.GetPredictionScores(
                    currentGeneration,
                    currentTimePoint,
                    currentTestData);
                this._predictionScoresRecorder.WriteRow(currentPredictionScores.ToStringArray());
            }
        }
Example #3
0
        /// <summary>
        /// Tries to deserialize the gray box random forest. This method is the counterpart of TryToSerializeGrayBoxRandomForest in <see cref="AlgorithmTuner{TTargetAlgorithm,TInstance,TResult}"/>.
        /// </summary>
        /// <param name="grayBoxRandomForest">The gray box random forest.</param>
        /// <returns>True, if successful.</returns>
        private bool TryToDeserializeGrayBoxRandomForest(out ClassificationForestModel grayBoxRandomForest)
        {
            try
            {
                var timer = Stopwatch.StartNew();
                using (var file = File.OpenRead(this._configuration.GrayBoxRandomForestFile.FullName))
                {
                    grayBoxRandomForest = new Hyperion.Serializer().Deserialize <ClassificationForestModel>(file);
                }

                timer.Stop();

                if (grayBoxRandomForest != null)
                {
                    LoggingHelper.WriteLine(
                        VerbosityLevel.Info,
                        $"The deserialization of the gray box random forest before generation {this._currentEvaluation.GenerationId} on evaluation actor with address {this.Self} took {timer.Elapsed.TotalSeconds} seconds.");
                    return(true);
                }

                LoggingHelper.WriteLine(
                    VerbosityLevel.Warn,
                    $"Cannot deserialize gray box random forest before generation {this._currentEvaluation.GenerationId} on evaluation actor with address {this.Self}, because it is null.");
                return(false);
            }
            catch (Exception exception)
            {
                LoggingHelper.WriteLine(
                    VerbosityLevel.Warn,
                    $"Cannot deserialize gray box random forest before generation {this._currentEvaluation.GenerationId} on evaluation actor with address {this.Self}, because: {exception.Message}");
                grayBoxRandomForest = null;
                return(false);
            }
        }
Example #4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="GrayBoxHandler{TInstance, TResult}" /> class.
        /// </summary>
        /// <param name="configuration">The <see cref="AlgorithmTunerConfiguration"/>.</param>
        /// <param name="grayBoxTargetAlgorithm">The gray box target algorithm.</param>
        /// <param name="actorId">The actor ID.</param>
        /// <param name="tunerDataRecord">The tuner data record.</param>
        /// <param name="useGrayBoxInCurrentEvaluation">Boolean indicating whether to use gray box tuning in current evaluation.</param>
        /// <param name="customGrayBoxMethods">The <see cref="ICustomGrayBoxMethods{TResult}"/>.</param>
        /// <param name="grayBoxRandomForest">The gray box random forest.</param>
        public GrayBoxHandler(
            AlgorithmTunerConfiguration configuration,
            IGrayBoxTargetAlgorithm <TInstance, TResult> grayBoxTargetAlgorithm,
            int actorId,
            TunerDataRecord <TResult> tunerDataRecord,
            bool useGrayBoxInCurrentEvaluation,
            ICustomGrayBoxMethods <TResult> customGrayBoxMethods,
            ClassificationForestModel grayBoxRandomForest)
        {
            this._configuration          = configuration ?? throw new ArgumentNullException(nameof(configuration));
            this._grayBoxTargetAlgorithm = grayBoxTargetAlgorithm ?? throw new ArgumentNullException(nameof(grayBoxTargetAlgorithm));
            this._actorId         = actorId;
            this._tunerDataRecord = tunerDataRecord ?? throw new ArgumentNullException(nameof(tunerDataRecord));

            if (useGrayBoxInCurrentEvaluation)
            {
                this._customGrayBoxMethods          = customGrayBoxMethods ?? throw new ArgumentNullException(nameof(customGrayBoxMethods));
                this._grayBoxRandomForest           = grayBoxRandomForest ?? throw new ArgumentNullException(nameof(grayBoxRandomForest));
                this._useGrayBoxInCurrentEvaluation = true;
            }

            this._grayBoxTargetAlgorithm.OnNewDataRecord += this.HandleDataRecordUpdate;

            lock (this._lock)
            {
                this._listOfDataRecords = new List <DataRecord <TResult> >();
            }
        }
Example #5
0
        /// <summary>
        /// Logs the feature importance.
        /// </summary>
        /// <param name="currentGeneration">The current generation.</param>
        /// <param name="grayBoxRandomForest">The gray box random forest.</param>
        private void LogFeatureImportance(int currentGeneration, ClassificationForestModel grayBoxRandomForest)
        {
            var featureImportanceEntry =
                new[] { (double)currentGeneration, }.Concat(
                GrayBoxUtils.GetScaledFeatureImportance(grayBoxRandomForest)).Select(d => $"{d:0.######}").ToArray();

            this._featureImportanceRecorder.WriteRow(featureImportanceEntry);
        }
Example #6
0
        public void ClassificationForestModel_Load()
        {
            var(observations, targets) = DataSetUtilities.LoadAptitudeDataSet();

            var reader = new StringReader(m_classificationForestModelString);
            var sut    = ClassificationForestModel.Load(() => reader);

            var predictions = sut.Predict(observations);

            var evaluator = new TotalErrorClassificationMetric <double>();
            var error     = evaluator.Error(targets, predictions);

            Assert.AreEqual(0.42307692307692307, error, m_delta);
        }
        public static void LearnModel(int trees = 30)
        {
            var keySignatureTable = Properties.Resources.keySignaturesDataset;

            var parser     = new CsvParser(() => new StringReader(keySignatureTable), ',');
            var targetName = "key";

            var observations = parser.EnumerateRows(c => c != targetName).ToF64Matrix();
            var targets      = parser.EnumerateRows(targetName).ToF64Vector();

            var learner = new ClassificationRandomForestLearner(trees: trees);

            model = learner.Learn(observations, targets);
        }
Example #8
0
        public void ClassificationForestModel_Load()
        {
            var parser       = new CsvParser(() => new StringReader(Resources.AptitudeData));
            var observations = parser.EnumerateRows(v => v != "Pass").ToF64Matrix();
            var targets      = parser.EnumerateRows("Pass").ToF64Vector();

            var reader = new StringReader(ClassificationForestModelString);
            var sut    = ClassificationForestModel.Load(() => reader);

            var predictions = sut.Predict(observations);

            var evaluator = new TotalErrorClassificationMetric <double>();
            var error     = evaluator.Error(targets, predictions);

            Assert.AreEqual(0.42307692307692307, error, m_delta);
        }
Example #9
0
        /// <summary>
        /// Gets the predictions of the gray box random forest.
        /// </summary>
        /// <param name="grayBoxRandomForest">The gray box random forest.</param>
        /// <param name="currentTestData">The current test data.</param>
        /// <returns>The predictions.</returns>
        private int[] GetPredictionsOfGrayBoxRandomForest(
            ClassificationForestModel grayBoxRandomForest,
            IReadOnlyList <DataRecord <TResult> > currentTestData)
        {
            var currentTestDataObservations = GrayBoxUtils.CreateF64MatrixFromDataRecords(currentTestData, this._customGrayBoxMethods);
            var originalPredictions         = grayBoxRandomForest.PredictProbability(currentTestDataObservations);

            // Use gray box confidence threshold to adjust predictions.
            var correctedPredictions = originalPredictions
                                       .Select(
                prediction =>
                prediction.Probabilities[GrayBoxUtils.GrayBoxLabelOfTimeouts]
                >= this._simulationConfiguration.GrayBoxConfidenceThreshold
                            ? GrayBoxUtils.GrayBoxLabelOfTimeouts
                            : GrayBoxUtils.GrayBoxLabelOfNonTimeouts)
                                       .ToArray();

            return(correctedPredictions);
        }
Example #10
0
        public static void Learn(string dataPath, int LEARNING_ROWS_COUNT, int TESTING_ROWS_COUNT, int FEATURES_COUNT = 10)
        {
            F64Matrix     observations     = new F64Matrix(LEARNING_ROWS_COUNT, FEATURES_COUNT);
            F64Matrix     testObservations = new F64Matrix(TESTING_ROWS_COUNT, FEATURES_COUNT);
            List <double> targets          = new List <double>();
            List <double> actualTargets    = new List <double>();

            Parse(dataPath, observations, targets, testObservations, actualTargets, TESTING_ROWS_COUNT, LEARNING_ROWS_COUNT);

            // Create a random forest learner for classification with 100 trees
            var learner = new ClassificationRandomForestLearner(trees: 100);

            // learn the model
            ClassificationForestModel model = learner.Learn(observations, targets.ToArray());

            Console.WriteLine("Finish learning !");

            // use the model for predicting new observations
            var predictions = model.Predict(testObservations);
            var test        = "";
            var testIdx     = 0;
            var error       = 0.0;
            var g           = 0;

            foreach (var p in predictions)
            {
                var e = Math.Abs(p - actualTargets[testIdx]);
                error += e;
                g      = Math.Max((int)g, (int)e);

                test += p.ToString() + " " + e + "\n";
                testIdx++;
            }
            File.WriteAllText("test.txt", test);

            // save the model for use with another application
            model.Save(() => new StreamWriter("randomforest.xml"));
        }
Example #11
0
        public void ClassificationForestModel_Trees()
        {
            var(observations, targets) = DataSetUtilities.LoadAptitudeDataSet();

            var reader = new StringReader(m_classificationForestModelString);
            var sut    = ClassificationForestModel.Load(() => reader);

            var rows        = observations.RowCount;
            var predictions = new double[rows];

            for (int row = 0; row < rows; row++)
            {
                var observation = observations.Row(row);
                predictions[row] = sut.Trees.Select(t => t.Predict(observation))
                                   .GroupBy(p => p).OrderByDescending(g => g.Count())
                                   .First().Key;
            }

            var evaluator = new TotalErrorClassificationMetric <double>();
            var error     = evaluator.Error(targets, predictions);

            Assert.AreEqual(0.42307692307692307, error, m_delta);
        }