Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OKBJavaConnector/ECJClient/src/ec/de/Rand1EitherOrDEBreeder.java @ 6834

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

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

File size: 4.0 KB
Line 
1package ec.de;
2
3import ec.*;
4import ec.util.*;
5import ec.vector.*;
6
7/*
8 * Rand1EitherOrDEBreeder.java
9 *
10 * Created: Wed Apr 26 18:01:11 2006
11 * By: Liviu Panait
12 */
13
14/**
15 * Rand1EitherOrDEBreeder is a differential evolution breeding operator.
16 * The code is derived from a DE algorithm, known as DE/rand/1/either-or,
17 * found on page 141 of
18 * "Differential Evolution: A Practical Approach to Global Optimization"
19 * by Kenneth Price, Rainer Storn, and Jouni Lampinen.
20 *
21 * <p>Rand1EitherOrDEBreeder requires that all individuals be DoubleVectorIndividuals.
22 *
23 * <p>In short, the algorithm is as follows.  For each individual in the population, we produce a child
24 * by selecting three (different) individuals, none the original individual, called r0, r1, and r2.
25 * We then create an individal c, defined either c = r0 + F * (r1 - r2), or as c = r0 + 0.5 * (F+1) * (r1 + r2 - 2 * r0),
26 * depending on a coin flip of probability "PF" (if 'true', the first equation is used, else the second).
27 * Unlike the other DEBreeders in this package, we do *not* cross over the child with the original individual.
28 * In fact, if the crossover probability is specified, Rand1EitherOrDEBreeder will issue a warning that it's
29 * not using it.
30 *
31 * <p>This class should be used in conjunction with
32 * DEEvaluator, which allows the children to enter the population only if they're superior to their
33 * parents (the original individuals).  If so, they replace their parents.
34 *
35 * <p><b>Parameters</b><br>
36 * <table>
37 * <tr><td valign=top><i>base.</i><tt>pf</tt><br>
38 * <font size=-1>0.0 &lt;= double &lt;= 1.0 </font></td>
39 * <td valign=top>The "PF" probability of mutation type</td></tr>
40 * </table>
41 *
42 * @author Liviu Panait and Sean Luke
43 * @version 2.0
44 */
45
46
47public class Rand1EitherOrDEBreeder extends DEBreeder
48    {
49    public double PF = 0.0;
50       
51    public static final String P_PF = "pf";
52       
53    public void setup(final EvolutionState state, final Parameter base)
54        {
55        super.setup(state,base);
56
57        PF = state.parameters.getDouble(base.push(P_PF),null,0.0);
58        if ( PF < 0.0 || PF > 1.0 )
59            state.output.fatal( "Parameter not found, or its value is outside of [0.0,1.0].", base.push(P_PF), null );
60                       
61        if (state.parameters.exists(base.push(P_Cr), null))
62            state.output.warning("Crossover parameter specified, but Rand1EitherOrDEBreeder does not use crossover.", base.push(P_Cr));
63        }
64       
65    public DoubleVectorIndividual createIndividual( final EvolutionState state,
66        int subpop,
67        int index,
68        int thread )
69        {
70        Individual[] inds = state.population.subpops[subpop].individuals;
71
72        DoubleVectorIndividual v = (DoubleVectorIndividual)(inds[index].clone());
73
74        do
75            {
76            // select three indexes different from each other and from that of the current parent
77            int r0, r1, r2;
78            do
79                {
80                r0 = state.random[thread].nextInt(inds.length);
81                }
82            while( r0 == index );
83            do
84                {
85                r1 = state.random[thread].nextInt(inds.length);
86                }
87            while( r1 == r0 || r1 == index );
88            do
89                {
90                r2 = state.random[thread].nextInt(inds.length);
91                }
92            while( r2 == r1 || r2 == r0 || r2 == index );
93
94            DoubleVectorIndividual g0 = (DoubleVectorIndividual)(inds[r0]);
95            DoubleVectorIndividual g1 = (DoubleVectorIndividual)(inds[r1]);
96            DoubleVectorIndividual g2 = (DoubleVectorIndividual)(inds[r2]);
97
98            for(int i = 0; i < v.genome.length; i++)
99                if (state.random[thread].nextBoolean(PF))
100                    v.genome[i] = g0.genome[i] + F * (g1.genome[i] - g2.genome[i]);
101                else
102                    v.genome[i] = g0.genome[i] + 0.5 * (F+1) * (g1.genome[i] + g2.genome[i] - 2 * g0.genome[i]);
103            }
104        while(!valid(v));
105
106        return v;       // no crossover is performed
107        }
108
109    }
Note: See TracBrowser for help on using the repository browser.