예제 #1
0
        public IList <GeneScore> Run(IList <BaseChromosomeType> chromosomeTypes, OptimiserParameters parameters)
        {
            var allScores = new List <GeneScore>();

            var exiting         = false;
            var iterationCount  = 0;
            var totalIterations = parameters.MaximumGenerations * parameters.PopulationSize;

            var geneValidator = GetGeneValidator(chromosomeTypes, parameters.ParameterConditions);

            var evolver = new Evolver(chromosomeTypes, parameters.Seed,
                                      new CompositeGeneValidator(geneValidator, new OnceOnlyGeneValidator()));

            evolver.ReportNoProgress += ReportNoProgress;
            evolver.ReportNoProgress += delegate(object sender, CancelEventArgs args) { exiting |= args.Cancel; };

            _fitnessEvaluator.Initialise(parameters);

            IList <Gene> population = evolver.GetInitialPopulation(parameters.PopulationSize);

            for (var generation = 0; generation < parameters.MaximumGenerations; generation++)
            {
                var scoresThisGeneration = new List <GeneScore>();

                foreach (var gene in population)
                {
                    var score = _fitnessEvaluator.Evaluate(gene);
                    scoresThisGeneration.Add(score);

                    iterationCount++;

                    if (OnIterationComplete(score, iterationCount, totalIterations - iterationCount))
                    {
                        exiting = true;
                        break;
                    }
                }

                scoresThisGeneration.Sort();

                if (exiting)
                {
                    break;
                }

                population = evolver.GetNextGeneration(parameters.ReproductionRate, parameters.MutationRate,
                                                       scoresThisGeneration.ConvertAll(score => score.Gene));

                allScores.AddRange(scoresThisGeneration);

                OnGenerationComplete(generation, scoresThisGeneration);
            }

            allScores.Sort();

            OnComplete();

            return(allScores);
        }
예제 #2
0
        private void Start()
        {
            btnStart.Text     = "Stop";
            tmrRedraw.Enabled = true;

            for (int i = Int32.Parse(seedTxtBox.Text); evolvers.Count < EVOLVER_COUNT; i++)
            {
                Evolver evolver = new Evolver(i, (Pixel[])sourceColours.Clone());
                evolvers.Add(evolver);
            }

            evolvers.ForEach(evolver => evolver.start());
        }
예제 #3
0
        private void actionButton_Click(object sender, EventArgs e)
        {
            if (mode == Mode.Evolve)
            {
                SetGUI(false);

                var cts = new CancellationTokenSource();

                // TODO: Handle Cancellation
                Evolver <Settings, Genome, Report> .Evolve(
                    settings, cts,
                    resultSet =>
                {
                    cohortsBox.Items.Insert(0, resultSet);

                    cohortsBox.Refresh();
                },
                    errors =>
                {
                    // TODO: Handle Errors Gracefully
                    throw errors;
                },
                    () =>
                {
                    SetGUI(true);

                    cohortsBox.SelectedIndex = 0;

                    mode = Mode.WalkTrail;

                    actionButton.Text = "Walk Route";
                });
            }
            else
            {
                antGrid.WalkTrail(SelectedEntity,
                                  Properties.Settings.Default.TrailDelay);
            }
        }
예제 #4
0
    void NewSimulation(GenerationRecord data)
    {
        // Increment random seed so that each new run is not identical,
        // but still provides determinism, and increased variety.
        generationLength = data.GenLength;
        InitDroids();
        var numWeights = Droids[0].GetNumberOfWeights();

        genAlg = new Evolver(
            populationSize: Droids.Length,
            mutationRate: 0.1f,
            crossoverRate: 0.7f,
            numWeights: numWeights,
            initialGenes: data.Genomes,
            elitism: 2,
            eliteCopies: 4);
        genLengthX      = 1f;
        generationStart = Time.time;
        generationCount = data.GenCount;
        SetGenerationText();
        DroidEpoch();
    }
