-
Notifications
You must be signed in to change notification settings - Fork 0
/
PopMember.java
111 lines (98 loc) · 1.92 KB
/
PopMember.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
/**
* PopMember.java
*
* This class represents a member of the population. Each member has
* a fitness, an ordering for how to pack the bins, and a mutate method.
*
* @author Brian Clee
* @version 3/1/2013
*/
import java.util.ArrayList;
import java.util.Random;
import java.util.Arrays;
public class PopMember
{
//fields
private double fitness;
private double[] order;
private Random r;
public PopMember()
{
r = new Random();
}
public void setFitness(double fitness)
{
this.fitness = fitness;
}
public void setOrder(double[] order)
{
this.order = order;
}
public double getFitness()
{
return fitness;
}
public double[] getOrder()
{
return order;
}
public void evaluate(double binCap)
{
double cost = 0;
ArrayList<Double> bins = new ArrayList<Double>();
bins.add(0.0);
for(int i = 0; i < order.length; i++)
{
int tempIndex = 0;
boolean placed = false;
while(!placed)
{
double binWeight = bins.get(tempIndex);
if(binWeight + order[i] <= binCap)
{
bins.set(tempIndex, binWeight + order[i]);
placed = true;
}
else
{
if(bins.size()-1 == tempIndex)
{
bins.add(0.0);
}
tempIndex++;
}
}
}
cost = bins.size();
fitness = cost;
}
public void mutate()
{
Arrays.sort(order);
for(int i = 0; i < order.length/2; i++)
{
int j = order.length - (i+1);
double temp = order[i];
order[i] = order[j];
order[j] = temp;
}
boolean mutated = false;
int count = 0;
while(!mutated)
{
int index1 = r.nextInt(order.length);
int index2 = r.nextInt(order.length);
if(index1 != index2)
{
count++;
double temp = order[index2];
order[index2] = order[index1];
order[index1] = temp;
if(count == order.length/4)
{
mutated = true;
}
}
}
}
}