示例#1
0
        static void Main(string[] args)
        {
            var problem = Problem.Read("Fitness\\sin-data.txt");

            var code =
                "((((0 = (0 - ((4 <= X0) / 9.5))) OR ((1.5 XOR 0) - (X0 AND 1.5))) XOR (((2 OR 7.1) / Y0)=>(Y0 / (Y1 = Y1)))) - ((((X0 + X0) = (Y1=>0)) XOR (Y0 = (3 >= X0))) > Y0))";

            var program = Parser.Parser.Parse(Tokenizer.Tokenizer.Tokenize(code));

            var fitness       = new FitnessEvaluation(program, problem);
            var stringBuilder = new StringBuilder();

            fitness.Evaluate(stringBuilder);

            FitnessEvaluation simplifiedFitness;

            do
            {
                gp.Program simplifiedProgram = program.Simplify(fitness, stringBuilder);
                if (simplifiedProgram == null || simplifiedProgram.ToString() == program.ToString())
                {
                    break;
                }
                simplifiedFitness = new FitnessEvaluation(simplifiedProgram, problem);
                simplifiedFitness.Evaluate(false, stringBuilder);
                program = simplifiedProgram;
            } while (fitness.ErrorSum == simplifiedFitness.ErrorSum);

            Console.Out.Write(stringBuilder);
        }
示例#2
0
        public Program()
        {
            GARandomManager.Random = new RandomClassic((int)DateTime.Now.Ticks);

            var generationManager = new GenerationManagerKeepLast();

            var initialGenerationGenerator = new GASumInitialGenerationGenerator
            {
                GeneCount = genesCount,
                MinValue  = minGeneValue,
                MaxValue  = maxGeneValue
            };

            var selection = new EliteSelection();
            var crossover = new OnePointCrossover(useBothChildren: true);

            var breeding = new BreedingClassic(
                crossoverPart,
                minProduction: 1,
                selection: selection,
                crossover: crossover,
                mutationManager: InitMutations());

            var reinsertion = new ReinsertionFromSelection(
                reinsertionPart,
                minProduction: 1,
                selection: new EliteSelection());

            var producers = new List <IGenomeProducer>
            {
                reinsertion,
                breeding
            };

            var genomeForge = new GenomeForge(producers);

            geneticManager = new GeneticManagerClassic(
                generationManager,
                initialGenerationGenerator,
                genomeForge,
                genomesCount
                );
            geneticManager.Init();

            fitnessEvaluation = new FitnessEvaluation();
        }