-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChromosome.cpp
51 lines (43 loc) · 987 Bytes
/
Chromosome.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
#include"Chromosome.h"
#include"Global.h"
#include<stdio.h>
#include <algorithm>
Chromosome::Chromosome(){
size_g = Global::_getInstance()->SIZE_GENE;
s =0;
}
Chromosome::~Chromosome(){
delete[] gene;
gene = NULL;
}
Chromosome::Chromosome(int* _gene){
size_g = Global::_getInstance()->SIZE_GENE;
this->gene = new int[size_g];
for(int i=0;i<size_g; i++) this->gene[i] = _gene[i];
}
void Chromosome::Init(){
this->s =0;
gene = new int[size_g];
for(int i=0;i<size_g; i++) gene[i] = i;
randomize(gene, size_g);
}
void Chromosome::SetFitness(double f){
fitness = f;
}
void Chromosome::printGene(){
printf("%d , ",this->s);
for(int i=0;i<size_g; i++){
printf("%d ", gene[i]);
}
printf("\n");
}
void Chromosome::randomize(int*& arr, int n){
// srand (0);
for (int i = n - 1; i > 0; i--)
{
int j = rand() % (i + 1);
int tem = arr[i];
arr[i] = arr[j];
arr[j] = tem;
}
}