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 | * IfFoodAhead.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 IfFoodAhead extends GPNode implements EvalPrint
|
---|
27 | {
|
---|
28 | public String toString() { return "if-food-ahead"; }
|
---|
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!=2)
|
---|
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 | if (p.map[p.posx][(p.posy-1+p.maxy)%p.maxy]==Ant.FOOD)
|
---|
54 | children[0].eval(state,thread,input,stack,individual,problem);
|
---|
55 | else children[1].eval(state,thread,input,stack,individual,problem);
|
---|
56 | break;
|
---|
57 | case Ant.O_LEFT:
|
---|
58 | if (p.map[(p.posx-1+p.maxx)%p.maxx][p.posy]==Ant.FOOD)
|
---|
59 | children[0].eval(state,thread,input,stack,individual,problem);
|
---|
60 | else children[1].eval(state,thread,input,stack,individual,problem);
|
---|
61 | break;
|
---|
62 | case Ant.O_DOWN:
|
---|
63 | if (p.map[p.posx][(p.posy+1)%p.maxy]==Ant.FOOD)
|
---|
64 | children[0].eval(state,thread,input,stack,individual,problem);
|
---|
65 | else children[1].eval(state,thread,input,stack,individual,problem);
|
---|
66 | break;
|
---|
67 | case Ant.O_RIGHT:
|
---|
68 | if (p.map[(p.posx+1)%p.maxx][p.posy]==Ant.FOOD)
|
---|
69 | children[0].eval(state,thread,input,stack,individual,problem);
|
---|
70 | else children[1].eval(state,thread,input,stack,individual,problem);
|
---|
71 | break;
|
---|
72 | default: // whoa!
|
---|
73 | state.output.fatal("Whoa, somehow I got a bad orientation! (" + p.orientation + ")");
|
---|
74 | break;
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 |
|
---|
79 | public void evalPrint(final EvolutionState state,
|
---|
80 | final int thread,
|
---|
81 | final GPData input,
|
---|
82 | final ADFStack stack,
|
---|
83 | final GPIndividual individual,
|
---|
84 | final Problem problem,
|
---|
85 | final int[][] map2)
|
---|
86 | {
|
---|
87 | Ant p = (Ant)problem;
|
---|
88 | switch (p.orientation)
|
---|
89 | {
|
---|
90 | case Ant.O_UP:
|
---|
91 | if (p.map[p.posx][(p.posy-1+p.maxy)%p.maxy]==Ant.FOOD)
|
---|
92 | ((EvalPrint)children[0]).evalPrint(state,thread,input,stack,individual,problem,map2);
|
---|
93 | else ((EvalPrint)children[1]).evalPrint(state,thread,input,stack,individual,problem,map2);
|
---|
94 | break;
|
---|
95 | case Ant.O_LEFT:
|
---|
96 | if (p.map[(p.posx-1+p.maxx)%p.maxx][p.posy]==Ant.FOOD)
|
---|
97 | ((EvalPrint)children[0]).evalPrint(state,thread,input,stack,individual,problem,map2);
|
---|
98 | else ((EvalPrint)children[1]).evalPrint(state,thread,input,stack,individual,problem,map2);
|
---|
99 | break;
|
---|
100 | case Ant.O_DOWN:
|
---|
101 | if (p.map[p.posx][(p.posy+1)%p.maxy]==Ant.FOOD)
|
---|
102 | ((EvalPrint)children[0]).evalPrint(state,thread,input,stack,individual,problem,map2);
|
---|
103 | else ((EvalPrint)children[1]).evalPrint(state,thread,input,stack,individual,problem,map2);
|
---|
104 | break;
|
---|
105 | case Ant.O_RIGHT:
|
---|
106 | if (p.map[(p.posx+1)%p.maxx][p.posy]==Ant.FOOD)
|
---|
107 | ((EvalPrint)children[0]).evalPrint(state,thread,input,stack,individual,problem,map2);
|
---|
108 | else ((EvalPrint)children[1]).evalPrint(state,thread,input,stack,individual,problem,map2);
|
---|
109 | break;
|
---|
110 | default: // whoa!
|
---|
111 | state.output.fatal("Whoa, somehow I got a bad orientation! (" + p.orientation + ")");
|
---|
112 | break;
|
---|
113 | }
|
---|
114 | }
|
---|
115 | }
|
---|
116 |
|
---|
117 |
|
---|
118 |
|
---|