예제 #5
0
파일: Program.cs 프로젝트: esfdk/OPMGFS
        /// <summary>
        /// Runs standard evolution and/or MOEA seeded with maps found by the constrained novelty search.
        /// </summary>
        /// <param name="maps"> The maps to run evolution on. </param>
        /// <param name="r"> The random number generator. </param>
        /// <param name="mapSearchOptions"> The map search options. </param>
        /// <param name="noveltySearchOptions"> The novelty search options. </param>
        /// <param name="mapFitnessOptions"> The map fitness options. </param>
        /// <param name="numberOfNoveltyGenerations"> The number of generations to run for the novelty search. </param>
        /// <param name="numberOfEvolutionGenerations"> The number of generations to run for the standard evolution. </param>
        /// <param name="numberOfMOEAGenerations"> The number of generations to run for the MOEA. </param>
        /// <param name="evolutionPopulationSize"> The population size of the standard evolution. </param>
        /// <param name="numberOfParents"> The number of parents for the standard evolution. </param>
        /// <param name="numberOfChildren"> The number of children spawned per generation in the standard evolution. </param>
        /// <param name="evolutionMutationChance"> The chance of mutation happening during evolution. </param>
        /// <param name="moeaPopulationSize"> The population size of the MOEA. </param>
        /// <param name="moeaMutationChance"> The chance of mutation happening in the MOEA. </param>
        /// <param name="selectionStrategy"> The selection strategy used in the standard evolution. </param>
        /// <param name="parentSelectionStrategy"> The parent selection strategy used in the standard evolution. </param>
        /// <param name="populationStrategy"> The population strategy used in the standard evolution. </param>
        /// <param name="folderName"> The folder to save results to in "Images/Finished Maps". </param>
        /// <param name="fileToWriteTo"> The file to write timings and fitness per generation to. </param>
        /// <param name="selectHighestFitness"> Determines if the maps for seeding are chosen by highest fitness or highest novelty. </param>
        /// <param name="lowestFitnessLevelForPrint"> The fitness required before a map is printed. </param>
        /// <param name="runEvo"> Determines if the standard evolution should be run. </param>
        /// <param name="runMOEA"> Determines if the MOEA should be run. </param>
        public static void RunEvolutionWithNoveltyAsBase(
            List <MapPhenotype> maps,
            Random r,
            MapSearchOptions mapSearchOptions         = null,
            NoveltySearchOptions noveltySearchOptions = null,
            MapFitnessOptions mapFitnessOptions       = null,
            int numberOfNoveltyGenerations            = 10,
            int numberOfEvolutionGenerations          = 10,
            int numberOfMOEAGenerations                     = 10,
            int evolutionPopulationSize                     = 20,
            int numberOfParents                             = 6,
            int numberOfChildren                            = 16,
            double evolutionMutationChance                  = 0.3,
            int moeaPopulationSize                          = 25,
            double moeaMutationChance                       = 0.3,
            Enums.SelectionStrategy selectionStrategy       = Enums.SelectionStrategy.ChanceBased,
            Enums.SelectionStrategy parentSelectionStrategy = Enums.SelectionStrategy.ChanceBased,
            Enums.PopulationStrategy populationStrategy     = Enums.PopulationStrategy.Mutation,
            string folderName                 = "MapNoveltyEvolution",
            string fileToWriteTo              = "NoveltyEvolutionHighestFitnessSearchGenerationTimes.txt",
            bool selectHighestFitness         = true,
            double lowestFitnessLevelForPrint = double.MinValue,
            bool runEvo  = true,
            bool runMOEA = true)
        {
            var stringToWrite  = new StringBuilder();
            var mso            = mapSearchOptions ?? new MapSearchOptions(null);
            var mfo            = mapFitnessOptions ?? new MapFitnessOptions();
            var nso            = noveltySearchOptions ?? new NoveltySearchOptions();
            var listOfArchives = new List <List <Solution> >();

            // Novelty search
            var sw = new Stopwatch();

            sw.Start();
            var baseMapCounter = 0;

            foreach (var map in maps)
            {
                sw.Restart();
                stringToWrite.AppendLine(string.Format("Performing novelty search on base map number {0}.", baseMapCounter));
                var heightLevels = map.HeightLevels.Clone() as Enums.HeightLevel[, ];
                var items        = map.MapItems.Clone() as Enums.Item[, ];
                var baseMap      = new MapPhenotype(heightLevels, items);
                baseMap.CreateCompleteMap(Enums.Half.Top, Enums.MapFunction.Turn).SaveMapToPngFile(string.Format("Base Map {0}", baseMapCounter), folderName, false);

                mso = new MapSearchOptions(map, mso);
                var searcher = new MapSearcher(r, mso, nso);

                searcher.RunGenerations(numberOfNoveltyGenerations, stringToWrite);

                listOfArchives.Add(searcher.Archive.Archive);

                stringToWrite.AppendLine(string.Format("It took {0} ms to perform novelty search on base map number {1}.", sw.ElapsedMilliseconds, baseMapCounter));
                stringToWrite.AppendLine();

                baseMapCounter++;
            }

            baseMapCounter = 0;

            sw.Restart();
            var solutions = new List <List <EvolvableMap> >();

            if (selectHighestFitness)
            {
                stringToWrite.AppendLine(string.Format("Sorting initial population based by highest fitness."));
                foreach (var map in maps)
                {
                    var evolvableMaps = new List <EvolvableMap>();
                    var archive       = listOfArchives[baseMapCounter];

                    foreach (var solution in archive)
                    {
                        var ms = (MapSolution)solution;
                        mso = new MapSearchOptions(map, mso);
                        var evolvableMap = new EvolvableMap(mso, evolutionMutationChance, r, mfo, ms.MapPoints);
                        evolvableMap.CalculateFitness();
                        evolvableMaps.Add(evolvableMap);
                    }

                    solutions.Add(evolvableMaps.OrderByDescending(s => s.Fitness).ToList());

                    baseMapCounter++;
                }

                stringToWrite.AppendLine(string.Format("It took {0} ms to find maps with the highest fitness.", sw.ElapsedMilliseconds));
                sw.Restart();
            }
            else
            {
                stringToWrite.AppendLine(string.Format("Sorting initial population based by highest novelty."));
                foreach (var map in maps)
                {
                    var evolvableMaps = new List <EvolvableMap>();
                    var archive       = listOfArchives[baseMapCounter];

                    var tempArchive = archive.OrderByDescending(s => s.Novelty);

                    foreach (var solution in tempArchive)
                    {
                        var ms = (MapSolution)solution;
                        mso = new MapSearchOptions(map, mso);
                        var evolvableMap = new EvolvableMap(mso, evolutionMutationChance, r, mfo, ms.MapPoints);
                        evolvableMap.CalculateFitness();
                        evolvableMaps.Add(evolvableMap);
                    }

                    solutions.Add(evolvableMaps);

                    baseMapCounter++;
                }

                stringToWrite.AppendLine(string.Format("It took {0} ms to find maps with the highest novelty.", sw.ElapsedMilliseconds));
                sw.Restart();
            }

            // Evolution
            if (runEvo)
            {
                sw.Restart();
                var bestMaps = new List <EvolvableMap>();
                baseMapCounter = 0;
                foreach (var map in maps)
                {
                    sw.Restart();
                    stringToWrite.AppendLine(string.Format("Performing evolution on base map number {0}.", baseMapCounter));
                    mso = new MapSearchOptions(map, mso);
                    var evolver = new Evolver <EvolvableMap>(
                        numberOfEvolutionGenerations,
                        evolutionPopulationSize,
                        numberOfParents,
                        numberOfChildren,
                        evolutionMutationChance,
                        r,
                        new object[] { mso, evolutionMutationChance, r, mfo })
                    {
                        PopulationSelectionStrategy = selectionStrategy,
                        ParentSelectionStrategy     = parentSelectionStrategy,
                        PopulationStrategy          = populationStrategy
                    };

                    evolver.Initialize(solutions[baseMapCounter].Take(evolutionPopulationSize), stringToWrite);
                    evolver.Evolve(stringToWrite);
                    var variationValue = 0;

                    foreach (var individual in evolver.Population)
                    {
                        variationValue++;
                        if (individual.Fitness >= lowestFitnessLevelForPrint)
                        {
                            individual.ConvertedPhenotype.SaveMapToPngFile(string.Format("Evo_Base Map {0}_Map {1}_Fitness {2}", baseMapCounter, variationValue, individual.Fitness), folderName, false);
                        }
                    }

                    stringToWrite.AppendLine(string.Format("It took {0} ms to perform evolution on base map number {1}.", sw.ElapsedMilliseconds, baseMapCounter));
                    stringToWrite.AppendLine();

                    bestMaps.Add(evolver.Population.OrderByDescending(evoMap => evoMap.Fitness).ToList()[0]);
                    MapHelper.SaveGreyscaleNoveltyMap(evolver.Population, string.Format("Evo_Base Map {0} NoveltyMap", baseMapCounter), folderName);

                    baseMapCounter++;
                }

                MapHelper.SaveGreyscaleNoveltyMap(bestMaps, string.Format("Evo_Best Maps NoveltyMap"), folderName);
            }

            // MOEA
            if (runMOEA)
            {
                baseMapCounter = 0;
                var bestMaps = new List <EvolvableMap>();
                foreach (var map in maps)
                {
                    sw.Restart();
                    stringToWrite.AppendLine(string.Format("Performing multi objective evolution for base map number {0}", baseMapCounter));

                    var heightLevels = map.HeightLevels.Clone() as Enums.HeightLevel[, ];
                    var items        = map.MapItems.Clone() as Enums.Item[, ];
                    var baseMap      = new MapPhenotype(heightLevels, items);
                    baseMap.CreateCompleteMap(Enums.Half.Top, Enums.MapFunction.Mirror);
                    baseMap.SaveMapToPngFile(string.Format("Base Map {0}", baseMapCounter), folderName, false);

                    mso = new MapSearchOptions(map, mso);
                    var evolver = new MultiObjectiveEvolver(
                        numberOfMOEAGenerations,
                        moeaPopulationSize,
                        moeaMutationChance,
                        r,
                        mso,
                        mfo);

                    evolver.RunEvolution(stringToWrite, solutions[baseMapCounter].Take(moeaPopulationSize));

                    var variationValue = 0;

                    foreach (var individual in evolver.Population)
                    {
                        variationValue++;
                        if (individual.Fitness >= lowestFitnessLevelForPrint)
                        {
                            individual.ConvertedPhenotype.SaveMapToPngFile(string.Format("MOEA_Base Map {0}_Map {1}_Fitness {2}", baseMapCounter, variationValue, individual.Fitness), folderName, false);
                        }
                    }

                    stringToWrite.AppendLine(string.Format("It took {0} ms to perform multi objective evolution on base map number {1}", sw.ElapsedMilliseconds, baseMapCounter));
                    stringToWrite.AppendLine();

                    bestMaps.Add(evolver.Population.OrderByDescending(evoMap => evoMap.Fitness).ToList()[0]);
                    MapHelper.SaveGreyscaleNoveltyMap(evolver.Population, string.Format("MOEA_Base Map {0} NoveltyMap", baseMapCounter), folderName);

                    baseMapCounter++;
                }

                MapHelper.SaveGreyscaleNoveltyMap(bestMaps, string.Format("MOEA_Best Maps NoveltyMap"), folderName);
            }

            WriteToTextFile(stringToWrite.ToString(), fileToWriteTo, folderName);
        }
