Neural Nets + Genetic Algorithms Help

I wrote some code for neural networks to learn the XOR function and it works fine. I am not trying to make a neural network that can classify water as a solid liquid or gas. While looping through the generations the program will crash and return 255. I have tested each function individually and everything appears to work fine. There appears to be a limit on how many times it loops and I cant pinpoint the error. I assume that there is something fundamentally wrong with my programming style so any tips will be much appreciated.
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
 #include <iostream>
#include <stdlib.h>
#include <ctime>
#include <cmath>
#define Ntrain 59
#define inputs 3
#define hidden 5
#define outputs 3
#define POP 19


using namespace std;

float randFloat(); ///declaring functions
float Sigmoid(float A);

class NeuralNet
{
public:
    NeuralNet()
    {
        ///assigning values to each weight
        for(int i = 0; i < hidden-1 ; i++)
        {
            for(int j = 0; j < inputs ;  j++)
            {
                weights1[i][j] = randFloat();
            }
        }
        for(int i = 0; i < outputs ; i++)
        {
            for(int j = 0; j < hidden ;  j++)
            {
                weights2[i][j] = randFloat();
            }
        }
    }
    float feedForward(int trainingIndex)
    {
        float hiddenNeuronSum[hidden-1]; ///sum of imputs to each node in hidden layer
        for(int i = 0 ;  i  < hidden-1 ;  i ++)///for each hidden neurone
        {
            hiddenNeuronSum[i] = 0;
            for(int j = 0; j < inputs ; j ++ ) ///for each weight
            hiddenNeuronSum[i] = hiddenNeuronSum[i] + (weights1[i][j])*(training[trainingIndex][j]);
        }

        for(int i = 0 ;  i < hidden-1 ; i++) ///sigmoid it
        {
            hiddenNeuronSum[i] = Sigmoid(hiddenNeuronSum[i]);
        }
        ///pass onto next layer
        float outputNeuronSum[outputs];
        for(int i = 0; i < outputs ;  i ++)///for each output
        {
            outputNeuronSum[i] = 0;
            for(int j  = 0;  j  < hidden-1 ; j ++)///for each neurone in hidden layer
            {
                outputNeuronSum[i] = outputNeuronSum[i] + (weights2[i][j])*(hiddenNeuronSum[j]);
            }
            outputNeuronSum[i] = outputNeuronSum[i] + weights2[i][hidden-1];
        }


        for(int i = 0;  i < outputs ; i ++)
        {
            outputNeuronSum[i] = Sigmoid(outputNeuronSum[i]);
        }///sigmoid it again

        float error =  0;
        for(int i = 0;  i < outputs ;  i ++)
        {
            error = error + abs(training[trainingIndex][i+3] - outputNeuronSum[i]); ///calculate the error from each output node
        }
        return error;
    }
    float getError() ///returns error
    {
        return totalerror;
    }
    void calculateError() ///calculates each error from each output using training set
    {
        totalerror = 0;
        for(int i = 0  ;  i < Ntrain ;  i ++)
        {
            totalerror = totalerror + feedForward(i);
        }
    }
    void setScore(int score) ///sets fitness score
    {
        fitnessScore = score;
    }
    
