// combine
        public Parents Combine(Parents parents)
        {
            // Crossover
            Random random = new Random();
            /*
            if(random.NextDouble() < .5 ) // 50% chance
            {
                int crossoverlocation = random.Next(0, 3); // Exclusive upper bound
                double temp = 0;
                switch(crossoverlocation)
                {
                    case 2: // Switch at 3rd val
                        temp = parents.father.c;
                        parents.father.c = parents.mother.c;
                        parents.mother.c = temp;
                        break;
                    case 1: // Switch at 2nd val
                        temp = parents.father.b;
                        parents.father.b = parents.mother.b;
                        parents.mother.b = temp;

                        temp = parents.father.c;
                        parents.father.c = parents.mother.c;
                        parents.mother.c = temp;
                        break;
                    case 0: // Switch at 1st val
                        temp = parents.father.a;
                        parents.father.a = parents.mother.a;
                        parents.mother.a = temp;

                        temp = parents.father.b;
                        parents.father.b = parents.mother.b;
                        parents.mother.b = temp;

                        temp = parents.father.c;
                        parents.father.c = parents.mother.c;
                        parents.mother.c = temp;
                        break;
                    default:
                        throw new IndexOutOfRangeException("The number provided must correlate with one of three fields of the line");
                }
            }
            */
            // For the first switch...
            double temp = 0;
            if (randGen.NextDouble() > 0.5)
            {
                temp = parents.father.a;
                parents.father.a = parents.mother.a;
                parents.mother.a = temp;
            }
            if (randGen.NextDouble() > 0.5)
            {
                temp = parents.father.b;
                parents.father.b = parents.mother.b;
                parents.mother.b = temp;
            }
            if (randGen.NextDouble() > 0.5)
            {
                temp = parents.father.c;
                parents.father.c = parents.mother.c;
                parents.mother.c = temp;
            }

            Mutate(ref parents.father.a);
            Mutate(ref parents.father.b);
            Mutate(ref parents.father.c);
            Mutate(ref parents.mother.a);
            Mutate(ref parents.mother.b);
            Mutate(ref parents.mother.c);

            return parents;
        }
Exemplo n.º 2
0
        public Line Run(Point[] _points, string id = "")
        {
            const int maxiterations = 1001;

            Point[] points = _points;
            Utilities utilities = new Utilities(points);
            Line[] newpopulation = new Line[utilities.lines.Length];
            Directory.CreateDirectory("Output/" + name + " Verbose Output/");
            using (TextWriter output = File.AppendText("Output/" + name + " Verbose Output/" + name + "-Verbose" + id + ".csv"))
            using (TextWriter meta = File.AppendText("Output/" + name + " Verbose Output/" + name + "-Meta" + id + ".csv")) // after every run, find the metadata file and add to it.
            {

                output.WriteLine("Seeds:");
                foreach (Line line in utilities.lines)
                {
                    Console.WriteLine("Seed: {0}x + {1}y = {2}", line.a, line.b, line.c);
                    output.WriteLine("{0}x + {1}y = {2}", line.a, line.b, line.c);
                }
                output.WriteLine(""); //newline
                Line bestLine = new Line(0, 0, 0);
                meta.WriteLine("Set,Best Average Distance,Average Average Distance");
                for (int enforcingmax = 0; enforcingmax < maxiterations; enforcingmax++)
                {
                    Console.WriteLine("Population {0} - Members:", enforcingmax);
                    output.WriteLine();
                    output.WriteLine("Population {0} - Members:", enforcingmax);
                    output.WriteLine("Line,Average Variance,Fitness");
                    if (utilities.lines.Length % 2 == 0) // An array with elements n has the length n-1 starting from 0
                    {
                        for (int i = 0; i < utilities.lines.Length; i += 2)
                        {
                            Line first = utilities.FairRandom();
                            Parents nextTwo = new Parents(first, utilities.FairRandom(utilities.lines.Where(x => x != first).ToArray())); // no need to worry about identification issues because this is reference object, and only bounced around in Select method - not copied
                            // combine and rise again? Something like that.
                            Parents pheonix = utilities.Combine(nextTwo);
                            newpopulation[i] = pheonix.father;
                            newpopulation[i + 1] = pheonix.mother;
                        }
                        utilities.lines = newpopulation.Clone() as Line[];
                        Array.Clear(newpopulation, 0, newpopulation.Length); // Reset for next gen
                        utilities.GiveChances();
                        foreach (Line line in utilities.lines)
                        {
                            // spit out put
                            Console.WriteLine("{0}x + {1}y = {2}", line.a, line.b, line.c);
                            output.WriteLine("{0}x + {1}y = {2},{3},{4}", line.a, line.b, line.c, line.averageDistance, line.selectionChance);
                        }
                        meta.WriteLine("Generation {0},{1},{2}", enforcingmax, utilities.lines.OrderBy(x => x.averageDistance).First().averageDistance, (utilities.lines.Sum(x => x.averageDistance) / utilities.lines.Length));
                    }
                    else if (utilities.lines.Length % 2 == 1)
                    {
                        for (int i = 0; i < utilities.lines.Length - 1; i += 2)
                        {
                            Line first = utilities.FairRandom();
                            Parents nextTwo = new Parents(first, utilities.FairRandom(utilities.lines.Where(x => x != first).ToArray()));

                            Parents pheonix = utilities.Combine(nextTwo);
                            newpopulation[i] = pheonix.father;
                            newpopulation[i + 1] = pheonix.mother;
                        }

                        Line second = utilities.FairRandom(); // There is room only for one
                        Parents nextOne = new Parents(second, utilities.FairRandom(utilities.lines.Where(x => x.selectionChance != second.selectionChance).ToArray()));
                        Line mysognist = utilities.Combine(nextOne).father;
                        newpopulation[newpopulation.Length - 1] = mysognist;

                        utilities.lines = newpopulation.Clone() as Line[];
                        Array.Clear(newpopulation, 0, newpopulation.Length);
                        utilities.GiveChances();
                        foreach(Line line in utilities.lines)
                        {
                            // spit out more put
                            Console.WriteLine("{0}x + {1}y = {2}", line.a, line.b, line.c);
                            output.WriteLine("{0}x + {1}y = {2},{3},{4}", line.a, line.b, line.c, line.averageDistance, line.selectionChance);
                        }
                        meta.WriteLine("Trial {1},{2},{3}", enforcingmax, utilities.lines.OrderBy(x => x.averageDistance).First().averageDistance, (utilities.lines.Sum(x => x.averageDistance) / utilities.lines.Length));
                    }
                }
                output.WriteLine();
                meta.WriteLine();
                bestLine = utilities.lines.OrderBy(x => x.averageDistance).First();
                Console.WriteLine("Final Best Line: {0}x + {1}y = {2}", bestLine.a, bestLine.b, bestLine.c);
                return bestLine;
            }
        }