+/GENN

genetically evolving neural networks

git clone https://git.julia.blue/+/GENN

Joseph RyanStill brokeneb6e103

v0.2
4.2 KiB151 linesraw
1#include "Genome.h"
2
3
4Genome::Genome(std::string g) {
5    decode(g);
6//    structure = g.structure;
7//    nodes = g.nodes;
8//    totalNodes = g.totalNodes;
9//    totalConnections = g.totalConnections;
10//    inputs = g.inputs;
11//    outputs = g.outputs;
12//    std::copy(std::begin(g.mutationRates), std::end(g.mutationRates), mutationRates);
13}
14
15Genome::Genome(int a, int b, double m[]) {
16    totalNodes = a + b;
17    inputs = a;
18    outputs = b;
19    totalConnections = a * b;
20    for (int i = 0; i < a; i++) {
21        nodes.push_back(new Neuron(i, 0));
22    }
23    for (int i = 0; i < b; i++) {
24        nodes.push_back(new Neuron(i + a, 1));
25    }
26    for (int i = 0; i < inputs; i++) {
27        for (int j = 0; j < outputs; j++) {
28            //Initializes all connections with random weight 0-2
29            structure.push_back(new Connection(nodes[i], nodes[inputs+j], double(rand()) * 4. / RAND_MAX - 2));
30        }
31    }
32    for (int i = 0; i < 8; i++) {
33        mutationRates[i] = m[i];
34    }
35    recomputeInputs();
36}
37
38void Genome::propagate(double a[], double b[]) {
39    for (int i = 0; i < inputs; i++) {
40        nodes[i]->value = a[i];
41    }
42    for (int i = inputs - 1; i < totalNodes; i++) {
43        nodes[i]->sum = 0;
44        for (int j = 0; j < nodes[i]->inputs.size(); j++) {
45            nodes[i]->sum +=
46                    nodes[i]->inputs[j]->input->value * nodes[i]->inputs[j]->weight * int(nodes[i]->inputs[j]->enabled);
47        }
48        nodes[i]->value = sigmoid(nodes[i]->sum);
49    }
50    for (int i = 0; i < outputs; i++) {
51        b[i] = nodes[inputs + i]->value;
52    }
53}
54
55double Genome::sigmoid(double a) {
56    return 2 / (1 + exp(-4.9 * a)) - 1;
57}
58
59std::string Genome::encode() {
60    //writes to a space delimited string with the format:
61    //totalNodes inputs outputs nodeN.id nodeN.type totalConnections conN.weight conN.enabled conN.input conN.output mutationN
62    std::ostringstream ss;
63    ss << totalNodes << ' ' << inputs << ' ' << outputs << ' ';
64    for (int i = 0; i < totalNodes; i++) {
65        ss << nodes[i]->id << ' ' << nodes[i]->type << ' ';
66    }
67    ss << totalConnections << ' ';
68    for (int i = 0; i < totalConnections; i++) {
69        ss << structure[i]->weight << ' ' << structure[i]->enabled << ' '
70           << structure[i]->input->id << ' ' << structure[i]->output->id << ' ';
71    }
72    for (int i = 0; i < 7; i++) {
73        ss << mutationRates[i] << ' ';
74    }
75    return ss.str();
76}
77
78void Genome::decode(std::string s) {
79    std::stringstream ss(s);
80    ss >> totalNodes;
81    ss >> inputs;
82    ss >> outputs;
83    nodes.resize(totalNodes);
84    for (int i = 0; i < totalNodes; i++) {
85        int tempid, temptype;
86        ss >> tempid;
87        ss >> temptype;
88        nodes[i] = new Neuron(tempid, temptype);
89    }
90    ss >> totalConnections;
91    structure.resize(totalConnections);
92    int temp;
93    for (int i = 0; i < totalConnections; i++) {
94        structure[i] = new Connection();
95        ss >> structure[i]->weight;
96        ss >> structure[i]->enabled;
97        ss >> temp;
98        for (int j = 0; j < totalNodes; j++) {
99            if (nodes[j]->id == temp) {
100                structure[i]->input = nodes[j];
101                break;
102            }
103        }
104        ss >> temp;
105        for (int j = 0; j < totalNodes; j++) {
106            if (nodes[j]->id == temp) {
107                structure[i]->output = nodes[j];
108                break;
109            }
110        }
111    }
112    for (int i = 0; i < 7; i++) {
113        ss >> mutationRates[i];
114    }
115    recomputeInputs();
116}
117
118void Genome::recomputeInputs() {
119    for (int i = 0; i < totalNodes; i++) {
120        for (int j = 0; j < totalConnections; j++) {
121            if (nodes[i] == structure[j]->output) {
122                nodes[i]->inputs.push_back(structure[j]);
123            }
124        }
125    }
126}
127
128bool Genome::hasConnection(int a, int b) {
129    for (int i = 0; i < structure.size(); i++) {
130        if (structure[i]->input->id == structure[i]->output->id) {
131            return true;
132        }
133    }
134    return false;
135}
136
137Genome::~Genome() {
138    for (int i = 0; i < structure.size(); i++) {
139        delete structure[i];
140    }
141    for (int i = 0; i < nodes.size(); i++) {
142        delete nodes[i];
143    }
144}
145
146void Genome::reset() {
147    for (int i = 0; i < nodes.size(); i++) {
148        nodes[i]->value = 0;
149    }
150}
151