-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
326 lines (284 loc) · 11.8 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using NCalc;
namespace GeneticProgrammingOptimizer
{
public static class RandomExtensions
{
public static double NextDouble(this Random RandGenerator, double MinValue, double MaxValue)
{
return RandGenerator.NextDouble() * (MaxValue - MinValue) + MinValue;
}
}
public class Population
{
public int PopulationNumber { get; set; } = 0;
public List<Individual> Individuals = new List<Individual>();
public static Population Create(int size, Func<Individual> individualProvider)
{
var individuals = new List<Individual>();
for (int i = 0; i < size; i++)
{
individuals.Add(individualProvider());
}
return new Population { Individuals = individuals };
}
}
public class TerminationCriteria
{
public bool IsSatisfied(Population pop)
{
return false;
}
}
public static class Utils
{
public static int PopulationSize = 60;
public static double MutationProbability = 0.05;
public static double CrossoverProbability = 1;
public static int TournamentSize = 10;
public static bool Elitism = true;
public static int MaxDepth = 15;
public static int InitialDepth = 3;
public static int DatasetSize = 50;
}
public class Program
{
public static Random rnd = new Random();
static void Test(string[] args)
{
var ind1 = new Individual();
var ind2 = new Individual();
var index = BinaryTree<string>.ReindexTree(ref ind1.Genes);
var index2 = BinaryTree<string>.ReindexTree(ref ind2.Genes);
Console.WriteLine("individual 1");
ind1.Genes.Print();
Console.WriteLine("Individual 2");
ind2.Genes.Print();
BinaryTree<string>.SwapSubtrees(1, 1, ref ind1.Genes, ref ind2.Genes);
Console.WriteLine("After swappin");
ind1.Genes.Print();
ind2.Genes.Print();
Console.WriteLine("Mutation:");
new Mutation().Perform(ref ind1);
ind1.Genes.Print();
}
static void Main(string[] args)
{
Console.WriteLine("Depth >");
Utils.MaxDepth = int.Parse(Console.ReadLine());
Console.WriteLine("DataseSize >");
Utils.DatasetSize = int.Parse(Console.ReadLine());
Console.WriteLine("Initial depth (will be +3) >");
Utils.InitialDepth = int.Parse(Console.ReadLine());
var resultsFile = DateTime.Now.Ticks + ".csv";
// Test(args);
// return;
// inicjalizacja
var pop = new Population();
var terminationCriteria = new TerminationCriteria();
do
{
pop.Individuals.Add(new Individual(Utils.InitialDepth));
} while (Utils.PopulationSize > pop.Individuals.Count);
var crossover = new Crossover();
var selection = new TournamentSelection();
var mutation = new Mutation();
var data = new List<DataRow>();
using (FileStream fs = new FileStream(@"INPUT_DATA_HERE.csv", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
using (StreamReader stream = new StreamReader(fs))
{
stream.ReadLine();
while (stream.EndOfStream == false)
{
var row = stream.ReadLine().Split(';').Select(x => double.Parse(x.Replace('.', ','))).ToList();
var parametersAmount = 5;
data.Add(new DataRow()
{
Parameters = row.Take(parametersAmount).ToArray(),
SatisfactionValue = (int)row[13],
failedTimes = (int)row[parametersAmount + 1],
threadsMin = (int)row[parametersAmount + 2],
threadsMax = (int)row[parametersAmount + 3],
threadsAvg = (int)row[parametersAmount + 4],
ramMax = (int)row[parametersAmount + 5],
processorAvg = (int)row[parametersAmount + 6],
processorMax = (int)row[parametersAmount + 7],
});
}
}
data = data.Take(Utils.DatasetSize).ToList();
File.AppendAllText(resultsFile,
$"DatasetSize: {data.Count()}; PopSize: {Utils.PopulationSize}; Mutation: {Utils.MutationProbability}; Crossover: {Utils.CrossoverProbability}, TournamentSize: {Utils.TournamentSize}, Elitism: {Utils.Elitism.ToString()}, MaxDepth: {Utils.MaxDepth} \n"
);
File.AppendAllText(resultsFile, "Epoch;BestFitness;Time;TreeDepth;Function \n");
var fitness = new Fitness(data);
Stopwatch sw = new Stopwatch();
while (terminationCriteria.IsSatisfied(pop) == false)
{
sw.Start();
pop.Individuals.RemoveAll(x => BinaryTree<string>.MaxDepth(x.Genes) > Utils.MaxDepth);
//fitness measure
fitness.Evaluate(pop.Individuals);
var freshPopulation = new List<Individual>();
if (Utils.Elitism == true)
{
freshPopulation.Add(pop.Individuals.OrderByDescending(f => f.Fitness).First());
}
while (freshPopulation.Count < Utils.PopulationSize)
{
var parent1 = selection.Select(pop.Individuals);
var parent2 = selection.Select(pop.Individuals.Where(i => BinaryTree<string>.ToExpression(i.Genes) != BinaryTree<string>.ToExpression(parent1.Genes)).ToList());
var children = crossover.Perform(parent1, parent2);
mutation.Perform(ref children[0]);
mutation.Perform(ref children[1]);
freshPopulation.Add(children[0]);
freshPopulation.Add(children[1]);
}
fitness.Evaluate(freshPopulation);
pop.PopulationNumber++;
pop.Individuals = freshPopulation;
var best = pop.Individuals.OrderByDescending(f => f.Fitness).ToList();
sw.Stop();
File.AppendAllText(resultsFile, string.Format("{1};{0};{2};{3};{4}\n", best.First().Fitness, pop.PopulationNumber, sw.ElapsedMilliseconds,BinaryTree<string>.MaxDepth(best.First().Genes) ,best.First().ToString()));
Console.WriteLine("Epoka: {1}, Trafnosc: {0}, Czas: {2} ms", best.First().Fitness, pop.PopulationNumber, sw.ElapsedMilliseconds);
}
}
}
public class DataRow
{
public int ramMax { get; set; }
public double[] Parameters { get; set; }
public int SatisfactionValue { get; set; }
public int failedTimes { get; set; }
public int threadsMin { get; set; }
public int threadsMax { get; set; }
public int threadsAvg { get; set; }
public int processorAvg { get; set; }
public int processorMax { get; set; }
public DataRow()
{
}
}
public class Fitness
{
private readonly List<DataRow> _dataset;
public Fitness(List<DataRow> dataset)
{
_dataset = dataset;
}
public double Function(Individual ind, DataRow dr)
{
var expression = BinaryTree<string>.ToExpression(ind.Genes);
var ncalcExpr = new Expression(expression);
var variables = "ABCDE";
for(int i = 0; i < dr.Parameters.Length; i++)
{
ncalcExpr.Parameters[variables[i].ToString()] = dr.Parameters[i];
}
var result = ncalcExpr.Evaluate() as double?;
if (result.HasValue)
{
return result.Value;
}
return Double.MinValue;
}
public void Evaluate(IEnumerable<Individual> pop)
{
var individuals = pop.ToList();
foreach (Individual individual in individuals)
{
double fitness = 0;
foreach (DataRow data in _dataset)
{
var expectedResult = data.SatisfactionValue;
var result = Function(individual, data);
if (result <= double.MinValue)
{
fitness = double.MinValue;
}
else
{
fitness -= Math.Abs(result - expectedResult); // odejmujemy od fitness pomyłkę.
}
}
individual.Fitness = fitness; // fitness to suma pomylek. Jak 0 to jest najlepiej.
}
}
}
public class Mutation
{
public void Perform(ref Individual ind)
{
if (Program.rnd.NextDouble() > Utils.MutationProbability)
return;
var maxIndex = BinaryTree<string>.ReindexTree(ref ind.Genes);
BinaryTree<string>.SwapTreeWithTarget(ref ind.Genes, Individual.GenerateSimpleTree(), Program.rnd.Next(0, maxIndex));
}
}
public class Crossover
{
public Individual[] Perform(Individual p1, Individual p2)
{
if (Program.rnd.NextDouble() > Utils.CrossoverProbability)
return new Individual[] { };
var maxIndex1 = BinaryTree<string>.ReindexTree(ref p1.Genes);
var maxIndex2 = BinaryTree<string>.ReindexTree(ref p2.Genes);
var c1 = p1.Clone();
var c2 = p2.Clone();
BinaryTree<string>.SwapSubtrees(Program.rnd.Next(0, maxIndex1), Program.rnd.Next(0, maxIndex2), ref c1.Genes, ref c2.Genes);
return new[] { c1, c2 };
}
}
public class TournamentSelection
{
public Individual Select(IList<Individual> individuals)
{
Individual best = null;
for (int i = 0; i < Utils.TournamentSize; i++)
{
var randomIndividual = individuals[Program.rnd.Next(0, individuals.Count - 1)];
if (best == null || randomIndividual.Fitness > best.Fitness)
{
best = randomIndividual;
}
}
return best;
}
}
public class RouletteWheelSelection
{
public int PopulationMinSize { get; set; }
private static Random rnd = new Random();
public void Perform(Population population)
{
var newIndividuals = new List<Individual>();
var sum = population.Individuals.Sum(x => x.Fitness);
double rand;
do
{
rand = rnd.NextDouble(0, sum);
double tempSum = 0;
foreach (var individual in population.Individuals)
{
tempSum += individual.Fitness;
if (tempSum > rand)
{
newIndividuals.Add(individual);
population.Individuals.Remove(individual);
break;
}
}
} while (population.Individuals.Count < PopulationMinSize);
}
}
}