예제 #6
0
파일: Program.cs 프로젝트: esfdk/OPMGFS
        /// <summary>
        /// Runs evolution with the given settings.
        /// </summary>
        /// <param name="maps"> The maps to run evolution on. </param>
        /// <param name="r"> The random number generator. </param>
        /// <param name="mapSearchOptions"> The map search options. </param>
        /// <param name="mapFitnessOptions"> The map fitness options. </param>
        /// <param name="numberOfGenerations"> The number of generations to run. </param>
        /// <param name="populationSize"> The population size of the evolution. </param>
        /// <param name="numberOfParents"> The number of parents used in the evolution. </param>
        /// <param name="numberOfChildren"> The number of children spawned per generation. </param>
        /// <param name="mutationChance"> The chance of mutation happening. </param>
        /// <param name="selectionStrategy"> The selection strategy. </param>
        /// <param name="parentSelectionStrategy"> The parent selection strategy. </param>
        /// <param name="populationStrategy"> The population strategy. </param>
        /// <param name="folderName"> The folder to save results to in "Images/Finished Maps". </param>
        /// <param name="fileToWriteTo"> The file to write timings and fitness per generation to. </param>
        /// <param name="lowestFitnessLevelForPrint"> The fitness required before a map is printed. </param>
        public static void RunEvolution(
            List <MapPhenotype> maps,
            Random r,
            MapSearchOptions mapSearchOptions   = null,
            MapFitnessOptions mapFitnessOptions = null,
            int numberOfGenerations             = 10,
            int populationSize    = 20,
            int numberOfParents   = 6,
            int numberOfChildren  = 16,
            double mutationChance = 0.3,
            Enums.SelectionStrategy selectionStrategy       = Enums.SelectionStrategy.ChanceBased,
            Enums.SelectionStrategy parentSelectionStrategy = Enums.SelectionStrategy.ChanceBased,
            Enums.PopulationStrategy populationStrategy     = Enums.PopulationStrategy.Mutation,
            string folderName    = "MapEvolution",
            string fileToWriteTo = "EvolutionGenerationTimes.txt",
            double lowestFitnessLevelForPrint = double.MinValue)
        {
            var stringToWrite = new StringBuilder();
            var mso           = mapSearchOptions ?? new MapSearchOptions(null);
            var mfo           = mapFitnessOptions ?? new MapFitnessOptions();
            var bestMaps      = new List <EvolvableMap>();

            var sw = new Stopwatch();

            sw.Start();
            var baseMapCounter = 0;

            foreach (var map in maps)
            {
                sw.Restart();
                stringToWrite.AppendLine(string.Format("Performing evolution on base map number {0}.", baseMapCounter));
                var heightLevels = map.HeightLevels.Clone() as Enums.HeightLevel[, ];
                var items        = map.MapItems.Clone() as Enums.Item[, ];
                var baseMap      = new MapPhenotype(heightLevels, items);
                baseMap.CreateCompleteMap(Enums.Half.Top, Enums.MapFunction.Turn).SaveMapToPngFile(string.Format("Base Map {0}", baseMapCounter), folderName, false);

                mso = new MapSearchOptions(map, mso);
                var evolver = new Evolver <EvolvableMap>(
                    numberOfGenerations,
                    populationSize,
                    numberOfParents,
                    numberOfChildren,
                    mutationChance,
                    r,
                    new object[] { mso, mutationChance, r, mfo })
                {
                    PopulationSelectionStrategy = selectionStrategy,
                    ParentSelectionStrategy     = parentSelectionStrategy,
                    PopulationStrategy          = populationStrategy
                };

                evolver.Initialize(stringToWrite);
                var bestMap        = evolver.Evolve(stringToWrite);
                var variationValue = 0;

                foreach (var individual in evolver.Population)
                {
                    variationValue++;
                    if (individual.Fitness >= lowestFitnessLevelForPrint)
                    {
                        individual.ConvertedPhenotype.SaveMapToPngFile(string.Format("Base Map {0}_Map {1}_Fitness {2}", baseMapCounter, variationValue, individual.Fitness), folderName, false);
                    }
                }

                stringToWrite.AppendLine(string.Format("It took {0} ms to perform evolution on base map number {1}.", sw.ElapsedMilliseconds, baseMapCounter));
                stringToWrite.AppendLine();

                bestMaps.Add(evolver.Population.OrderByDescending(evoMap => evoMap.Fitness).ToList()[0]);
                MapHelper.SaveGreyscaleNoveltyMap(evolver.Population, string.Format("Base Map {0} NoveltyMap", baseMapCounter), folderName);

                Console.WriteLine("The map fitness values of base map {0} were: {1}", baseMapCounter, ((EvolvableMap)bestMap).MapFitnessValues);

                baseMapCounter++;
            }

            MapHelper.SaveGreyscaleNoveltyMap(bestMaps, string.Format(" Best Maps NoveltyMap"), folderName);
            WriteToTextFile(stringToWrite.ToString(), fileToWriteTo, folderName);
        }
