static void Main(string[] args)
        {
            DataTable table = LoadData("dataset.txt");

            LGPConfig config = new LGPConfig();

            LGPPop pop = new LGPPop(config);

            pop.OperatorSet.AddOperator(new LGPOperator_Plus());
            pop.OperatorSet.AddOperator(new LGPOperator_Minus());
            pop.OperatorSet.AddOperator(new LGPOperator_Division());
            pop.OperatorSet.AddOperator(new LGPOperator_Multiplication());
            pop.OperatorSet.AddOperator(new LGPOperator_Sin());
            pop.OperatorSet.AddOperator(new LGPOperator_Cos());
            pop.OperatorSet.AddIfgtOperator();

            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.EvaluateCostFromAllCases += (fitness_cases) =>
            {
                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(pop.GlobalBestProgram.ToString());
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            DataTable table = LoadData();

            LGPConfig config = new LGPConfig();

            LGPPop pop = new LGPPop(config);

            pop.OperatorSet.AddOperator(new LGPOperator_Plus());
            pop.OperatorSet.AddOperator(new LGPOperator_Minus());
            pop.OperatorSet.AddOperator(new LGPOperator_Division());
            pop.OperatorSet.AddOperator(new LGPOperator_Multiplication());
            pop.OperatorSet.AddOperator(new LGPOperator_Power());
            pop.OperatorSet.AddIfltOperator();

            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.EvaluateCostFromAllCases += (fitness_cases) =>
            {
                double cost = 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.PredictedY;
                    cost += (correct_y - computed_y) * (correct_y - computed_y);
                }

                return(cost);
            };


            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(pop.GlobalBestProgram.ToString());
        }
Beispiel #3
0
 public LGPEnvironment(LGPConfig config)
 {
     Config = config;
 }