    friend void crossOver(NeuralNet a, NeuralNet b, NeuralNet &c);
    friend void transferGenes(NeuralNet a, NeuralNet &b);
    int getScore()
    {
        return fitnessScore;
    }
private:
    float weights1[hidden-1][inputs]; ///weights in layer one
    float weights2[outputs][hidden]; ///weights in layers two
    float totalerror; 
    int fitnessScore; 
    float training[Ntrain][6] = {
    ///{Bias, log(Pa), Kelvin *10^-2, S, L ,G} the error is calculated using the last 3 values
    {1, 5.0, 2.5, 1.0, 0, 0} ,
    {1, 7.0, 3.15, 0, 1.0, 0} ,
    {1, 3.0, 4.76, 0, 0, 1.0},
    //3

    ///solids
    {1, 7.0, 2.0, 1.0, 0, 0},
    {1, 7.0, 2.5, 1.0, 0, 0},
    {1, 6.0, 1.5, 1.0, 0, 0},
    {1, 6.0, 2.5, 1.0, 0, 0},
    {1, 5.0, 1.5, 1.0, 0, 0},
    {1, 5.0, 2.0, 1.0, 0, 0},
    {1, 4.0, 2.0, 1.0, 0, 0},
    {1, 4.0, 2.5, 1.0, 0, 0},
    {1, 3.0, 1.5, 1.0, 0, 0},
    {1, 3.0, 2.5, 1.0, 0, 0},
    {1, 2.0, 2.0, 1.0, 0, 0},
    {1, 2.0, 2.5, 1.0, 0, 0},
    {1, 1.0, 1.5, 1.0, 0, 0},
    {1, 1.0, 2.0, 1.0, 0, 0},
    {1, 1.0, 2.5, 1.0, 0, 0},

//15
    ///liquids

    {1, 1.0, 2.5, 0, 1.0, 0},
    {1, 1.0, 3.0, 0, 1.0, 0},
    {1, 1.0, 3.5, 0, 1.0, 0},
    {1, 1.0, 4.5, 0, 1.0, 0},
    {1, 1.0, 5.0, 0, 1.0, 0},
    {1, 2.0, 3.0, 0, 1.0, 0},
    {1, 2.0, 3.5, 0, 1.0, 0},
    {1, 2.0, 4.0, 0, 1.0, 0},
    {1, 2.0, 4.5, 0, 1.0, 0},
    {1, 2.0, 2.8, 0, 1.0, 0},
    {1, 3.0, 3.0, 0, 1.0, 0},
    {1, 3.0, 3.5, 0, 1.0, 0},
    {1, 3.0, 4.0, 0, 1.0, 0},
    {1, 3.0, 4.5, 0, 1.0, 0},
    {1, 3.0, 5.0, 0, 1.0, 0},
    {1, 4.0, 3.0, 0, 1.0, 0},
    {1, 4.0, 3.3, 0, 1.0, 0},
    {1, 4.0, 4.0, 0, 1.0, 0},
    {1, 4.0, 4.5, 0, 1.0, 0},
    {1, 4.0, 5.0, 0, 1.0, 0},
    {1, 5.0, 4.0, 0, 1.0, 0},
    {1, 5.0, 4.5, 0, 1.0, 0},
    {1, 5.0, 3.8, 0, 1.0, 0},
    {1, 5.0, 5.0, 0, 1.0, 0},
    {1, 6.0, 4.7, 0, 1.0, 0},
    {1, 6.0, 5.0, 0, 1.0, 0},
    {1, 7.0, 6.0, 0, 1.0, 0},
    //27
    ///gas
    {1, 3.0, 2.8, 0, 0, 1.0},
    {1, 4.0, 3.0, 0, 0, 1.0},
    {1, 5.0, 2.8, 0, 0, 1.0},
    {1, 5.0, 3.5, 0, 0, 1.0},
    {1, 6.0, 3.0, 0, 0, 1.0},
    {1, 6.0, 3.5, 0, 0, 1.0},
    {1, 6.0, 4.0, 0, 0, 1.0},
    {1, 6.0, 4.5, 0, 0, 1.0},
    {1, 7.0, 2.8, 0, 0, 1.0},
    {1, 7.0, 3.5, 0, 0, 1.0},
    {1, 7.0, 4.0, 0, 0, 1.0},
    {1, 7.0, 4.5, 0, 0, 1.0},
    {1, 7.0, 5.0, 0, 0, 1.0},
    {1, 7.0, 5.8, 0, 0, 1.0},
    //14
    };
};
int chooseParent(NeuralNet Population[POP]);

int main()
{
    srand(time(0));

    NeuralNet Population[POP];
    NeuralNet Children[POP];
    int nextgen =1; ///using nextgen to test if the loop is working
    while(nextgen == 1){
    for(int i =  0 ; i < POP ; i ++)
    {
        Population[i].calculateError();
    }
    for(int i = 0 ; i < POP ;  i++)
    {
        int A = chooseParent(Population);
        int B = chooseParent(Population);
        crossOver(Population[A], Population[B], Children[i]);
    }
    for(int i =0 ; i < POP ; i ++)
    {
        transferGenes(Children[i], Population[i]);
    }
    nextgen = 0;
    cin >> nextgen;
    cin.ignore();
    }



    return 0;
}

void transferGenes(NeuralNet a, NeuralNet &b) //transfer weights from a to b
{
    for(int i = 0; i < hidden-1 ; i++)
        {
            for(int j = 0; j < inputs ;  j++)
            {
            b.weights1[i][j] = a.weights1[i][j];
            }
        }


    for(int i = 0; i < outputs ; i++)
        {
            for(int j = 0; j < hidden ;  j++)
            {
                b.weights2[i][j] = a.weights2[i][j];
                }
            }
}


void crossOver(NeuralNet a, NeuralNet b, NeuralNet &c) ///crossover
{
    int random;
    for(int i = 0; i < hidden-1 ; i++)
        {
            for(int j = 0; j < inputs ;  j++)
            {
                random = rand()%2;
                if(random ==  1)
                {
                    c.weights1[i][j] = a.weights1[i][j];
                }else{
                c.weights1[i][j] = b.weights1[i][j];
                }
            }
        }

    for(int i = 0; i < outputs ; i++)
        {
            for(int j = 0; j < hidden ;  j++)
            {
                random = rand()%2;
                if(random ==  1)
                {
                    c.weights2[i][j] = a.weights2[i][j];
                }else{
                c.weights2[i][j] = b.weights2[i][j];
                }
            }
        }
}

int chooseParent(NeuralNet Population[POP]) ///using probability to select parents
{
    int greatestError = Population[0].getError();
    for(int i =1 ; i < POP ;  i++)
    {
        if(Population[i].getError() > greatestError)
            greatestError = Population[i].getError();
    }
cout << greatestError << endl;
    int totalFitness = 0;

    for(int i =0 ; i < POP; i++)
    {
      Population[i].setScore(greatestError +1 - Population[i].getError());
      totalFitness = totalFitness + Population[i].getScore();
    }

    int random = rand()%totalFitness +1;
    int parentSelector = 0;
    int parentIndex;
    for(int i=0; parentSelector<random;i++)
    {
        parentSelector = parentSelector + Population[i].getScore();
        parentIndex = i;
    }


    return parentIndex;
}

float randFloat()
{
    float f = static_cast <float> (rand()) / (static_cast <float> (RAND_MAX));
    int Pos_or_Neg = rand()%2;
    if(Pos_or_Neg)
    {
        return f;
    }else{
    return -f;
    }
}

float Sigmoid(float A)
{
    return 1/(1+exp(-A));
}
Topic archived. No new replies allowed.