program runs slower and slower as time goes on

Hey guys,I'm using the hyperNEAT genetic algorithm library to evolve a neural network that approximates the xor function. I'm using codeblocks. When I click "Build and Run," the program works fine. It is very fast at the start, but it gradually slows down. The first few generations are fast but then the generations take longer and longer to complete. It comes to an eventual standstill. If I set the of the number of generations to a maximum of 100, the time the program takes to complete its process varies widely. Can anyone see why the code slows down more and more? Or point me in the right direction? This is the only code I've written for the project, all of the other files are from the hyperNEAT library posted on the hyperNEAT user's page (so I assume they aren't the problem). Also, if the population size is 100, the evaluate function only gets called 100 times per generation, even as the code is slowing down. I checked. I guess the areas I can see it going wrong is in the main for loop, or in the evaluate function. Can any one recommend a fix?

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
#include <boost/lambda/lambda.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/serialization/vector.hpp>
#include <stdlib.h>
#include <boost/random.hpp>
#include <iostream>
#include <iterator>
#include <algorithm>
#include <cmath>
#include "Substrate.h"
#include "Genome.h"
#include "NeuralNetwork.h"
#include "Random.h"
#include "Parameters.h"
#include "Population.h"
#include "Species.h"

using namespace std;
using namespace NEAT;


double evaluate(Genome& genome, Substrate& substr)
{
    //create network and variables needed for fitness testing
    NeuralNetwork net;
    genome.BuildHyperNEATPhenotype(net, substr);
    double error=0;
    std::vector<double> output_now;

    //run one trial and return the fitness
    net.Flush();

    std::vector<double> inp_set_1;
    inp_set_1.push_back(1);
    inp_set_1.push_back(0);
    inp_set_1.push_back(1);

    net.Input(inp_set_1);
    net.Activate();
    output_now=net.Output();
    error += abs(output_now[0] - 1);

    std::vector<double> inp_set_2;
    inp_set_2.push_back(0);
    inp_set_2.push_back(1);
    inp_set_2.push_back(1);

    net.Input(inp_set_2);
    net.Activate();
    output_now=net.Output();
    error += abs(output_now[0] - 1);

    std::vector<double> inp_set_3;
    inp_set_3.push_back(1);
    inp_set_3.push_back(1);
    inp_set_3.push_back(1);

    net.Input(inp_set_3);
    net.Activate();
    output_now=net.Output();
    error += abs(output_now[0] - 0);

    std::vector<double> inp_set_4;
    inp_set_4.push_back(0);
    inp_set_4.push_back(0);
    inp_set_4.push_back(1);

    net.Input(inp_set_4);
    net.Activate();
    output_now=net.Output();
    error += abs(output_now[0] - 0);

    double fitness = (pow((4 - error),2));
    return(fitness);
    //perfect fitness would be 16

}



int main()
{
    //create the network architecture
    std::vector<double> input1;
    std::vector<double> input2;
    std::vector<double> input3;
    std::vector<double> hidden1;
    std::vector<double> hidden2;
    std::vector<double> output1;

    input1.push_back(-1);
    input1.push_back(1);
    input1.push_back(0);

    input2.push_back(1);
    input2.push_back(0);
    input2.push_back(0);

    input3.push_back(0);
    input3.push_back(1);
    input3.push_back(0);

    hidden1.push_back(0.5);
    hidden1.push_back(0.5);
    hidden1.push_back(0.5);

    hidden2.push_back(-0.5);
    hidden2.push_back(1.5);
    hidden2.push_back(0.5);

    output1.push_back(0);
    output1.push_back(0);
    output1.push_back(1);

    std::vector< std::vector<double> > inputs;
    std::vector< std::vector<double> > hiddens;
    std::vector< std::vector<double> > outputs;

    inputs.push_back(input1);
    inputs.push_back(input2);
    inputs.push_back(input3);

    hiddens.push_back(hidden1);
    hiddens.push_back(hidden2);

    outputs.push_back(output1);

    Substrate substrate(inputs,hiddens,outputs);

    //configure network to disallow recurrence
    substrate.m_allow_hidden_hidden_links = false;
    substrate.m_allow_hidden_output_links = false;
    substrate.m_allow_looped_hidden_links = false;
    substrate.m_allow_looped_output_links = false;

    //set activation functions
    substrate.m_hidden_nodes_activation = NEAT::UNSIGNED_SIGMOID;
    substrate.m_output_nodes_activation = NEAT::UNSIGNED_SIGMOID;

    //set when to output a link and max weight
    substrate.m_link_threshold = 0.2;
    substrate.m_max_weight_and_bias = 5.0;

    //create parameters for evolution
    Parameters params;

    params.PopulationSize = 100;
    params.MutateRemLinkProb = 0;
    params.RecurrentProb = 0;
    params.OverallMutationRate = 0.15;
    params.MutateAddLinkProb = 0.05;
    params.MutateAddNeuronProb = 0.01;
    params.MutateWeightsProb = 0.96;
    params.MutateNeuronActivationTypeProb = 0.01;

    //probabilities for a particular activation function appearance
    params.ActivationFunction_SignedSigmoid_Prob = 0.0;
    params.ActivationFunction_UnsignedSigmoid_Prob = 0.0;
    params.ActivationFunction_Tanh_Prob = 1.0;
    params.ActivationFunction_TanhCubic_Prob = 0.0;
    params.ActivationFunction_SignedStep_Prob = 1.0;
    params.ActivationFunction_UnsignedStep_Prob = 0.0;
    params.ActivationFunction_SignedGauss_Prob = 1.0;
    params.ActivationFunction_UnsignedGauss_Prob = 0.0;
    params.ActivationFunction_Abs_Prob = 1.0;
    params.ActivationFunction_SignedSine_Prob = 1.0;
    params.ActivationFunction_UnsignedSine_Prob = 0.0;
    params.ActivationFunction_Linear_Prob = 1.0;

    //create neural network
    NeuralNetwork net;

    //create genome
    Genome g(0,
             substrate.GetMinCPPNInputs(),
             2,
             substrate.GetMinCPPNOutputs(),
             false,
             TANH,
             TANH,
             1,
             params);


     //create the population of agents
     Population pop(g, params, true, 1.0);

     //evolution, max generations 1000
     for(int iGenerations=0; iGenerations<1000; iGenerations++)
     {
         for(int j=0; j<pop.NumSpecies(); j++)
         {
             for(int k=0; k<pop.m_Species[j].m_Individuals.size(); k++)
                {
                    double   fitness=evaluate(pop.m_Species[j].m_Individuals[k],substrate);
                    pop.m_Species[j].m_Individuals[k].SetFitness(fitness);
                }
         }
         //output information
         cout << "Generation: " << pop.m_Generation << "\n";
         cout << "Best ever fitness: " << pop.GetBestFitnessEver() << "\n";
         cout << "Number of species: " << pop.NumSpecies() << "\n";
         //new generation
         pop.Epoch();
     }

     /*   //test
    using namespace boost::lambda;
    typedef std::istream_iterator<int> in;

    std::for_each(
        in(std::cin), in(), std::cout << (_1 * 3) << " " );

        */

}


I think it's your for loops in int main();

I don't know what you do exactly; but if NumSpecies() returns a higher number per generation, and m_Individuals.size() increases as well, you have an exponential increase in calculations to perform; thus slowing you down.
Kerfin: I thought that too, but the evaluate function is only called 100 times per generation, so that means the number of individuals is staying constant and not increasing.
Topic archived. No new replies allowed.