forked from hoanghungbk/GAs-for-TSP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Individual.java
82 lines (67 loc) · 1.66 KB
/
Individual.java
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
public class Individual {
// Individual la lo trinh di qua tat ca cac thanh pho -- nhiem sac the chua
// thong tin cac thanh pho di qua-> mang luu thong tin cac thanh pho
// chromosome : Nhiem sac the
// gene : gen tren NST ( chính là 1 thành phố )
private int[] chromosome;
private int chromosomeLength;
private double fitness = -1;
// constructor khoi tao Individual
public Individual(int[] chromosome) {
this.chromosome = chromosome;
}
public Individual(int chromosomeLength) {
// Create random individual
int[] individual;
individual = new int[chromosomeLength];
for (int gene = 0; gene < chromosomeLength; gene++) {
individual[gene] = gene;
}
this.chromosome = individual;
}
// getter and setter
public int[] getChromosome() {
return chromosome;
}
public void setChromosome(int[] chromosome) {
this.chromosome = chromosome;
}
public int getChromosomeLength() {
return chromosomeLength;
}
public void setChromosomeLength(int chromosomeLength) {
this.chromosomeLength = chromosomeLength;
}
/**
* set gene at offset
*
* @param gene
* @param offset
*/
public void setGene(int offset, int gene) {
this.chromosome[offset] = gene;
}
/**
* get gene at offset
*
* @param offset
* @return gene
*/
public int getGene(int offset) {
return this.chromosome[offset];
}
public String toString() {
String output = "";
for (int gene = 0; gene < this.chromosome.length; gene++) {
output += this.chromosome[gene] + ",";
}
return output;
}
public double getFitness() {
// TODO Auto-generated method stub
return this.fitness;
}
public void setFitness(double fitness) {
this.fitness = fitness;
}
}