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.func;
|
---|
9 | import ec.*;
|
---|
10 | import ec.app.lawnmower.*;
|
---|
11 | import ec.gp.*;
|
---|
12 | import ec.util.*;
|
---|
13 | import java.io.*;
|
---|
14 |
|
---|
15 | /*
|
---|
16 | * LawnERC.java
|
---|
17 | *
|
---|
18 | * Created: Wed Nov 3 18:26:37 1999
|
---|
19 | * By: Sean Luke
|
---|
20 | */
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * @author Sean Luke
|
---|
24 | * @version 1.0
|
---|
25 | */
|
---|
26 |
|
---|
27 | public class LawnERC extends ERC
|
---|
28 | {
|
---|
29 | public int maxx;
|
---|
30 | public int maxy;
|
---|
31 |
|
---|
32 | public int x;
|
---|
33 | public int y;
|
---|
34 |
|
---|
35 | public void setup(final EvolutionState state, final Parameter base)
|
---|
36 | {
|
---|
37 | super.setup(state,base);
|
---|
38 | // figure the coordinate base -- this will break if the underlying
|
---|
39 | // base changes, oops
|
---|
40 | Parameter newbase =
|
---|
41 | new Parameter(EvolutionState.P_EVALUATOR).push(Evaluator.P_PROBLEM);
|
---|
42 |
|
---|
43 | // obviously not using the default base for any of this stuff
|
---|
44 |
|
---|
45 | // load our map coordinates
|
---|
46 | maxx = state.parameters.getInt(newbase.push(Lawnmower.P_X),null,1);
|
---|
47 | if (maxx==0)
|
---|
48 | state.output.error("The width (x dimension) of the lawn must be >0",
|
---|
49 | newbase.push(Lawnmower.P_X));
|
---|
50 | maxy = state.parameters.getInt(newbase.push(Lawnmower.P_Y),null,1);
|
---|
51 | if (maxy==0)
|
---|
52 | state.output.error("The length (y dimension) of the lawn must be >0",
|
---|
53 | newbase.push(Lawnmower.P_X));
|
---|
54 | state.output.exitIfErrors();
|
---|
55 | }
|
---|
56 |
|
---|
57 | public void resetNode(final EvolutionState state, final int thread)
|
---|
58 | {
|
---|
59 | x = state.random[thread].nextInt(maxx);
|
---|
60 | y = state.random[thread].nextInt(maxy);
|
---|
61 | }
|
---|
62 |
|
---|
63 | public int nodeHashCode()
|
---|
64 | {
|
---|
65 | // a reasonable hash code
|
---|
66 | return this.getClass().hashCode() + x*maxy + y;
|
---|
67 | }
|
---|
68 |
|
---|
69 | public boolean nodeEquals(final GPNode node)
|
---|
70 | {
|
---|
71 | // check first to see if we're the same kind of ERC --
|
---|
72 | // won't work for subclasses; in that case you'll need
|
---|
73 | // to change this to isAssignableTo(...)
|
---|
74 | if (this.getClass() != node.getClass()) return false;
|
---|
75 | // now check to see if the ERCs hold the same value
|
---|
76 | LawnERC n = (LawnERC)node;
|
---|
77 | return (n.x==x && n.y==y);
|
---|
78 | }
|
---|
79 |
|
---|
80 | public void readNode(final EvolutionState state, final DataInput dataInput) throws IOException
|
---|
81 | {
|
---|
82 | x = dataInput.readInt();
|
---|
83 | y = dataInput.readInt();
|
---|
84 | }
|
---|
85 |
|
---|
86 | public void writeNode(final EvolutionState state, final DataOutput dataOutput) throws IOException
|
---|
87 | {
|
---|
88 | dataOutput.writeInt(x);
|
---|
89 | dataOutput.writeInt(y);
|
---|
90 | }
|
---|
91 |
|
---|
92 | public String encode()
|
---|
93 | { return Code.encode(x) + Code.encode(y); }
|
---|
94 |
|
---|
95 | public boolean decode(DecodeReturn dret)
|
---|
96 | {
|
---|
97 | // store the position and the string in case they
|
---|
98 | // get modified by Code.java
|
---|
99 | int pos = dret.pos;
|
---|
100 | String data = dret.data;
|
---|
101 |
|
---|
102 | // decode
|
---|
103 | Code.decode(dret);
|
---|
104 |
|
---|
105 | if (dret.type != DecodeReturn.T_INT) // uh oh!
|
---|
106 | {
|
---|
107 | // restore the position and the string; it was an error
|
---|
108 | dret.data = data;
|
---|
109 | dret.pos = pos;
|
---|
110 | return false;
|
---|
111 | }
|
---|
112 |
|
---|
113 | // store the data
|
---|
114 | x = (int)(dret.l);
|
---|
115 |
|
---|
116 | // decode
|
---|
117 | Code.decode(dret);
|
---|
118 |
|
---|
119 | if (dret.type != DecodeReturn.T_INT) // uh oh!
|
---|
120 | {
|
---|
121 | // restore the position and the string; it was an error
|
---|
122 | dret.data = data;
|
---|
123 | dret.pos = pos;
|
---|
124 | return false;
|
---|
125 | }
|
---|
126 |
|
---|
127 | // store the data
|
---|
128 | y = (int)(dret.l);
|
---|
129 |
|
---|
130 | return true;
|
---|
131 | }
|
---|
132 |
|
---|
133 | public String toStringForHumans()
|
---|
134 | { return "[" + x + "," + y + "]"; }
|
---|
135 |
|
---|
136 | public void eval(final EvolutionState state,
|
---|
137 | final int thread,
|
---|
138 | final GPData input,
|
---|
139 | final ADFStack stack,
|
---|
140 | final GPIndividual individual,
|
---|
141 | final Problem problem)
|
---|
142 | {
|
---|
143 | LawnmowerData rd = ((LawnmowerData)(input));
|
---|
144 | rd.x = x;
|
---|
145 | rd.y = y;
|
---|
146 | }
|
---|
147 | }
|
---|
148 |
|
---|
149 |
|
---|
150 |
|
---|