-
Notifications
You must be signed in to change notification settings - Fork 1
/
JCellIslands.java
executable file
·154 lines (121 loc) · 5.13 KB
/
JCellIslands.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
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
/**
* @author Bernabe Dorronsoro
*
* Description
* Example of using JCell without config file.
* Here, the MTTP problem is solved with an island GA
*
* For more information:
* https://gforge.uni.lu
* http://neo.lcc.uma.es/Software/JCell/index.htm
*
*/
import gui.CGADisplay;
import jcell.*;
import MO.*;
import adaptiveCGA.*;
import java.util.Random;
import java.util.Date;
import operators.mutation.*;
import operators.recombination.*;
import operators.selection.*;
import operators.replacement.*;
import problems.Combinatorial.*;
public class JCellIslands implements GenerationListener
{
static int islands = 5;
static int islandSize = 80;
static int longitCrom ;
static int numberOfFuncts ;
// Default maximum number of function evaluations
static int evaluationsLimit = 1000000;
//private static boolean showDisplay = false;
private boolean verbose = false;
public static void main (String args[]) throws Exception
{
// O nœmero de argumentos necess‡rio carece de valida‹o, mas parece que n‹o Ž preciso passar o nome do ficheiro de configura‹o
if (args.length != 2)
{
System.out.println("Error. Try java JCell <DataFile> <GenerationLimit>.");
System.exit(-1);
}
Random r = new Random(); // seed for the random number generator
long start, end; // starting and ending time
JCellIslands sel = new JCellIslands();
EvolutionaryAlg ea = new DistributedGA(r, Integer.parseInt(args[1]));
//Problem prob = new MMDP();
Problem prob = new MTTP(args[0]);
ea.setParam(CellularGA.PARAM_PROBLEM, prob);
longitCrom = prob.getVariables();
numberOfFuncts = prob.numberOfObjectives();
Population pop = new PopIsland(islands, islandSize);
Individual ind = new BinaryIndividual(longitCrom);
pop.setRandomPop(r, ind);
Double mutac = new Double(1.0); // probability of individual mutation
Double cross = new Double(1.0); // crossover probability
// Set parameters of CEA
ea.setParam(EvolutionaryAlg.PARAM_MIGRATION_FREQUENCY, new Integer(1000));
ea.setParam(CellularGA.PARAM_POPULATION, pop);
ea.setParam(CellularGA.PARAM_STATISTIC, new ComplexStats());
ea.setParam(CellularGA.PARAM_LISTENER,sel);
ea.setParam(CellularGA.PARAM_MUTATION_PROB, mutac);
ea.setParam(CellularGA.PARAM_CROSSOVER_PROB, cross);
ea.setParam(CellularGA.PARAM_EVALUATION_LIMIT, new Integer(evaluationsLimit));
ea.setParam(CellularGA.PARAM_TARGET_FITNESS, (Double) new Double(prob.getMaxFitness()));
ea.setParam("selection1", new TournamentSelection(r)); // selection of one parent
ea.setParam("selection2", new TournamentSelection(r)); // selection of one parent
ea.setParam("crossover", new Dpx(r));
ea.setParam("mutation", new BinaryMutation(r,ea));
ea.setParam("replacement", new ReplaceIfBetter());
start = (new Date()).getTime();
// generation cycles
ea.experiment();
end = (new Date()).getTime();
Double best = (Double) ((Statistic) ea.getParam(CellularGA.PARAM_STATISTIC)).getStat(0);
int evals = ((Problem) (ea.getParam(CellularGA.PARAM_PROBLEM))).getNEvals();
// Writes: best found solution, number of generations, elapsed time (mseconds)
System.out.println("Solution: Best Generations Evaluations Time (ms) Problem");
System.out.println(best + " " + (Integer) ea.getParam(CellularGA.PARAM_GENERATION_NUMBER)+" "+ evals +" "+(end-start) + " "
+ ((Problem) ea.getParam(CellularGA.PARAM_PROBLEM)));
}
private void writeLine(String line)
{
if (verbose)
System.out.println(line);
}
public void generation(EvolutionaryAlg ea)
{
//CellularGA cea = (CellularGA) ea;
verbose = ((Boolean) ea.getParam(CellularGA.PARAM_VERBOSE)).booleanValue();
//writeLine("Generation: " + (Integer) ea.getParam(CellularGA.PARAM_GENERATION_NUMBER));
// Migration occurs here:
int evals = ((Problem) (ea.getParam(CellularGA.PARAM_PROBLEM))).getNEvals();
PopIsland pop = (PopIsland)(ea.getParam(CellularGA.PARAM_POPULATION));
//if (evals >= ea.getParam(ea.PARAM_MIGRATION_FREQUENCY))
Individual ind0, ind1;
int j;
int migrFreq = ((Integer)ea.getParam(EvolutionaryAlg.PARAM_MIGRATION_FREQUENCY)).intValue();
if (((double)evals/migrFreq >= 1) && (evals%(migrFreq*islands)==0))
{
System.out.println("Evaluaciones: " + evals);
for (int i=0; i<islands; i++)
{
ind0 = pop.getIndividual(i,((DistributedGA)ea).bestInds[i]);
if (i==islands-1)
j = 0;
else
j = i+1;
ind1 = pop.getIndividual(j,((DistributedGA)ea).worstInds[j]);
if (Target.isBetter(ind0, ind1))
{
pop.setIndividual(j,((DistributedGA)ea).worstInds[j],ind0);
if (Target.isBetter(ind0, pop.getIndividual(j, ((DistributedGA)ea).bestInds[j])))
((DistributedGA)ea).bestInds[j] = ((DistributedGA)ea).worstInds[j];
for(int k=0; k<islandSize; k++)
if (Target.isWorse(pop.getIndividual(j,k), pop.getIndividual(j, ((DistributedGA)ea).worstInds[j])))
((DistributedGA)ea).worstInds[j] = k;
}
}
}
}
}