1 | /*
|
---|
2 | Copyright 2006 by Sean Luke
|
---|
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.lawnmower;
|
---|
9 | import ec.util.*;
|
---|
10 | import ec.*;
|
---|
11 | import ec.gp.*;
|
---|
12 | import ec.gp.koza.*;
|
---|
13 | import ec.simple.*;
|
---|
14 |
|
---|
15 | /*
|
---|
16 | * Lawnmower.java
|
---|
17 | *
|
---|
18 | * Created: Mon Nov 1 15:46:19 1999
|
---|
19 | * By: Sean Luke
|
---|
20 | */
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * Lawnmower implements the Koza-II Lawnmower problem.
|
---|
24 | *
|
---|
25 | <p><b>Parameters</b><br>
|
---|
26 | <table>
|
---|
27 | <tr><td valign=top><i>base</i>.<tt>data</tt><br>
|
---|
28 | <font size=-1>classname, inherits or == ec.app.lawnmower.LawnmowerData</font></td>
|
---|
29 | <td valign=top>(the class for the prototypical GPData object for the Lawnmower problem)</td></tr>
|
---|
30 | <tr><td valign=top><i>base</i>.<tt>file</tt><br>
|
---|
31 | <font size=-1>String</font></td>
|
---|
32 | <td valign=top>(filename of the .trl file for the Lawnmower problem)</td></tr>
|
---|
33 | <tr><td valign=top><i>base</i>.<tt>turns</tt><br>
|
---|
34 | <font size=-1>int >= 1</td>
|
---|
35 | <td valign=top>(maximal number of moves the lawnmower may make)</td></tr>
|
---|
36 | </table>
|
---|
37 |
|
---|
38 | <p><b>Parameter bases</b><br>
|
---|
39 | <table>
|
---|
40 | <tr><td valign=top><i>base</i>.<tt>data</tt></td>
|
---|
41 | <td>species (the GPData object)</td></tr>
|
---|
42 | </table>
|
---|
43 | *
|
---|
44 | * @author Sean Luke
|
---|
45 | * @version 1.0
|
---|
46 | */
|
---|
47 |
|
---|
48 | public class Lawnmower extends GPProblem implements SimpleProblemForm
|
---|
49 | {
|
---|
50 | public static final String P_X = "x";
|
---|
51 | public static final String P_Y = "y";
|
---|
52 |
|
---|
53 | // map point descriptions
|
---|
54 | public static final int UNMOWED = 0;
|
---|
55 |
|
---|
56 | // orientations
|
---|
57 | public static final int O_UP = 0;
|
---|
58 | public static final int O_LEFT = 1;
|
---|
59 | public static final int O_DOWN = 2;
|
---|
60 | public static final int O_RIGHT = 3;
|
---|
61 |
|
---|
62 | // We'll deep clone this
|
---|
63 | public LawnmowerData input;
|
---|
64 |
|
---|
65 | // our map
|
---|
66 | public int map[][];
|
---|
67 |
|
---|
68 | // map[][]'s bounds
|
---|
69 | public int maxx;
|
---|
70 | public int maxy;
|
---|
71 |
|
---|
72 | // our current position
|
---|
73 | public int posx;
|
---|
74 | public int posy;
|
---|
75 |
|
---|
76 | // how many points we've gotten
|
---|
77 | public int sum;
|
---|
78 |
|
---|
79 | // our orientation
|
---|
80 | public int orientation;
|
---|
81 |
|
---|
82 | // how many moves we've made
|
---|
83 | public int moves;
|
---|
84 |
|
---|
85 | // print modulo for doing the abcdefg.... thing at print-time
|
---|
86 | public int pmod;
|
---|
87 |
|
---|
88 | public Object clone()
|
---|
89 | {
|
---|
90 | Lawnmower myobj = (Lawnmower) (super.clone());
|
---|
91 | myobj.input = (LawnmowerData)(input.clone());
|
---|
92 | myobj.map = new int[map.length][];
|
---|
93 | for(int x=0;x<map.length;x++)
|
---|
94 | myobj.map[x] = (int[])(map[x].clone());
|
---|
95 | return myobj;
|
---|
96 | }
|
---|
97 |
|
---|
98 | public void setup(final EvolutionState state,
|
---|
99 | final Parameter base)
|
---|
100 | {
|
---|
101 | // very important, remember this
|
---|
102 | super.setup(state,base);
|
---|
103 |
|
---|
104 | // I'm not using the default base for any of this stuff;
|
---|
105 | // it's not safe I think.
|
---|
106 |
|
---|
107 | // set up our input
|
---|
108 | input = (LawnmowerData) state.parameters.getInstanceForParameterEq(
|
---|
109 | base.push(P_DATA),null, LawnmowerData.class);
|
---|
110 | input.setup(state,base.push(P_DATA));
|
---|
111 |
|
---|
112 | // load our map coordinates
|
---|
113 | maxx = state.parameters.getInt(base.push(P_X),null,1);
|
---|
114 | if (maxx==0)
|
---|
115 | state.output.error("The width (x dimension) of the lawn must be >0",
|
---|
116 | base.push(P_X));
|
---|
117 | maxy = state.parameters.getInt(base.push(P_Y),null,1);
|
---|
118 | if (maxy==0)
|
---|
119 | state.output.error("The length (y dimension) of the lawn must be >0",
|
---|
120 | base.push(P_Y));
|
---|
121 | state.output.exitIfErrors();
|
---|
122 |
|
---|
123 | // set up the map
|
---|
124 |
|
---|
125 | map = new int[maxx][maxy];
|
---|
126 | for(int x=0;x<maxx;x++)
|
---|
127 | for(int y=0;y<maxy;y++)
|
---|
128 | map[x][y]=UNMOWED;
|
---|
129 | }
|
---|
130 |
|
---|
131 | public void evaluate(final EvolutionState state,
|
---|
132 | final Individual ind,
|
---|
133 | final int subpopulation,
|
---|
134 | final int threadnum)
|
---|
135 | {
|
---|
136 | if (!ind.evaluated) // don't bother reevaluating
|
---|
137 | {
|
---|
138 | sum = 0;
|
---|
139 | moves = 0;
|
---|
140 | posx = maxx/2+1;
|
---|
141 | posy = maxy/2+1;
|
---|
142 | orientation = O_UP;
|
---|
143 |
|
---|
144 | // evaluate the individual
|
---|
145 | ((GPIndividual)ind).trees[0].child.eval(
|
---|
146 | state,threadnum,input,stack,((GPIndividual)ind),this);
|
---|
147 |
|
---|
148 | // clean up the map
|
---|
149 | for(int x=0;x<maxx;x++)
|
---|
150 | for(int y=0;y<maxy;y++)
|
---|
151 | map[x][y]=UNMOWED;
|
---|
152 |
|
---|
153 | // the fitness better be KozaFitness!
|
---|
154 | KozaFitness f = ((KozaFitness)ind.fitness);
|
---|
155 | f.setStandardizedFitness(state,(float)(maxx*maxy - sum));
|
---|
156 | f.hits = sum;
|
---|
157 | ind.evaluated = true;
|
---|
158 | }
|
---|
159 | }
|
---|
160 |
|
---|
161 | public void describe(
|
---|
162 | final EvolutionState state,
|
---|
163 | final Individual ind,
|
---|
164 | final int subpopulation,
|
---|
165 | final int threadnum,
|
---|
166 | final int log)
|
---|
167 | {
|
---|
168 | state.output.println("\n\nBest Individual's Map\n=====================", log);
|
---|
169 |
|
---|
170 | sum = 0;
|
---|
171 | moves = 0;
|
---|
172 | posx = maxx/2+1;
|
---|
173 | posy = maxy/2+1;
|
---|
174 | orientation = O_UP;
|
---|
175 |
|
---|
176 | // evaluate the individual
|
---|
177 | ((GPIndividual)ind).trees[0].child.eval(
|
---|
178 | state,threadnum,input,stack,((GPIndividual)ind),this);
|
---|
179 |
|
---|
180 | // print out the map
|
---|
181 | state.output.println(" Y ->", log);
|
---|
182 | for(int x=0;x<map.length;x++)
|
---|
183 | {
|
---|
184 | if (x==1) state.output.print("v", log);
|
---|
185 | else if (x==0) state.output.print("X",log);
|
---|
186 | else state.output.print(" ",log);
|
---|
187 | state.output.print("+",log);
|
---|
188 | for(int y=0;y<map[x].length;y++)
|
---|
189 | state.output.print("----+",log);
|
---|
190 | state.output.println("",log);
|
---|
191 | if (x==0) state.output.print("|",log);
|
---|
192 | else state.output.print(" ",log);
|
---|
193 | state.output.print("|",log);
|
---|
194 |
|
---|
195 | for(int y=0;y<map[x].length;y++)
|
---|
196 | {
|
---|
197 | if (map[x][y]==UNMOWED)
|
---|
198 | state.output.print(" ",log);
|
---|
199 | else
|
---|
200 | {
|
---|
201 | String s = "" + (map[x][y]);
|
---|
202 | while (s.length()<4) s = " " + s;
|
---|
203 | state.output.print(s + "|",log);
|
---|
204 | }
|
---|
205 | }
|
---|
206 | state.output.println("",log);
|
---|
207 | }
|
---|
208 | if (map.length==1) state.output.print("v",log);
|
---|
209 | else state.output.print(" ",log);
|
---|
210 | state.output.print("+",log);
|
---|
211 | for(int y=0;y<map[map.length-1].length;y++)
|
---|
212 | state.output.print("----+",log);
|
---|
213 | state.output.println("",log);
|
---|
214 |
|
---|
215 |
|
---|
216 | // clean up the map
|
---|
217 | for(int x=0;x<maxx;x++)
|
---|
218 | for(int y=0;y<maxy;y++)
|
---|
219 | map[x][y]=UNMOWED;
|
---|
220 | }
|
---|
221 | }
|
---|