public void Run() { GA Ga; Ga = new GA(); Ga.EncodingType = EncodingType.Integer; Ga.MinIntValue = -5000; Ga.MaxIntValue = 5000; Ga.ChromosomeLength = Coefficients.Length; Ga.PopulationSize = Coefficients.Length * 10; Ga.Objective = new ObjectiveDelegate( this.LinearDiophantineObjective ); Ga.ObjectiveType = ObjectiveType.MinimizeObjective; Ga.Terminate += new TerminateEventHandler( new ObjectiveThresholdTerminator(0).Terminate ); Ga.Terminate += new TerminateEventHandler( new EvolutionTimeTerminator( new TimeSpan(0,0,20) ).Terminate ); Ga.NewPopulation += new NewPopulationEventHandler( OnNewPopulation_ShowSummary ); Ga.FitnessScaling = FitnessScaling.LinearRanked; Ga.Run(); Console.WriteLine("Best Individual: (obj={0})", Ga.BestObjective); PopulationSummary ps = new PopulationSummary( Ga, Ga.Population ); Console.WriteLine(ps.BestChromosome); Console.WriteLine("Finished. Hit return."); Console.ReadLine(); }
static void Main() { // // Create the genetic algorithm component, and set the required // properties. // GA ga = new GA(); ga.EncodingType = EncodingType.Binary; ga.ChromosomeLength = 10; ga.PopulationSize = 50; ga.MaxGenerations = 10; // // Set the objective function for the run, which provides feedback // to the algorithm as to the relative merit of candidate solutions. // ga.Objective = new ObjectiveDelegate( BinaryAlternateObjective ); // // Set a "NewPopulation" event handler so that we get a callback on // each new generation. During the callback, we print out the // generation's statistics and the best chromosome found so far. // ga.NewPopulation += new NewPopulationEventHandler( OnNewPopulation_ShowSummary ); // // Run the algorithm. It will stop after ga.MaxGenerations have elapsed. // ga.Run(); // // Output the best individual that was found during the run. // Console.WriteLine("Best Individual:"); PopulationSummary ps = new PopulationSummary( ga, ga.Population ); Console.WriteLine(ps.BestChromosome); Console.WriteLine("Finished. Hit return."); Console.ReadLine(); }
static void Main() { // // Describe the coding of our problem as an array of 10 integers // in the range [-10,10]. // GA ga = new GA(); ga.ChromosomeLength = 10; ga.PopulationSize = 100; ga.MaxGenerations = 100; ga.EncodingType = EncodingType.Integer; ga.MinIntValue = -10; ga.MaxIntValue = 10; ga.Objective = new ObjectiveDelegate( IntegerMaximizationObjective ); ga.MutationOperator = MutationOperator.GeneSpecific; // // Set a "NewPopulation" event handler so that we get a callback on // each new generation. During the callback, we print out the // generation's statistics and the best chromosome found so far. // ga.NewPopulation += new NewPopulationEventHandler( OnNewPopulation_ShowSummary ); // // Let it run until default termination criteria are met, or // MaxGenerations has elapsed. // ga.Run(); // // Output the best individual that was found during the run. // Console.WriteLine("Best Individual:"); PopulationSummary ps = new PopulationSummary( ga, ga.Population ); Console.WriteLine( ps.BestChromosome ); Console.WriteLine("Finished. Hit return."); Console.ReadLine(); }
static void Main(string[] args) { GA Ga; TravellingSalesman1dObjective Objective; TravelingSalesman2dObjective Objective2; int[] cities = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1400, 15000}; Objective = new TravellingSalesman1dObjective(cities); // // Create the objective. // System.Collections.ArrayList cities2 = new System.Collections.ArrayList(); //System.IO.StreamReader sr = System.IO.File.OpenText("../../xpf131.tsp"); System.IO.StreamReader sr = System.IO.File.OpenText("../../xit1083.tsp"); string s; bool gotime = false; s = sr.ReadLine(); while ( s != null ) { s.Trim(); if ( !gotime ) { if (s == "NODE_COORD_SECTION" ) { gotime = true; } s = sr.ReadLine(); continue; } if ( s == "EOF" ) { break; } string[] sa = s.Split(' ', '\t'); if ( sa.Length != 3 ) { throw new Exception(); } int city = Int32.Parse(sa[0]); int x = Int32.Parse(sa[1]); int y = Int32.Parse(sa[2]); cities2.Add( new Point(x, y) ); s = sr.ReadLine(); } sr.Close(); //Point[] cities2 = new Point[] { }; Objective2 = new TravelingSalesman2dObjective( (Point[]) cities2.ToArray(typeof(Point))); // // Create the mutator // Ga = new GA(); Ga.GeneDescriptors = new GeneDescriptor[] { new IntegerGeneDescriptor() }; Ga.Homogeneous = true; //Ga.ChromosomeLength = cities.Length; Ga.ChromosomeLength = cities2.Count; Ga.Recombinator = new RecombinationDelegate( new genX.Recombination.PartiallyMatchedCrossover().Recombine ); Ga.OrderMutator = new OrderMutationDelegate( genX.Reordering.Swap.Reorder ); Ga.ValueMutator = null; Ga.Scaler = new ScalingDelegate( new genX.Scaling.LinearRankedFitnessScaler(1.6).Scale ); Ga.RecombinationProbability = 0.7; Ga.PopulationSize = 500; // Ga.Objective = new ObjectiveDelegate( Objective.GetObjective ); Ga.Objective = new ObjectiveDelegate( Objective2.GetObjective ); Ga.ObjectiveType = ObjectiveType.MinimizeObjective; Ga.MaxGenerations = 1000000; Ga.NewPopulation += new NewPopulationEventHandler( OnNewPopulation ); Ga.Run(); Console.WriteLine("Best Individual:"); Console.WriteLine( Ga.Population.Summary.BestChromosome ); Console.WriteLine("Finished. Hit return."); Console.ReadLine(); }
static void Main() { // // Create the genetic algorithm, and set the required properties. // GA ga = new GA(); ga.EncodingType = EncodingType.Binary; ga.ChromosomeLength = 25; //ga.Objective = new ObjectiveDelegate( BinaryMaximizationObjective ); ga.Objective = new ObjectiveDelegate( BinaryAlternateObjective ); ga.PopulationSize = 25; //ga.Mutated += new MutatedEventHandler( OnMutated ); //ga.NewPopulation += new NewPopulationEventHandler( OnNewPopulation ); //ga.NewPopulation += new NewPopulationEventHandler( OnNewPopulation_ShowParents ); ga.NewPopulation += new NewPopulationEventHandler( OnNewPopulation_ShowSummary ); //ga.NewPopulation += new NewPopulationEventHandler( new BinaryPersist().NewPopulationHandler ); //ga.NewPopulation += new NewPopulationEventHandler( new BinaryPersist().NewPopulationHandlerSoap ); optimalObjective = 25; //ga.Terminate += new TerminateEventHandler( OnTerminate_CheckForOptimal ); ga.Terminate += new TerminateEventHandler( new ObjectiveThresholdTerminator(optimalObjective).Terminate ); ga.FitnessScaling = FitnessScaling.LinearRanked; ga.GeneMutationProbability = 0.05; ga.Run(50); // // Output the best individual that was found during the run. // Console.WriteLine("Best Individual:"); PopulationSummary ps = new PopulationSummary( ga, ga.Population ); Console.WriteLine(ps.BestChromosome); Console.WriteLine("Finished. Hit return."); Console.ReadLine(); }
static void Main(string[] args) { GA ga; ga = new GA(); ga.Homogeneous = true; ga.ChromosomeLength = 5; ga.EncodingType = EncodingType.Real; ga.MinDoubleValue = -30; ga.MaxDoubleValue = 30; ga.Objective = new ObjectiveDelegate(AckleyMinimizationObjective); ga.ObjectiveType = ObjectiveType.MinimizeObjective; ga.Run(); PopulationSummary ps = new PopulationSummary( ga, ga.Population ); Console.WriteLine("Best Individual:"); Console.WriteLine(ps.BestChromosome); Console.WriteLine("Finished. Hit return."); Console.ReadLine(); }
static void Main(string[] args) { GA Ga; Ga = new GA(); Ga.PopulationSize = 25; Ga.EncodingType = EncodingType.Binary; Ga.ChromosomeLength = 10; Ga.NewPopulation += new NewPopulationEventHandler( OnNewPopulation ); Ga.Objective = new ObjectiveDelegate( Objective.GetObjective ); Ga.FitnessScaling = FitnessScaling.LinearRanked; Ga.MaxGenerations = 35; Ga.RecombinationProbability = 0.6; Objective.Dump(); Ga.Run(); Console.WriteLine("Finished. Hit return."); Console.ReadLine(); }