2
2
using System . Collections . Generic ;
3
3
using System . Collections . ObjectModel ;
4
4
using System . Linq ;
5
+ using System . Threading ;
6
+ using System . Windows . Threading ;
5
7
using MattEland . FSharpGeneticAlgorithm . Genetics ;
6
8
using MattEland . FSharpGeneticAlgorithm . Logic ;
7
9
@@ -17,14 +19,20 @@ public MainViewModel()
17
19
RandomizeCommand = new ActionCommand ( RandomizeWorlds ) ;
18
20
AdvanceCommand = new ActionCommand ( AdvanceToNextGeneration ) ;
19
21
Advance10Command = new ActionCommand ( AdvanceToNext10Generation ) ;
22
+ Advance100Command = new ActionCommand ( AdvanceToNext100Generation ) ;
20
23
21
24
RandomizeWorlds ( ) ;
22
25
RandomizeBrains ( ) ;
26
+
27
+ DispatcherTimer timer = new DispatcherTimer ( ) ;
28
+ timer . Interval = TimeSpan . FromMilliseconds ( 200 ) ;
29
+ timer . Tick += ( sender , e ) => _brain . AdvanceTimer ( ) ;
30
+ timer . Start ( ) ;
23
31
}
24
32
25
33
private void RandomizeWorlds ( )
26
34
{
27
- _worlds = WorldGeneration . makeWorlds ( _random , 10 ) ;
35
+ _worlds = WorldGeneration . makeWorlds ( _random , 1 ) ;
28
36
SimulateCurrentPopulation ( ) ;
29
37
}
30
38
@@ -64,6 +72,7 @@ public bool ShowHeatMap
64
72
public ActionCommand RandomizeCommand { get ; }
65
73
public ActionCommand AdvanceCommand { get ; }
66
74
public ActionCommand Advance10Command { get ; }
75
+ public ActionCommand Advance100Command { get ; }
67
76
68
77
public ObservableCollection < SimulationResultViewModel > Population { get ; } =
69
78
new ObservableCollection < SimulationResultViewModel > ( ) ;
@@ -94,23 +103,28 @@ private void UpdatePopulation(IEnumerable<Genes.SimulationResult> generation)
94
103
95
104
private void AdvanceToNext10Generation ( )
96
105
{
97
- var priorResults = Population . Select ( p => p . Model ) . ToArray ( ) ;
98
-
99
- var generation = Genetics . Population . mutateAndSimulateMultiple ( _random , _worlds , 10 , priorResults ) ;
106
+ AdvanceGenerations ( 10 ) ;
107
+ }
100
108
101
- UpdatePopulation ( generation ) ;
109
+ private void AdvanceToNext100Generation ( )
110
+ {
111
+ AdvanceGenerations ( 100 ) ;
102
112
}
103
113
104
- private void AdvanceToNextGeneration ( )
114
+ private void AdvanceGenerations ( int numGenerations )
105
115
{
106
116
var priorResults = Population . Select ( p => p . Model ) . ToArray ( ) ;
107
117
108
- var brains = Genetics . Population . mutateBrains ( _random , priorResults . Select ( r => r . brain ) . ToArray ( ) ) ;
109
- var generation = Genetics . Population . simulateGeneration ( _worlds , brains ) . ToList ( ) ;
118
+ var generation = Genetics . Population . mutateAndSimulateMultiple ( _random , _worlds , numGenerations , priorResults ) ;
110
119
111
120
UpdatePopulation ( generation ) ;
112
121
}
113
122
123
+ private void AdvanceToNextGeneration ( )
124
+ {
125
+ AdvanceGenerations ( 1 ) ;
126
+ }
127
+
114
128
private SimulationResultViewModel _brain ;
115
129
private World . World [ ] _worlds ;
116
130
private bool _showHeatMap ;
0 commit comments