$darkmode
DENOPTIM
SelectionHelper.java
Go to the documentation of this file.
1/*
2 * DENOPTIM
3 * Copyright (C) 2019 Vishwesh Venkatraman <vishwesh.venkatraman@ntnu.no>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as published
7 * by the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19package denoptim.ga;
20
21import java.util.ArrayList;
22import java.util.Arrays;
23import java.util.Collections;
24import java.util.List;
25import java.lang.Math;
26
27import denoptim.graph.Candidate;
28import denoptim.programs.RunTimeParameters;
29
30
36public class SelectionHelper
37{
38
39//------------------------------------------------------------------------------
40
55 List<Candidate> eligibleParents, int sz, RunTimeParameters settings)
56 {
57 Candidate[] selection = new Candidate[sz];
58 for (int i=0; i<sz; i++)
59 {
60 // Pick two candidates at random.
61 Candidate p1 = settings.getRandomizer().randomlyChooseOne(eligibleParents);
62 Candidate p2 = settings.getRandomizer().randomlyChooseOne(eligibleParents);
63
64 // Use a random value to decide weather to select the fitter individual
65 // or the weaker one.
66 boolean selectFitter = settings.getRandomizer().nextBoolean();
67
68 if (selectFitter)
69 {
70 // Select the fitter candidate.
71 selection[i] = p1.getFitness() > p2.getFitness() ? p1 : p2;
72 }
73 else
74 {
75 // Select the weaker candidate.
76 selection[i] = p2.getFitness() > p1.getFitness() ? p1 : p2;
77 }
78 }
79 return selection;
80 }
81
82//------------------------------------------------------------------------------
83
92 List<Candidate> population, int sz, RunTimeParameters settings)
93 {
94 Candidate[] selection = new Candidate[sz];
95 for (int i=0; i<sz; i++)
96 selection[i] = settings.getRandomizer().randomlyChooseOne(population);
97
98 return selection;
99 }
100
101//------------------------------------------------------------------------------
102
112 protected static Candidate[] performSUS(List<Candidate> population,
113 int sz, RunTimeParameters settings)
114 {
115 int k = population.size();
116 Candidate[] selection = new Candidate[sz];
117 // Calculate the sum of all fitness values.
118 double aggregateFitness = 0;
119 List<Double> fitnesses = new ArrayList<>();
120
121
122 // Get module of the lowest fitness
123 for (int i=0; i<k; i++)
124 {
125 fitnesses.add(population.get(i).getFitness());
126 }
127 double offSet = Math.abs(Collections.min(fitnesses));
128
129 // Sum all candidates' fitness translated by the offSet
130 // to ensure feasibility also with negative values
131 for (int i=0; i<k; i++)
132 {
133 aggregateFitness += population.get(i).getFitness() + offSet;
134 }
135
136 // Pick a random pointer between 0 and 1 as the starting point for selection.
137 double randomPointer = settings.getRandomizer().nextDouble();
138 double cumulativeExpectation = 0;
139 int index = 0;
140 int c = 0;
141 for (int i=0; i<k; i++)
142 {
143
144 // Calculate the probability of the i-th candidate to be selected and
145 // sum it to the previous values
146 cumulativeExpectation += (population.get(i).getFitness() + offSet)
147 / aggregateFitness * sz;
148
149 // Select the candidate i if the randomPointer falls in the values spanned
150 // by the cumulative probabilities
151 while (cumulativeExpectation > randomPointer + index)
152 {
153 selection[c] = population.get(i);
154 c++;
155 index++;
156 }
157 }
158
159 return selection;
160 }
161
162//------------------------------------------------------------------------------
163
178 protected static Candidate[] performRWS(List<Candidate> population,
179 int sz, RunTimeParameters settings)
180 {
181 int k = population.size();
182 Candidate[] selection = new Candidate[sz];
183
184 double[] cumulativeFitnesses = new double[k];
185 cumulativeFitnesses[0] = population.get(0).getFitness();
186
187 for (int i=1; i<k; i++)
188 {
189 double fitness = population.get(i).getFitness();
190
191 cumulativeFitnesses[i] = cumulativeFitnesses[i-1] + fitness;
192 }
193
194 for (int i=0; i<sz; i++)
195 {
196 double randomFitness = settings.getRandomizer().nextDouble() *
197 cumulativeFitnesses[cumulativeFitnesses.length-1];
198 int index = Arrays.binarySearch(cumulativeFitnesses, randomFitness);
199 if (index < 0)
200 {
201 // Convert negative insertion point to array index.
202 index = Math.abs(index + 1);
203 }
204 selection[i] = population.get(index);
205 }
206
207 return selection;
208 }
209
210//------------------------------------------------------------------------------
211}
Class that offers methods to performs fitness-driven selection of candidates.
static Candidate[] performRandomSelection(List< Candidate > population, int sz, RunTimeParameters settings)
Randomly select k individuals from the population.
static Candidate[] performTournamentSelection(List< Candidate > eligibleParents, int sz, RunTimeParameters settings)
Select p individuals at random.
static Candidate[] performRWS(List< Candidate > population, int sz, RunTimeParameters settings)
Roulette wheel selection is implemented as follows:
static Candidate[] performSUS(List< Candidate > population, int sz, RunTimeParameters settings)
Stochastic Uniform Sampling Note: this implementation is based on the WATCHMAKER framework http://wat...
A candidate is the combination of a denoptim graph with molecular representation and may include also...
Definition: Candidate.java:40
Collection of parameters controlling the behavior of the software.
Randomizer getRandomizer()
Returns the current program-specific randomizer.
boolean nextBoolean()
Returns the next pseudo-random, uniformly distributed boolean value from this random number generator...
public< T > T randomlyChooseOne(Collection< T > c)
Chooses one member among the given collection.
double nextDouble()
Returns the next pseudo-random, uniformly distributed double value between 0.0 and 1....