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.regression.func;
|
---|
9 | import ec.*;
|
---|
10 | import ec.app.regression.*;
|
---|
11 | import ec.gp.*;
|
---|
12 | import ec.util.*;
|
---|
13 | import java.io.*;
|
---|
14 |
|
---|
15 |
|
---|
16 | /*
|
---|
17 | * RegERC.java
|
---|
18 | *
|
---|
19 | * Created: Wed Nov 3 18:26:37 1999
|
---|
20 | * By: Sean Luke
|
---|
21 | */
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * @author Sean Luke
|
---|
25 | * @version 1.0
|
---|
26 | */
|
---|
27 |
|
---|
28 | public class RegERC extends ERC
|
---|
29 | {
|
---|
30 | public double value;
|
---|
31 |
|
---|
32 | // making sure that we don't have any children is already
|
---|
33 | // done in ERC.checkConstraints(), so we don't need to implement that.
|
---|
34 |
|
---|
35 | // this will produce numbers from [-1.0, 1.0), which is probably
|
---|
36 | // okay but you might want to modify it if you don't like seeing
|
---|
37 | // -1.0's occasionally showing up very rarely.
|
---|
38 | public void resetNode(final EvolutionState state, final int thread)
|
---|
39 | { value = state.random[thread].nextDouble() * 2 - 1.0; }
|
---|
40 |
|
---|
41 | public int nodeHashCode()
|
---|
42 | {
|
---|
43 | // a reasonable hash code
|
---|
44 | return this.getClass().hashCode() + Float.floatToIntBits((float)value);
|
---|
45 | }
|
---|
46 |
|
---|
47 | public boolean nodeEquals(final GPNode node)
|
---|
48 | {
|
---|
49 | // check first to see if we're the same kind of ERC --
|
---|
50 | // won't work for subclasses; in that case you'll need
|
---|
51 | // to change this to isAssignableTo(...)
|
---|
52 | if (this.getClass() != node.getClass()) return false;
|
---|
53 | // now check to see if the ERCs hold the same value
|
---|
54 | return (((RegERC)node).value == value);
|
---|
55 | }
|
---|
56 |
|
---|
57 | public void readNode(final EvolutionState state, final DataInput dataInput) throws IOException
|
---|
58 | {
|
---|
59 | value = dataInput.readDouble();
|
---|
60 | }
|
---|
61 |
|
---|
62 | public void writeNode(final EvolutionState state, final DataOutput dataOutput) throws IOException
|
---|
63 | {
|
---|
64 | dataOutput.writeDouble(value);
|
---|
65 | }
|
---|
66 |
|
---|
67 | public String encode()
|
---|
68 | { return Code.encode(value); }
|
---|
69 |
|
---|
70 | public boolean decode(DecodeReturn dret)
|
---|
71 | {
|
---|
72 | // store the position and the string in case they
|
---|
73 | // get modified by Code.java
|
---|
74 | int pos = dret.pos;
|
---|
75 | String data = dret.data;
|
---|
76 |
|
---|
77 | // decode
|
---|
78 | Code.decode(dret);
|
---|
79 |
|
---|
80 | if (dret.type != DecodeReturn.T_DOUBLE) // uh oh!
|
---|
81 | {
|
---|
82 | // restore the position and the string; it was an error
|
---|
83 | dret.data = data;
|
---|
84 | dret.pos = pos;
|
---|
85 | return false;
|
---|
86 | }
|
---|
87 |
|
---|
88 | // store the data
|
---|
89 | value = dret.d;
|
---|
90 | return true;
|
---|
91 | }
|
---|
92 |
|
---|
93 | public String toStringForHumans()
|
---|
94 | { return "" + (float)value; }
|
---|
95 |
|
---|
96 | public void eval(final EvolutionState state,
|
---|
97 | final int thread,
|
---|
98 | final GPData input,
|
---|
99 | final ADFStack stack,
|
---|
100 | final GPIndividual individual,
|
---|
101 | final Problem problem)
|
---|
102 | {
|
---|
103 | RegressionData rd = ((RegressionData)(input));
|
---|
104 | rd.x = value;
|
---|
105 | }
|
---|
106 | }
|
---|
107 |
|
---|
108 |
|
---|
109 |
|
---|