Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OKBJavaConnector/ECJClient/src/ec/pso/PSOBreeder.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: 5.7 KB
RevLine 
[6152]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.Breeder;
10import ec.EvolutionState;
11import ec.Population;
12import ec.util.Parameter;
13import ec.vector.DoubleVectorIndividual;
14/**
15 * PSOBreeder.java
16 *
17 
18 <p>The PSOBreeder performs the calculations to determine new particle locations
19 and performs the bookkeeping to keep track of personal, neighborhood, and global
20 best solutions.
21 
22 <p><b>Parameters</b><br>
23 <table>
24 <tr><td valign=top><i>base.</i><tt>debug-info</tt><br>
25 <font size=-1>boolean</font></td>
26 <td valign=top>(whether the system should display information useful for debugging purposes)<br>
27 </td></tr>
28
29 </table>
30
31 * @author Joey Harrison, Ankur Desai
32 * @version 1.0
33 */
34public class PSOBreeder extends Breeder
35    {
36    public void setup(EvolutionState state, Parameter base)
37        {
38        // intentionally empty
39        }
40               
41    public Population breedPopulation(EvolutionState state)
42        {
43        PSOSubpopulation subpop = (PSOSubpopulation) state.population.subpops[0];
44               
45        // update bests
46        assignPersonalBests(subpop);
47        assignNeighborhoodBests(subpop);
48        assignGlobalBest(subpop);
49
50        // make a temporary copy of locations so we can modify the current location on the fly
51        DoubleVectorIndividual[] tempClone = new DoubleVectorIndividual[subpop.individuals.length];
52        System.arraycopy(subpop.individuals, 0, tempClone, 0, subpop.individuals.length);
53               
54        // update particles             
55        for (int i = 0; i < subpop.individuals.length; i++)
56            {
57            DoubleVectorIndividual ind = (DoubleVectorIndividual)subpop.individuals[i];
58            DoubleVectorIndividual prevInd = (DoubleVectorIndividual)subpop.previousIndividuals[i];
59            // the individual's personal best
60            DoubleVectorIndividual pBest = (DoubleVectorIndividual)subpop.personalBests[i];
61            // the individual's neighborhood best
62            DoubleVectorIndividual nBest = (DoubleVectorIndividual)subpop.neighborhoodBests[i];
63            // the individuals's global best
64            DoubleVectorIndividual gBest = (DoubleVectorIndividual)subpop.globalBest;
65                       
66            // calculate update for each dimension in the genome
67            for (int j = 0; j < ind.genomeLength(); j++)
68                {
69                double velocity = ind.genome[j] - prevInd.genome[j];
70                double pDelta = pBest.genome[j] - ind.genome[j];                        // difference to personal best
71                double nDelta = nBest.genome[j] - ind.genome[j];                        // difference to neighborhood best
72                double gDelta = gBest.genome[j] - ind.genome[j];                        // difference to global best
73                double pWeight = state.random[0].nextDouble();                          // weight for personal best
74                double nWeight = state.random[0].nextDouble();                          // weight for neighborhood best
75                double gWeight = state.random[0].nextDouble();                          // weight for global best
76                double newDelta = (velocity + pWeight*pDelta + nWeight*nDelta + gWeight*gDelta) / (1+pWeight+nWeight+gWeight);
77                       
78                // update this individual's genome for this dimension
79                ind.genome[j] += newDelta * subpop.velocityMultiplier;     // it's obvious if you think about it
80                }
81           
82            if (subpop.clampRange)
83                ind.clamp();                 
84            }               
85               
86        // update previous locations
87        subpop.previousIndividuals = tempClone;
88                               
89        return state.population;
90        }
91
92    public void assignPersonalBests(PSOSubpopulation subpop)
93        {
94        for (int i = 0; i < subpop.personalBests.length; i++)                   
95            if ((subpop.personalBests[i] == null) || subpop.individuals[i].fitness.betterThan(subpop.personalBests[i].fitness))
96                subpop.personalBests[i] = (DoubleVectorIndividual)subpop.individuals[i].clone();
97        }
98
99    public void assignNeighborhoodBests(PSOSubpopulation subpop)
100        {
101        for (int j = 0; j < subpop.individuals.length; j++)
102            {
103            DoubleVectorIndividual hoodBest = subpop.neighborhoodBests[j];
104            int start = (j - subpop.neighborhoodSize / 2);
105            if (start < 0)
106                start += subpop.individuals.length;
107                       
108            for (int i = 0; i < subpop.neighborhoodSize; i++)
109                {
110                DoubleVectorIndividual ind = (DoubleVectorIndividual)subpop.individuals[(start + i) % subpop.individuals.length];
111                if((hoodBest == null) || ind.fitness.betterThan(hoodBest.fitness))
112                    hoodBest = ind;
113                }
114                       
115            if (hoodBest != subpop.neighborhoodBests[j])
116                subpop.neighborhoodBests[j] = (DoubleVectorIndividual)hoodBest.clone();
117            }
118        }
119       
120    public void assignGlobalBest(PSOSubpopulation subpop)
121        {
122        DoubleVectorIndividual globalBest = subpop.globalBest;
123        for (int i = 0; i < subpop.individuals.length; i++)
124            {
125            DoubleVectorIndividual ind = (DoubleVectorIndividual)subpop.individuals[i];
126            if ((globalBest == null) || ind.fitness.betterThan(globalBest.fitness))
127                globalBest = ind;
128            }
129        if (globalBest != subpop.globalBest)
130            subpop.globalBest = (DoubleVectorIndividual)globalBest.clone();
131        }
132    }
Note: See TracBrowser for help on using the repository browser.