-
-
Notifications
You must be signed in to change notification settings - Fork 334
/
Program.cs
109 lines (92 loc) · 3.75 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
using System;
using System.IO;
using GeneticSharp.Runner.ConsoleApp.Samples;
namespace GeneticSharp.Runner.ConsoleApp
{
public class Program
{
public static void Main(string[] args)
{
Run();
}
private static void Run()
{
Console.SetError(TextWriter.Null);
Console.Clear();
Console.ForegroundColor = ConsoleColor.DarkGreen;
Console.WriteLine("GeneticSharp - ConsoleApp");
Console.ResetColor();
Console.WriteLine("Select the sample:");
var sampleNames = TypeHelper.GetDisplayNamesByInterface<ISampleController>();
for (int i = 0; i < sampleNames.Count; i++)
{
Console.WriteLine("{0}) {1}", i + 1, sampleNames[i]);
}
int sampleNumber = 0;
string selectedSampleName = string.Empty;
try
{
sampleNumber = Convert.ToInt32(Console.ReadLine());
selectedSampleName = sampleNames[sampleNumber - 1];
}
catch (Exception)
{
Console.WriteLine("Invalid option.");
return;
}
var sampleController = TypeHelper.CreateInstanceByName<ISampleController>(selectedSampleName);
DrawSampleName(selectedSampleName);
sampleController.Initialize();
Console.WriteLine("Starting...");
var selection = sampleController.CreateSelection();
var crossover = sampleController.CreateCrossover();
var mutation = sampleController.CreateMutation();
var fitness = sampleController.CreateFitness();
var population = new Population(100, 200, sampleController.CreateChromosome());
population.GenerationStrategy = new PerformanceGenerationStrategy();
var ga = new GeneticAlgorithm(population, fitness, selection, crossover, mutation);
ga.Termination = sampleController.CreateTermination();
var terminationName = ga.Termination.GetType().Name;
ga.GenerationRan += delegate
{
DrawSampleName(selectedSampleName);
var bestChromosome = ga.Population.BestChromosome;
Console.WriteLine("Termination: {0}", terminationName);
Console.WriteLine("Generations: {0}", ga.Population.GenerationsNumber);
Console.WriteLine("Fitness: {0,10}", bestChromosome.Fitness);
Console.WriteLine("Time: {0}", ga.TimeEvolving);
Console.WriteLine("Speed (gen/sec): {0:0.0000}", ga.Population.GenerationsNumber / ga.TimeEvolving.TotalSeconds);
sampleController.Draw(bestChromosome);
};
try
{
sampleController.ConfigGA(ga);
ga.Start();
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.DarkRed;
Console.WriteLine();
Console.WriteLine("Error: {0}", ex.Message);
Console.ResetColor();
Console.ReadKey();
return;
}
Console.ForegroundColor = ConsoleColor.DarkGreen;
Console.WriteLine();
Console.WriteLine("Evolved.");
Console.ResetColor();
Console.ReadKey();
Run();
}
private static void DrawSampleName(string selectedSampleName)
{
Console.Clear();
Console.ForegroundColor = ConsoleColor.DarkGreen;
Console.WriteLine("GeneticSharp - ConsoleApp");
Console.WriteLine();
Console.WriteLine(selectedSampleName);
Console.ResetColor();
}
}
}