예제 #7
0
        private void Start()
        {
            btnStart.Text = "Stop";
            tmrRedraw.Enabled = true;

            for (int i = Int32.Parse(seedTxtBox.Text); evolvers.Count < EVOLVER_COUNT; i++)
            {
                Evolver evolver = new Evolver(i, (Pixel[])sourceColours.Clone());
                evolvers.Add(evolver);
            }

            evolvers.ForEach(evolver => evolver.start());
        }
예제 #8
0
 public GeneticTrainer(Evolver <EvolvableNet> evolver, GeneticTrainerOptions options) : base(options, new GeneticSession(null))
 {
     _evolver = evolver;
 }
예제 #9
0
    //Class for miscellaneous test functions.
    public static void TestFullMultiGeneration()
    {
        //Should probably generalize this to a "tag case" object...
        double    dx      = 1.1d;
        StopWatch tagload = new StopWatch("tag load");

        tagload.tic();
        Tag[] tags = Tag.ExtractFromFile("geom-sets/cylinder-hollow/tags.tg");
        tagload.toc();
        Console.WriteLine("Loading tags...");
        Console.WriteLine(tagload.Result());
        string[] filenames = new string[tags.Length];
        int      k         = 0;

        foreach (Tag t in tags)
        {
            Console.WriteLine(t.ToString());
            filenames[k] = t.RegionFilename;
            Console.WriteLine(filenames[k] + "?exist=" + File.Exists(filenames[k]).ToString());
            k++;
        }
        Console.WriteLine();

        Console.WriteLine("Generating points...");
        StopWatch buildpoints = new StopWatch("build points");

        buildpoints.tic();
        double[]   bounds = PointCloud.GetBoundsFromStls(filenames);
        PointCloud pc     = PointCloud.FromBounds(bounds, dx);

        pc.WriteToCsv("./testcsv/diag.csv");
        buildpoints.toc();
        Console.WriteLine(buildpoints.Result());
        Console.WriteLine();

        Console.WriteLine("Initializing lattice...");
        StopWatch initlat = new StopWatch("initialize lattice");

        initlat.tic();
        LatticeStateInitializer init = new LatticeStateInitializer(pc, tags, 4, 20, 1.01 * dx);

        init.InitializeNodes();
        initlat.toc();
        Console.WriteLine(initlat.Result());
        Console.WriteLine();

        Console.WriteLine("Building lattice...");
        StopWatch blat = new StopWatch("build lattice");

        blat.tic();
        LatticeState s = init.BuildLatticeState();

        blat.toc();
        Console.WriteLine(blat.Result());
        Console.WriteLine();

        StopWatch file = new StopWatch("write to file");

        Console.WriteLine("Writing to directory...");
        file.tic();
        s.WriteToDirectory("./lattice-output-test/cylgen", true);
        file.toc();
        Console.WriteLine(file.Result());
        Console.WriteLine();
        Console.WriteLine("Done.");
        Console.WriteLine();

        Console.WriteLine("Evolving...");
        Evolver e = new Evolver(s, 0.1, 4);

        e.EvolveAll("evolvetest", 500, true);
    }
