-
Notifications
You must be signed in to change notification settings - Fork 0
/
Genotype.cs
103 lines (89 loc) · 3.37 KB
/
Genotype.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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Rosenbrock
{
class Genotype
{
public int Length {get; set;}
public EvolutionStrategy EvolutionStrategy{get; set;}
public Double FunctionValue { get; set; }
public double[] Genes { get; set; }
public Genotype(int length, EvolutionStrategy algorithm)
{
EvolutionStrategy = algorithm;
Length = length;
Genes = GenerateGenes();
}
/// <summary>
/// Funkcja generuje geny
/// </summary>
/// <returns></returns>
private double[] GenerateGenes()
{
var genesArray = new double[Length];
for(int i = 0; i < Length; i++)
{
var rnd = EvolutionStrategy.Random.NextDouble() * (100 - 0) + 0; //0,100
genesArray[i] = rnd;
}
return genesArray;
}
/// <summary>
/// Tutaj modyfikujemy geny rodzicielskich chromosomów
/// </summary>
/// <param name="parent2"></param>
/// <param name="parent2"></param>
/// <returns>2 child array</returns>
public static Genotype[] Crossover(Genotype parent1, Genotype parent2)
{
if (parent1.Length != parent2.Length) throw new Exception("Nie można krzyżować osobników z różną długością!");
var rnd = parent1.EvolutionStrategy.Random.NextDouble();//zmienna losowa z zakresu 0..1
var length = parent1.Length;
var child1 = new Genotype(length, parent1.EvolutionStrategy);
var child2 = new Genotype(length, parent1.EvolutionStrategy);
for (int i = 0; i < length; i++)
{
child1.Genes[i] = parent1.Genes[i] + rnd * (parent2.Genes[i] - parent1.Genes[i]);
child2.Genes[i] = parent2.Genes[i] + parent1.Genes[i] - child1.Genes[i];
}
return new Genotype[2] { child1, child2 };
}
/// <summary>
/// Funkcja mutuje genotyp osobnika przez dodanie do wartości genu zmiennej losowej z rozkładu normanego Gaussa.
/// </summary>
public void Mutate(float mutationRate)
{
var gaussian = new GaussianGenerator();
var normal = gaussian.NextDouble(0,1);
for(int i=0; i < Length; i++)
{
if(EvolutionStrategy.Random.NextDouble() < mutationRate)
this.Genes[i] = this.Genes[i] + normal;
}
}
/// <summary>
/// Zwraca wartości genów w postaci tablicy liczb rzeczywistych. Ogólniej mówiać, zwraca punkt funkcji f.
/// Zgodnie z ksiażką Zbigniewa Michalewicza, strona 45.
/// </summary>
/// <returns>Tablica wartości wymiarów funkcji</returns>
public double[] GetValues()
{
var genesArray = new double[Length];
for (int i = 0; i < Length; i++)
{
genesArray[i] = this.Genes[i];
}
return genesArray;
}
public Genotype Copy()
{
var copyGenotype = new Genotype(this.Length, this.EvolutionStrategy);
copyGenotype.Genes = this.Genes;
return copyGenotype;
}
}
}