$darkmode
DENOPTIM
Population.java
Go to the documentation of this file.
1/*
2 * DENOPTIM
3 * Copyright (C) 2021 Marco Foscato <marco.foscato@uib.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.Collection;
23import java.util.Comparator;
24import java.util.LinkedHashMap;
25import java.util.List;
26import java.util.concurrent.atomic.AtomicInteger;
27import java.util.logging.Level;
28
29import denoptim.exception.DENOPTIMException;
30import denoptim.fragspace.FragmentSpace;
31import denoptim.fragspace.FragmentSpaceParameters;
32import denoptim.graph.Candidate;
33import denoptim.graph.DGraph;
34import denoptim.programs.RunTimeParameters.ParametersType;
35import denoptim.programs.denovo.GAParameters;
36
47public class Population extends ArrayList<Candidate> implements Cloneable
48{
52 private static final long serialVersionUID = 1L;
53
57 private AtomicInteger populationUpdate = new AtomicInteger();
58
63
68
69//------------------------------------------------------------------------------
70
72 {
73 super();
74 this.settings = settings;
77 if (fsParams!=null &&
79 {
81 }
82 }
83
84//------------------------------------------------------------------------------
85
86 @Override
87 public boolean add(Candidate c)
88 {
89 boolean result = super.add(c);
90 if (result)
91 populationUpdate.getAndIncrement();
92 return result;
93 }
94
95//------------------------------------------------------------------------------
96
97 @Override
98 public void add(int index, Candidate c)
99 {
100 super.add(index, c);
101 populationUpdate.getAndIncrement();
102 }
103
104//------------------------------------------------------------------------------
105
106 @Override
107 public Candidate set(int index, Candidate c)
108 {
109 populationUpdate.getAndIncrement();
110 return super.set(index, c);
111 }
112
113//------------------------------------------------------------------------------
114
115 @Override
116 public Candidate remove(int index)
117 {
118 populationUpdate.getAndIncrement();
119 return super.remove(index);
120 }
121
122//------------------------------------------------------------------------------
123
124 @Override
125 public boolean remove(Object c)
126 {
127 boolean result = super.remove(c);
128 if (result)
129 populationUpdate.getAndIncrement();
130 return result;
131 }
132
133//------------------------------------------------------------------------------
134
135 @Override
136 public boolean removeAll(Collection<?> c)
137 {
138 boolean result = super.removeAll(c);
139 if (result)
140 populationUpdate.getAndIncrement();
141 return result;
142 }
143
144//------------------------------------------------------------------------------
145
146 @Override
147 public void clear()
148 {
149 populationUpdate.getAndIncrement();
150 super.clear();
151 }
152
153//------------------------------------------------------------------------------
154
155 @Override
156 public boolean retainAll(Collection<?> c)
157 {
158 boolean result = super.retainAll(c);
159 if (result)
160 populationUpdate.getAndIncrement();
161 return result;
162 }
163
164//------------------------------------------------------------------------------
165
173 public int getVersionID()
174 {
175 return populationUpdate.get();
176 }
177
178//------------------------------------------------------------------------------
179
185 {
187
188 for (Candidate c : this)
189 {
190 clone.add(c);
191 }
192
193 clone.xoverCompatibilities = xoverCompatibilities.clone();
194
195 return clone;
196 }
197
198//------------------------------------------------------------------------------
199
211 {
212 private LinkedHashMap<Candidate,
213 LinkedHashMap<Candidate,List<XoverSite>>> data;
214
219 {
220 data = new LinkedHashMap<Candidate,LinkedHashMap<Candidate,
221 List<XoverSite>>>();
222 }
223
232 public void put(Candidate c1, Candidate c2,
233 List<XoverSite> xoversite)
234 {
235 if (data.containsKey(c1))
236 {
237 data.get(c1).put(c2, xoversite);
238 } else {
239 LinkedHashMap<Candidate,List<XoverSite>> toC1 =
240 new LinkedHashMap<Candidate,List<XoverSite>>();
241 toC1.put(c2, xoversite);
242 data.put(c1, toC1);
243 }
244
245 List<XoverSite> revPairs =
246 new ArrayList<XoverSite>();
247 for (XoverSite pair : xoversite)
248 {
249 XoverSite revPair = pair.createMirror();
250 revPairs.add(revPair);
251 }
252
253 if (data.containsKey(c2))
254 {
255 data.get(c2).put(c1, revPairs);
256 } else {
257 LinkedHashMap<Candidate,List<XoverSite>> toC2 =
258 new LinkedHashMap<Candidate,List<XoverSite>>();
259 toC2.put(c1, revPairs);
260 data.put(c2, toC2);
261 }
262 }
263
270 public List<XoverSite> get(Candidate c1, Candidate c2)
271 {
272 if (data.containsKey(c1))
273 {
274 return data.get(c1).get(c2);
275 } else {
276 return null;
277 }
278 }
279
285 public List<Candidate> getMembersCompatibleWith(Candidate cA)
286 {
287 List<Candidate> compatibleMembers = new ArrayList<Candidate>();
288 if (data.keySet().contains(cA))
289 {
290 for (Candidate cB : data.get(cA).keySet())
291 {
292 if (!data.get(cA).get(cB).isEmpty())
293 {
294 compatibleMembers.add(cB);
295 }
296 }
297 }
298 return compatibleMembers;
299 }
300
312 public boolean contains(Candidate memberA, Candidate memberB)
313 {
314 return data.keySet().contains(memberA) &&
315 data.get(memberA).containsKey(memberB);
316 }
317
322 public void remove(Candidate c)
323 {
324 data.remove(c);
325 for (LinkedHashMap<Candidate, List<XoverSite>> m :
326 data.values())
327 {
328 m.remove(c);
329 }
330 }
331
338 {
340 for (Candidate c1 : data.keySet())
341 {
342 LinkedHashMap<Candidate, List<XoverSite>> inner =
343 new LinkedHashMap<Candidate, List<XoverSite>>();
344 for (Candidate c2 : data.get(c1).keySet())
345 {
346 List<XoverSite> lst = new ArrayList<XoverSite>();
347 for (XoverSite arr : data.get(c1).get(c2))
348 {
349 lst.add(arr.clone());
350 }
351 inner.put(c2,lst);
352 }
353 cloned.data.put(c1, inner);
354 }
355 return cloned;
356 }
357 }
358
359//------------------------------------------------------------------------------
360
370 public List<Candidate> getXoverPartners(Candidate memberA,
371 List<Candidate> eligibleParents, FragmentSpace fragSpace)
372 {
373 DGraph gA = memberA.getGraph();
374
375 // Update to make sure we cover any combination of members that has not
376 // been considered before
377 for (Candidate memberB : eligibleParents)
378 {
379 if (memberA == memberB)
380 {
381 continue;
382 }
383
384 if (xoverCompatibilities.contains(memberA, memberB))
385 {
386 continue;
387 }
388
389 DGraph gB = memberB.getGraph();
390
391 if (gA.sameAs(gB, new StringBuilder()))
392 continue;
393
394 try
395 {
396 List<XoverSite> xoverSites = GraphOperations
397 .locateCompatibleXOverPoints(gA, gB, fragSpace,
399 xoverCompatibilities.put(memberA, memberB, xoverSites);
400 } catch (DENOPTIMException e)
401 {
402 settings.getLogger().log(Level.FINE, "Could not identify "
403 + "crossover sites between " + memberA.getName()
404 + " and " + memberB.getName() + ".");
405 e.printStackTrace();
406 }
407 }
409 }
410
411//------------------------------------------------------------------------------
412
423 public List<XoverSite> getXoverSites(Candidate parentA, Candidate parentB)
424 {
425 return xoverCompatibilities.get(parentA,parentB);
426 }
427
428//------------------------------------------------------------------------------
429
435 public void trim(int populationSize)
436 {
437 int k = this.size();
438 for (Candidate c : this.subList(settings.getPopulationSize(), k))
439 {
441 }
442 this.subList(settings.getPopulationSize(), k).clear();
443 }
444
445//------------------------------------------------------------------------------
446
452 public double getMinFitness()
453 {
455 }
456
457//------------------------------------------------------------------------------
458
465 {
466 return this.stream()
467 .min(Comparator.comparing(Candidate::getFitness))
468 .orElse(null);
469 }
470
471//------------------------------------------------------------------------------
472
477 public double getMaxFitness()
478 {
479 return this.stream()
480 .max(Comparator.comparing(Candidate::getFitness))
481 .orElse(null)
482 .getFitness();
483 }
484
485//------------------------------------------------------------------------------
486
496 public boolean isWithinPercentile(double value, double percentile)
497 {
498 double min = getMinFitness();
499 double max = getMaxFitness();
500 double threshold = (1.0 - percentile) * (max-min);
501
502 if (value > (threshold+min))
503 return true;
504 else
505 return false;
506 }
507
508//------------------------------------------------------------------------------
509
515 public Candidate getCandidateNamed(String name)
516 {
517 return this.stream()
518 .filter(candidate -> name.equals(candidate.getName()))
519 .findAny()
520 .orElse(null);
521 }
522
523//------------------------------------------------------------------------------
524
525}
Class defining a space of building blocks.
boolean useAPclassBasedApproach()
Check usage of APClass-based approach, i.e., uses attachment points with annotated data (i....
Parameters defining the fragment space.
Collection of operators meant to alter graphs and associated utilities.
static List< XoverSite > locateCompatibleXOverPoints(DGraph graphA, DGraph graphB, FragmentSpace fragSpace, int maxSizeXoverSubGraph)
Identify crossover sites, i.e., subgraphs that can be swapped between two graphs (i....
A data structure collecting crossover-compatible sites.
XoverSitesAmongCandidates()
Initializes an empty data structure.
LinkedHashMap< Candidate, LinkedHashMap< Candidate, List< XoverSite > > > data
boolean contains(Candidate memberA, Candidate memberB)
Check is this data structure contains information about the combination of the given members.
void remove(Candidate c)
removes all references to the specified candidate.
void put(Candidate c1, Candidate c2, List< XoverSite > xoversite)
Creates the entry corresponding to the pair of given candidates.
List< Candidate > getMembersCompatibleWith(Candidate cA)
Returns a list of items that has crossover sites with the given item.
XoverSitesAmongCandidates clone()
Return a somewhat-shallow clone of this object: the map and list are new objects, but the references ...
List< XoverSite > get(Candidate c1, Candidate c2)
Gets the value corresponding to the pair of keys in the given order.
A collection of candidates.
Definition: Population.java:48
double getMinFitness()
Gets the minimum value of the fitness in this population.
List< XoverSite > getXoverSites(Candidate parentA, Candidate parentB)
Returns a list of crossover sites between the two given parents.
GAParameters settings
Parameters controlling the GA experiment.
Definition: Population.java:67
List< Candidate > getXoverPartners(Candidate memberA, List< Candidate > eligibleParents, FragmentSpace fragSpace)
Returns a list of population members that can do crossover with the specified member.
XoverSitesAmongCandidates xoverCompatibilities
Crossover compatibility between members.
Definition: Population.java:62
int getVersionID()
Returns an integer that represent the current status of the population.
boolean isWithinPercentile(double value, double percentile)
Checks if a given fitness value if within the given percentile of best candidates.
boolean add(Candidate c)
Definition: Population.java:87
boolean removeAll(Collection<?> c)
Population(GAParameters settings)
Definition: Population.java:71
Population clone()
Does not clone the cross-over compatibility relations between each pairs of population members.
boolean retainAll(Collection<?> c)
static final long serialVersionUID
Version UID.
Definition: Population.java:52
Candidate getMinFitnessMember()
Gets the Candidate with minimum value of the fitness in this population.
Candidate getCandidateNamed(String name)
Returns the candidate with the given name, if present, or null.
AtomicInteger populationUpdate
An integer that changes every time we change the population.
Definition: Population.java:57
void trim(int populationSize)
Removes all the elements exceeding the given size.
void add(int index, Candidate c)
Definition: Population.java:98
double getMaxFitness()
Gets the maximum value of the fitness in this population.
This class collects the data identifying the subgraphs that would be swapped by a crossover event.
Definition: XoverSite.java:36
XoverSite createMirror()
Creates a new XoverSite that considers the opposite order of candidates of this one.
Definition: XoverSite.java:149
A candidate is the combination of a denoptim graph with molecular representation and may include also...
Definition: Candidate.java:40
Container for the list of vertices and the edges that connect them.
Definition: DGraph.java:102
boolean sameAs(DGraph other, StringBuilder reason)
Compare this and another graph ignoring the vertex IDs.
Definition: DGraph.java:3665
RunTimeParameters getParameters(ParametersType type)
Logger getLogger()
Get the name of the program specific logger.
Parameters for genetic algorithm.
int maxXOverableSubGraphSize
Limit to the size of subgraphs that are exchanged during crossover.
FS_PARAMS
Parameters pertaining the definition of the fragment space.