Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OKBJavaConnector/ECJClient/src/ec/pso/PSOSubpopulation.java @ 10501

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

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

File size: 17.4 KB
Line 
1/*
2  Copyright 2006 by Ankur Desai, Sean Luke, and George Mason University
3  Licensed under the Academic Free License version 3.0
4  See the file "LICENSE" for more information
5*/
6
7package ec.pso;
8
9import ec.*;
10import ec.util.*;
11import ec.vector.*;
12import java.io.*;
13
14/**
15 * PSOSubpopulation.java
16 *
17 
18 <p>Particle Swarm Optimization (PSO) is a population-oriented stochastic search
19 technique similar to genetic algorithms, evolutionary strategies, and other evolutionary
20 computation algorithms. The technique discovers solutions for N-dimensional
21 parameterized problems: basically it discovers the point in N-dimensional space which
22 maximizes some quality function.
23   
24 <p>PSOSubpopulation handles initialization and input/output of the swarm.   
25 
26 <p><b>Parameters</b><br>
27 <table>
28 
29 <tr><td valign=top><i>base</i>.<tt>neighborhood-size</tt><br>
30 <font size=-1>integer</font></td>
31 <td valign=top>(the number of individuals per neighborhood)<br></td></tr>
32 
33 <tr><td valign=top><i>base</i>.<tt>clamp</tt><br>
34 <font size=-1>boolean</font></td>
35 <td valign=top>(clamp the individual to stay within the bounds or not)<br></td></tr>
36 
37 <tr><td valign=top><i>base</i>.<tt>initial-velocity-scale</tt><br>
38 <font size=-1>double</font></td>
39 <td valign=top>(particles are initialized with a random velocity and this value provides bounds.
40 A value of 1.0 means that the velocity will be within +/- the range of the genotype.)<br></td></tr>
41
42 <tr><td valign=top><i>base</i>.<tt>velocity-multiplier</tt><br>
43 <font size=-1>double</font></td>
44 <td valign=top>(particle velocities are multiplied by this value before the particle is updated.
45 Increasing this value helps particles to escape local optima, but slows convergence. The default
46 value of 1.5 is geared toward multi-modal landscapes.)<br></td></tr>
47 
48 </table>
49 
50 <p><b>Parameter bases</b><br>
51 <table>
52 <tr><td valign=top><i>base</i>.<tt>data</tt></td>
53 <td>Subpopulation</td></tr>
54 </table>
55 
56 * @author Joey Harrison, Ankur Desai
57 * @version 1.0
58 */
59public class PSOSubpopulation extends Subpopulation
60    {
61    public int neighborhoodSize;       
62    public static final String P_NEIGHBORHOOD_SIZE = "neighborhood-size";
63       
64    public boolean clampRange;
65    public static final String P_CLAMP_RANGE = "clamp";
66       
67    public double initialVelocityScale;
68    public static final String P_INITIAL_VELOCITY_SCALE = "initial-velocity-scale";
69   
70    public double velocityMultiplier;
71    public static final String P_VELOCITY_MULTIPLIER = "velocity-multiplier";
72       
73    public DoubleVectorIndividual globalBest;
74    public DoubleVectorIndividual[] neighborhoodBests;
75    public DoubleVectorIndividual[] personalBests;
76    public DoubleVectorIndividual[] previousIndividuals;
77   
78    public static final String GLOBAL_BEST_PREAMBLE = "Global-Best Individual: ";
79    public static final String NEIGHBORHOOD_BEST_PREAMBLE = "Neighborhood Best Individuals: ";
80    public static final String PERSONAL_BEST_PREAMBLE = "Personal Best Individuals ";
81    public static final String PREVIOUS_INDIVIDUAL_PREAMBLE = "Previous Individuals ";
82    public static final String INDIVIDUAL_EXISTS_PREAMBLE = "Exists: ";
83
84    public void setup(final EvolutionState state, final Parameter base)
85        {
86        super.setup(state, base);
87       
88        if (!(species instanceof FloatVectorSpecies))
89            state.output.error("PSOSubpopulation requires that its species is ec.vector.FloatVectorSpecies or a subclass.  Yours is: " + species.getClass(),
90                null,null);
91        if (!(species.i_prototype instanceof DoubleVectorIndividual))
92            state.output.error("PSOSubpopulation requires that its species' prototypical individual be is ec.vector.DoubleVectorSpecies or a subclass.  Yours is: " + species.getClass(),
93                null,null);
94       
95        neighborhoodBests = new DoubleVectorIndividual[individuals.length];
96        personalBests = new DoubleVectorIndividual[individuals.length];
97        previousIndividuals = new DoubleVectorIndividual[individuals.length];
98       
99        neighborhoodSize = state.parameters.getInt(base.push(P_NEIGHBORHOOD_SIZE), null);
100        clampRange = state.parameters.getBoolean(base.push(P_CLAMP_RANGE), null, false);
101        initialVelocityScale = state.parameters.getDouble(base.push(P_INITIAL_VELOCITY_SCALE), null,0);
102        velocityMultiplier = state.parameters.getDouble(base.push(P_VELOCITY_MULTIPLIER), null,0.1);
103        }
104   
105    void clear(DoubleVectorIndividual[] inds)
106        {
107        for(int x=0;x<inds.length;x++) { inds[x] = null; }
108        }
109       
110    public void populate(EvolutionState state, int thread)
111        {
112        super.populate(state, thread);
113       
114        if (loadInds == null)  // we're generating new individuals, not reading them from a file
115            {
116            clear(neighborhoodBests);
117            clear(personalBests);
118       
119            FloatVectorSpecies fvSpecies = (FloatVectorSpecies)species;
120            /* double range = fvSpecies.maxGene - fvSpecies.minGene; */
121                   
122            for (int i = 0; i < individuals.length; i++)
123                {
124                DoubleVectorIndividual prevInd = (DoubleVectorIndividual)individuals[i].clone();
125                                   
126                // pick a genome near prevInd but not outside the box
127                for(int j = 0; j < prevInd.genomeLength(); j++)
128                    {
129                    double val = prevInd.genome[j];
130                    double range = fvSpecies.maxGene(j) - fvSpecies.minGene(j);
131                    do
132                        prevInd.genome[j] = val + (range * initialVelocityScale) * (state.random[thread].nextDouble()*2.0 - 1.0);
133                    while (prevInd.genome[j] < fvSpecies.minGene(j) || prevInd.genome[j] > fvSpecies.maxGene(j));
134                    }
135                previousIndividuals[i] = prevInd;
136                }
137            }
138        }
139
140    /** Overridden to include the global best, neighborhood bests, personal bests, and previous individuals in the stream.
141        The neighborhood size, clamp range, and initial velocity scale are not included -- it's assumed you're using the
142        same values for them on reading, or understand that the values are revised. */
143    public void printSubpopulationForHumans(final EvolutionState state,
144        final int log)
145        {
146        // global best
147        state.output.println(GLOBAL_BEST_PREAMBLE, log);
148        if (globalBest == null)
149            state.output.println(INDIVIDUAL_EXISTS_PREAMBLE + "false", log);
150        else
151            {
152            state.output.println(INDIVIDUAL_EXISTS_PREAMBLE + "true", log);
153            globalBest.printIndividualForHumans(state, log);
154            }
155       
156        // neighborhoodBests
157        state.output.println(NEIGHBORHOOD_BEST_PREAMBLE, log);
158        for(int i = 0; i < individuals.length; i++)
159            if (neighborhoodBests[i] == null)
160                state.output.println(INDIVIDUAL_EXISTS_PREAMBLE + "false", log);
161            else
162                {
163                state.output.println(INDIVIDUAL_EXISTS_PREAMBLE + "true", log);
164                neighborhoodBests[i].printIndividualForHumans(state, log);
165                }
166           
167        // personalBests
168        state.output.println(PERSONAL_BEST_PREAMBLE, log);
169        for(int i = 0; i < individuals.length; i++)
170            if (personalBests[i] == null)
171                state.output.println(INDIVIDUAL_EXISTS_PREAMBLE + "false", log);
172            else
173                {
174                state.output.println(INDIVIDUAL_EXISTS_PREAMBLE + "true", log);
175                personalBests[i].printIndividualForHumans(state, log);
176                }
177
178        // neighborhoodBests
179        state.output.println(PREVIOUS_INDIVIDUAL_PREAMBLE, log);
180        for(int i = 0; i < individuals.length; i++)
181            if (previousIndividuals[i] == null)
182                state.output.println(INDIVIDUAL_EXISTS_PREAMBLE + "false", log);
183            else
184                {
185                state.output.println(INDIVIDUAL_EXISTS_PREAMBLE + "true", log);
186                previousIndividuals[i].printIndividualForHumans(state, log);
187                }
188
189        super.printSubpopulationForHumans(state, log);
190        }
191       
192    /** Overridden to include the global best, neighborhood bests, personal bests, and previous individuals in the stream.
193        The neighborhood size, clamp range, and initial velocity scale are not included -- it's assumed you're using the
194        same values for them on reading, or understand that the values are revised. */
195    public void printSubpopulation(final EvolutionState state,
196        final int log)
197        {
198        // global best
199        state.output.println(GLOBAL_BEST_PREAMBLE, log);
200        if (globalBest == null)
201            state.output.println(INDIVIDUAL_EXISTS_PREAMBLE + Code.encode(false), log);
202        else
203            {
204            state.output.println(INDIVIDUAL_EXISTS_PREAMBLE + Code.encode(true), log);
205            globalBest.printIndividual(state, log);
206            }
207       
208        // neighborhoodBests
209        state.output.println(NEIGHBORHOOD_BEST_PREAMBLE, log);
210        for(int i = 0; i < individuals.length; i++)
211            if (neighborhoodBests[i] == null)
212                state.output.println(INDIVIDUAL_EXISTS_PREAMBLE + Code.encode(false), log);
213            else
214                {
215                state.output.println(INDIVIDUAL_EXISTS_PREAMBLE + Code.encode(true), log);
216                neighborhoodBests[i].printIndividual(state, log);
217                }
218           
219        // personalBests
220        state.output.println(PERSONAL_BEST_PREAMBLE, log);
221        for(int i = 0; i < individuals.length; i++)
222            if (personalBests[i] == null)
223                state.output.println(INDIVIDUAL_EXISTS_PREAMBLE + Code.encode(false), log);
224            else
225                {
226                state.output.println(INDIVIDUAL_EXISTS_PREAMBLE + Code.encode(true), log);
227                personalBests[i].printIndividual(state, log);
228                }
229
230        // neighborhoodBests
231        state.output.println(PREVIOUS_INDIVIDUAL_PREAMBLE, log);
232        for(int i = 0; i < individuals.length; i++)
233            if (previousIndividuals[i] == null)
234                state.output.println(INDIVIDUAL_EXISTS_PREAMBLE + Code.encode(false), log);
235            else
236                {
237                state.output.println(INDIVIDUAL_EXISTS_PREAMBLE + Code.encode(true), log);
238                previousIndividuals[i].printIndividual(state, log);
239                }
240
241        super.printSubpopulation(state, log);
242        }
243       
244    /** Overridden to include the global best, neighborhood bests, personal bests, and previous individuals in the stream.
245        The neighborhood size, clamp range, and initial velocity scale are not included -- it's assumed you're using the
246        same values for them on reading, or understand that the values are revised. */
247    public void printSubpopulation(final EvolutionState state,
248        final PrintWriter writer)
249        {
250        // global best
251        writer.println(GLOBAL_BEST_PREAMBLE);
252        if (globalBest == null)
253            writer.println(INDIVIDUAL_EXISTS_PREAMBLE + Code.encode(false));
254        else
255            {
256            writer.println(INDIVIDUAL_EXISTS_PREAMBLE + Code.encode(true));
257            globalBest.printIndividual(state, writer);
258            }
259       
260        // neighborhoodBests
261        writer.println(NEIGHBORHOOD_BEST_PREAMBLE);
262        for(int i = 0; i < individuals.length; i++)
263            if (neighborhoodBests[i] == null)
264                writer.println(INDIVIDUAL_EXISTS_PREAMBLE + Code.encode(false));
265            else
266                {
267                writer.println(INDIVIDUAL_EXISTS_PREAMBLE + Code.encode(true));
268                neighborhoodBests[i].printIndividual(state, writer);
269                }
270           
271        // personalBests
272        writer.println(PERSONAL_BEST_PREAMBLE);
273        for(int i = 0; i < individuals.length; i++)
274            if (personalBests[i] == null)
275                writer.println(INDIVIDUAL_EXISTS_PREAMBLE + Code.encode(false));
276            else
277                {
278                writer.println(INDIVIDUAL_EXISTS_PREAMBLE + Code.encode(true));
279                personalBests[i].printIndividual(state, writer);
280                }
281
282        // neighborhoodBests
283        writer.println(PREVIOUS_INDIVIDUAL_PREAMBLE);
284        for(int i = 0; i < individuals.length; i++)
285            if (previousIndividuals[i] == null)
286                writer.println(INDIVIDUAL_EXISTS_PREAMBLE + Code.encode(false));
287            else
288                {
289                writer.println(INDIVIDUAL_EXISTS_PREAMBLE + Code.encode(true));
290                previousIndividuals[i].printIndividual(state, writer);
291                }
292
293        super.printSubpopulation(state, writer);
294        }
295   
296    DoubleVectorIndividual possiblyReadIndividual(final EvolutionState state, final LineNumberReader reader) throws IOException
297        {
298        if (Code.readBooleanWithPreamble(INDIVIDUAL_EXISTS_PREAMBLE, state, reader))
299            return (DoubleVectorIndividual)species.newIndividual(state, reader);
300        else return null;
301        }
302   
303    /** Overridden to include the global best, neighborhood bests, personal bests, and previous individuals in the stream.
304        The neighborhood size, clamp range, and initial velocity scale are not included -- it's assumed you're using the
305        same values for them on reading, or understand that the values are revised. */
306    public void readSubpopulation(final EvolutionState state,
307        final LineNumberReader reader) throws IOException
308        {
309        // global best
310        Code.checkPreamble(GLOBAL_BEST_PREAMBLE, state, reader);
311        globalBest = possiblyReadIndividual(state, reader);
312       
313        // neighborhoodBests
314        Code.checkPreamble(NEIGHBORHOOD_BEST_PREAMBLE, state, reader);
315        for(int i = 0; i < individuals.length; i++)
316            neighborhoodBests[i] = possiblyReadIndividual(state, reader);
317           
318        // personalBests
319        Code.checkPreamble(PERSONAL_BEST_PREAMBLE, state, reader);
320        for(int i = 0; i < individuals.length; i++)
321            personalBests[i] = possiblyReadIndividual(state, reader);
322
323        // neighborhoodBests
324        Code.checkPreamble(PREVIOUS_INDIVIDUAL_PREAMBLE, state, reader);
325        for(int i = 0; i < individuals.length; i++)
326            previousIndividuals[i] = possiblyReadIndividual(state, reader);
327       
328        super.readSubpopulation(state, reader);
329        }
330       
331    /** Overridden to include the global best, neighborhood bests, personal bests, and previous individuals in the stream.
332        The neighborhood size, clamp range, and initial velocity scale are not included -- it's assumed you're using the
333        same values for them on reading, or understand that the values are revised. */
334    public void writeSubpopulation(final EvolutionState state,
335        final DataOutput dataOutput) throws IOException
336        {
337        // global best
338        if (globalBest == null)
339            dataOutput.writeBoolean(false);
340        else
341            {
342            dataOutput.writeBoolean(true);
343            globalBest.writeIndividual(state, dataOutput);
344            }
345       
346        // neighborhoodBests
347        for(int i = 0; i < individuals.length; i++)
348            if (neighborhoodBests[i] == null)
349                dataOutput.writeBoolean(false);
350            else
351                {
352                dataOutput.writeBoolean(true);
353                neighborhoodBests[i].writeIndividual(state, dataOutput);
354                }
355           
356        // personalBests
357        for(int i = 0; i < individuals.length; i++)
358            if (personalBests[i] == null)
359                dataOutput.writeBoolean(false);
360            else
361                {
362                dataOutput.writeBoolean(true);
363                personalBests[i].writeIndividual(state, dataOutput);
364                }
365
366        // previous Individuals
367        for(int i = 0; i < individuals.length; i++)
368            if (previousIndividuals[i] == null)
369                dataOutput.writeBoolean(false);
370            else
371                {
372                dataOutput.writeBoolean(true);
373                previousIndividuals[i].writeIndividual(state, dataOutput);
374                }
375
376        super.writeSubpopulation(state, dataOutput);
377        }
378   
379    /** Overridden to include the global best, neighborhood bests, personal bests, and previous individuals in the stream.
380        The neighborhood size, clamp range, and initial velocity scale are not included -- it's assumed you're using the
381        same values for them on reading, or understand that the values are revised. */
382    public void readSubpopulation(final EvolutionState state,
383        final DataInput dataInput) throws IOException
384        {
385        // global best
386        globalBest = (dataInput.readBoolean() ? (DoubleVectorIndividual)species.newIndividual(state, dataInput) : null);
387       
388        // neighborhoodBests
389        for(int i = 0; i < individuals.length; i++)
390            neighborhoodBests[i] = (dataInput.readBoolean() ? (DoubleVectorIndividual)species.newIndividual(state, dataInput): null);
391           
392        // personalBests
393        for(int i = 0; i < individuals.length; i++)
394            personalBests[i] = (dataInput.readBoolean() ? (DoubleVectorIndividual)species.newIndividual(state, dataInput): null);
395
396        // previous Individuals
397        for(int i = 0; i < individuals.length; i++)
398            previousIndividuals[i] = (dataInput.readBoolean() ? (DoubleVectorIndividual)species.newIndividual(state, dataInput): null);
399
400        super.readSubpopulation(state, dataInput);
401        }       
402    }
Note: See TracBrowser for help on using the repository browser.