Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OKBJavaConnector/ECJClient/src/ec/gp/koza/KozaFitness.java @ 12912

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

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

File size: 6.4 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.gp.koza;
9import ec.util.*;
10import ec.*;
11import java.io.*;
12
13/*
14 * KozaFitness.java
15 *
16 * Created: Fri Oct 15 14:26:44 1999
17 * By: Sean Luke
18 */
19
20/**
21 * KozaFitness is a Fitness which stores an individual's fitness as described in
22 * Koza I.  Well, almost.  In KozaFitness, standardized fitness and raw fitness
23 * are considered the same (there are different methods for them, but they return
24 * the same thing).  Standardized fitness ranges from 0.0 inclusive (the best)
25 * to infinity exclusive (the worst).  Adjusted fitness converts this, using
26 * the formula adj_f = 1/(1+f), into a scale from 0.0 exclusive (worst) to 1.0
27 * inclusive (best).  While it's the standardized fitness that is stored, it
28 * is the adjusted fitness that is printed out.
29 * This is all just convenience stuff anyway; selection methods
30 * generally don't use these fitness values but instead use the betterThan
31 * and equalTo methods.
32 *
33 <p><b>Default Base</b><br>
34 gp.koza.fitness
35 *
36 *
37 * @author Sean Luke
38 * @version 1.0
39 */
40
41public class KozaFitness extends Fitness
42    {
43    public static final String P_KOZAFITNESS = "fitness";
44
45    /** This ranges from 0 (best) to infinity (worst).    I
46        define it here as equivalent to the standardized fitness. */
47    protected float standardizedFitness;
48
49    /** This auxillary measure is used in some problems for additional
50        information.  It's a traditional feature of Koza-style GP, and so
51        although I think it's not very useful, I'll leave it in anyway. */
52    public int hits;
53
54    public Parameter defaultBase()
55        {
56        return GPKozaDefaults.base().push(P_KOZAFITNESS);
57        }
58       
59    /**
60       Do not use this function.  Use the identical setStandardizedFitness() instead.
61       The reason for the name change is that fitness() returns a differently-defined
62       value than setFitness() sets, ugh.
63       @deprecated
64    */
65    public final void setFitness(final EvolutionState state, final float _f)
66        {
67        setStandardizedFitness(state,_f);
68        }
69
70    /** Set the standardized fitness in the half-open interval [0.0,infinity)
71        which is defined (NOTE: DIFFERENT FROM fitness()!!!) as 0.0
72        being the IDEAL and infinity being worse than the worst possible.
73        This is the GP tradition.  The fitness() function instead will output
74        the equivalent of Adjusted Fitness.
75    */
76    public final void setStandardizedFitness(final EvolutionState state, final float _f)
77        {
78        if (_f < 0.0f || _f == Float.POSITIVE_INFINITY || Float.isNaN(_f))
79            {
80            state.output.warning("Bad fitness (may not be < 0, NaN, or infinity): " + _f  + ", setting to 0.");
81            standardizedFitness = 0;
82            }
83        else standardizedFitness = _f;
84        }
85
86    /** Returns the adjusted fitness metric, which recasts the
87        fitness to the half-open interval (0,1], where 1 is ideal and
88        0 is worst.  Same as adjustedFitness().  */
89
90    public final float fitness()
91        {
92        return 1.0f/(1.0f+standardizedFitness);     
93        }
94
95    /** Returns the raw fitness metric. 
96        @deprecated use standardizedFitness()
97    */
98    public final float rawFitness()
99        {
100        return standardizedFitness();
101        }
102
103    /** Returns the standardized fitness metric. */
104
105    public final float standardizedFitness()
106        {
107        return standardizedFitness;
108        }
109
110    /** Returns the adjusted fitness metric, which recasts the fitness
111        to the half-open interval (0,1], where 1 is ideal and 0 is worst.
112        This metric is used when printing the fitness out. */
113
114    public final float adjustedFitness()
115        {
116        return fitness();
117        }
118
119    public void setup(final EvolutionState state, final Parameter base) { }
120   
121    public final boolean isIdealFitness()
122        {
123        return standardizedFitness == 0.0f;
124        }
125   
126    public boolean equivalentTo(final Fitness _fitness)
127        {
128        return _fitness.fitness() == fitness();
129        }
130
131    public boolean betterThan(final Fitness _fitness)
132        {
133        return _fitness.fitness() < fitness();
134        }
135 
136    public String fitnessToString()
137        {
138        return FITNESS_PREAMBLE + Code.encode(standardizedFitness) + Code.encode(hits);
139        }
140       
141    public String fitnessToStringForHumans()
142        {
143        return FITNESS_PREAMBLE + "Standardized=" + standardizedFitness + " Adjusted=" + adjustedFitness() + " Hits=" + hits;
144        }
145           
146    public final void readFitness(final EvolutionState state,
147        final LineNumberReader reader)
148        throws IOException
149        {
150        DecodeReturn d = Code.checkPreamble(FITNESS_PREAMBLE, state, reader);
151       
152        // extract fitness
153        Code.decode(d);
154        if (d.type!=DecodeReturn.T_FLOAT)
155            state.output.fatal("Reading Line " + d.lineNumber + ": " +
156                "Bad Fitness.");
157        standardizedFitness = (float)d.d;
158       
159        // extract hits
160        Code.decode(d);
161        if (d.type!=DecodeReturn.T_INT)
162            state.output.fatal("Reading Line " + d.lineNumber + ": " +
163                "Bad Fitness.");
164        hits = (int)d.l;
165        }
166
167    public void writeFitness(final EvolutionState state,
168        final DataOutput dataOutput) throws IOException
169        {
170        dataOutput.writeFloat(standardizedFitness);
171        dataOutput.writeInt(hits);
172        }
173
174    public void readFitness(final EvolutionState state,
175        final DataInput dataInput) throws IOException
176        {
177        standardizedFitness = dataInput.readFloat();
178        hits = dataInput.readInt();
179        }
180
181    public void setToMeanOf(EvolutionState state, Fitness[] fitnesses)
182        {
183        // this is not numerically stable.  Perhaps we should have a numerically stable algorithm for sums
184        // we're presuming it's not a very large number of elements, so it's probably not a big deal,
185        // since this function is meant to be used mostly for gathering trials together.
186        double f = 0;
187        long h = 0;
188        for(int i = 0; i < fitnesses.length; i++)
189            {
190            KozaFitness fit = (KozaFitness)(fitnesses[i]);
191            f += fit.standardizedFitness;
192            h += fit.hits;
193            }
194        f /= fitnesses.length;
195        h /= fitnesses.length;
196        standardizedFitness = (float)f;
197        hits = (int)h;
198        }
199    }
Note: See TracBrowser for help on using the repository browser.