-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPopulation.java
127 lines (117 loc) · 2.39 KB
/
Population.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
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
import java.util.*;
public class Population
{
ArrayList<Chromosome> pop;
public Population(int size, int graphsize)
{
pop = new ArrayList<Chromosome>();
for(int i=0; i<size; i++)
{
//make a new graph
AdjMatrixGraph G = new AdjMatrixGraph(graphsize);
//then a new coloring for that graph
ColorMatrix C = new ColorMatrix(G);
//then add that coloring to the pop
pop.add(new Chromosome(C));
}
}
//blank constructor to use for manual inserting (from file)
public Population()
{
pop = new ArrayList<Chromosome>();
}
//makes a pop from a pre formed list of chromosomes
public Population(ArrayList<Chromosome> list)
{
//hopefully this copies everything over, might need to make a manual move
System.out.println(list);
pop = list;
}
public int getSize()
{
return pop.size();
}
public Chromosome getWorst()
{
Chromosome worst = pop.get(0);
for(Chromosome c:pop)
{
if(c.getFitness(5)>worst.getFitness(5))
worst = c;
}
return worst;
}
public int getWorstPosition()
{
int worst = 0;
for(int i=0; i<pop.size(); i++)
{
if(pop.get(i).getFitness(5)>pop.get(worst).getFitness(5))
worst = i;
}
return worst;
}
public Chromosome getBest()
{
Chromosome best = pop.get(0);
for(Chromosome c:pop)
{
if(c.getFitness(5)<best.getFitness(5))
best = c;
}
return best;
}
public int getBestPosition()
{
int best = 0;
for(int i=0; i<pop.size(); i++)
{
if(pop.get(i).getFitness(5)<pop.get(best).getFitness(5))
best = i;
}
return best;
}
@Override
public String toString()
{
int i = 0;
String p="";
int terms = 25;
for(Chromosome c:pop)
{
//p+="Fitness for graph "+i+": "+c.getFitness(5);
p+=c.getFitness(5)+" ";
terms--;
//if(c.getFitness(5)==0)
// c.getColorMatrix().printColoring();
if(terms==0)
{
p+="\n";
terms=25;
}
i++;
}
return p;
}
public Chromosome getChromosome(int index)
{
return pop.get(index);
}
public void addChromosome(int index, Chromosome c)
{
//if element at index exists replace it
if(pop.get(index) != null)
pop.set(index, c);
//otherwise just add it
else
pop.add(c);
}
public void addChromosome(Chromosome c)
{
pop.add(c);
}
public ArrayList<Chromosome> getPop()
{
return this.pop;
}
}