-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIndividual.cpp
85 lines (81 loc) · 2.4 KB
/
Individual.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
81
82
83
84
#include <iostream>
#include <cstdlib>
#include <random>
#include <cmath>
#include "Benchmark.hpp"
#include "Objectives.hpp"
#include "Individual.hpp"
using namespace std;
Individual::Individual()
{
this->Distance=0;
}
void Individual::InitializeIndividual(Benchmark *ObjBenchmark)
{
this->nbits = ObjBenchmark->getNBits();
this->NumberObjectives = ObjBenchmark->getNObjectives();
this->Dimension = ObjBenchmark->getDimension();
this->DecisionVariables.resize( this->Dimension, vector<bool>(nbits));
this->setRandom();
ObjObjectives.setBenchmark(ObjBenchmark);
Bounds = ObjBenchmark->Bounds;
this->EvalIndividual();
}
void Individual::EvalIndividual()
{
this->DecodeIndividual(ObjObjectives.DecisionVariables);
ObjObjectives.Eval();
}
void Individual::DecodeIndividual(vector<double> &Genotype)
{
//double Min = -100.0, Max = 100.0;
for(int i = 0; i < this->Dimension; i++)
{
Genotype[i] = (getBase10(this->DecisionVariables[i], Bounds[i][0], Bounds[i][1]));
}
}
vector<bool> Individual::random_bool( double p )
{
vector<bool> bits(this->nbits);
std::random_device rd;
std::mt19937 gen( rd());
std::bernoulli_distribution d( p);
/**
si el número de bits son muchos,
entonces existe la posibilidad de un desboradmiento
por el tipo de dato, por lo que sólo se randomiza
una sección de cromosoma, como un centrómero, o
los extremos adaptadores de 3 prima o 5 prima.
**/
for( int n = 0; n < bits.size()/10; ++n) {
bits[n] = d(gen);
}
return bits;
}
void Individual::setRandom()
{
for(int j = 0; j < this->Dimension; j++)
{
DecisionVariables[j] = random_bool();
/*for(int i = 0; i < DecisionVariables[j].size(); i++)
cout << DecisionVariables[j][i]<<" ";
cout << endl;*/
}
}
int Individual::BinarytoInt(vector<bool> &Individuo)
{
int Integer=0;
for(int i =0; i < Individuo.size(); i++)
{
if(Individuo[i])
Integer+=pow(2, Individuo.size()-1-i);
}
return Integer;
}
double Individual::getBase10(vector<bool> &Individuo, double Min , double Max)
{
double Delta = (double)(Max - Min) / ( pow(2,this->nbits) -1);
double Xbase10 = (double) BinarytoInt(Individuo);
Xbase10 =(Xbase10 *Delta) +Min;
return Xbase10;
}