forked from manuelperuzzi/evolutionary-cars
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Controller.cs
67 lines (59 loc) · 2.27 KB
/
Controller.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
using System;
using System.Collections.Generic;
using CarDrivers;
/// <summary>
/// The controller of the application. This class controls the flow of the computation between the evolution and evaluation phases of the genetic algorithm.
/// </summary>
public class Controller
{
private static readonly Controller _instance = new Controller();
private static readonly int max_iter = 500;
private static readonly int genotypeDimension = 67;//100;
private GeneticAlgorithm geneticAlgorithm;
private bool firstGeneration;
private Controller() { }
/// <summary>
/// Returns the only instance of this class (Sigleton pattern).
/// </summary>
/// <returns>The instance of the controller.</returns>
public static Controller Instance
{
get { return _instance; }
}
/// <summary>
/// Starts the computation, creating a random population of genotypes, and start the evaluation. <br/><br/>
/// Note, this method has to be called only once, at the start of the programme.
/// </summary>
/// <param name="populationSize">The size of the population (eg number of cars)</param>
public void Start(int populationSize)
{
this.geneticAlgorithm = new GeneticAlgorithm(genotypeDimension, populationSize);
this.geneticAlgorithm.InitializePopulation();
RaceManager.Instance.AllCarsDead += this.CarEvolution;
this.firstGeneration = true;
this.CarEvolution();
}
private void CarEvolution()
{
if (this.geneticAlgorithm.GenerationCount < max_iter)
{
if (!this.firstGeneration) // generation 1 must not be evolved!
this.geneticAlgorithm.Evolution();
else
this.firstGeneration = false;
RaceManager.Instance.SetupCars(this.CreateAgents(this.geneticAlgorithm.CurrentPopulation));
RaceManager.Instance.Restart(geneticAlgorithm.GenerationCount);
}
else
{
// end, something to do?
}
}
private IDriverAgent[] CreateAgents(List<Genotype> population)
{
IDriverAgent[] agents = new IDriverAgent[population.Count];
for (int i = 0; i < population.Count; i++)
agents[i] = new DriverAgent(population[i]);
return agents;
}
}