static void Main(string[] args) { DataTable table = LoadData("dataset.txt"); TGPConfig config = new TGPConfig("TGPConfig.xml"); //config.CrossoverRate = 0.35; //config.MicroMutationRate = 0.15; //config.MacroMutationRate = 0.45; //config.ReproductionRate = 0.05; config.ElitismRatio = 0.1; TGPPop <TGPSolution> pop = new TGPPop <TGPSolution>(config); pop.PopulationReplacement = TGPPop <TGPSolution> .PopulationReplacementMode.TinyGP; pop.OperatorSet.AddOperator(new TGPOperator_Plus()); pop.OperatorSet.AddOperator(new TGPOperator_Minus()); pop.OperatorSet.AddOperator(new TGPOperator_Division()); pop.OperatorSet.AddOperator(new TGPOperator_Multiplication()); pop.OperatorSet.AddOperator(new TGPOperator_Sin()); pop.OperatorSet.AddOperator(new TGPOperator_Cos()); pop.OperatorSet.AddIfgtOperator(); for (int i = 1; i < 10; ++i) { pop.ConstantSet.AddConstant(string.Format("C{0}", i), i); } pop.VariableSet.AddVariable("x"); pop.VariableSet.AddVariable("y"); pop.CreateFitnessCase += (index) => { SpiralFitnessCase fitness_case = new SpiralFitnessCase(); fitness_case.X = double.Parse(table.Rows[index]["X"].ToString()); fitness_case.Y = double.Parse(table.Rows[index]["Y"].ToString()); fitness_case.Label = int.Parse(table.Rows[index]["Label"].ToString()); return(fitness_case); }; pop.GetFitnessCaseCount += () => { return(table.Rows.Count); }; pop.EvaluateObjectiveForSolution += (fitness_cases, solution, objective_index) => { double fitness = 0; for (int i = 0; i < fitness_cases.Count; i++) { SpiralFitnessCase fitness_case = (SpiralFitnessCase)fitness_cases[i]; int correct_y = fitness_case.Label; int computed_y = fitness_case.ComputedLabel; fitness += (correct_y == computed_y) ? 0 : 1; } return(fitness); }; pop.BreedInitialPopulation(); while (!pop.IsTerminated) { pop.Evolve(); Console.WriteLine("Spiral Classification Generation: {0}", pop.CurrentGeneration); Console.WriteLine("Global Fitness: {0}\tCurrent Fitness: {1}", pop.GlobalBestProgram.Fitness, pop.FindFittestProgramInCurrentGeneration().Fitness); Console.WriteLine("Global Best Solution:\n{0}", pop.GlobalBestProgram); } Console.WriteLine(pop.GlobalBestProgram.ToString()); }
static void Main(string[] args) { DataTable table = LoadData(); TGPConfig config = new TGPConfig("TGPConfig.xml"); TGPPop <TGPSolution> pop = new TGPPop <TGPSolution>(config); pop.OperatorSet.AddOperator(new TGPOperator_Plus()); pop.OperatorSet.AddOperator(new TGPOperator_Minus()); pop.OperatorSet.AddOperator(new TGPOperator_Division()); pop.OperatorSet.AddOperator(new TGPOperator_Multiplication()); pop.OperatorSet.AddOperator(new TGPOperator_Power()); pop.OperatorSet.AddIfltOperator(); for (int i = 1; i < 10; ++i) { pop.ConstantSet.AddConstant(string.Format("C{0}", i), i); } pop.VariableSet.AddVariable("X1"); pop.VariableSet.AddVariable("X2"); pop.CreateFitnessCase += (index) => { MexicanHatFitnessCase fitness_case = new MexicanHatFitnessCase(); fitness_case.X1 = double.Parse(table.Rows[index]["X1"].ToString()); fitness_case.X2 = double.Parse(table.Rows[index]["X2"].ToString()); fitness_case.Y = double.Parse(table.Rows[index]["Y"].ToString()); return(fitness_case); }; pop.GetFitnessCaseCount += () => { return(table.Rows.Count); }; pop.EvaluateObjectiveForSolution += (fitness_cases, solution, objective_index) => { double fitness = 0; for (int i = 0; i < fitness_cases.Count; i++) { MexicanHatFitnessCase fitness_case = (MexicanHatFitnessCase)fitness_cases[i]; double correct_y = fitness_case.Y; double computed_y = fitness_case.ComputedY; fitness += (correct_y - computed_y) * (correct_y - computed_y); } return(fitness); }; pop.BreedInitialPopulation(); while (!pop.IsTerminated) { pop.Evolve(); Console.WriteLine("Mexican Hat Symbolic Regression Generation: {0}", pop.CurrentGeneration); Console.WriteLine("Global Fitness: {0}\tCurrent Fitness: {1}", pop.GlobalBestProgram.Fitness.ToString("0.000"), pop.FindFittestProgramInCurrentGeneration().Fitness.ToString("0.000")); Console.WriteLine("Global Best Solution:\n{0}", pop.GlobalBestProgram); } Console.WriteLine(pop.GlobalBestProgram.ToString()); }
static void Main(string[] args) { DataTable table = LoadData(); TGPConfig config = new TGPConfig("TGPConfig.xml"); // The problem is to minimize the sum of errors between predicted and actual function config.IsMaximization = false; // As specified in Chapter 4 of "A Field Guide to Genetic Programming" config.PopulationSize = 4; config.ElitismRatio = 0; //non elitist config.CrossoverRate = 0.5; // subtree crossover rate set to 0.5 config.MacroMutationRate = 0.25; // subtree mutation rate set to 0.25; config.MicroMutationRate = 0.0; // point mutation rate set to 0.0 config.ReproductionRate = 0.25; // reproduction rate set to 0.25 config.NormalizeEvolutionRates(); //Question 1: Is the performance normal for the GP? config.MaximumDepthForCreation = 2; config.MaximumDepthForCrossover = 2; // no tree size limit by setting a very large max depth config.MaximumDepthForMutation = 2; TGPPop <TGPSolution> pop = new TGPPop <TGPSolution>(config); pop.ReproductionSelectionInstruction = new SelectionInstruction_RouletteWheel <TGPSolution>(); //use roulette wheel selection // Function Set = {+, -, %, *} where % is protected division that returns 1 if the denominator is 0 pop.OperatorSet.AddOperator(new TGPOperator_Plus()); pop.OperatorSet.AddOperator(new TGPOperator_Minus()); pop.OperatorSet.AddOperator(new TGPOperator_Division()); pop.OperatorSet.AddOperator(new TGPOperator_Multiplication()); // Terminal Set = {R, x} pop.ConstantSet.AddConstant("R", DistributionModel.GetUniform() * 10.0 - 5.0); pop.VariableSet.AddVariable("x"); pop.CreateFitnessCase += (index) => { SymRegFitnessCase fitness_case = new SymRegFitnessCase(); fitness_case.X = double.Parse(table.Rows[index]["X"].ToString()); fitness_case.Y = double.Parse(table.Rows[index]["Y"].ToString()); return(fitness_case); }; pop.GetFitnessCaseCount += () => { return(table.Rows.Count); }; pop.EvaluateObjectiveForSolution += (fitness_cases, solution, objective_index) => { double sum_of_error = 0; for (int i = 0; i < fitness_cases.Count; i++) { SymRegFitnessCase fitness_case = (SymRegFitnessCase)fitness_cases[i]; double correct_y = fitness_case.Y; double computed_y = fitness_case.ComputedY; sum_of_error += System.Math.Abs(correct_y - computed_y); } return(sum_of_error); }; pop.BreedInitialPopulation(); double error = pop.GlobalBestProgram.ObjectiveValue; while (error > 0.1) { pop.Evolve(); error = pop.GlobalBestProgram.ObjectiveValue; Console.WriteLine("Symbolic Regression Generation: {0}", pop.CurrentGeneration); Console.WriteLine("Minimum Error: {0}", error.ToString("0.000")); Console.WriteLine("Global Best Solution:\n{0}", pop.GlobalBestProgram); } Console.WriteLine(pop.GlobalBestProgram.ToString()); }