-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSevenOnes.h
More file actions
50 lines (42 loc) · 1.47 KB
/
SevenOnes.h
File metadata and controls
50 lines (42 loc) · 1.47 KB
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
#include <stdlib.h>
#include <string>
#include <iostream>
#include <fstream>
#include "Individual.h"
#include "SimpleGeneticAlgorithm.h"
using namespace std;
//Class looking for seven ones
class SevenOnes: public SimpleGeneticAlgorithm {
public:
SevenOnes(int chromosome_size, int population_size, int tournament_size, int generations, double mutation_probability, double cross_probability):
SimpleGeneticAlgorithm(chromosome_size, population_size, tournament_size, generations, mutation_probability, cross_probability){}
double evaluateIndividual(Individual ind) override {
int zeros=0;
for(int i=0; i<this->chromosome_size; i++)
if(!ind.getBit(i))
zeros += 1;
double error = abs(zeros - 3) *0.1;
return error;
}
//This function converts a bit_Srting into an int
int toInt(string bit_string, int length){
int temp;
int num=0;
for(int i=0; i<length; i++){
temp=bit_string[i]-'0';
num |= (1 << (length-1-i)) * temp;
}
return num;
}
//This function obtains the fenotype of an individual
string getFenotype(Individual ind) {
string fenotype = "";
for(int i=0; i<ind.size(); i++) {
if(ind.getBit(i))
fenotype += "1";
else
fenotype += "0";
}
return fenotype + " --> " + to_string(toInt(fenotype, 10)) + " -- ";
}
};