1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.math4.legacy.genetics;
18
19 import java.util.ArrayList;
20 import java.util.Collection;
21 import java.util.Collections;
22 import java.util.Iterator;
23 import java.util.List;
24
25 import org.apache.commons.math4.legacy.exception.NotPositiveException;
26 import org.apache.commons.math4.legacy.exception.NullArgumentException;
27 import org.apache.commons.math4.legacy.exception.NumberIsTooLargeException;
28 import org.apache.commons.math4.legacy.exception.NumberIsTooSmallException;
29 import org.apache.commons.math4.legacy.exception.util.LocalizedFormats;
30
31
32
33
34
35
36 public abstract class ListPopulation implements Population {
37
38
39 private final List<Chromosome> chromosomes;
40
41
42 private int populationLimit;
43
44
45
46
47
48
49
50 public ListPopulation(final int populationLimit) throws NotPositiveException {
51 this(Collections.<Chromosome>emptyList(), populationLimit);
52 }
53
54
55
56
57
58
59
60
61
62
63
64
65 public ListPopulation(final List<Chromosome> chromosomes, final int populationLimit)
66 throws NullArgumentException, NotPositiveException, NumberIsTooLargeException {
67
68 if (chromosomes == null) {
69 throw new NullArgumentException();
70 }
71 if (populationLimit <= 0) {
72 throw new NotPositiveException(LocalizedFormats.POPULATION_LIMIT_NOT_POSITIVE, populationLimit);
73 }
74 if (chromosomes.size() > populationLimit) {
75 throw new NumberIsTooLargeException(LocalizedFormats.LIST_OF_CHROMOSOMES_BIGGER_THAN_POPULATION_SIZE,
76 chromosomes.size(), populationLimit, false);
77 }
78 this.populationLimit = populationLimit;
79 this.chromosomes = new ArrayList<>(populationLimit);
80 this.chromosomes.addAll(chromosomes);
81 }
82
83
84
85
86
87
88
89
90 public void addChromosomes(final Collection<Chromosome> chromosomeColl) throws NumberIsTooLargeException {
91 if (chromosomes.size() + chromosomeColl.size() > populationLimit) {
92 throw new NumberIsTooLargeException(LocalizedFormats.LIST_OF_CHROMOSOMES_BIGGER_THAN_POPULATION_SIZE,
93 chromosomes.size(), populationLimit, false);
94 }
95 this.chromosomes.addAll(chromosomeColl);
96 }
97
98
99
100
101
102 public List<Chromosome> getChromosomes() {
103 return Collections.unmodifiableList(chromosomes);
104 }
105
106
107
108
109
110
111 protected List<Chromosome> getChromosomeList() {
112 return chromosomes;
113 }
114
115
116
117
118
119
120
121
122 @Override
123 public void addChromosome(final Chromosome chromosome) throws NumberIsTooLargeException {
124 if (chromosomes.size() >= populationLimit) {
125 throw new NumberIsTooLargeException(LocalizedFormats.LIST_OF_CHROMOSOMES_BIGGER_THAN_POPULATION_SIZE,
126 chromosomes.size(), populationLimit, false);
127 }
128 this.chromosomes.add(chromosome);
129 }
130
131
132
133
134
135 @Override
136 public Chromosome getFittestChromosome() {
137
138 Chromosome bestChromosome = this.chromosomes.get(0);
139 for (Chromosome chromosome : this.chromosomes) {
140 if (chromosome.compareTo(bestChromosome) > 0) {
141
142 bestChromosome = chromosome;
143 }
144 }
145 return bestChromosome;
146 }
147
148
149
150
151
152 @Override
153 public int getPopulationLimit() {
154 return this.populationLimit;
155 }
156
157
158
159
160
161
162
163
164 public void setPopulationLimit(final int populationLimit) throws NotPositiveException, NumberIsTooSmallException {
165 if (populationLimit <= 0) {
166 throw new NotPositiveException(LocalizedFormats.POPULATION_LIMIT_NOT_POSITIVE, populationLimit);
167 }
168 if (populationLimit < chromosomes.size()) {
169 throw new NumberIsTooSmallException(populationLimit, chromosomes.size(), true);
170 }
171 this.populationLimit = populationLimit;
172 }
173
174
175
176
177
178 @Override
179 public int getPopulationSize() {
180 return this.chromosomes.size();
181 }
182
183
184
185
186 @Override
187 public String toString() {
188 return this.chromosomes.toString();
189 }
190
191
192
193
194
195
196
197 @Override
198 public Iterator<Chromosome> iterator() {
199 return getChromosomes().iterator();
200 }
201 }