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.ant.func;
|
---|
9 | import ec.*;
|
---|
10 | import ec.app.ant.*;
|
---|
11 | import ec.gp.*;
|
---|
12 | import ec.util.*;
|
---|
13 |
|
---|
14 | /*
|
---|
15 | * Move.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 Move extends GPNode implements EvalPrint
|
---|
27 | {
|
---|
28 | public String toString() { return "move"; }
|
---|
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!=0)
|
---|
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 | Ant p = (Ant)problem;
|
---|
50 | switch (p.orientation)
|
---|
51 | {
|
---|
52 | case Ant.O_UP:
|
---|
53 | p.posy--;
|
---|
54 | if (p.posy<0) p.posy = p.maxy-1;
|
---|
55 | break;
|
---|
56 | case Ant.O_LEFT:
|
---|
57 | p.posx--;
|
---|
58 | if (p.posx<0) p.posx = p.maxx-1;
|
---|
59 | break;
|
---|
60 | case Ant.O_DOWN:
|
---|
61 | p.posy++;
|
---|
62 | if (p.posy>=p.maxy) p.posy=0;
|
---|
63 | break;
|
---|
64 | case Ant.O_RIGHT:
|
---|
65 | p.posx++;
|
---|
66 | if (p.posx>=p.maxx) p.posx=0;
|
---|
67 | break;
|
---|
68 | default: // whoa!
|
---|
69 | state.output.fatal("Whoa, somehow I got a bad orientation! (" + p.orientation + ")");
|
---|
70 | break;
|
---|
71 | }
|
---|
72 |
|
---|
73 | p.moves++;
|
---|
74 | if (p.map[p.posx][p.posy]==Ant.FOOD && p.moves < p.maxMoves )
|
---|
75 | {
|
---|
76 | p.sum++;
|
---|
77 | p.map[p.posx][p.posy]=Ant.ATE;
|
---|
78 | }
|
---|
79 | }
|
---|
80 |
|
---|
81 | /** Just like eval, but it retraces the map and prints out info */
|
---|
82 | public void evalPrint(final EvolutionState state,
|
---|
83 | final int thread,
|
---|
84 | final GPData input,
|
---|
85 | final ADFStack stack,
|
---|
86 | final GPIndividual individual,
|
---|
87 | final Problem problem,
|
---|
88 | final int[][] map2)
|
---|
89 | {
|
---|
90 | Ant p = (Ant)problem;
|
---|
91 | switch (p.orientation)
|
---|
92 | {
|
---|
93 | case Ant.O_UP:
|
---|
94 | p.posy--;
|
---|
95 | if (p.posy<0) p.posy = p.maxy-1;
|
---|
96 | break;
|
---|
97 | case Ant.O_LEFT:
|
---|
98 | p.posx--;
|
---|
99 | if (p.posx<0) p.posx = p.maxx-1;
|
---|
100 | break;
|
---|
101 | case Ant.O_DOWN:
|
---|
102 | p.posy++;
|
---|
103 | if (p.posy>=p.maxy) p.posy=0;
|
---|
104 | break;
|
---|
105 | case Ant.O_RIGHT:
|
---|
106 | p.posx++;
|
---|
107 | if (p.posx>=p.maxx) p.posx=0;
|
---|
108 | break;
|
---|
109 | default: // whoa!
|
---|
110 | state.output.fatal("Whoa, somehow I got a bad orientation! (" + p.orientation + ")");
|
---|
111 | break;
|
---|
112 | }
|
---|
113 |
|
---|
114 | p.moves++;
|
---|
115 | if (p.map[p.posx][p.posy]==Ant.FOOD && p.moves < p.maxMoves)
|
---|
116 | {
|
---|
117 | p.sum++;
|
---|
118 | p.map[p.posx][p.posy]=Ant.ATE;
|
---|
119 | }
|
---|
120 |
|
---|
121 | if (p.moves<p.maxMoves)
|
---|
122 | {
|
---|
123 | if (++p.pmod > 122 /* ascii z */) p.pmod=97; /* ascii a */
|
---|
124 | map2[p.posx][p.posy]=p.pmod;
|
---|
125 | }
|
---|
126 | }
|
---|
127 | }
|
---|
128 |
|
---|
129 |
|
---|
130 |
|
---|