Exemple #1
0
        public GenotypeDiophantine(GenotypeDiophantine parentA, GenotypeDiophantine parentB)
        {
            if (parentA.genes.Length != parentB.genes.Length)
            {
                throw new ArgumentException(string.Format("parentA has {0} genes, parentB has {1}", parentA.genes.Length, parentB.genes.Length));
            }

            genes = new int[parentA.genes.Length];

            // shuffle parents
            GenotypeDiophantine p1 = parentA;
            GenotypeDiophantine p2 = parentB;

            if (rand.Next() % 2 == 1)
            {
                p1 = parentA;
                p2 = parentB;
            }

            // crossover line
            int crossover = rand.Next(1, genes.Length - 1);

            // 1 2 3 4
            // 5 6 7 8
            // cross = 1
            // 1 6 7 8
            Array.Copy(p1.genes, 0, genes, 0, crossover);
            Array.Copy(p2.genes, crossover, genes, crossover, genes.Length - crossover);

            //Console.WriteLine(string.Format("spawned GenotypeDiophantine {0}", genes));
        }
Exemple #2
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            int[] factors = new int[5] {
                -37, 4, 2, 8, -5
            };

            DiophantineSelection selection = new DiophantineSelection(
                initialPop: 10,
                fitnessCutoffRatio: 0.5,
                procreationsPerGenotype: 3,
                mutationRange: 5,
                mutationProbability: 0.25);

            GenotypeDiophantine solution = selection.Solve(factors, initGenesRange: 5, maxGenerations: 10);

            if (solution != null)
            {
                Console.WriteLine("Solution found: " + solution);
                solution.CheckEquation(factors);
            }
            else
            {
                Console.WriteLine("No solution found");
            }
        }
Exemple #3
0
        public GenotypeDiophantine Solve(int[] factors, int initGenesRange, int maxGenerations)
        {
            initPopulation(factors.Length - 1, initGenesRange);
            List <GenotypeDiophantine> generationSelected = new List <GenotypeDiophantine>();

            for (int gi = 0; gi < maxGenerations; ++gi)
            {
                int maxDelta = int.MinValue;
                int minDelta = int.MaxValue;

                for (int pi = 0; pi < population.Count; ++pi)
                {
                    int delta = population[pi].TestDiophantineEquation(factors);
                    if (delta == 0)
                    {
                        return(population[pi]); // We've got a winner!
                    }
                    if (delta > maxDelta)
                    {
                        maxDelta = delta;
                    }
                    if (delta < minDelta)
                    {
                        minDelta = delta;
                    }
                }


                int cutoffDelta = (int)(minDelta + (maxDelta - minDelta) * fitnessCutoffRatio);

                Console.WriteLine(string.Format("Generation {0}: population = {1}; delta = ({2}, {3})", gi, population.Count, minDelta, maxDelta));

                generationSelected.Clear();
                for (int pi = 0; pi < population.Count; ++pi)
                {
                    if (population[pi].TestedDelta <= cutoffDelta)
                    {
                        generationSelected.Add(population[pi]);
                        //Console.Write(population[pi].TestedDelta);
                        //Console.Write(" ");
                    }
                }
                //Console.Write("\n");

                // procreation
                population.Clear();

                for (int si = 0; si < generationSelected.Count; ++si)
                {
                    for (int proci = 0; proci < procreationsPerGenotype; ++proci)
                    {
                        int partner = si;
                        while (partner == si)
                        {
                            partner = GenotypeDiophantine.rand.Next(generationSelected.Count);
                        }

                        GenotypeDiophantine child = new GenotypeDiophantine(generationSelected[si], generationSelected[partner]);
                        child.Mutate(mutationRange, mutationProbability);
                        population.Add(child);
                    }
                }
            }
            return(null);
        }