Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OKBJavaConnector/ECJClient/src/ec/breed/BufferedBreedingPipeline.java @ 8614

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

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

File size: 4.2 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.breed;
9import ec.*;
10import ec.util.*;
11
12/*
13 * BufferedBreedingPipeline.java
14 *
15 * Created: December 28, 1999
16 * By: Sean Luke
17 */
18
19/**
20 * If empty, a
21 * BufferedBreedingPipeline makes a request of exactly <i>num-inds</i>
22 * individuals from a single child source; it then uses these
23 * individuals to fill requests (returning min each time),
24 * until the buffer is emptied, at
25 * which time it grabs exactly <i>num-inds</i> more individuals, and so on.
26 *
27 * <p>What is this useful for?  Well, let's say for example that
28 * you want to cross over two individuals, then cross
29 * them over again.  You'd like to hook up two CrossoverPipelines
30 * in series.  Unfortunately, CrossoverPipeline takes
31 * two sources; even if you set them to the same source, it requests
32 * <i>one</i> individual from the first source and then <i>one</i>
33 * from the second, where what you really want is for it to request
34 * <i>two</i> individuals from a single source (the other CrossoverPipeline).
35 *
36 * <p>The solution to this is to hook a CrossoverPipeline as the
37 * source to a BufferedBreedingPipeline of buffer-size 2 (or some
38 * multiple of 2 actually).  Then the BufferedBreedingPipeline is
39 * set as both sources to another CrossoverPipeline.
40 
41 <p><b>Typical Number of Individuals Produced Per <tt>produce(...)</tt> call</b><br>
42 1
43
44 <p><b>Number of Sources</b><br>
45 1
46 
47 <p><b>Parameters</b><br>
48 <table>
49 <tr><td valign=top><i>base</i>.<tt>num-inds</tt><br>
50 <font size=-1>int &gt;= 1</font></td>
51 <td valign=top>(the buffer size)</td></tr>
52 </table>
53
54 <p><b>Default Base</b><br>
55 breed.buffered
56
57 *
58 * @author Sean Luke
59 * @version 1.0
60 */
61
62public class BufferedBreedingPipeline extends BreedingPipeline
63    {
64    public static final String P_BUFSIZE = "num-inds";
65    public static final String P_BUFFERED = "buffered";
66    public static final int INDS_PRODUCED = 1;
67    public static final int NUM_SOURCES = 1;
68
69    public Individual[] buffer;
70    public int currentSize;
71
72    public Parameter defaultBase()
73        {
74        return BreedDefaults.base().push(P_BUFFERED);
75        }
76
77    public int numSources() { return NUM_SOURCES; }   
78    public int typicalIndsProduced() { return INDS_PRODUCED;}
79
80    public void setup(final EvolutionState state, final Parameter base)
81        {
82        super.setup(state,base);
83
84        Parameter def = defaultBase();
85
86        int bufsize = state.parameters.getInt(base.push(P_BUFSIZE),
87            def.push(P_BUFSIZE),1);
88        if (bufsize == 0)
89            state.output.fatal("BufferedBreedingPipeline's number of individuals must be >= 1.",base.push(P_BUFSIZE),def.push(P_BUFSIZE));
90       
91        buffer = new Individual[bufsize];
92        currentSize=0; // just in case
93
94        // declare that likelihood isn't used
95        if (likelihood < 1.0f)
96            state.output.warning("BufferedBreedingPipeline does not respond to the 'likelihood' parameter.",
97                base.push(P_LIKELIHOOD), def.push(P_LIKELIHOOD));
98        }
99
100
101    public void prepareToProduce(final EvolutionState state,
102        final int subpopulation,
103        final int thread)
104        {
105        super.prepareToProduce(state,subpopulation,thread);
106        // reset my number of individuals to 0
107        currentSize=0;
108        }
109
110
111    public int produce(final int min,
112        final int max,
113        final int start,
114        final int subpopulation,
115        final Individual[] inds,
116        final EvolutionState state,
117        final int thread)
118
119        {
120        for(int q=start;q<min+start; q++ )
121            {
122            if (currentSize==0)         // reload
123                {
124                sources[0].produce(buffer.length,buffer.length,
125                    0,subpopulation,buffer,state,thread);
126                currentSize=buffer.length;
127               
128                // clone if necessary
129                if (sources[0] instanceof SelectionMethod)
130                    for(int z=0; z < buffer.length; z++)
131                        buffer[z] = (Individual)(buffer[z].clone());
132                }
133           
134            inds[q] = buffer[currentSize-1];
135            currentSize--;
136            }
137        return min;
138        }
139    }
Note: See TracBrowser for help on using the repository browser.