private static void SumTo50() { Console.WriteLine("Sum to 50:..."); GA ga = new GA( 20, // Number of individuals 3, // Number of genes in the genotype individual => // Fitness function Math.Abs(50 - individual.Genotype.Sum(val => val)) ); ga.RunEpoch( 500, // Number of generations to run for null, // Action to perform for each generation () => // Action to perform once fitness has improved { Console.WriteLine( "Gen {2}: Fit={1}, Genotype={0}", string.Join( " ", ga.BestIndividual.Genotype.Select(val => val.ToString("0.00"))), ga.BestIndividual.Fitness.ToString("0.00"), ga.CurrentEpochGeneration); }); Console.WriteLine("Sum to 5: done!"); Console.WriteLine(""); }
private static void Find12345() { Console.WriteLine("Find 1 2 3 4 5:..."); GA ga = new GA( 200, // Number of individuals 5, // Number of genes in the genotype individual => // Fitness function Math.Abs(individual.Genotype[0] - 1) + Math.Abs(individual.Genotype[1] - 2) + Math.Abs(individual.Genotype[2] - 3) + Math.Abs(individual.Genotype[3] - 4) + Math.Abs(individual.Genotype[4] - 5)); ga.RunEpoch(500, null, () => { Console.WriteLine( "Gen {2}: Fit={1}, Genotype={0}", string.Join( " ", ga.BestIndividual.Genotype.Select( val => val.ToString("0.00"))), ga.BestIndividual.Fitness.ToString("0.00"), ga.CurrentEpochGeneration); }); Console.WriteLine("Find 1 2 3 4 5: done!"); Console.WriteLine(""); }
public void FindProjectionMatrix() { List<WorldToScreenCase> cases = new List<WorldToScreenCase> { new WorldToScreenCase(688, 349, 0,0 ,0), // 1 new WorldToScreenCase(454, 155, 52.5,0,0), // 2 new WorldToScreenCase(353, 70, 105,0,0), // 3 new WorldToScreenCase(127, 169, 52.5, 34,0), // 4 new WorldToScreenCase(155, 411, 0, 34,0), // 5 new WorldToScreenCase(220, 364, 0, 34 - 7.32*0.5, 2.44), // 6 new WorldToScreenCase(80, 378, 0, 34+7.32*0.5,2.44), // 7 new WorldToScreenCase(92, 57, 105, 34 + 7.32*0.5, 2.44), // 8 new WorldToScreenCase(145, 56, 105, 34-7.32*0.5,2.44), // 9 }; GA ga = new GA( 5000, // Number of individuals 16, // Number of genes in the genotype individual => // Fitness function { double errorSum = 0; foreach (WorldToScreenCase test in cases) { Point3D testScreen = ProjectCase(test.World, individual.Genotype); double sqrError = (testScreen - test.Screen).LengthSquared; errorSum += sqrError; } return errorSum; }); ga.RunEpoch(100000, () => { ga.BreakEpochRun = ga.BestIndividual.Fitness <= 1.0 || stopButton.Enabled == false; Application.DoEvents(); }, () => { generationLabel.Text = string.Format( "Gen {2}: Fit={1}, Genotype={0}", string.Join( " ", ga.BestIndividual.Genotype.Select(val => val.ToString("0.00"))), ga.BestIndividual.Fitness.ToString("0.00"), ga.CurrentEpochGeneration); DrawCasesToBitmap(cases, ga.BestIndividual.Genotype); }); }
public static void FindProjectionMatrix() { List<WorldToScreenCase> cases = new List<WorldToScreenCase> { // New image /*new WorldToScreenCase(690, 350, 0, 0), new WorldToScreenCase(455, 155, 105*0.5, 0), new WorldToScreenCase(353, 70, 105, 0), new WorldToScreenCase(128, 169, 105*0.5, 68*0.5), new WorldToScreenCase(153, 412, 0, 68*0.5),*/ // Old Image new WorldToScreenCase(165,178 , 0, 0), new WorldToScreenCase(377,120 , 52.5, 0), new WorldToScreenCase(513,85 , 105, 0), new WorldToScreenCase(473,157 , 52.5, 34), new WorldToScreenCase(611,208 , 52.5, 68), new WorldToScreenCase(735,136 , 105, 68), }; GA ga = new GA( 2000, // Number of individuals 16, // Number of genes in the genotype individual => // Fitness function { double errorSum = 0; foreach (WorldToScreenCase test in cases) { Point3D testScreen = ProjectCase(test.World, individual.Genotype); double sqrError = (testScreen - test.Screen).LengthSquared; errorSum += sqrError; } return errorSum; }); ga.RunEpoch(10000, () => { ga.BreakEpochRun = ga.BestIndividual.Fitness <= 1.0 || Console.KeyAvailable; }, () => { Console.WriteLine( "Gen {2}: Fit={1}, Genotype={0}", string.Join( " ", ga.BestIndividual.Genotype.Take(5).Select( val => val.ToString("0.00"))), ga.BestIndividual.Fitness.ToString("0.00"), ga.CurrentEpochGeneration); }); if (Console.KeyAvailable) { Console.ReadKey(); } Console.WriteLine("Results for training set:"); foreach (WorldToScreenCase test in cases) { ShowTestResult(ga, test); } Console.WriteLine(""); Console.WriteLine("Additional tests:"); ShowTestResult(ga, new WorldToScreenCase(120, 73, 105, 34)); Console.WriteLine("Find ProjectionMatrix: done!"); Console.WriteLine(""); }