-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
executable file
·80 lines (61 loc) · 1.98 KB
/
main.cpp
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
#include <iostream>
#include "populacao.cpp"
#include "cromossomo.cpp"
#include "avaliacao.cpp"
#include "Rand.cpp"
#include "gene.cpp"
#include "selecao.h"
#include "crossover.h"
#include <vector>
#include <fstream>
using namespace std;
extern Rand ra;
int main(){
Populacao populacao(256,10);
Avaliacao fitness;
int i, j, tempo = 0, tam = populacao.getTamPopulacao();
//CRIAR ARQUIVO
ofstream out; // out é uma variavel.
out.open("grafico.txt"); // o arquivo que será criado;
while(tempo < 1024){
//VETOR DE CROMOSSOMO TEMPORARIO
vector<Cromossomo*> tmpC = populacao.getCromossomos();
//CALCULO DO FITNESS DA GERAÇÃO PARA CADA CROMOSSOMO
for(i = 0; i < tam; i++){
double f = fitness.calcularFitness(tmpC[i]);
tmpC[i]->setFitness(f);
}
//VARIÁVEL COM O INDICE DO CROMOSSOMO DE MELHOR FITNESS
int selF = 0;
//CROMOSSOMO COM O MELHOR FITNESS
Cromossomo* selCromossomo = tmpC[0];
//PROCURAR O MELHOR CROMOSSOMO
for(int i = 1; i < tam; i++){
if(tmpC[i]->getFitness() < tmpC[selF]->getFitness()){
selF = i;
selCromossomo = tmpC[selF];
}
}
cout << tmpC[selF]->getFitness() << endl;
//IMPRIMIR GENES
vector<Gene*> g;
g = tmpC[selF]->getGenes();
double d;
Avaliacao av;
for(int k = 0; k<g.size(); k++){
d = av.decodificar(tmpC[selF], k);
cout << d << " ";
}
cout << endl;
//GRAVAR NO ARQUIVO
out<<tmpC[selF]->getFitness()<<"\n";
tmpC = populacao.crossover(populacao, tmpC);
populacao.setCromossomos(tmpC);
populacao.mutacao();
tmpC = populacao.getCromossomos();
int r1 = ra.Uniform(0, tam);
tmpC[r1] = selCromossomo;
populacao.setCromossomos(tmpC);
tempo++;
}
}