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.parity;
|
---|
9 | import ec.util.*;
|
---|
10 | import ec.*;
|
---|
11 | import ec.gp.*;
|
---|
12 | import ec.gp.koza.*;
|
---|
13 | import ec.simple.*;
|
---|
14 |
|
---|
15 | /*
|
---|
16 | * Parity.java
|
---|
17 | *
|
---|
18 | * Created: Mon Nov 1 15:46:19 1999
|
---|
19 | * By: Sean Luke
|
---|
20 | */
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * Parity implements the family of <i>n</i>-[even|odd]-Parity problems up
|
---|
24 | * to 32-parity. Read the README file in this package for information on
|
---|
25 | * how to set up the parameter files to your liking -- it's a big family.
|
---|
26 | *
|
---|
27 | * <p>The Parity family evolves a boolean function on <i>n</i> sets of bits,
|
---|
28 | * which returns true if the number of 1's is even (for even-parity) or odd
|
---|
29 | * (for odd-parity), false otherwise.
|
---|
30 | *
|
---|
31 | <p><b>Parameters</b><br>
|
---|
32 | <table>
|
---|
33 | <tr><td valign=top><i>base</i>.<tt>data</tt><br>
|
---|
34 | <font size=-1>classname, inherits or == ec.app.parity.ParityData</font></td>
|
---|
35 | <td valign=top>(the class for the prototypical GPData object for the Parity problem)</td></tr>
|
---|
36 | <tr><td valign=top><i>base</i>.<tt>even</tt><br>
|
---|
37 | <font size=-1> bool = <tt>true</tt> (default) or <tt>false</tt></font></td>
|
---|
38 | <td valign=top>(is this even-parity (as opposed to odd-parity)?)</td></tr>
|
---|
39 | <tr><td valign=top><i>base</i>.<tt>bits</tt><br>
|
---|
40 | <font size=-1> 2 >= int <= 31</font></td>
|
---|
41 | <td valign=top>(The number of data bits)</td></tr>
|
---|
42 | </table>
|
---|
43 |
|
---|
44 | <p><b>Parameter bases</b><br>
|
---|
45 | <table>
|
---|
46 | <tr><td valign=top><i>base</i>.<tt>data</tt></td>
|
---|
47 | <td>species (the GPData object)</td></tr>
|
---|
48 | </table>
|
---|
49 | *
|
---|
50 | * @author Sean Luke
|
---|
51 | * @version 1.0
|
---|
52 | */
|
---|
53 |
|
---|
54 | public class Parity extends GPProblem implements SimpleProblemForm
|
---|
55 | {
|
---|
56 | public static final String P_NUMBITS = "bits";
|
---|
57 | public static final String P_EVEN = "even";
|
---|
58 |
|
---|
59 | public boolean doEven;
|
---|
60 | public int numBits;
|
---|
61 | public int totalSize;
|
---|
62 |
|
---|
63 | public int bits; // data bits
|
---|
64 |
|
---|
65 | // we'll need to deep clone this one though.
|
---|
66 | public ParityData input;
|
---|
67 |
|
---|
68 | public Object clone()
|
---|
69 | {
|
---|
70 | Parity myobj = (Parity) (super.clone());
|
---|
71 | myobj.input = (ParityData)(input.clone());
|
---|
72 | return myobj;
|
---|
73 | }
|
---|
74 |
|
---|
75 | public void setup(final EvolutionState state,
|
---|
76 | final Parameter base)
|
---|
77 | {
|
---|
78 | // very important, remember this
|
---|
79 | super.setup(state,base);
|
---|
80 |
|
---|
81 | // not using a default base here
|
---|
82 |
|
---|
83 | // can't use all 32 bits -- Java is signed. Must use 31 bits.
|
---|
84 |
|
---|
85 | numBits = state.parameters.getIntWithMax(base.push(P_NUMBITS),null,2,31);
|
---|
86 | if (numBits<2)
|
---|
87 | state.output.fatal("The number of bits for Parity must be between 2 and 31 inclusive",base.push(P_NUMBITS));
|
---|
88 |
|
---|
89 | totalSize = 1;
|
---|
90 | for(int x=0;x<numBits;x++)
|
---|
91 | totalSize *=2; // safer than Math.pow()
|
---|
92 |
|
---|
93 | doEven = state.parameters.getBoolean(base.push(P_EVEN),null,true);
|
---|
94 |
|
---|
95 | // set up our input
|
---|
96 | input = (ParityData) state.parameters.getInstanceForParameterEq(
|
---|
97 | base.push(P_DATA),null, ParityData.class);
|
---|
98 | input.setup(state,base.push(P_DATA));
|
---|
99 | }
|
---|
100 |
|
---|
101 |
|
---|
102 | public void evaluate(final EvolutionState state,
|
---|
103 | final Individual ind,
|
---|
104 | final int subpopulation,
|
---|
105 | final int threadnum)
|
---|
106 | {
|
---|
107 | if (!ind.evaluated) // don't bother reevaluating
|
---|
108 | {
|
---|
109 | int sum = 0;
|
---|
110 |
|
---|
111 | for(bits=0;bits<totalSize;bits++)
|
---|
112 | {
|
---|
113 | int tb = 0;
|
---|
114 | // first, is #bits even or odd?
|
---|
115 | for(int b=0;b<numBits;b++)
|
---|
116 | tb += (bits >>> b) & 1;
|
---|
117 | tb &= 1; // now tb is 1 if we're odd, 0 if we're even
|
---|
118 |
|
---|
119 | ((GPIndividual)ind).trees[0].child.eval(
|
---|
120 | state,threadnum,input,stack,((GPIndividual)ind),this);
|
---|
121 |
|
---|
122 | if ((doEven && ((input.x & 1) != tb)) ||
|
---|
123 | ((!doEven) && ((input.x & 1) == tb)))
|
---|
124 | sum++;
|
---|
125 | }
|
---|
126 |
|
---|
127 | // the fitness better be KozaFitness!
|
---|
128 | KozaFitness f = ((KozaFitness)ind.fitness);
|
---|
129 | f.setStandardizedFitness(state,(float)(totalSize - sum));
|
---|
130 | f.hits = sum;
|
---|
131 | ind.evaluated = true;
|
---|
132 | }
|
---|
133 | }
|
---|
134 | }
|
---|