-
Notifications
You must be signed in to change notification settings - Fork 1
/
genetic.cpp
156 lines (130 loc) · 3.96 KB
/
genetic.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
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
152
153
154
155
156
/********************Yi Zhuo******************************
Utils for the Genetic Algorithms
**************************************************/
#include <iostream>
#include <string>
#include <stdlib.h>
#include <ctime>
using namespace std;
/**************************************Variables need to be predetermined********************************************************/
//initial population
#define POPULATION 10
//number of elements inside the function EX: COSX+4+5^2 had 7 elements MUST BE ODD SO THE LAST ELEMENTS IS ALWAY TERM
#define ELEMENTS 9
//number of fitness point we need to compare to determined the fitness level
#define CHECK_POINTS 10
//all the items that will be in the function to get graphs.
const string ope[] = {"+","-","*","/","^"};
const string term[] = {"cosx","sinx","x","0","1""2","3","4","5","6","7","8","9",".1",".2",".3",".4",".5",".6",".7",".8",".9"};
//populating the fitness array from the graph we are comparing to fitness_map[pos][value]
double fitness_map[2][CHECK_POINTS];
/********************************************************Things need to keep track of**********************************************/
double value_at_check_point[POPULATION][CHECK_POINTS] ={0}; //need to get the value every time the elements mutate or crossover;
double fitness_level[POPULATION]={0};
double total_fitness=0;
//double array for the initial population
string population[POPULATION][ELEMENTS];
/*************************************************************** FUNCTIONS********************************************************/
//populting the fitness_map
void populating_check(){
//grap data from the graph we need to compare to
}
//random generator - take the size as input produce result vary from 0 to (size-1)
int random(int size){
if(size==0){
return 0;
}
else {
return rand()%size;
}
}
//populating the array population
void populating() {
for( int x =0; x<POPULATION; ++x){
for (int y =0; y<ELEMENTS; ++y){
if( y%2==0){
population[x][y]=term[random(sizeof(term)/sizeof(*term))];
}
else{
population[x][y]=ope[random(sizeof(ope)/sizeof(*ope))];
}
}
}
}
//roulette wheel
int roulette_wheel(){
int selected=0;
double fSlice = random(total_fitness);
while (fSlice>0){
if(selected <POPULATION){
fSlice = fSlice - fitness_level[selected];
++selected;
}
else{
break;
}
}
return selected;
}
//crossover
void crossover(int mom, int dad){
int crossoverRate = random(ELEMENTS);
for(int x=0; x<crossoverRate; ++x){
string temp;
temp=population[dad][x];
population[dad][x]=population[mom][x];
population[mom][x]=temp;
}
}
//mutation
void mutation(int victim){
int mutation_rate=575; //some random number
int randValue;
for(int x=0; x<ELEMENTS; ++x){
randValue=random(1000); //assignment random number to this variable
if( randValue==mutation_rate){ //if it meet the mutation_rate then mutate
if( x%2==0){
population[victim][x]=term[random(sizeof(term)/sizeof(*term))];
}
else{
population[victim][x]=ope[random(sizeof(ope)/sizeof(*ope))];
}
}
}
}
//calculate total fitness
void cal_total_fitness(){
for(int x= 0; x<POPULATION; ++x){
total_fitness=total_fitness+fitness_level[x];
}
}
//check fitness - highest fit will result in 0, since it's the difference between the prefered graph and populated graphs
void check_fitness(){
for(int x=0;x<POPULATION;++x){
//return fitness level of each population
for (int y=0; y<CHECK_POINTS; ++y){
fitness_level[x] += abs(fitness_map[1][y]-value_at_check_point[x][y]); //average out the all the vaule at check points.
}
fitness_level[x] = fitness_level[x]/CHECK_POINTS;
fitness_level[x]=100-fitness_level[x];
if(fitness_level[x]<0){
fitness_level[x]=0.0;
}
}
}
int main(){
srand(time(0));
int mom, dad;
populating();
populating_check(); //populate fitness map
//start
mom=roulette_wheel();
dad=roulette_wheel();
crossover(mom,dad);
mutation(mom);
mutation(dad);
check_fitness();
cal_total_fitness();
//go back to the start
getchar();
}