$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.Comparator;
25import java.util.List;
26import java.lang.Math;
27
28import denoptim.graph.Candidate;
29import denoptim.programs.RunTimeParameters;
30import denoptim.programs.denovo.GAParameters;
31
32
38public class SelectionHelper
39{
40
41//------------------------------------------------------------------------------
42
58 List<Candidate> eligibleParents, int sz,
59 GAParameters settings)
60 {
61 Candidate[] selection = new Candidate[sz];
62 for (int i=0; i<sz; i++)
63 {
64 List<Candidate> pool = new ArrayList<Candidate>();
65 for (int j=0; j<settings.getSelectivePressure(); j++)
66 {
67 pool.add(settings.getRandomizer().randomlyChooseOne(
68 eligibleParents));
69 }
70
71 selection[i] = Collections.max(pool, Comparator.comparing(
72 c -> c.getFitness()));
73 }
74 return selection;
75 }
76
77//------------------------------------------------------------------------------
78
87 List<Candidate> population, int sz, RunTimeParameters settings)
88 {
89 Candidate[] selection = new Candidate[sz];
90 for (int i=0; i<sz; i++)
91 selection[i] = settings.getRandomizer().randomlyChooseOne(population);
92
93 return selection;
94 }
95
96//------------------------------------------------------------------------------
97
107 protected static Candidate[] performSUS(List<Candidate> population,
108 int sz, RunTimeParameters settings)
109 {
110 int k = population.size();
111 Candidate[] selection = new Candidate[sz];
112 // Calculate the sum of all fitness values.
113 double aggregateFitness = 0;
114 List<Double> fitnesses = new ArrayList<>();
115
116
117 // Get module of the lowest fitness
118 for (int i=0; i<k; i++)
119 {
120 fitnesses.add(population.get(i).getFitness());
121 }
122 double offSet = Math.abs(Collections.min(fitnesses));
123
124 // Sum all candidates' fitness translated by the offSet
125 // to ensure feasibility also with negative values
126 for (int i=0; i<k; i++)
127 {
128 aggregateFitness += population.get(i).getFitness() + offSet;
129 }
130
131 // Pick a random pointer between 0 and 1 as the starting point for selection.
132 double randomPointer = settings.getRandomizer().nextDouble();
133 double cumulativeExpectation = 0;
134 int index = 0;
135 int c = 0;
136 for (int i=0; i<k; i++)
137 {
138
139 // Calculate the probability of the i-th candidate to be selected and
140 // sum it to the previous values
141 cumulativeExpectation += (population.get(i).getFitness() + offSet)
142 / aggregateFitness * sz;
143
144 // Select the candidate i if the randomPointer falls in the values spanned
145 // by the cumulative probabilities
146 while (cumulativeExpectation > randomPointer + index)
147 {
148 selection[c] = population.get(i);
149 c++;
150 index++;
151 }
152 }
153
154 return selection;
155 }
156
157//------------------------------------------------------------------------------
158
173 protected static Candidate[] performRWS(List<Candidate> population,
174 int sz, RunTimeParameters settings)
175 {
176 int k = population.size();
177 Candidate[] selection = new Candidate[sz];
178
179 double[] cumulativeFitnesses = new double[k];
180 cumulativeFitnesses[0] = population.get(0).getFitness();
181
182 for (int i=1; i<k; i++)
183 {
184 double fitness = population.get(i).getFitness();
185
186 cumulativeFitnesses[i] = cumulativeFitnesses[i-1] + fitness;
187 }
188
189 for (int i=0; i<sz; i++)
190 {
191 double randomFitness = settings.getRandomizer().nextDouble() *
192 cumulativeFitnesses[cumulativeFitnesses.length-1];
193 int index = Arrays.binarySearch(cumulativeFitnesses, randomFitness);
194 if (index < 0)
195 {
196 // Convert negative insertion point to array index.
197 index = Math.abs(index + 1);
198 }
199 selection[i] = population.get(index);
200 }
201
202 return selection;
203 }
204
205//------------------------------------------------------------------------------
206}
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[] 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...
static Candidate[] performTournamentSelection(List< Candidate > eligibleParents, int sz, GAParameters settings)
Select a number individuals at random (i.e., tournamentSize).
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.
Parameters for genetic algorithm.
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....