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.multiplexerslow;
|
---|
9 | import ec.util.*;
|
---|
10 | import ec.*;
|
---|
11 | import ec.gp.*;
|
---|
12 | import ec.gp.koza.*;
|
---|
13 | import ec.simple.*;
|
---|
14 |
|
---|
15 | /*
|
---|
16 | * Multiplexer.java
|
---|
17 | *
|
---|
18 | * Created: Mon Nov 1 15:46:19 1999
|
---|
19 | * By: Sean Luke
|
---|
20 | */
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * Multiplexer implements the family of <i>n</i>-Multiplexer problems.
|
---|
24 | *
|
---|
25 | <p><b>Parameters</b><br>
|
---|
26 | <table>
|
---|
27 | <tr><td valign=top><i>base</i>.<tt>data</tt><br>
|
---|
28 | <font size=-1>classname, inherits or == ec.app.multiplexer.MultiplexerData</font></td>
|
---|
29 | <td valign=top>(the class for the prototypical GPData object for the Multiplexer problem)</td></tr>
|
---|
30 | <tr><td valign=top><i>base</i>.<tt>bits</tt><br>
|
---|
31 | <font size=-1>1, 2, or 3</font></td>
|
---|
32 | <td valign=top>(The number of address bits (1 == 3-multiplexer, 2 == 6-multiplexer, 3==11-multiplexer)</td></tr>
|
---|
33 | </table>
|
---|
34 |
|
---|
35 | <p><b>Parameter bases</b><br>
|
---|
36 | <table>
|
---|
37 | <tr><td valign=top><i>base</i>.<tt>data</tt></td>
|
---|
38 | <td>species (the GPData object)</td></tr>
|
---|
39 | </table>
|
---|
40 | *
|
---|
41 | * @author Sean Luke
|
---|
42 | * @version 1.0
|
---|
43 | */
|
---|
44 |
|
---|
45 | public class Multiplexer extends GPProblem implements SimpleProblemForm
|
---|
46 | {
|
---|
47 | public static final int NUMINPUTS = 20;
|
---|
48 | public static final String P_NUMBITS = "bits";
|
---|
49 |
|
---|
50 | public int bits; // number of bits in the data
|
---|
51 | public int amax; // maximum address value
|
---|
52 | public int dmax; // maximum data value
|
---|
53 | public int addressPart; // the current address part
|
---|
54 | public int dataPart; // the current data part
|
---|
55 |
|
---|
56 | // we'll need to deep clone this one though.
|
---|
57 | public MultiplexerData input;
|
---|
58 |
|
---|
59 | public Object clone()
|
---|
60 | {
|
---|
61 | Multiplexer myobj = (Multiplexer) (super.clone());
|
---|
62 | myobj.input = (MultiplexerData)(input.clone());
|
---|
63 | return myobj;
|
---|
64 | }
|
---|
65 |
|
---|
66 | public void setup(final EvolutionState state,
|
---|
67 | final Parameter base)
|
---|
68 | {
|
---|
69 | // very important, remember this
|
---|
70 | super.setup(state,base);
|
---|
71 |
|
---|
72 | // not using any default base -- it's not safe
|
---|
73 |
|
---|
74 | // I figure 3 bits is plenty -- otherwise we'd be dealing with
|
---|
75 | // REALLY big arrays!
|
---|
76 | bits = state.parameters.getIntWithMax(base.push(P_NUMBITS),null,1,3);
|
---|
77 | if (bits<1)
|
---|
78 | state.output.fatal("The number of bits for Multiplexer must be between 1 and 3 inclusive");
|
---|
79 |
|
---|
80 | amax=1;
|
---|
81 | for(int x=0;x<bits;x++) amax *=2; // safer than Math.pow(...)
|
---|
82 |
|
---|
83 | dmax=1;
|
---|
84 | for(int x=0;x<amax;x++) dmax *=2; // safer than Math.pow(...)
|
---|
85 |
|
---|
86 | // set up our input
|
---|
87 | input = (MultiplexerData) state.parameters.getInstanceForParameterEq(
|
---|
88 | base.push(P_DATA),null, MultiplexerData.class);
|
---|
89 | input.setup(state,base.push(P_DATA));
|
---|
90 | }
|
---|
91 |
|
---|
92 |
|
---|
93 | public void evaluate(final EvolutionState state,
|
---|
94 | final Individual ind,
|
---|
95 | final int subpopulation,
|
---|
96 | final int threadnum)
|
---|
97 | {
|
---|
98 | if (!ind.evaluated) // don't bother reevaluating
|
---|
99 | {
|
---|
100 | int sum = 0;
|
---|
101 |
|
---|
102 | for(addressPart = 0; addressPart < amax; addressPart++)
|
---|
103 | for(dataPart = 0; dataPart < dmax; dataPart++)
|
---|
104 | {
|
---|
105 | ((GPIndividual)ind).trees[0].child.eval(
|
---|
106 | state,threadnum,input,stack,((GPIndividual)ind),this);
|
---|
107 | sum += 1- ( /* "Not" */
|
---|
108 | ((dataPart >>> addressPart) & 1) /* extracts the address-th
|
---|
109 | bit in data and moves
|
---|
110 | it to position 0,
|
---|
111 | clearing out all
|
---|
112 | other bits */
|
---|
113 | ^ /* "Is Different from" */
|
---|
114 | (input.x & 1)); /* A 1 if input.x is
|
---|
115 | non-zero, else 0. */
|
---|
116 | }
|
---|
117 |
|
---|
118 | // the fitness better be KozaFitness!
|
---|
119 | KozaFitness f = ((KozaFitness)ind.fitness);
|
---|
120 | f.setStandardizedFitness(state,(float)(amax*dmax - sum));
|
---|
121 | f.hits = sum;
|
---|
122 | ind.evaluated = true;
|
---|
123 | }
|
---|
124 | }
|
---|
125 | }
|
---|