Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OKBJavaConnector/ECJClient/src/ec/app/ant/Ant.java @ 6152

Last change on this file since 6152 was 6152, checked in by bfarka, 13 years ago

added ecj and custom statistics to communicate with the okb services #1441

File size: 8.6 KB
Line 
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
8package ec.app.ant;
9import ec.app.ant.func.*;
10import ec.util.*;
11import ec.*;
12import ec.gp.*;
13import ec.gp.koza.*;
14import java.io.*;
15import java.util.*;
16import ec.simple.*;
17
18/*
19 * Ant.java
20 *
21 * Created: Mon Nov  1 15:46:19 1999
22 * By: Sean Luke
23 */
24
25/**
26 * Ant implements the Artificial Ant problem.
27 *
28 <p><b>Parameters</b><br>
29 <table>
30 <tr><td valign=top><i>base</i>.<tt>data</tt><br>
31 <font size=-1>classname, inherits or == ec.app.ant.AntData</font></td>
32 <td valign=top>(the class for the prototypical GPData object for the Ant problem)</td></tr>
33 <tr><td valign=top><i>base</i>.<tt>file</tt><br>
34 <font size=-1>String</font></td>
35 <td valign=top>(filename of the .trl file for the Ant problem)</td></tr>
36 <tr><td valign=top><i>base</i>.<tt>turns</tt><br>
37 <font size=-1>int &gt;= 1</td>
38 <td valign=top>(maximal number of moves the ant may make)</td></tr>
39 </table>
40
41 <p><b>Parameter bases</b><br>
42 <table>
43 <tr><td valign=top><i>base</i>.<tt>data</tt></td>
44 <td>species (the GPData object)</td></tr>
45 </table>
46 *
47 * @author Sean Luke
48 * @version 1.0
49 */
50
51public class Ant extends GPProblem implements SimpleProblemForm
52    {
53    public static final String P_FILE = "file";
54    public static final String P_MOVES = "moves";
55
56    // map point descriptions
57    public static final int ERROR = 0;
58    public static final int FOOD = -1;
59    public static final int EMPTY = 1;
60    public static final int TRAIL = 2;
61    public static final int ATE = 3;
62
63    // orientations
64    public static final int O_UP = 0;
65    public static final int O_LEFT = 1;
66    public static final int O_DOWN = 2;
67    public static final int O_RIGHT = 3;
68
69    // We'll deep clone this anyway, even though we don't
70    // need it by default!
71    public AntData input;
72
73    // maximum number of moves
74    public int maxMoves;
75
76    // how much food we have
77    public int food;
78
79    // our map
80    public int map[][];
81   
82    // store the positions of food so we can reset our map
83    // don't need to be deep-cloned, they're read-only
84    public int foodx[];
85    public int foody[];
86
87    // map[][]'s bounds
88    public int maxx;
89    public int maxy;
90
91    // our position
92    public int posx;
93    public int posy;
94
95    // how many points we've gotten
96    public int sum;
97   
98    // our orientation
99    public int orientation;
100
101    // how many moves we've made
102    public int moves;
103
104    // print modulo for doing the abcdefg.... thing at print-time
105    public int pmod;
106
107    public Object clone()
108        {
109        Ant myobj = (Ant) (super.clone());
110        myobj.input = (AntData)(input.clone());
111        myobj.map = new int[map.length][];
112        for(int x=0;x<map.length;x++)
113            myobj.map[x] = (int[])(map[x].clone());
114        return myobj;
115        }
116
117    public void setup(final EvolutionState state,
118        final Parameter base)
119        {
120        // very important, remember this
121        super.setup(state,base);
122
123        // not using any default base -- it's not safe
124
125        // set up our input
126        input = (AntData) state.parameters.getInstanceForParameterEq(
127            base.push(P_DATA),null, AntData.class);
128        input.setup(state,base.push(P_DATA));
129
130        // how many maxMoves?
131        maxMoves = state.parameters.getInt(base.push(P_MOVES),null,1);
132        if (maxMoves==0)
133            state.output.error("The number of moves an ant has to make must be >0");
134       
135        // load our file
136        File filename = state.parameters.getFile(base.push(P_FILE),null);
137        if (filename==null)
138            state.output.fatal("Ant trail file name not provided.");
139
140        food = 0;
141        try
142            {
143            LineNumberReader lnr =
144                new LineNumberReader(new FileReader(filename));
145           
146            StringTokenizer st = new StringTokenizer(lnr.readLine()); // ugh
147            maxx = Integer.parseInt(st.nextToken());
148            maxy = Integer.parseInt(st.nextToken());
149            map = new int[maxx][maxy];
150            int y;
151            for(y=0;y<maxy;y++)
152                {
153                String s = lnr.readLine();
154                if (s==null)
155                    {
156                    state.output.warning("Ant trail file ended prematurely");
157                    break;
158                    }
159                int x;
160                for(x=0;x<s.length();x++)
161                    {
162                    if (s.charAt(x)==' ')
163                        map[x][y]=EMPTY;
164                    else if (s.charAt(x)=='#')
165                        { map[x][y]=FOOD; food++; }
166                    else if (s.charAt(x)=='.')
167                        map[x][y]=TRAIL;
168                    else state.output.error("Bad character '" + s.charAt(x) + "' on line number " + lnr.getLineNumber() + " of the Ant trail file.");
169                    }
170                // fill out rest of X's
171                for(int z=x;z<maxx;z++)
172                    map[z][y]=EMPTY;
173                }
174            // fill out rest of Y's
175            for (int z=y;z<maxy;z++)
176                for(int x=0;x<maxx;x++)
177                    map[x][z]=EMPTY;
178            }
179        catch (NumberFormatException e)
180            {
181            state.output.fatal("The Ant trail file does not begin with x and y integer values.");
182            }
183        catch (IOException e)
184            {
185            state.output.fatal("The Ant trail file could not be read due to an IOException:\n" + e);
186            }
187        state.output.exitIfErrors();
188
189        // load foodx and foody reset arrays
190        foodx = new int[food];
191        foody = new int[food];
192        int tmpf = 0;
193        for(int x=0;x<map.length;x++)
194            for(int y=0;y<map[0].length;y++)
195                if (map[x][y]==FOOD)
196                    { foodx[tmpf] = x; foody[tmpf] = y; tmpf++; }
197        }
198
199    public void evaluate(final EvolutionState state,
200        final Individual ind,
201        final int subpopulation,
202        final int threadnum)
203        {
204        if (!ind.evaluated)  // don't bother reevaluating
205            {
206            sum = 0;           
207            posx = 0;
208            posy = 0;
209            orientation = O_RIGHT;
210
211            for(moves=0;moves<maxMoves && sum<food; )
212                ((GPIndividual)ind).trees[0].child.eval(
213                    state,threadnum,input,stack,((GPIndividual)ind),this);
214               
215            // the fitness better be KozaFitness!
216            KozaFitness f = ((KozaFitness)ind.fitness);
217            f.setStandardizedFitness(state,(float)(food - sum));
218            f.hits = sum;
219            ind.evaluated = true;
220
221            // clean up array
222            for(int y=0;y<food;y++)
223                map[foodx[y]][foody[y]] = FOOD;
224            }
225        }
226
227    public void describe(
228        final EvolutionState state,
229        final Individual ind,
230        final int subpopulation,
231        final int threadnum,
232        final int log)
233
234        {
235        state.output.println("\n\nBest Individual's Map\n=====================", log);
236
237        sum = 0;
238        pmod = 97; /** ascii a */
239        posx = 0;
240        posy = 0;
241        orientation = O_RIGHT;
242
243        int[][] map2 = new int[map.length][];
244        for(int x=0;x<map.length;x++)
245            map2[x] = (int[])(map[x].clone());
246
247        map2[posx][posy] = pmod; pmod++;
248        for(moves=0; moves<maxMoves && sum<food; )
249            ((EvalPrint)(((GPIndividual)ind).trees[0].child)).evalPrint(
250                state,threadnum,input,stack,((GPIndividual)ind),this,
251                map2);
252        // print out the map
253        for(int y=0;y<map2.length;y++)
254            {
255            for(int x=0;x<map2.length;x++)
256                {
257                switch(map2[x][y])
258                    {
259                    case FOOD:
260                        state.output.print("#",log);
261                        break;
262                    case EMPTY:
263                        state.output.print(".",log);
264                        break;
265                    case TRAIL:
266                        state.output.print("+",log);
267                        break;
268                    case ATE:
269                        state.output.print("?",log);
270                        break;
271                    default:
272                        state.output.print(""+((char)map2[x][y]),log);
273                        break;
274                    }
275                }
276            state.output.println("",log);
277            }
278
279        }
280    }
Note: See TracBrowser for help on using the repository browser.