-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
151 lines (131 loc) · 4.44 KB
/
main.c
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
// gcc main.c cgp.c cgp.h -lm -fopenmp -o out && time ./out && dot -Tpdf chromo.dot -o chromo.pdf
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "lib/cgp.h"
#define PREPARE_DATA
#define RUN_CGP
#define NUM_NODES 50
#define MUTATION_RATE 0.025
#define MAX_GENERATIONS 1000000
#define ADDER_SIZE 3
#define NUM_SAMPLES 8 * 8
#define NUM_INPUTS ADDER_SIZE * 2 // 8
#define NUM_OUTPUTS ADDER_SIZE + 1 // 5
#define MAX_INPUTS_PER_GATE 2
double inputs[NUM_SAMPLES][NUM_INPUTS];
double outputs[NUM_SAMPLES][NUM_OUTPUTS];
void generateAdder()
{
int linha = 0;
int maxN = pow(2, ADDER_SIZE);
for (int nA = 0; nA < maxN; nA++)
{
for (int nB = 0; nB < maxN; nB++)
{
printf("%d >> %d (", linha, nA);
// Entrada A
for (int i = 0; i < NUM_INPUTS / 2; i++)
{
int bitInA = (nA >> ((NUM_INPUTS / 2) - i - 1)) & 1;
inputs[linha][i] = (double)bitInA;
printf("%d", (int)inputs[linha][i]);
}
printf(") + %d (", nB);
// Entrada B
for (int i = 0; i < NUM_INPUTS / 2; i++)
{
int bitInB = (nB >> ((NUM_INPUTS / 2) - i - 1)) & 1;
inputs[linha][i + NUM_INPUTS / 2] = (double)bitInB;
printf("%d", (int)inputs[linha][i + NUM_INPUTS / 2]);
}
int calcOut = nA + nB;
printf(") = %d (", calcOut);
for (int i = 0; i < NUM_OUTPUTS; i++)
{
int bitInOut = (calcOut >> NUM_OUTPUTS - i - 1) & 1;
outputs[linha][i] = (double)bitInOut;
printf("%d", (int)outputs[linha][i]);
}
printf(")\n");
linha++;
}
}
}
double majority(const int numInputs, const double *inputs, const double *connectionWeights)
{
int nHighs = 0;
for (int i = 0; i < numInputs; i++)
{
if (inputs[i] > 0)
nHighs++;
if (nHighs > (numInputs / 2))
return 1.0;
}
return 0.0;
}
double checkTruthTable(struct parameters *params, struct chromosome *chromo, struct dataSet *data)
{
double linesErrors = 0;
for (int i = 0; i < getNumDataSetSamples(data); i++)
{
int errors = 0;
executeChromosome(chromo, getDataSetSampleInputs(data, i));
for (int j = 0; j < getNumChromosomeOutputs(chromo); j++)
{
if (getDataSetSampleOutput(data, i, j) != getChromosomeOutput(chromo, j))
errors += 1;
}
if (errors > 0)
linesErrors += 1;
}
return linesErrors;
}
int main(void)
{
/////////////////////
// PREPARA O DATASET
/////////////////////
#ifdef PREPARE_DATA
int i;
struct dataSet *data = NULL;
generateAdder();
data = initialiseDataSetFromArrays(NUM_INPUTS, NUM_OUTPUTS, NUM_SAMPLES, inputs[0], outputs[0]);
saveDataSet(data, "TruthTable.data");
freeDataSet(data);
#endif
/////////////////////
// RODAR O CGP
/////////////////////
#ifdef RUN_CGP
struct parameters *params = NULL;
struct dataSet *trainingData = NULL;
struct chromosome *chromo = NULL;
params = initialiseParameters(NUM_INPUTS, NUM_NODES, NUM_OUTPUTS, MAX_INPUTS_PER_GATE);
addNodeFunction(params, "and,nand,or,nor,not,xor");
// addCustomNodeFunction(params, majority, "maj", -1);
setTargetFitness(params, 0);
setCustomFitnessFunction(params, checkTruthTable, "CTT");
setMu(params, 1); // The number of parents selected each iteration.
setLambda(params, 4); // Size of the population.
// lambda / mu: Number of children generated from each selected parent.
setEvolutionaryStrategy(params, '+');
// (mu, lambda)-ES: A version of evolution strategies where children replace parents.
// (mu + lambda)-ES: A version of evolution strategies where children and parents are added to the population.
// setMutationRate(params, MUTATION_RATE);
setMutationType(params, "single");
setUpdateFrequency(params, 500);
setNumThreads(params, 12);
printParameters(params);
trainingData = initialiseDataSetFromFile("TruthTable.data");
chromo = runCGP(params, trainingData, MAX_GENERATIONS);
removeInactiveNodes(chromo);
printChromosome(chromo, 0);
saveChromosomeDot(chromo, 0, "chromo.dot");
saveChromosomeLatex(chromo, 0, "chromo.tex");
freeDataSet(trainingData);
freeChromosome(chromo);
freeParameters(params);
#endif
return 0;
}