Пример #1
0
        private void LogMostRecentOvertake()
        {
            var          overtakes      = Data.Competition.Overtakes.GetData();
            OvertakeData newestOvertake = (OvertakeData)overtakes.Last();

            MostRecentOvertake = $"{newestOvertake.ParticipantName} overtook {newestOvertake.Overtaken}!";
        }
Пример #2
0
        //Get data from the OvertakeAI program
        public static string[][] GetOvertakeData(int loop)
        {
            Library.Overtake overtake;
            string[][]       overtakeData = new string[loop][];

            for (var i = 0; i < loop; i++)
            {
                overtake = OvertakeData.GetData();

                overtakeData[i] = new string[4]
                {
                    overtake.InitialSeparationM.ToString(),
                        overtake.OvertakingSpeedMPS.ToString(),
                        overtake.OncomingSpeedMPS.ToString(),
                        overtake.Success.ToString()
                };
            }

            return(overtakeData);
        }
Пример #3
0
        public void Run()
        {
            Library.Overtake overtake;
            bool             firstSuccess = true;

            WriteLine("ML.Net");

            //Get amount of data the user to train
            int trainAmount = GetUserInput("Amount of data to train");

            //Get the amount of data the user wants to predict against the model
            int testAmount = GetUserInput("Amount of data to predict");

            WriteLine();

            string path = @"..\..\..\testData.csv";
            var    csv  = new StringBuilder();

            //Create file if it doesn't exist or overwrite it with null data
            File.WriteAllText(path, null);

            var overtakeData = "InitialSeperation," +
                               "OvertakingSpeed," +
                               "OncomingSpeed," +
                               "Success";

            csv.AppendLine(overtakeData);

            //Get data from OvertakeAI program and add it to csv var
            for (int i = 0; i < trainAmount; i++)
            {
                overtake = OvertakeData.GetData();

                //Store first success result so table can be formatted correctly later
                if (i == 0)
                {
                    firstSuccess = overtake.Success;
                }

                overtakeData = $"{overtake.InitialSeparationM}," +
                               $"{overtake.OvertakingSpeedMPS}," +
                               $"{overtake.OncomingSpeedMPS}," +
                               $"{overtake.Success}";

                csv.AppendLine(overtakeData);
            }

            //Write all the data to csv file
            File.AppendAllText(path, csv.ToString());

            //Create ML.Net model
            ModelBuilder.CreateModel();

            int[]  testOutputs = new int[testAmount];
            string actualOutcome;
            int    outcomeIndex;
            var    scoreCard = new List <bool>();

            string[] possibleOutcomes = { "Won't Pass", "Will Pass" };
            string   predictionOutcome;

            WriteLine($"\n{"Initial Seperation (m)",21}" +
                      $"{"Overtaking Speed (m/s)",28}" +
                      $"{"Oncoming Speed (m/s)",26}" +
                      $"{"Outcome",14}" +
                      $"{"Prediction",17}" +
                      $"{"Pass Chance",16}" +
                      $"{"Won't Pass Chance",22}");

            for (int i = 0; i < testAmount; i++)
            {
                //Get the data from OvertakeAI
                overtake = OvertakeData.GetData();

                ModelInput testInputs = new ModelInput()
                {
                    InitialSeperation = (float)overtake.InitialSeparationM,
                    OvertakingSpeed   = (float)overtake.OvertakingSpeedMPS,
                    OncomingSpeed     = (float)overtake.OncomingSpeedMPS
                };

                actualOutcome = overtake.Success ? "Will Pass" : "Won't Pass";

                //Preict the result using the model
                var predictionResult = ConsumeModel.Predict(testInputs);

                //Compare actual outcome to the predicted outcome
                if (predictionResult.Prediction == "False")
                {
                    outcomeIndex = 0;
                }
                else
                {
                    outcomeIndex = 1;
                }

                scoreCard.Add(actualOutcome == possibleOutcomes[outcomeIndex]);
                predictionOutcome = scoreCard[i] ? "Correct" : "Incorrect";

                //Print out the prediction data
                Write($"{Round(testInputs.InitialSeperation,2),14}" +
                      $"{Round(testInputs.OvertakingSpeed,2),27}" +
                      $"{Round(testInputs.OncomingSpeed,2),27}" +
                      $"{actualOutcome,22}" +
                      $"{predictionOutcome,17}");

                if (firstSuccess == true)
                {
                    WriteLine($"{Round(predictionResult.Score[0] * 100, 2),15}%" +
                              $"{Round(predictionResult.Score[1] * 100, 2),21}%");
                }
                else
                {
                    WriteLine($"{Round(predictionResult.Score[1] * 100, 2),15}%" +
                              $"{Round(predictionResult.Score[0] * 100, 2),21}%");
                }
            }

            //Count amount of correct values in score card to show accuracy percentage
            WriteLine($"\nAccuracy: {Round((scoreCard.Count(x => x) / ToDouble(scoreCard.Count)) * 100, 2)}%");
        }
Пример #4
0
        public void Run()
        {
            Library.Overtake overtake;

            WriteLine("Decision Tree - C45 Learning");

            //Get amount of data the user wants the decision tree to train
            int trainAmount = GetUserInput("Amount of data to train");

            double[][] trainInputs  = new double[trainAmount][];
            int[]      trainOutputs = new int[trainAmount];

            //Get data from OvertakeAI program and insert it into train inputs and outputs arrays
            for (int i = 0; i < trainAmount; i++)
            {
                overtake = OvertakeData.GetData();

                trainInputs[i] = new double[3]
                {
                    overtake.InitialSeparationM,
                    overtake.OvertakingSpeedMPS,
                    overtake.OncomingSpeedMPS
                };

                trainOutputs[i] = ToInt32(overtake.Success);
            }

            //Train decison tree using C4.5 algorithm using the trainInputs and trainOutputs
            var          learningAlgorithm = new C45Learning();
            DecisionTree tree = learningAlgorithm.Learn(trainInputs, trainOutputs);

            //Get the amount of data the user wants to predict against the decision tree
            int testAmount = GetUserInput("Amount of data to predict");

            double[] testInputs;
            int      outcomeIndex;
            string   actualOutcome;
            var      scoreCard = new List <bool>();

            string[] possibleOutcomes = { "Won't Pass", "Will Pass" };
            string   predictedOutcome;

            WriteLine($"\n{"Initial Seperation (m)",21}" +
                      $"{"Overtaking Speed (m/s)",28}" +
                      $"{"Oncoming Speed (m/s)",26}" +
                      $"{"Outcome",14}" +
                      $"{"Prediction",17}");

            //Loop for amount of times that want to be predicted
            for (int i = 0; i < testAmount; i++)
            {
                //Get the data from OvertakeAI
                overtake = OvertakeData.GetData();

                testInputs = new double[3] {
                    overtake.InitialSeparationM,
                    overtake.OvertakingSpeedMPS,
                    overtake.OncomingSpeedMPS
                };

                actualOutcome = overtake.Success ? "Will Pass" : "Won't Pass";

                //Preict the result using the decision tree
                outcomeIndex = tree.Decide(testInputs);

                //Compare actual outcome to the predicted outcome
                scoreCard.Add(actualOutcome == possibleOutcomes[outcomeIndex]);

                //Print out the data
                predictedOutcome = scoreCard[i] ? "Correct" : "Incorrect";
                WriteLine($"{Round(testInputs[0], 2).ToString("F"),14}" +
                          $"{Round(testInputs[1], 2).ToString("F"),27}" +
                          $"{Round(testInputs[2], 2).ToString("F"),27}" +
                          $"{actualOutcome,22}" +
                          $"{predictedOutcome,17}");
            }

            //Count amount of correct values in score card to show accuracy percentage
            WriteLine($"\nAccuracy: {Round((scoreCard.Count(x => x) / ToDouble(scoreCard.Count)) * 100, 2)}%");

            //Get the training error of the decision tree
            int[]  predicted = tree.Decide(trainInputs);
            double error     = new ZeroOneLoss(trainOutputs).Loss(predicted);

            WriteLine($"Training Error: {Round(error, 2)}\n");

            //Print out the rules that the decision tree came up with
            WriteLine("Decision Tree Rules:");
            DecisionSet rules        = tree.ToRules();
            var         codebook     = new Codification("Possible Results", possibleOutcomes);
            var         encodedRules = rules.ToString(codebook, "Possible Results", CultureInfo.InvariantCulture);

            WriteLine($"{encodedRules}");
        }