-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScheduling.java
More file actions
executable file
·182 lines (150 loc) · 5.83 KB
/
Scheduling.java
File metadata and controls
executable file
·182 lines (150 loc) · 5.83 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import java.io.*;
import java.util.*;
import java.text.*;
public class Scheduling extends FitnessFunction{
/*******************************************************************************
* INSTANCE VARIABLES *
*******************************************************************************/
/*******************************************************************************
* STATIC VARIABLES *
*******************************************************************************/
/*******************************************************************************
* CONSTRUCTORS *
*******************************************************************************/
int num_people = 7;
int num_times = 35;
public Scheduling(){
name = "Scheduling Problem";
}
/*******************************************************************************
* MEMBER METHODS *
*******************************************************************************/
// COMPUTE A CHROMOSOME'S RAW FITNESS *************************************
public void doRawFitness(Chromo X){
/*
X.rawFitness = 0;
for (int z=0; z<Parameters.numGenes * Parameters.geneSize; z++){
if (X.chromo.charAt(z) == '1') X.rawFitness += 1;
}
*/
// Chromosome contains a bit string of length: log_2P * T, where P is the number
// of people being scheduled and T is the number of possible times
// Each time has log_2P bits to tell what person is being scheduled there
// Example Chromo:
// 011 010 011 100 100 001 101 010 100 001 110 001 .........
// First 3 bits are Monday 8-10, then Monday 10-12, etc. throughout the week
int top_choice_reward = 10;
int second_choice_reward = 7;
int third_choice_reward = 3;
int available = 1;
int[] num_assigned = new int[7];
for (int day = 0; day < 7; day++)
{
for (int time = 0; time < 5; time++)
{
int person_num = gray_to_decimal(X.chromo.substring(day*15 + 3*time, day*15 + 3*time + 3));
if (person_num == 7)
{
System.out.println("oops1");
X.rawFitness = 0;
return;
}
int[][] person_preferences = InputSchedule.people.get(person_num).preferences;
// Keep track of how many times a person has been assigned
if (person_preferences[day][time] != 0)
{
if (++num_assigned[person_num] > 5)
{
X.rawFitness = 0;
return;
}
}
if (person_preferences[day][time] == 1)
X.rawFitness += top_choice_reward;
if (person_preferences[day][time] == 2)
X.rawFitness += second_choice_reward;
if (person_preferences[day][time] == 3)
X.rawFitness += third_choice_reward;
if (person_preferences[day][time] == 4)
X.rawFitness += available;
}
}
}
public static int gray_to_decimal(String gray)
{
switch (gray){
case "000":
return 0;
case "001":
return 1;
case "011":
return 2;
case "010":
return 3;
case "110":
return 4;
case "111":
return 5;
case "101":
return 6;
case "100":
return 7;
default:
return 0;
}
}
public static String decimal_to_gray(int decimal)
{
switch (decimal){
case 0:
return "000";
case 1:
return "001";
case 2:
return "011";
case 3:
return "010";
case 4:
return "110";
case 5:
return "111";
case 6:
return "101";
case 7:
return "100";
default:
return "000";
}
}
// PRINT OUT AN INDIVIDUAL GENE TO THE SUMMARY FILE *********************************
public void doPrintGenes(Chromo X, FileWriter output) throws java.io.IOException{
System.out.println("Gene Alpha:");
for (int i=0; i<Parameters.numGenes; i++){
Hwrite.right(X.getGeneAlpha(i),11,output);
System.out.println(X.getGeneAlpha(i));
}
output.write(" RawFitness");
output.write("\n ");
for (int i=0; i<Parameters.numGenes; i++){
Hwrite.right(X.getPosIntGeneValue(i),11,output);
}
Hwrite.right((int) X.rawFitness,13,output);
output.write("\n\n");
String[] days = new String[]{"Mon", "Tues", "Wed", "Thurs", "Fri", "Sat", "Sun"};
String[] times = new String[]{"(8-10)", "(10-12)", "(12-2)", "(2-4)", "(4-6)"};
// Print genes in a time table friendly format
for (int day = 0; day < 7; day++)
{
for (int time = 0; time < 5; time++)
{
int person_num = gray_to_decimal(X.chromo.substring(day*15 + 3*time, day*15 + 3*time + 3));
System.out.printf("%30s", days[day] + " " + times[time] + ": " + InputSchedule.people.get(person_num).name);
}
System.out.println();
}
return;
}
/*******************************************************************************
* STATIC METHODS *
*******************************************************************************/
} // End of OneMax.java ******************************************************