1 | /*
|
---|
2 | Copyright 2006 by Sean Luke and George Mason University
|
---|
3 | Licensed under the Academic Free License version 3.0
|
---|
4 | See the file "LICENSE" for more information
|
---|
5 | */
|
---|
6 |
|
---|
7 |
|
---|
8 | package ec.app.ecsuite;
|
---|
9 |
|
---|
10 | import ec.util.*;
|
---|
11 | import ec.*;
|
---|
12 | import ec.simple.*;
|
---|
13 | import ec.vector.*;
|
---|
14 |
|
---|
15 | /*
|
---|
16 | * ECSuite.java
|
---|
17 | *
|
---|
18 | * Created: Thu MAr 22 16:27:15 2001
|
---|
19 | * By: Liviu Panait and Sean Luke
|
---|
20 | */
|
---|
21 |
|
---|
22 | /*
|
---|
23 | * @author Liviu Panait and Sean Luke
|
---|
24 | * @version 1.0
|
---|
25 | */
|
---|
26 |
|
---|
27 | /**
|
---|
28 | Several standard Evolutionary Computation functions are implemented: Rastrigin, De Jong's test suite
|
---|
29 | F1-F4 problems (Sphere, Rosenbrock, Step, Noisy-Quartic), Booth (from [Schwefel, 1995]), and Griewangk.
|
---|
30 | As the SimpleFitness is used for maximization problems, the mapping f(x) --> -f(x) is used to transform
|
---|
31 | the problems into maximization ones.
|
---|
32 |
|
---|
33 | <p><b>Parameters</b><br>
|
---|
34 | <table>
|
---|
35 | <tr><td valign=top><i>base</i>.<tt>type</tt><br>
|
---|
36 | <font size=-1>String, one of: rosenbrock rastrigin sphere step noisy-quartic kdj-f1 kdj-f2 kdj-f3 kdj-f4 booth median [or] griewangk</font>/td>
|
---|
37 | <td valign=top>(The vector problem to test against. Some of the types are synonyms: kdj-f1 = sphere, kdj-f2 = rosenbrock, kdj-f3 = step, kdj-f4 = noisy-quartic. "kdj" stands for "Ken DeJong", and the numbers are the problems in his test suite)</td></tr>
|
---|
38 | </table>
|
---|
39 |
|
---|
40 |
|
---|
41 | */
|
---|
42 |
|
---|
43 | public class ECSuite extends Problem implements SimpleProblemForm
|
---|
44 | {
|
---|
45 | public static final String P_WHICH_PROBLEM = "type";
|
---|
46 |
|
---|
47 | public static final String V_ROSENBROCK = "rosenbrock";
|
---|
48 | public static final String V_RASTRIGIN = "rastrigin";
|
---|
49 | public static final String V_SPHERE = "sphere";
|
---|
50 | public static final String V_STEP = "step";
|
---|
51 | public static final String V_NOISY_QUARTIC = "noisy-quartic";
|
---|
52 | public static final String V_F1 = "kdj-f1";
|
---|
53 | public static final String V_F2 = "kdj-f2";
|
---|
54 | public static final String V_F3 = "kdj-f3";
|
---|
55 | public static final String V_F4 = "kdj-f4";
|
---|
56 | public static final String V_BOOTH = "booth";
|
---|
57 | public static final String V_GRIEWANGK = "griewangk";
|
---|
58 | public static final String V_MEDIAN = "median";
|
---|
59 |
|
---|
60 | public static final int PROB_ROSENBROCK = 0;
|
---|
61 | public static final int PROB_RASTRIGIN = 1;
|
---|
62 | public static final int PROB_SPHERE = 2;
|
---|
63 | public static final int PROB_STEP = 3;
|
---|
64 | public static final int PROB_NOISY_QUARTIC = 4;
|
---|
65 | public static final int PROB_BOOTH = 5;
|
---|
66 | public static final int PROB_GRIEWANGK = 6;
|
---|
67 | public static final int PROB_MEDIAN = 7;
|
---|
68 |
|
---|
69 | public int problemType = PROB_ROSENBROCK; // defaults on Rosenbrock
|
---|
70 |
|
---|
71 | // for RASTRIGIN function
|
---|
72 | public final static float A = 10.0f;
|
---|
73 |
|
---|
74 | // nothing....
|
---|
75 | public void setup(final EvolutionState state, final Parameter base)
|
---|
76 | {
|
---|
77 | super.setup(state, base);
|
---|
78 | String wp = state.parameters.getStringWithDefault( base.push( P_WHICH_PROBLEM ), null, "" );
|
---|
79 | if( wp.compareTo( V_ROSENBROCK ) == 0 || wp.compareTo (V_F2)==0 )
|
---|
80 | problemType = PROB_ROSENBROCK;
|
---|
81 | else if ( wp.compareTo( V_RASTRIGIN ) == 0 )
|
---|
82 | problemType = PROB_RASTRIGIN;
|
---|
83 | else if ( wp.compareTo( V_SPHERE ) == 0 || wp.compareTo (V_F1)==0)
|
---|
84 | problemType = PROB_SPHERE;
|
---|
85 | else if ( wp.compareTo( V_STEP ) == 0 || wp.compareTo (V_F3)==0)
|
---|
86 | problemType = PROB_STEP;
|
---|
87 | else if ( wp.compareTo( V_NOISY_QUARTIC ) == 0 || wp.compareTo (V_F4)==0)
|
---|
88 | problemType = PROB_NOISY_QUARTIC;
|
---|
89 | else if( wp.compareTo( V_BOOTH ) == 0 )
|
---|
90 | problemType = PROB_BOOTH;
|
---|
91 | else if( wp.compareTo( V_GRIEWANGK ) == 0 )
|
---|
92 | problemType = PROB_GRIEWANGK;
|
---|
93 | else if( wp.compareTo( V_MEDIAN ) == 0 )
|
---|
94 | problemType = PROB_MEDIAN;
|
---|
95 | else state.output.fatal(
|
---|
96 | "Invalid value for parameter, or parameter not found.\n" +
|
---|
97 | "Acceptable values are:\n" +
|
---|
98 | " " + V_ROSENBROCK + "(or " + V_F2 + ")\n" +
|
---|
99 | " " + V_RASTRIGIN + "\n" +
|
---|
100 | " " + V_SPHERE + "(or " + V_F1 + ")\n" +
|
---|
101 | " " + V_STEP + "(or " + V_F3 + ")\n" +
|
---|
102 | " " + V_NOISY_QUARTIC + "(or " + V_F4 + ")\n"+
|
---|
103 | " " + V_BOOTH + "\n" +
|
---|
104 | " " + V_GRIEWANGK + "\n" +
|
---|
105 | " " + V_MEDIAN + "\n",
|
---|
106 | base.push( P_WHICH_PROBLEM ) );
|
---|
107 | }
|
---|
108 |
|
---|
109 | public void evaluate(final EvolutionState state,
|
---|
110 | final Individual ind,
|
---|
111 | final int subpopulation,
|
---|
112 | final int threadnum)
|
---|
113 | {
|
---|
114 | if( !( ind instanceof DoubleVectorIndividual ) )
|
---|
115 | state.output.fatal( "The individuals for this problem should be DoubleVectorIndividuals." );
|
---|
116 |
|
---|
117 | DoubleVectorIndividual temp = (DoubleVectorIndividual)ind;
|
---|
118 | double[] genome = temp.genome;
|
---|
119 | int len = genome.length;
|
---|
120 |
|
---|
121 | // this curious break-out makes it easy to use the isOptimal() and function() methods
|
---|
122 | // for other purposes, such as coevolutionary versions of this class.
|
---|
123 |
|
---|
124 | // compute the fitness on a per-function basis
|
---|
125 | double fit = (function(state, problemType, temp.genome, threadnum));
|
---|
126 |
|
---|
127 | // compute if we're optimal on a per-function basis
|
---|
128 | boolean isOptimal = isOptimal(problemType, fit);
|
---|
129 |
|
---|
130 | // set the fitness appropriately
|
---|
131 | ((SimpleFitness)(ind.fitness)).setFitness( state, (float)fit, isOptimal );
|
---|
132 | ind.evaluated = true;
|
---|
133 | }
|
---|
134 |
|
---|
135 |
|
---|
136 | public boolean isOptimal(int function, double fitness)
|
---|
137 | {
|
---|
138 | switch(problemType)
|
---|
139 | {
|
---|
140 | case PROB_ROSENBROCK:
|
---|
141 | case PROB_RASTRIGIN:
|
---|
142 | case PROB_SPHERE:
|
---|
143 | case PROB_STEP:
|
---|
144 | return fitness == 0.0f;
|
---|
145 |
|
---|
146 | case PROB_NOISY_QUARTIC:
|
---|
147 | case PROB_BOOTH:
|
---|
148 | case PROB_GRIEWANGK:
|
---|
149 | case PROB_MEDIAN:
|
---|
150 | default:
|
---|
151 | return false;
|
---|
152 | }
|
---|
153 | }
|
---|
154 |
|
---|
155 | public double function(EvolutionState state, int function, double[] genome, int threadnum)
|
---|
156 | {
|
---|
157 | double value = 0;
|
---|
158 | int len = genome.length;
|
---|
159 | switch(function)
|
---|
160 | {
|
---|
161 | case PROB_ROSENBROCK:
|
---|
162 | for( int i = 1 ; i < len ; i++ )
|
---|
163 | value += 100*(genome[i-1]*genome[i-1]-genome[i])*
|
---|
164 | (genome[i-1]*genome[i-1]-genome[i]) +
|
---|
165 | (1-genome[i-1])*(1-genome[i-1]);
|
---|
166 | return -value;
|
---|
167 |
|
---|
168 |
|
---|
169 | case PROB_RASTRIGIN:
|
---|
170 | value = len * A;
|
---|
171 | for( int i = 0 ; i < len ; i++ )
|
---|
172 | value += ( genome[i]*genome[i] - A * Math.cos( 2 * Math.PI * genome[i] ) );
|
---|
173 | return -value;
|
---|
174 |
|
---|
175 |
|
---|
176 | case PROB_SPHERE:
|
---|
177 | for( int i = 0 ; i < len ; i++ )
|
---|
178 | value += genome[i]*genome[i];
|
---|
179 | return -value;
|
---|
180 |
|
---|
181 |
|
---|
182 | case PROB_STEP:
|
---|
183 | for( int i = 0 ; i < len ; i++ )
|
---|
184 | value += 6 + Math.floor( genome[i] );
|
---|
185 | return -value;
|
---|
186 |
|
---|
187 |
|
---|
188 | case PROB_NOISY_QUARTIC:
|
---|
189 | for( int i = 0 ; i < len ; i++ )
|
---|
190 | value += (i+1)*(genome[i]*genome[i]*genome[i]*genome[i]) + // no longer : Math.pow( genome[i], 4 ) +
|
---|
191 | state.random[threadnum].nextDouble();
|
---|
192 | return -value;
|
---|
193 |
|
---|
194 |
|
---|
195 | case PROB_BOOTH:
|
---|
196 | if( len != 2 )
|
---|
197 | state.output.fatal( "The Booth problem is defined for only two terms, and as a consequence the genome of the DoubleVectorIndividual should have size 2." );
|
---|
198 | value = (genome[0] + 2*genome[1] - 7) * (genome[0] + 2*genome[1] - 7) +
|
---|
199 | (2*genome[0] + genome[1] - 5) * (2*genome[0] + genome[1] - 5);
|
---|
200 | return -value;
|
---|
201 |
|
---|
202 |
|
---|
203 | case PROB_GRIEWANGK:
|
---|
204 | value = 1;
|
---|
205 | double prod = 1;
|
---|
206 | for( int i = 0 ; i < len ; i++ )
|
---|
207 | {
|
---|
208 | value += (genome[i]*genome[i])/4000.0;
|
---|
209 | prod *= Math.cos( genome[i] / Math.sqrt(i+1) );
|
---|
210 | }
|
---|
211 | value -= prod;
|
---|
212 | return -value;
|
---|
213 |
|
---|
214 |
|
---|
215 | case PROB_MEDIAN: // FIXME, need to do a better median-finding algorithm, such as http://www.ics.uci.edu/~eppstein/161/960130.html
|
---|
216 | double[] sorted = new double[genome.length];
|
---|
217 | System.arraycopy(genome, 0, sorted, 0, sorted.length);
|
---|
218 | ec.util.QuickSort.qsort(sorted);
|
---|
219 | return sorted[sorted.length / 2]; // note positive
|
---|
220 |
|
---|
221 | default:
|
---|
222 | state.output.fatal( "ec.app.ecsuite.ECSuite has an invalid problem -- how on earth did that happen?" );
|
---|
223 | return 0; // never happens
|
---|
224 | }
|
---|
225 | }
|
---|
226 | }
|
---|