Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OKBJavaConnector/ECJClient/src/ec/simple/SimpleFitness.java @ 12912

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

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

File size: 4.7 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.simple;
9import ec.Fitness;
10import ec.EvolutionState;
11import ec.util.*;
12import java.io.*;
13
14/*
15 * SimpleFitness.java
16 *
17 * Created: Tue Aug 10 20:10:42 1999
18 * By: Sean Luke
19 */
20
21/**
22 * A simple default fitness, consisting of a single floating-point value
23 * where fitness A is superior to fitness B if and only if A > B. 
24 * Fitness values may range from (-infinity,infinity) exclusive -- that is,
25 * you may not have infinite fitnesses. 
26 *
27 * <p>Some kinds of selection methods require a more stringent definition of
28 * fitness.  For example, FitProportionateSelection requires that fitnesses
29 * be non-negative (since it must place them into a proportionate distribution).
30 * You may wish to restrict yourself to values in [0,1] or [0,infinity) in
31 * such cases.
32 *
33 <p><b>Default Base</b><br>
34 simple.fitness
35
36 * @author Sean Luke
37 * @version 1.0
38 */
39
40public class SimpleFitness extends Fitness
41    {
42    protected float fitness;
43    protected boolean isIdeal;
44
45    public Parameter defaultBase()
46        {
47        return SimpleDefaults.base().push(P_FITNESS);
48        }
49
50    /**
51       Deprecated -- now redefined to set the fitness but ALWAYS say that it's not ideal.
52       If you need to specify that it's ideal, you should use the new function
53       setFitness(final EvolutionState state, float _f, boolean _isIdeal).
54       @deprecated
55    */
56    public void setFitness(final EvolutionState state, float _f)
57        {
58        setFitness(state,_f,false);
59        }
60       
61    public void setFitness(final EvolutionState state, float _f, boolean _isIdeal)
62        {
63        // we now allow f to be *any* value, positive or negative
64        if (_f == Float.POSITIVE_INFINITY || _f == Float.NEGATIVE_INFINITY || Float.isNaN(_f))
65            {
66            state.output.warning("Bad fitness: " + _f + ", setting to 0.");
67            fitness = 0;
68            }
69        else fitness = _f;
70        isIdeal = _isIdeal;
71        }
72
73    public float fitness()
74        {
75        return fitness;
76        }
77
78    public void setup(final EvolutionState state, Parameter base)
79        {
80        super.setup(state,base);  // unnecessary but what the heck
81        }
82
83    public boolean isIdealFitness()
84        {
85        return isIdeal;
86        }
87
88    public boolean equivalentTo(final Fitness _fitness)
89        {
90        return _fitness.fitness() == fitness();
91        }
92
93    public boolean betterThan(final Fitness _fitness)
94        {
95        return _fitness.fitness() < fitness();
96        }
97
98    public String fitnessToString()
99        {
100        return FITNESS_PREAMBLE + Code.encode(fitness());
101        }
102       
103    public String fitnessToStringForHumans()
104        {
105        return FITNESS_PREAMBLE + fitness();
106        }
107
108    /** Presently does not decode the fact that the fitness is ideal or not */
109    public void readFitness(final EvolutionState state,
110        final LineNumberReader reader)
111        throws IOException
112        {
113        setFitness(state, Code.readFloatWithPreamble(FITNESS_PREAMBLE, state, reader));
114
115/*
116  int linenumber = reader.getLineNumber();
117  String s = reader.readLine();
118  if (s==null || s.length() < FITNESS_PREAMBLE.length()) // uh oh
119  state.output.fatal("Reading Line " + linenumber + ": " +
120  "Bad Fitness.");
121  DecodeReturn d = new DecodeReturn(s, FITNESS_PREAMBLE.length());
122  Code.decode(d);
123  if (d.type!=DecodeReturn.T_FLOAT)
124  state.output.fatal("Reading Line " + linenumber + ": " +
125  "Bad Fitness.");
126  setFitness(state,(float)d.d,false);
127*/
128        }
129
130    public void writeFitness(final EvolutionState state,
131        final DataOutput dataOutput) throws IOException
132        {
133        dataOutput.writeFloat(fitness);
134        dataOutput.writeBoolean(isIdeal);
135        }
136
137    public void readFitness(final EvolutionState state,
138        final DataInput dataInput) throws IOException
139        {
140        fitness = dataInput.readFloat();
141        isIdeal = dataInput.readBoolean();
142        }
143
144    public void setToMeanOf(EvolutionState state, Fitness[] fitnesses)
145        {
146        // this is not numerically stable.  Perhaps we should have a numerically stable algorithm for sums
147        // we're presuming it's not a very large number of elements, so it's probably not a big deal,
148        // since this function is meant to be used mostly for gathering trials together.
149        double f = 0;
150        boolean ideal = true;
151        for(int i = 0; i < fitnesses.length; i++)
152            {
153            SimpleFitness fit = (SimpleFitness)(fitnesses[i]);
154            f += fit.fitness;
155            ideal = ideal && fit.isIdeal;
156            }
157        f /= fitnesses.length;
158        fitness = (float)f;
159        isIdeal = ideal;
160        }
161    }
Note: See TracBrowser for help on using the repository browser.