Ejemplo n.º 1
0
 private void Reset()
 {
     population = default(Population);
 }
Ejemplo n.º 2
0
        private void Go(object sender, DoWorkEventArgs e)
        {
            bool[,] iterationSolution;
            bool needToStop = false;
            for (int j = 4; j < 8; j++)
            {
                iterationSolution = GetIterationSolution(j);

                EvolutiveFitnessFunction fitness = new EvolutiveFitnessFunction(iterationSolution);

                ITreeGene gene = (ITreeGene)new BooleanFunction(4);

                if ((Selection is IMixedSelection) || (Replacement is IMixedSelection))
                {
                    Population = new Population(Individuals,
                            (IIndividual)new TreeIndividual(gene),
                        fitness, Selection, Replacement, Iterations,
                        MutationRate, SelectionFirstCount, ReplacementFirstCount,
                        SelectionSecondCount, ReplacementSecondCount);
                }
                else
                {
                    Population = new Population(Individuals,
                            (IIndividual)new TreeIndividual(gene),
                        fitness, Selection, Replacement, Iterations,
                        MutationRate, SelectionFirstCount, ReplacementFirstCount);
                }
                Population.GenerationEnded += new Population.GenerationEndedEventHandler(OnGenerationEnded);
                int i = 1;

                while (!needToStop)
                {
                    //corremos la simulacion para una generacion
                    population.RunEpoch();
                    population.OnGenerationEnded(new GenerationEndedEventArgs(population, i));
                    try
                    {
                        string bestFunction = population.BestIndividual.ToString();
                        OutputLine(bestFunction);
                    }
                    catch (Exception) { }
                    i++;
                    if ((Iterations != 0) && (i > Iterations))
                        needToStop = true;
                }
                needToStop = false;

                bool[] variables = new bool[4];
                OutputLine("bit " + (3 - (j - 4)).ToString());
                for (int k = 0; k < iterationSolution.GetLength(0); k++)
                {
                    variables[0] = iterationSolution[k, 0];
                    variables[1] = iterationSolution[k, 1];
                    variables[2] = iterationSolution[k, 2];
                    variables[3] = iterationSolution[k, 3];
                    OutputLine(iterationSolution[k, 0].ToString() + iterationSolution[k, 1].ToString() + iterationSolution[k, 2].ToString() + iterationSolution[k, 3].ToString() + " = " +
                            PolishBooleanExpression.Evaluate(population.BestSolution.ToString(), variables).ToString()
                        );
                }
                OutputLine(population.BestSolution.ToString());
                OutputLine(PolishBooleanExpression.ToInfix(population.BestSolution.ToString(), variables));
                OutputLine("Aptitud: " + population.BestSolution.Fitness);
                OutputLine("Es correcta: " + ((population.BestSolution.Fitness > 69000.00 - population.BestSolution.Fitness.ToString().Length) ? "Si" : "No"));
                SetSolution((3 - (j - 4)), PolishBooleanExpression.ToInfix(population.BestSolution.ToString(), variables));
            }
            lblStatus.Text = "Finalizado";
        }
        /*private IIndividual generationBestIndividual;
        public IIndividual GenerationBestIndividual { get { return generationBestIndividual; } }

        private IIndividual bestIndividual;
        public IIndividual BestIndividual { get { return generationBestIndividual; } }*/
        public GenerationEndedEventArgs(Population p, int genNum)
        {
            population = p;
            generationNumber = genNum;
        }
