static void Main() { IGAconfiguration configuration = new GAConfiguration(100, 0.9, null, 0.6, 0.3); IList<IIndividual<Wrapper<MyValuableClass>>> individualsList = new List<IIndividual<Wrapper<MyValuableClass>>>(); //Individuals initializing for (int i = 0; i < 1000; i++) { //Wrap source LOs to pass to GA List<Wrapper<MyValuableClass>> wrappedList = new List<Wrapper<MyValuableClass>>(); for (int j = 0; j < 5; j++) { wrappedList.Add(new Wrapper<MyValuableClass>(new MyValuableClass(GAUtils.GetRandom(0, 100)))); } individualsList.Add(new Individual<Wrapper<MyValuableClass>>(wrappedList)); } Console.WriteLine("Population creating"); var population = new Population<Wrapper<MyValuableClass>>(individualsList, IndividualEvaluateFunc, MutationFunc, CrossoverFunc, SelectionFunc); IGeneticAlgorithm<Wrapper<MyValuableClass>> ga = new GA<Wrapper<MyValuableClass>>(population); ObjectId id = new ObjectId(); Console.WriteLine("GA launching"); BackgroundWorker bgBackgroundWorker = new BackgroundWorker(); ga.Run(StopCondition, configuration, id, bgBackgroundWorker, WriteToCOnsole); foreach (var val in ga.Statistics.AverageFitnessFunctionValues) { Console.WriteLine(val); } string fileName = "data.txt"; using (StreamWriter outfile = new StreamWriter(fileName)) { foreach (var averageFitnessFunctionValue in ga.Statistics.AverageFitnessFunctionValues) { outfile.WriteLine(averageFitnessFunctionValue); } } //ga.Run(statistics => statistics.CurrentIteration > configuration.IterationsNumber, configuration, id, bgBackgroundWorker, WriteToCOnsole); }
private void bw_DoWork(object sender, DoWorkEventArgs e) { BackgroundWorker worker = sender as BackgroundWorker; GaTask task = (GaTask)e.Argument; try { task.ProgressPercent = 0; task.State = TaskState.Launched; task.LaunchTime = DateTime.Now; IList<IIndividual<Wrapper<LearningObject>>> individualsList = new List<IIndividual<Wrapper<LearningObject>>>(); //Individuals initializing for (int i = 0; i < task.GaConfigs.PopulationSize; i++) { //Wrap source LOs to pass to GA //List<Wrapper<LearningObject>> wrappedListOfLo = new List<Wrapper<LearningObject>>(); = task.SourceLearningObjects.Select(sourceLearningObject => new Wrapper<LearningObject>(GAUtils.GetProbability(0.5), sourceLearningObject)).ToList(); List<Wrapper<LearningObject>> wrappedListOfLo = new List<Wrapper<LearningObject>>(); foreach (var sourceLearningObject in task.SourceLearningObjects) { wrappedListOfLo.Add(new Wrapper<LearningObject>(GAUtils.GetProbability(0.5), sourceLearningObject)); } wrappedListOfLo = GAUtils.Shuffle(wrappedListOfLo).ToList(); individualsList.Add(new Individual<Wrapper<LearningObject>>(wrappedListOfLo)); } var population = new Population<Wrapper<LearningObject>>(individualsList, IndividualEvaluateFunc, MutationFunc, CrossoverFunc, SelectionFunc); IGeneticAlgorithm<Wrapper<LearningObject>> ga = new GA<Wrapper<LearningObject>>(population); IGAconfiguration configuration = new GAConfiguration(task.GaConfigs.GAIterationsNumber, task.GaConfigs.MutationProbability, task.GaConfigs.MutationPeriod, task.GaConfigs.CrossoverProbability, task.GaConfigs.ElitismPercentage); ga.Run(StopCondition, configuration, task.Id, worker); if (worker != null && worker.CancellationPending) { task.State = TaskState.Cancelled; e.Result = task; } else { task.State = TaskState.Finished; } if (task.State == TaskState.Finished) { if (worker != null) worker.ReportProgress(100, task.Id); } //Put result data to task task.AverageFFvalues = ga.Statistics.AverageFitnessFunctionValues; task.BestFfValues = ga.Statistics.BestFitnessFunctionValues; task.WorstFfValues = ga.Statistics.WorstFitnessFunctionValues; List<LearningObject> resultList = new List<LearningObject>(); foreach (var wrapper in ga.BestIndividual.Chromosome.Genes) { if (wrapper.Used) { resultList.Add(wrapper.Value); } } task.ResultLearningObjects = resultList; e.Result = task; } catch (Exception ex) { Console.WriteLine(ex.Message); task.State = TaskState.Error; task.ErrorMessage = ex.Message; e.Result = task; } }
static void Main(string[] args) { GA Ga; TravellingSalesmanObjective Objective; Galib.Mutation.IntegerSwapMutator Mutator; IntegerChromosomeSpecifier ChromosomeSpecifier; // // Create the objective objective. // int numberOfCities = 10; Objective = new TravellingSalesmanObjective(numberOfCities); // // Create the mutator // Mutator = new Galib.Mutation.IntegerSwapMutator(); // // Create the chromosome specifier. // ChromosomeSpecifier = new IntegerChromosomeSpecifier(); ChromosomeSpecifier.Length = numberOfCities; ChromosomeSpecifier.MinValue = 0; ChromosomeSpecifier.MaxValue = numberOfCities; ChromosomeSpecifier.PositionDependent = true; // // Create the recombinator // Galib.Recombination.PartiallyMatchedRecombinator Recombinator; Recombinator = new Galib.Recombination.PartiallyMatchedRecombinator(); Ga = new GA(); Ga.PopulationSize = 100; Ga.Mutator = Mutator; Ga.ChromosomeSpecifier = ChromosomeSpecifier; Ga.Objective = Objective; Ga.InvertedObjective = true; Ga.Recombinator = Recombinator; Ga.MaxGenerations = 500; Ga.Run(); Console.WriteLine("Best Individual:"); Console.WriteLine( Ga.Population.BestChromosome ); Console.WriteLine("Finished. Hit return."); Console.ReadLine(); }