blob: d85bd3b59053cfeaaafb72b4cc17f96639061f58 [file] [log] [blame]
Thomas Vachuska58de4162015-09-10 16:15:33 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Thomas Vachuska58de4162015-09-10 16:15:33 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Nikhil Cheerlaf7c2e1a2015-07-09 12:06:37 -070016package org.onlab.graph;
17
Ray Milkeyb68bbbc2017-12-18 10:05:49 -080018import java.security.SecureRandom;
Nikhil Cheerlaf7c2e1a2015-07-09 12:06:37 -070019import java.util.ArrayList;
20import java.util.Collections;
21import java.util.List;
22import java.util.Random;
23
24/**
25 * Represents a population of GAOrganisms. This class can be used
26 * to run a genetic algorithm on the population and return the fittest solutions.
27 */
28class GAPopulation<Organism extends GAOrganism> extends ArrayList<Organism> {
Ray Milkeyb68bbbc2017-12-18 10:05:49 -080029 Random r = new SecureRandom();
Nikhil Cheerlaf7c2e1a2015-07-09 12:06:37 -070030
31 /**
32 * Steps the population through one generation. The 75% least fit
33 * organisms are killed off and replaced with the children of the
34 * 25% (as well as some "random" newcomers).
35 */
36 void step() {
Andrey Komarov2398d962016-09-26 15:11:23 +030037 Collections.sort(this, (org1, org2) ->
38 org1.fitness().compareTo(org2.fitness()));
Nikhil Cheerlaf7c2e1a2015-07-09 12:06:37 -070039 int maxSize = size();
40 for (int i = size() - 1; i > maxSize / 4; i--) {
41 remove(i);
42 }
43 for (Organism org: this) {
44 if (r.nextBoolean()) {
45 org.mutate();
46 }
47 }
48 while (size() < maxSize * 4 / 5) {
49 Organism org1 = get(r.nextInt(size()));
50 Organism org2 = get(r.nextInt(size()));
51 add((Organism) org1.crossWith(org2));
52 }
53
54 while (size() < maxSize) {
55 Organism org1 = get(r.nextInt(size()));
56 add((Organism) org1.random());
57 }
58 }
59
60 /**
61 * Runs GA for the specified number of iterations, and returns
62 * a sample of the resulting population of solutions.
63 *
64 * @param generations Number of generations to run GA for
65 * @param populationSize Population size of GA
66 * @param sample Number of solutions to ask for
67 * @param template Template GAOrganism to seed the population with
68 * @return ArrayList containing sample number of organisms
69 */
70 List<Organism> runGA(int generations, int populationSize, int sample, Organism template) {
71 for (int i = 0; i < populationSize; i++) {
72 add((Organism) template.random());
73 }
74
75 for (int i = 0; i < generations; i++) {
76 step();
77 }
78 for (int i = size() - 1; i >= sample; i--) {
79 remove(i);
80 }
81 return new ArrayList<>(this);
82 }
83}
84