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 |
|
---|
14 | /*
|
---|
15 | * Frog.java
|
---|
16 | *
|
---|
17 | * Created: Wed Nov 3 18:26:37 1999
|
---|
18 | * By: Sean Luke
|
---|
19 | */
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * @author Sean Luke
|
---|
23 | * @version 1.0
|
---|
24 | */
|
---|
25 |
|
---|
26 | public class Frog extends GPNode
|
---|
27 | {
|
---|
28 | public String toString() { return "frog"; }
|
---|
29 |
|
---|
30 | public void checkConstraints(final EvolutionState state,
|
---|
31 | final int tree,
|
---|
32 | final GPIndividual typicalIndividual,
|
---|
33 | final Parameter individualBase)
|
---|
34 | {
|
---|
35 | super.checkConstraints(state,tree,typicalIndividual,individualBase);
|
---|
36 | if (children.length!=1)
|
---|
37 | state.output.error("Incorrect number of children for node " +
|
---|
38 | toStringForError() + " at " +
|
---|
39 | individualBase);
|
---|
40 | }
|
---|
41 |
|
---|
42 | public void eval(final EvolutionState state,
|
---|
43 | final int thread,
|
---|
44 | final GPData input,
|
---|
45 | final ADFStack stack,
|
---|
46 | final GPIndividual individual,
|
---|
47 | final Problem problem)
|
---|
48 | {
|
---|
49 | Lawnmower p = (Lawnmower)problem;
|
---|
50 | LawnmowerData d = (LawnmowerData)input;
|
---|
51 |
|
---|
52 | children[0].eval(state,thread,input,stack,individual,problem);
|
---|
53 |
|
---|
54 | // we follow the Koza-II example, not the lil-gp example.
|
---|
55 | // that is, we "assume" that in our orientation the X axis
|
---|
56 | // is moving out away from us, and the Y axis is moving
|
---|
57 | // out to the left. In lil-gp, the assumption is that the Y axis
|
---|
58 | // axis is moving out away from us, and the X axis is moving out
|
---|
59 | // to the right.
|
---|
60 |
|
---|
61 | switch (p.orientation)
|
---|
62 | {
|
---|
63 | case Lawnmower.O_UP:
|
---|
64 | // counter-clockwise rotation
|
---|
65 | p.posx -= d.y;
|
---|
66 | p.posy += d.x;
|
---|
67 | break;
|
---|
68 | case Lawnmower.O_LEFT:
|
---|
69 | // flipped orientation
|
---|
70 | p.posx -= d.x;
|
---|
71 | p.posy -= d.y;
|
---|
72 | break;
|
---|
73 | case Lawnmower.O_DOWN:
|
---|
74 | // clockwise rotation
|
---|
75 | p.posx += d.y;
|
---|
76 | p.posy -= d.x;
|
---|
77 | break;
|
---|
78 | case Lawnmower.O_RIGHT:
|
---|
79 | // proper orientation
|
---|
80 | p.posx += d.x;
|
---|
81 | p.posy += d.y;
|
---|
82 | break;
|
---|
83 | default: // whoa!
|
---|
84 | state.output.fatal("Whoa, somehow I got a bad orientation! (" + p.orientation + ")");
|
---|
85 | break;
|
---|
86 | }
|
---|
87 |
|
---|
88 | // shift back into the lawn frame.
|
---|
89 | // because Java's % on negative numbers preserves the
|
---|
90 | // minus sign, we have to mod twice with an addition.
|
---|
91 | // C has to do this too.
|
---|
92 | p.posx = ((p.posx % p.maxx) + p.maxx ) % p.maxx ;
|
---|
93 | p.posy = ((p.posy % p.maxy) + p.maxy ) % p.maxy ;
|
---|
94 |
|
---|
95 | p.moves++;
|
---|
96 | if (p.map[p.posx][p.posy]==Lawnmower.UNMOWED)
|
---|
97 | {
|
---|
98 | p.sum++;
|
---|
99 | p.map[p.posx][p.posy] = p.moves;
|
---|
100 | }
|
---|
101 |
|
---|
102 | // return [x,y] -- to do this, simply don't modify input
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 |
|
---|
107 |
|
---|