예제 #10
0
        private void tmrRedraw_Tick(object sender, EventArgs e)
        {
            // select current;
            Evolver evolver = currentEvolver;

            if (evolver == null)
            {
                Console.WriteLine("evolver fail");
                return;
            }

            lock (evolver.evolverLock)
            {
                currentDrawing = evolver.currentDrawing.Clone();
            }
            if (currentDrawing == null)
            {
                Console.WriteLine("currentDrawing fail");
                return;
            }
            String txt = String.Format("Current     : {0}\r\n", currIndex);

            for (int i = 0; i < EVOLVER_COUNT; i++)
            {
                txt += String.Format("Evolver {0} : {1}\r\n", i, evolvers[i].errorLevel);
            }
            textBox1.Text = txt;

            toolStripStatusLabelFitness.Text    = evolver.errorLevel.ToString();
            toolStripStatusLabelGeneration.Text = evolver.generation.ToString();
            toolStripStatusLabelSelected.Text   = evolver.selected.ToString();
            toolStripStatusLabelPolygons.Text   = evolver.currentDrawing.Shapes.Count.ToString();

            if (EVOLVER_COUNT > 1)
            {
                Evolver e1 = evolvers.Find(ev => ev.readyToBreed);
                if (e1 != null)
                {
                    Evolver e2 = evolvers.Find(ev => ev.readyToBreed && ev != e1);
                    if (e2 != null)
                    {
                        e1.breed(e2);
                    }
                }
            }

            bool shouldRepaint = false;

            if (repaintIntervall.Ticks > 0)
            {
                if (lastRepaint < DateTime.Now - repaintIntervall)
                {
                    shouldRepaint = true;
                }
            }

            if (repaintOnSelectedSteps > 0)
            {
                if (lastSelected + repaintOnSelectedSteps < evolver.selected)
                {
                    shouldRepaint = true;
                }
            }

            if (shouldRepaint)
            {
                redraw();
            }
        }