4
4
using System . Linq ;
5
5
using System . Text ;
6
6
using System . Threading . Tasks ;
7
- namespace GeNeural {
7
+ namespace GeNeural . Genetics {
8
8
public class GeneticNeuralNetworkFacilitator : IMutatable , IDeepCloneable < GeneticNeuralNetworkFacilitator > {
9
9
private const double VARIANCE_FACTOR = 0.01 ;
10
10
@@ -17,8 +17,10 @@ public class GeneticNeuralNetworkFacilitator : IMutatable, IDeepCloneable<Geneti
17
17
private double neuronMutationFactorVarianceFactor = 0.01 ;
18
18
private double neuronMutationFactor = 0.50 ; // Adds round(-x to x) neurons per layer
19
19
private NeuralNetwork network ;
20
- public GeneticNeuralNetworkFacilitator ( NeuralNetwork network ) {
20
+ private Random rnd ;
21
+ public GeneticNeuralNetworkFacilitator ( NeuralNetwork network , Random random ) {
21
22
this . network = network ;
23
+ this . rnd = random ;
22
24
}
23
25
protected GeneticNeuralNetworkFacilitator ( GeneticNeuralNetworkFacilitator parent ) {
24
26
network = parent . network . DeepClone ( ) ;
@@ -76,38 +78,38 @@ public void Mutate(double mutationFactor = 1) {
76
78
public void MutateHiddenLayerCount ( ) {
77
79
int numberOfLayersToClone = GetRandomCount ( layerMutationFactor ) ;
78
80
//Debug.WriteLine("Creating {0} more layers.", numberOfLayersToClone);
79
- if ( RandomHelper . rnd . Next ( 0 , 2 ) == 1 ) {
81
+ if ( this . rnd . Next ( 0 , 2 ) == 1 ) {
80
82
for ( int _ = 0 ; _ < numberOfLayersToClone ; _ ++ ) {
81
- int layerIndex = RandomHelper . rnd . Next ( 0 , network . LayerCount - 1 ) ;
83
+ int layerIndex = this . rnd . Next ( 0 , network . LayerCount - 1 ) ;
82
84
network . InsertAfterLayer ( layerIndex ) ;
83
85
}
84
86
} else {
85
87
for ( int _ = 0 ; _ < numberOfLayersToClone ; _ ++ ) {
86
88
if ( network . LayerCount <= 1 ) { break ; }
87
- int layerIndex = RandomHelper . rnd . Next ( 0 , network . LayerCount - 1 ) ;
89
+ int layerIndex = this . rnd . Next ( 0 , network . LayerCount - 1 ) ;
88
90
network . RemoveLayer ( layerIndex ) ;
89
91
}
90
92
}
91
93
}
92
94
public void MutateHiddenNeuronCount ( ) {
93
95
int numberOfNeuronsToClone = GetRandomCount ( neuronMutationFactor ) ;
94
96
//Debug.WriteLine("Creating {0} more neurons", numberOfNeuronsToClone);
95
- if ( RandomHelper . rnd . Next ( 0 , 2 ) == 1 ) {
97
+ if ( this . rnd . Next ( 0 , 2 ) == 1 ) {
96
98
for ( int _ = 0 ; _ < numberOfNeuronsToClone ; _ ++ ) {
97
99
if ( network . LayerCount <= 1 ) {
98
100
break ;
99
101
}
100
- int layerIndex = RandomHelper . rnd . Next ( 0 , network . LayerCount - 1 ) ;
102
+ int layerIndex = this . rnd . Next ( 0 , network . LayerCount - 1 ) ;
101
103
//Debug.WriteLine("New neuron at layer: {0}", layerIndex);
102
- int neuronIndex = RandomHelper . rnd . Next ( 0 , network . GetLayer ( layerIndex ) . Length ) ;
104
+ int neuronIndex = this . rnd . Next ( 0 , network . GetLayer ( layerIndex ) . Length ) ;
103
105
network . SplitNeuronNonDestructive ( layerIndex , neuronIndex ) ;
104
106
}
105
107
} else {
106
108
for ( int _ = 0 ; _ < numberOfNeuronsToClone ; _ ++ ) {
107
109
if ( network . LayerCount <= 1 ) { break ; }
108
- int layerIndex = RandomHelper . rnd . Next ( 0 , network . LayerCount - 1 ) ;
110
+ int layerIndex = this . rnd . Next ( 0 , network . LayerCount - 1 ) ;
109
111
//Debug.WriteLine("New neuron at layer: {0}", layerIndex);
110
- int neuronIndex = RandomHelper . rnd . Next ( 0 , network . GetLayer ( layerIndex ) . Length ) ;
112
+ int neuronIndex = this . rnd . Next ( 0 , network . GetLayer ( layerIndex ) . Length ) ;
111
113
network . RemoveNeuron ( layerIndex , neuronIndex ) ;
112
114
}
113
115
}
@@ -122,22 +124,20 @@ public void MutateWeights() {
122
124
double delta = GetDeltaMutatableValue ( weightMutationFactor ) ;
123
125
weight += delta ;
124
126
//Debug.WriteLine("Changing weight by: {0}", delta);
125
- //weight += 0.05 + 0.1 * RandomHelper.rnd.NextDouble();//delta;
126
- //weight += RandomHelper.rnd.NextDouble();
127
127
neuron . SetWeight ( w , weight ) ;
128
128
}
129
129
}
130
130
}
131
131
}
132
132
public double GetMultiplicativeMutableFactor ( double mutableFactor ) {
133
- return 1 + ( mutableFactor - RandomHelper . rnd . NextDouble ( ) * mutableFactor * 2.0 ) ;
133
+ return 1 + ( mutableFactor - this . rnd . NextDouble ( ) * mutableFactor * 2.0 ) ;
134
134
}
135
135
public int GetRandomCount ( double mutationFactor ) {
136
- return ( int ) Math . Round ( RandomHelper . rnd . NextDouble ( ) * mutationFactor ) ;
136
+ return ( int ) Math . Round ( this . rnd . NextDouble ( ) * mutationFactor ) ;
137
137
}
138
138
public double GetDeltaMutatableValue ( double mutationFactor ) {
139
- double delta = RandomHelper . rnd . NextDouble ( ) * mutationFactor ;
140
- if ( RandomHelper . rnd . Next ( 0 , 2 ) == 1 )
139
+ double delta = this . rnd . NextDouble ( ) * mutationFactor ;
140
+ if ( this . rnd . Next ( 0 , 2 ) == 1 )
141
141
return delta ;
142
142
else
143
143
return - delta ;
0 commit comments