-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeneticAlgorithm.java
More file actions
180 lines (147 loc) · 5.41 KB
/
GeneticAlgorithm.java
File metadata and controls
180 lines (147 loc) · 5.41 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
package aiproject1;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Random;
/**
*
* @author Layan
*/
public class GeneticAlgorithm {
public static int length = 0;
private static ArrayList jobs = new ArrayList(); // array of jobs
static Population population = new Population();
static Gene firstOptimal;
static Gene secondOptimal;
int generation = 0;
public static void main(String[] args) throws IOException{
FileInputStream in = null;
try {
in = new FileInputStream("MPSP_35.txt"); //text file to test !!
Scanner scanner = new Scanner(in);
scanner.useDelimiter("\\D");
int k;
while (scanner.hasNext()){
k = scanner.nextInt();
jobs.add(k);
length++;
}//while
} finally {
if (in != null) {
in.close();
}
}//END read
//Initialization
Random rand = new Random();
GeneticAlgorithm GA = new GeneticAlgorithm();
GeneticAlgorithm.population.initializePopulation(length);//population
GeneticAlgorithm.population.calculateFitnessAll();//calculate fitness for all genes
System.out.println("Generation: "+ GA.generation + " Most Optimal: "
+ GeneticAlgorithm.population.optimal);
//^ start with generation 0 ^
int min = GeneticAlgorithm.population.optimal , g = GA.generation;
char [] seok = new char[length];//stores the optimal gene
while(GeneticAlgorithm.population.optimal !=0){
GA.generation++;
GeneticAlgorithm.selection();
GeneticAlgorithm.crossover();
if(rand.nextInt()%7 < length){
GeneticAlgorithm.mutation();
}
GA.addOptimalOffspring();
GeneticAlgorithm.population.calculateFitnessAll();
System.out.println("Generation: "+ GA.generation + " Most Optimal: "+GeneticAlgorithm.population.optimal);
if(GeneticAlgorithm.population.optimal < min){
min = GeneticAlgorithm.population.optimal;
g = GA.generation;
System.arraycopy(GeneticAlgorithm.population.getOptimal().genes, 0, seok, 0, length);
}
if(GA.generation > 2000){
break;
}//due to hardware limitation we terminate the search if it gets too long
//if we let it search 10k generation for example, it might find the optimal solution but my laptop will burn...
}//END WHILE
ArrayList s1 = new ArrayList();
ArrayList s2 = new ArrayList();
System.out.println("Solution found in generation: "+g);
System.out.println("Fitness: "+min);
System.out.print("Genes: ");
for(int i= 0; i< length ; i++){
System.out.print(seok[i]+" ");
if(seok[i]=='X'){
s1.add(jobs.get(i));
}else{
s2.add(jobs.get(i));
}
}
System.out.println();
System.out.print("Jobs in subset 1: ");
for(int i=0;i<s1.size();i++){
System.out.print(s1.get(i)+" ");
}
System.out.println();
System.out.print("Jobs in subset 2: ");
for(int i=0;i<s2.size();i++){
System.out.print(s2.get(i)+" ");
}
System.out.println();
}//END MAIN
public static int getJob(int i){
int time = (int) jobs.get(i);
return time;
}
//Selection
public static void selection() {
//select the most optimal
firstOptimal=population.getOptimal();
//select the second most optimal
secondOptimal=population.getSecondOptimal();
}
//Crossover
public static void crossover(){
Random rn=new Random();
//Select a random crossover point
int crossOverPoint=rn.nextInt(length)+0;
// Swap values among parents
for(int i=0;i<crossOverPoint;i++){
char temp=firstOptimal.genes[i];
firstOptimal.genes[i]=secondOptimal.genes[i];
secondOptimal.genes[i]=temp;
}//end if
}
//Mutation
public static void mutation(){
Random rand = new Random();
int mutationPoint = rand.nextInt(length)+0;
if(firstOptimal.genes[mutationPoint]=='X')
firstOptimal.genes[mutationPoint]='Y';
else
firstOptimal.genes[mutationPoint]='X';
mutationPoint = rand.nextInt(length)+0;
if(secondOptimal.genes[mutationPoint]=='X')
secondOptimal.genes[mutationPoint]='Y';
else
secondOptimal.genes[mutationPoint]='X';
}
//Get optimal gene / offspring
Gene getOptimalOffspring(){
if(firstOptimal.fitness > secondOptimal.fitness)
return firstOptimal;
else
return secondOptimal;
}
//Replace least fittest gene from most fittest offspring
void addOptimalOffspring(){
firstOptimal.calcfitness();
secondOptimal.calcfitness();
int leastOptimalIndex = population.getLeastOptimal();
//replace least optimal with most
population.genes[leastOptimalIndex] = getOptimalOffspring();
}
}//END GENETIC ALGORITHM
/*
MADE FOR CS340
BY: LAYAN , NADA , RAHAF
LAST REVISION: 4/8/2018
*/