Ejemplo n.º 4
0
        private void Go()
        {
            bool[,] truthTable = new bool[16, 8]{
                {false,false,false,false,false,false,false,false},
                {false,false,false,true,false,false,false,false},
                {false,false,true,false,false,false,false,false},
                {false,false,true,true,false,false,false,false},
                {false,true,false,false,false,false,false,false},
                {false,true,false,true,false,false,false,true},
                {false,true,true,false,false,false,true,false},
                {false,true,true,true,false,false,true,true},
                {true,false,false,false,false,false,false,false},
                {true,false,false,true,false,false,true,false},
                {true,false,true,false,false,true,false,false},
                {true,false,true,true,false,true,true,false},
                {true,true,false,false,false,false,false,false},
                {true,true,false,true,false,false,true,true},
                {true,true,true,false,false,true,true,false},
                {true,true,true,true,true,false,false,true}
            };
            bool[,] iterationSolution;
            for (int j = 4; j < 8; j++)
            {
                iterationSolution = new bool[16, 5]{
                    {false,false,false,false,truthTable[0,j]},
                    {false,false,false,true,truthTable[1,j]},
                    {false,false,true,false,truthTable[2,j]},
                    {false,false,true,true,truthTable[3,j]},
                    {false,true,false,false,truthTable[4,j]},
                    {false,true,false,true,truthTable[5,j]},
                    {false,true,true,false,truthTable[6,j]},
                    {false,true,true,true,truthTable[7,j]},
                    {true,false,false,false,truthTable[8,j]},
                    {true,false,false,true,truthTable[9,j]},
                    {true,false,true,false,truthTable[10,j]},
                    {true,false,true,true,truthTable[11,j]},
                    {true,true,false,false,truthTable[12,j]},
                    {true,true,false,true,truthTable[13,j]},
                    {true,true,true,false,truthTable[14,j]},
                    {true,true,true,true,truthTable[15,j]}
                };

                EvolutiveFitnessFunction fitness = new EvolutiveFitnessFunction(iterationSolution);

                ITreeGene gene = (ITreeGene)new BooleanFunction(4);

                Population population = new Population(populationSize,
                        (IIndividual)new TreeIndividual(gene),
                    fitness, (ISelection)new EliteWheelMixedSelection(),new EliteWheelMixedSelection(), 100, 0.005,20,40,20, 80);

                int i = 1;

                while (!needToStop)
                {

                    population.RunEpoch();

                    try
                    {

                        string bestFunction = population.BestIndividual.ToString();

                        Debug.WriteLine(bestFunction);

                    }
                    catch (Exception)
                    {

                    }

                    i++;

                    //
                    if ((iterations != 0) && (i > iterations))
                        break;
                }
                bool[] variables = new bool[4];
                Debug.WriteLine("solution " + j.ToString());
                for (int k = 0; k < iterationSolution.GetLength(0); k++)
                {
                    variables[0] = iterationSolution[k, 0];
                    variables[1] = iterationSolution[k, 1];
                    variables[2] = iterationSolution[k, 2];
                    variables[3] = iterationSolution[k, 3];
                    Debug.WriteLine(iterationSolution[k, 0].ToString() + iterationSolution[k, 1].ToString() + iterationSolution[k, 2].ToString() + iterationSolution[k, 3].ToString() + " = " +
                            PolishBooleanExpression.Evaluate(population.BestSolution.ToString(), variables).ToString()
                        );
                }

                Debug.WriteLine(population.BestIndividual.ToString());

            }
        }
Ejemplo n.º 5
0
        static void Main(string[] args)
        {
            Population pop1 = new Population(Karyotype, 2, 4);

            Population pop = new Population(SimpleKaryotype, 0, 100);

            for (int i = 0; i < 100; i++)
            {

                Console.WriteLine($"-------------------------------GENERATION {i}------------------------------------");
                foreach (Organism organism in pop1)
                {
                    Console.WriteLine($"Organism {organism.Age}");

                    foreach (Chromosome chromosome in organism)
                    {
                        Console.WriteLine();

                        Console.Write("X-Point:    |");
                        foreach (Gene g in chromosome)
                        {
                            Console.Write(g.CrossPoint ? "|".PadLeft(chromosome.Genotype.GeneLength + 1, 'X') : "|".PadLeft(chromosome.Genotype.GeneLength + 1, ' '));
                        }
                        Console.WriteLine();

                        Console.Write("Chromosome: ");
                        Console.WriteLine(chromosome.ToString());

                        Console.Write("Mutation:   |");
                        foreach (Gene g in chromosome)
                        {
                            Console.Write(g.Mutation ? "|".PadLeft(chromosome.Genotype.GeneLength + 1, 'X') : "|".PadLeft(chromosome.Genotype.GeneLength + 1, ' '));
                        }
                        Console.WriteLine();
                    }

                    Console.WriteLine();
                }
                Console.WriteLine($"------------------------------END GENERATION {i}---------------------------------");

                Console.ReadLine();

                pop1.Update();

            }
               /* Chromosome parent1 = new Chromosome(SimpleKaryotype[0]);
            Chromosome parent2 = new Chromosome(SimpleKaryotype[0]);

            Chromosome child1 = pop.Crossover(parent1, parent2);
            Chromosome child2 = pop.Crossover(parent1, parent2);

            Console.Write("Parent 1: ");
            Console.WriteLine(parent1.ToString());
            Console.Write("Parent 2: ");
            Console.WriteLine(parent2.ToString());
            Console.WriteLine();

            Console.Write("X-Point1: |");
            for (int i = 0; i < _genomeSize; i++) {
                Console.Write(child1[i].crossPoint ? "|".PadLeft(_geneLength + 1, 'X') : "|".PadLeft(_geneLength + 1, ' '));
            }
            Console.WriteLine();
            Console.Write("Child  1: ");
            Console.WriteLine(child1.ToString());
            Console.Write("Mutation: |");
            for (int i = 0; i < _genomeSize; i++) {
                Console.Write(child1[i].mutation ? "|".PadLeft(_geneLength + 1, 'X') : "    |".PadLeft(_geneLength + 1, ' '));
            }
            Console.WriteLine();

            Console.WriteLine();
            Console.Write("X-Point2: |");
            for (int i = 0; i < _genomeSize; i++) {
                Console.Write(child2[i].crossPoint ? "|".PadLeft(_geneLength + 1, 'X') : "|".PadLeft(_geneLength + 1, ' '));
            }
            Console.WriteLine();
            Console.Write("Child  2: ");
            Console.WriteLine(child2.ToString());
            Console.Write("Mutation: |");
            for (int i = 0; i < _genomeSize; i++) {
                Console.Write(child2[i].mutation ? "|".PadLeft(_geneLength + 1, 'X') : "|".PadLeft(_geneLength + 1, ' '));
            }*/

            Console.Read();
        }