Skip to content

Commit

Permalink
Merge pull request #15 from felixreimann/feature/archives-test
Browse files Browse the repository at this point in the history
abstract archive tests added
  • Loading branch information
FedorSmirnov89 committed Mar 21, 2018
2 parents e472c45 + d8d4dbe commit 6f469a4
Show file tree
Hide file tree
Showing 7 changed files with 339 additions and 109 deletions.
@@ -1,23 +1,18 @@
/*******************************************************************************
* Copyright (c) 2014 Opt4J
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*******************************************************************************/

package org.opt4j.core.common.archive;
Expand Down Expand Up @@ -54,17 +49,31 @@ public abstract class AbstractArchive extends Archive {
@Override
public boolean update(Set<? extends Individual> individuals) {
List<Individual> candidates = new ArrayList<Individual>(individuals);
Objectives o1, o2;

/*
* Remove all individuals that are already in the archive and those that
* are dominated among the candidates
* Remove all individuals that are already in the archive.
*/
candidates.removeAll(this);
removeDominatedCandidates(candidates);

removeArchiveDominated(candidates);

return updateWithNondominated(candidates);
}

/**
* Remove candidates, which are weakly dominated by another candidate.
*
* Thus, the list of candidates which need to be tested against the whole
* archive is reduced.
*
* @param candidates
* the list of candidates to sanitize
*/
void removeDominatedCandidates(List<Individual> candidates) {
for (int i = 0; i < candidates.size() - 1; i++) {
o1 = candidates.get(i).getObjectives();
Objectives o1 = candidates.get(i).getObjectives();
for (int j = i + 1; j < candidates.size(); j++) {
o2 = candidates.get(j).getObjectives();
Objectives o2 = candidates.get(j).getObjectives();
if (o2.weaklyDominates(o1)) {
candidates.remove(i);
i--;
Expand All @@ -75,19 +84,23 @@ public boolean update(Set<? extends Individual> individuals) {
}
}
}
}

/*
* Remove those individuals from the candidates which are weakly
* dominated by the archive. Remove those individuals from the archive
* which are dominated by the candidates. In case of equal objectives,
* this gives priority to the individuals in the archive and avoids
* unnecessary archive updates.
*/
/**
* Remove those individuals from the candidates which are weakly
* dominated by the archive. Remove those individuals from the archive
* which are dominated by the candidates. In case of equal objectives,
* this gives priority to the individuals in the archive and avoids
* unnecessary archive updates.
* @param candidates
* the list of candidates to sanitize
*/
void removeArchiveDominated(List<Individual> candidates) {
Iterator<Individual> i1, i2;
for (i1 = candidates.iterator(); i1.hasNext();) {
o1 = i1.next().getObjectives();
Objectives o1 = i1.next().getObjectives();
for (i2 = this.iterator(); i2.hasNext();) {
o2 = i2.next().getObjectives();
Objectives o2 = i2.next().getObjectives();
if (o2.weaklyDominates(o1)) {
i1.remove();
break;
Expand All @@ -96,8 +109,6 @@ public boolean update(Set<? extends Individual> individuals) {
}
}
}

return updateWithNondominated(candidates);
}

/**
Expand Down
@@ -1,26 +1,20 @@
/*******************************************************************************
* Copyright (c) 2014 Opt4J
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*******************************************************************************/


package org.opt4j.core.genotype;

import java.lang.reflect.Constructor;
Expand All @@ -31,8 +25,7 @@

/**
* <p>
* The {@link DoubleGenotype} consists of double values that can be used as a
* {@link Genotype}.
* The {@link DoubleGenotype} consists of double values that can be used as a {@link Genotype}.
* </p>
* <p>
* Example problem: Select filling level of five bottles<br/>
Expand All @@ -43,8 +36,8 @@
* genotype.init(new Random(), 5);
* </pre>
*
* </blockquote> Example instance: [0.5035947840006195, 0.9693492473483428,
* 0.12786372316728167, 0.5299369900029843, 0.8055193291478467]<br/>
* </blockquote> Example instance: [0.5035947840006195, 0.9693492473483428, 0.12786372316728167, 0.5299369900029843,
* 0.8055193291478467]<br/>
* Example search space size: [0;1]<sup>5</sup>
* </p>
*
Expand All @@ -57,16 +50,14 @@ public class DoubleGenotype extends ArrayList<Double> implements ListGenotype<Do
protected final Bounds<Double> bounds;

/**
* Constructs a {@link DoubleGenotype} with lower bounds {@code 0.0} and
* upper bounds {@code 1.0}.
* Constructs a {@link DoubleGenotype} with lower bounds {@code 0.0} and upper bounds {@code 1.0}.
*/
public DoubleGenotype() {
this(0, 1);
}

/**
* Constructs a {@link DoubleGenotype} with a specified lower and upper
* bound for all values.
* Constructs a {@link DoubleGenotype} with a specified lower and upper bound for all values.
*
* @param lowerBound
* the lower bound
Expand Down Expand Up @@ -138,9 +129,9 @@ public void init(Random random, int n) {
getLowerBound(n - 1);
getUpperBound(n - 1);
} catch (IndexOutOfBoundsException outOfBoundException) {
String message = outOfBoundException.getMessage() == null ? "" : outOfBoundException.getMessage() + "\n";
message += "Can not initialize a genotype with " + n + " entries with the specified bounds";
throw new IndexOutOfBoundsException(message);
throw new IllegalArgumentException(
"Cannot initialize a genotype with " + n + " entries with the specified bounds",
outOfBoundException);
}
for (int i = 0; i < n; i++) {
double lo = getLowerBound(i);
Expand Down
@@ -1,23 +1,18 @@
/*******************************************************************************
* Copyright (c) 2014 Opt4J
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*******************************************************************************/

package org.opt4j.core.genotype;
Expand All @@ -30,8 +25,7 @@

/**
* <p>
* The {@link IntegerGenotype} is a {@link Genotype} that consists of
* {@link Integer} values.
* The {@link IntegerGenotype} is a {@link Genotype} that consists of {@link Integer} values.
* </p>
* <p>
* Example problem: Select the outcome of throwing five dice<br/>
Expand All @@ -54,8 +48,7 @@ public class IntegerGenotype extends ArrayList<Integer> implements ListGenotype<
protected final Bounds<Integer> bounds;

/**
* Constructs a {@link IntegerGenotype} with a specified lower and upper
* bound for all values.
* Constructs a {@link IntegerGenotype} with a specified lower and upper bound for all values.
*
* @param lowerBound
* the lower bound
Expand Down Expand Up @@ -127,9 +120,9 @@ public void init(Random random, int n) {
getLowerBound(n - 1);
getUpperBound(n - 1);
} catch (IndexOutOfBoundsException outOfBoundException) {
String message = outOfBoundException.getMessage() == null ? "" : outOfBoundException.getMessage() + "\n";
message += "Can not initialize a genotype with " + n + " entries with the specified bounds";
throw new IndexOutOfBoundsException(message);
throw new IllegalArgumentException(
"Cannot initialize a genotype with " + n + " entries with the specified bounds",
outOfBoundException);
}

for (int i = 0; i < n; i++) {
Expand Down

0 comments on commit 6f469a4

Please sign in to comment.