Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OKBJavaConnector/ECJClient/src/ec/Population.java @ 7611

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

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

File size: 9.5 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;
9import ec.util.*;
10import java.io.*;
11
12/*
13 * Population.java
14 *
15 * Created: Tue Aug 10 20:50:54 1999
16 * By: Sean Luke
17 */
18
19/**
20 * A Population is the repository for all the Individuals being bred or
21 * evaluated in the evolutionary run at a given time.
22 * A Population is basically an array of Subpopulations, each of which
23 * are arrays of Individuals coupled with a single Species per Subpoulation.
24 *
25 * <p>The first Population is created using the initializePopulation method
26 * of the Initializer object, which typically calls the Population's
27 * populate() method in turn.  On generational systems, subsequent populations
28 * are created on a generation-by-generation basis by the Breeder object,
29 * replacing the previous Population.
30 *
31 * <p>In a multithreaded area of a run, Populations should be considered
32 * immutable.  That is, once they are created, they should not be modified,
33 * nor anything they contain.  This protocol helps ensure read-safety under
34 * multithreading race conditions.
35 *
36
37 <p><b>Parameters</b><br>
38 <table>
39 <tr><td valign=top><i>base.</i><tt>subpops</tt><br>
40 <font size=-1>int &gt;= 1</font></td>
41 <td valign=top>(the number of subpopulations)</td></tr>
42
43 <tr><td valign=top><i>base.</i><tt>subpop</tt><i>.n</i><br>
44 <font size=-1>classname, inherits or = ec.Subpopulation</font></td>
45 <td valign=top>(the class for subpopulation #<i>n</i>)</td></tr>
46
47 <tr><td valign=top><i>base.</i><tt>default-subpop</tt><br>
48 <font size=-1>classname, inherits or = ec.Subpopulation</font></td>
49 <td valign=top>(the class for subpopulation #<i>n</i> if it wasn't specified with <i>base.</i><tt>subpop</tt><i>.n</i>. 
50 Don't use this except in unusual circumstances.  The parameter base is still <i>base.</i><tt>subpop</tt><i>.n</i>.</td></tr>
51 </table>
52
53 <p><b>Parameter bases</b><br>
54 <table>
55 <tr><td valign=top><i>base.</i><tt>subpop</tt><i>.n</i></td>
56 <td>Subpopulation #<i>n</i>.</td></tr>
57 </table>
58 *
59 * @author Sean Luke
60 * @version 1.0
61 */
62
63public class Population implements Group
64    {
65    public Subpopulation[] subpops;
66    public static final String P_SIZE = "subpops";
67    public static final String P_SUBPOP = "subpop";
68    public static final String P_DEFAULT_SUBPOP = "default-subpop";
69    public static final String NUM_SUBPOPS_PREAMBLE = "Number of Subpopulations: ";
70    public static final String SUBPOP_INDEX_PREAMBLE = "Subpopulation Number: ";
71
72
73    /** Returns an instance of Population just like it had been before it was
74        populated with individuals. You may need to override this if you override
75        Population. <b>IMPORTANT NOTE</b>: if the size of the array in
76        Population has been changed, then the clone will take on the new array
77        size.  This helps some evolution strategies.
78        @see Group#emptyClone()
79    */
80
81    public Group emptyClone()
82        {
83        try
84            {
85            Population p = (Population)clone();
86            p.subpops = new Subpopulation[subpops.length];
87            for(int x=0;x<subpops.length;x++)
88                p.subpops[x] = (Subpopulation)(subpops[x].emptyClone());
89            return p;   
90            }
91        catch (CloneNotSupportedException e) { throw new InternalError(); } // never happens
92        }
93
94    public void setup(final EvolutionState state, final Parameter base)
95        {
96        // how big should subpops be?  Don't have a default base
97
98        Parameter p;
99
100        p = base.push(P_SIZE);
101        int size = state.parameters.getInt(p,null,1);
102        if (size==0) // uh oh
103            state.output.fatal("Population size must be >0.\n",base.push(P_SIZE));
104        subpops = new Subpopulation[size];
105
106        // Load the subpopulations
107        for (int x=0;x<size;x++)
108            {
109            p = base.push(P_SUBPOP).push(""+x);
110            if (!state.parameters.exists(p,null))
111                {
112                p = base.push(P_DEFAULT_SUBPOP);
113                if (state.parameters.exists(p, null))
114                    {
115                    state.output.warning("Class for subpopulation " + x + " not specified, using provided default: " + state.parameters.getString(p, null));
116                    }
117                // else an error will occur on the next line anyway.
118                }
119            subpops[x] = (Subpopulation)(state.parameters.getInstanceForParameterEq(p,null,Subpopulation.class));  // Subpopulation.class is fine
120            subpops[x].setup(state,p);
121            }
122        }
123
124    /** Populates the population with new random individuals. */
125    public void populate(EvolutionState state, int thread)
126        {
127        // let's populate!
128        for(int x=0;x<subpops.length;x++)
129            subpops[x].populate(state, thread);
130        }
131       
132       
133    /** Prints an entire population in a form readable by humans.
134        @deprecated Verbosity no longer has meaning
135    */
136    public final void printPopulationForHumans(final EvolutionState state,
137        final int log,
138        final int verbosity)
139        {
140        printPopulationForHumans(state, log);
141        }
142       
143    /** Prints an entire population in a form readable by humans but also parseable by the computer using readPopulation(EvolutionState, LineNumberReader).
144        @deprecated Verbosity no longer has meaning
145    */
146    public final void printPopulation(final EvolutionState state,
147        final int log,
148        final int verbosity)
149        {
150        printPopulation(state, log);
151        }
152       
153    /** Prints an entire population in a form readable by humans, with a verbosity of Output.V_NO_GENERAL. */
154    public void printPopulationForHumans(final EvolutionState state,
155        final int log)
156        {
157        state.output.println(NUM_SUBPOPS_PREAMBLE + subpops.length,  log);
158        for(int i = 0 ; i < subpops.length; i++)
159            {
160            state.output.println(SUBPOP_INDEX_PREAMBLE + i,  log);
161            subpops[i].printSubpopulationForHumans(state, log);
162            }
163        }
164       
165    /** Prints an entire population in a form readable by humans but also parseable by the computer using readPopulation(EvolutionState, LineNumberReader), with a verbosity of Output.V_NO_GENERAL. */
166    public void printPopulation(final EvolutionState state,
167        final int log)
168        {
169        state.output.println(NUM_SUBPOPS_PREAMBLE + Code.encode(subpops.length),  log);
170        for(int i = 0 ; i < subpops.length; i++)
171            {
172            state.output.println(SUBPOP_INDEX_PREAMBLE + Code.encode(i),  log);
173            subpops[i].printSubpopulation(state, log);
174            }
175        }
176       
177    /** Prints an entire population in a form readable by humans but also parseable by the computer using readPopulation(EvolutionState, LineNumberReader). */
178    public void printPopulation(final EvolutionState state,
179        final PrintWriter writer)
180        {
181        writer.println(NUM_SUBPOPS_PREAMBLE + Code.encode(subpops.length));
182        for(int i = 0 ; i < subpops.length; i++)
183            {
184            writer.println(SUBPOP_INDEX_PREAMBLE + Code.encode(i));         
185            subpops[i].printSubpopulation(state, writer);
186            }
187        }
188   
189    /** Reads a population from the format generated by printPopulation(....).  The number of subpopulations and the species information must be identical. */
190    public void readPopulation(final EvolutionState state,
191        final LineNumberReader reader) throws IOException
192        {
193        // read the number of subpops and check to see if this appears to be a valid individual
194        int numSubpops = Code.readIntegerWithPreamble(NUM_SUBPOPS_PREAMBLE, state, reader);
195       
196        // read in subpops
197        if (numSubpops != subpops.length)  // definitely wrong
198            state.output.fatal("On reading population from text stream, the number of subpopulations was wrong.");
199
200        for(int i = 0 ; i < subpops.length; i++)
201            {
202            int j = Code.readIntegerWithPreamble(SUBPOP_INDEX_PREAMBLE, state, reader);
203            // sanity check
204            if (j!=i) state.output.warnOnce("On reading population from text stream, some subpopulation indexes in the population did not match.");
205            subpops[i].readSubpopulation(state, reader);
206            }
207        }
208   
209    /** Writes a population in binary form, in a format readable by readPopulation(EvolutionState, DataInput). */
210    public void writePopulation(final EvolutionState state,
211        final DataOutput dataOutput) throws IOException
212        {
213        dataOutput.writeInt(subpops.length);
214        for(int i = 0 ; i < subpops.length; i++)
215            subpops[i].writeSubpopulation(state, dataOutput);
216        }
217   
218    /** Reads a population in binary form, from the format generated by writePopulation(...). The number of subpopulations and the species information must be identical. */
219    public void readPopulation(final EvolutionState state,
220        final DataInput dataInput) throws IOException
221        {
222        int numSubpopulations = dataInput.readInt();
223        if (numSubpopulations != subpops.length)
224            state.output.fatal("On reading subpopulation from binary stream, the number of subpopulations was wrong.");
225
226        for(int i = 0 ; i < subpops.length; i++)
227            subpops[i].readSubpopulation(state, dataInput);
228        }
229
230
231    }
Note: See TracBrowser for help on using the repository browser.