private static void Main(string[] args) { GA gA = new GA(); gA.Test(); Console.WriteLine("按任意键结束"); Console.ReadKey(); }
//Main GA logic funtion private void StartToFind() { bool isFound = false; defaultMaze = maze.DeepCopy(); GA ga = new GA() { MaxMove = N * 3, PopulationSize = 100, MutationRate = 0.6f }; List <ChromosomeEntity> best = new List <ChromosomeEntity>(); ga.CreatePopulation(); //Creating First Population int generationCount = 1; ChromosomeEntity tmp = new ChromosomeEntity(); int bestscore = int.MaxValue; do { List <ChromosomeEntity> fitness = new List <ChromosomeEntity>(); foreach (ChromosomeEntity ch in ga.Population) { if (ga.Fitness(maze, ch) == -1) //fitness for generation untill arrive finish { isFound = true; tmp = ch; best.Add(ch.DeepCopy()); break; } fitness.Add(ch); //adding chromosome to list with scores maze = defaultMaze.DeepCopy(); //settings for next generation } if (!isFound) //if is not found { var sorted = fitness.OrderBy(x => x.Score).ToList(); //sort list by Score var newpop = ga.Crossover(sorted.Take(sorted.Count / 2).ToList(), defaultMaze.DeepCopy()); //sending half fit ch for next generation bestscore = sorted[0].Score; tmp = sorted[0]; //to show bestfit chromosome ga.Population = newpop; } //showing best fit in generation Thread t1 = new Thread(() => { ga.Fitness(maze, tmp); pnlMain.Invalidate(); maze = defaultMaze.DeepCopy(); }); t1.Start(); t1.Join(); generationCount++; } while (!isFound); MessageBox.Show("Path found! Generation Count=" + generationCount.ToString()); }
private void BtnRun_Click(object sender, EventArgs e) { ga = new GA((int)nudPopulation.Value, (int)nudItrations.Value); ga.SetData(((double)nudCrossover.Value, (double)nudMutation.Value)); // Event handlers ga.OnStart += GA_OnStart; ga.OnIteration += GA_OnIteration; ga.OnEnd += GA_OnEnd; ga.Run(optimizationProblem); }
public FittingDetailsWindow(GA GAInstance) { InitializeComponent(); GA = GAInstance; series = new SeriesCollection(); heatseries = new SeriesCollection(); Summary(); Reload_Individuals(); DrawIndividuals(); DrawHeatMap(); }
static void Main(string[] args) { StoppingCriteria criteria = new StoppingCriteria(StoppingCriteria.NUMBER_OF_GENERATIONS, 1000000); GAOptions options = new GAOptions(10, (float)0.001, criteria); geneticAlgorithm = new GA(options); while (geneticAlgorithm.continueExecution) { geneticAlgorithm.run(); Console.WriteLine("Generation: " + geneticAlgorithm.updateInformation["GenerationNumber"]); Console.WriteLine("Max Fitness: " + geneticAlgorithm.updateInformation["MaxPopulationFitness"]); for (int i = 0; i < Fitness.nInputVars; i++) { Console.WriteLine("Var" + (i + 1) + ": " + geneticAlgorithm.updateInformation["Var" + (i + 1)]); } Console.WriteLine("-----"); } Console.WriteLine("Genetic Algorithm has finished"); Console.ReadLine(); }
private void DrawHeatMap() { HeatSeries heatSeries = new HeatSeries(); ChartValues <HeatPoint> Values = new ChartValues <HeatPoint>(); int xStart = (int)GA.evaluatedFunction.xDomain.X, xEnd = (int)GA.evaluatedFunction.xDomain.Y, yStart = (int)GA.evaluatedFunction.yDomain.X, yEnd = (int)GA.evaluatedFunction.yDomain.Y; for (int i = xStart; i < xEnd; i++) { for (int j = yStart; j < yEnd; j++) { Values.Add(new HeatPoint(i, j, GA.fitnessFunction(i, j))); } } heatSeries.Values = Values; heatSeries.Title = "f(x,y) = "; heatseries.Add(heatSeries); HeatMap.Series.Add(heatseries[0]); }
public void Start() { // Start timer Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); Settings.Init3rdMachines(); printer = new Printer(); printer.PrintProblemDescription(); // Initialize ga instance ga = new GA( Settings.SelectionOperator, Settings.CrossoverOperator, Settings.MutationOperator ); out1_csvWriter = new Writer( @"..\..\..\out1.csv", "Generation,Fitness" ); out2_csvWriter = new Writer( @"..\..\..\out2.csv", "Index, OriginalLatitude, OriginalLongitude, OriginalReadyTime, ProcRate, Latitude, Longitude, LatestReadyTime, LoadingUnitCost, IsGearAccepting, IsDedicated, DedicatedCustomer, IsThirdParty, IsCompulsary, EventIndex, Type, StartTime, EndTime, JobIndex, Latitude, Longitude, ReadyTime, RequestedProcRate, RequestedProcTime, ProcTime, Quantity, IsGeared, IsDedicated, Shipper, IsOutOfLaycan, IsUnloading, MachineIdUnload, IsBarge, MachineIdBarge, Demurrage, Despatch, Priority, TotalCost, TravelCost, HandlingCost, DndCost" ); // Update algorithm until stopping conditions are met: // Condition 1: if total number of generation reaches a threshold // Condition 2: if number of generation where the fitness remained constant reaches a threshold // Condition 3: if global optimizor (if defined) is reached int repeated_generations = 0; while ( ga.Generation < Settings.MaxGenerations && repeated_generations < Settings.MaxRepeatedGenerations && ga.BestFitness > Settings.Solution || ga.BestFitness == 0 ) { double prevBestFitness = ga.BestFitness; // Update ga.NewGeneration(); printer.PrintCurrentGen(ga); out1_csvWriter.WriteLine(string.Format("{0}, {1}", ga.Generation, ga.BestFitness)); if (prevBestFitness == ga.BestFitness) { repeated_generations++; } else { repeated_generations = 0; } } // Get the elapsed time as a TimeSpan value and format the TimeSpan value. stopWatch.Stop(); string elapsedTime = string.Format("{0:00}:{1:00}:{2:00}.{3:000}", stopWatch.Elapsed.Hours, stopWatch.Elapsed.Minutes, stopWatch.Elapsed.Seconds, stopWatch.Elapsed.Milliseconds); printer.PrintResult(ga, elapsedTime); foreach (Machine machine in ga.BestChromosome.Schedule.Machines) { foreach (Event evt in machine.ScheduledEvents) { out2_csvWriter.WriteLine( string.Join(",", machine.Index, machine.OriginalLatitude, machine.OriginalLongitude, machine.OriginalReadyTime, machine.ProcRate, machine.Latitude, machine.Longitude, machine.LatestReadyTime, machine.LoadingUnitCost, machine.IsGearAccepting, machine.IsDedicated, machine.DedicatedCustomer, machine.IsThirdParty, machine.IsCompulsary, evt.Index, evt.Type, evt.StartTime, evt.EndTime, evt._Job.Index, evt._Job.Latitude, evt._Job.Longitude, evt._Job.ReadyTime, evt._Job.RequestedProcRate, evt._Job.RequestedProcTime, evt._Job.ProcTime, evt._Job.Quantity, evt._Job.IsGeared, evt._Job.IsDedicated, evt._Job.Shipper, evt._Job.IsOutOfLaycan, evt._Job.IsUnloading, evt._Job.MachineIdUnload, evt._Job.IsBarge, evt._Job.MachineIdBarge, evt._Job.Demurrage, evt._Job.Despatch, evt._Job.Priority, evt._Job.TotalCost, evt._Job.TravelCost, evt._Job.HandlingCost, evt._Job.DndCost ) ); } } out1_csvWriter.SaveFile(); out2_csvWriter.SaveFile(); }