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.multiplexer;
|
---|
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 |
|
---|
52 | // we'll need to deep clone this one though.
|
---|
53 | public MultiplexerData input;
|
---|
54 |
|
---|
55 | public Object clone()
|
---|
56 | {
|
---|
57 | Multiplexer myobj = (Multiplexer) (super.clone());
|
---|
58 | myobj.input = (MultiplexerData)(input.clone());
|
---|
59 | return myobj;
|
---|
60 | }
|
---|
61 |
|
---|
62 | public void setup(final EvolutionState state,
|
---|
63 | final Parameter base)
|
---|
64 | {
|
---|
65 | // very important, remember this
|
---|
66 | super.setup(state,base);
|
---|
67 |
|
---|
68 | // not using any default base -- it's not safe
|
---|
69 |
|
---|
70 | // I figure 3 bits is plenty -- otherwise we'd be dealing with
|
---|
71 | // REALLY big arrays!
|
---|
72 | bits = state.parameters.getIntWithMax(base.push(P_NUMBITS),null,1,3);
|
---|
73 | if (bits<1)
|
---|
74 | state.output.fatal("The number of bits for Multiplexer must be between 1 and 3 inclusive");
|
---|
75 |
|
---|
76 | // set up our input
|
---|
77 | input = (MultiplexerData) state.parameters.getInstanceForParameterEq(
|
---|
78 | base.push(P_DATA),null, MultiplexerData.class);
|
---|
79 | input.setup(state,base.push(P_DATA));
|
---|
80 | }
|
---|
81 |
|
---|
82 |
|
---|
83 | public void evaluate(final EvolutionState state,
|
---|
84 | final Individual ind,
|
---|
85 | final int subpopulation,
|
---|
86 | final int threadnum)
|
---|
87 | {
|
---|
88 | input.status = (byte)bits;
|
---|
89 |
|
---|
90 | if (!ind.evaluated) // don't bother reevaluating
|
---|
91 | {
|
---|
92 | int sum = 0;
|
---|
93 |
|
---|
94 | ((GPIndividual)ind).trees[0].child.eval(
|
---|
95 | state,threadnum,input,stack,((GPIndividual)ind),this);
|
---|
96 |
|
---|
97 | if (bits==1)
|
---|
98 | {
|
---|
99 | byte item1 = input.dat_3;
|
---|
100 | byte item2 = Fast.M_3[Fast.M_3_OUTPUT];
|
---|
101 | for(int y=0;y<MultiplexerData.MULTI_3_BITLENGTH;y++)
|
---|
102 | {
|
---|
103 | // if the first bit matches, grab it as:
|
---|
104 | // sum += 1 and not(item1 xor item2)
|
---|
105 | // that is, if item1 and item2 are the SAME at bit 1
|
---|
106 | // then we increase
|
---|
107 | sum += ( 1 & ((item1 ^ item2) ^ (-1)));
|
---|
108 | // shift to the next bit
|
---|
109 | item1 = (byte)(item1 >>> 1);
|
---|
110 | item2 = (byte)(item2 >>> 1);
|
---|
111 | }
|
---|
112 | }
|
---|
113 | else if (bits==2)
|
---|
114 | {
|
---|
115 | long item1 = input.dat_6;
|
---|
116 | long item2 = Fast.M_6[Fast.M_6_OUTPUT];
|
---|
117 | for(int y=0;y<MultiplexerData.MULTI_6_BITLENGTH;y++)
|
---|
118 | {
|
---|
119 | // if the first bit matches, grab it
|
---|
120 | sum += ( 1L & ((item1 ^ item2) ^(-1L)));
|
---|
121 | // shift to the next bit
|
---|
122 | item1 = item1 >>> 1;
|
---|
123 | item2 = item2 >>> 1;
|
---|
124 | }
|
---|
125 | }
|
---|
126 | else // bits==3
|
---|
127 | {
|
---|
128 | long item1, item2;
|
---|
129 | for(int y=0;y<MultiplexerData.MULTI_11_NUM_BITSTRINGS; y++)
|
---|
130 | {
|
---|
131 | item1 = input.dat_11[y];
|
---|
132 | item2 = Fast.M_11[Fast.M_11_OUTPUT][y];
|
---|
133 | //System.out.println("" + y + " ### " + item1 + " " + item2);
|
---|
134 | for(int z=0;z<MultiplexerData.MULTI_11_BITLENGTH;z++)
|
---|
135 | {
|
---|
136 | // if the first bit matches, grab it
|
---|
137 | sum += ( 1L & ((item1 ^ item2) ^(-1L)));
|
---|
138 | // shift to the next bit
|
---|
139 | item1 = item1 >>> 1;
|
---|
140 | item2 = item2 >>> 1;
|
---|
141 | }
|
---|
142 | }
|
---|
143 | }
|
---|
144 |
|
---|
145 | // the fitness better be KozaFitness!
|
---|
146 | KozaFitness f = ((KozaFitness)ind.fitness);
|
---|
147 | if (bits==1)
|
---|
148 | f.setStandardizedFitness(state,(float)(Fast.M_3_SIZE - sum));
|
---|
149 | else if (bits==2)
|
---|
150 | f.setStandardizedFitness(state,(float)(Fast.M_6_SIZE - sum));
|
---|
151 | else // (bits==3)
|
---|
152 | f.setStandardizedFitness(state,(float)(Fast.M_11_SIZE - sum));
|
---|
153 | f.hits = sum;
|
---|
154 | ind.evaluated = true;
|
---|
155 | }
|
---|
156 | }
|
---|
157 | }
|
---|