|
From: Wiehann M. <wcm...@gm...> - 2009-05-23 00:47:37
|
From: Gary Pampara <gpa...@gm...>
I'll wait for you to look at this first before applying to 'next'
---
.../selection/ElitistSelectionStrategy.java | 2 +-
.../solutionweighing/AntiClusterWeighing.java | 30 ++--
.../archive/solutionweighing/SolutionWeighing.java | 10 +-
.../cilib/util/selection/OrderingSyntax.java | 33 ----
.../cilib/util/selection/Selection.java | 159 +++++++++++++++++---
.../cilib/util/selection/SelectionSyntax.java | 12 ++-
.../cilib/util/selection/WeighedSelection.java | 102 -------------
.../cilib/util/selection/WeighingSyntax.java | 34 ----
.../cilib/util/selection/ordering/Ordering.java | 5 +-
.../selection/ordering/ProportionalOrdering.java | 16 +-
.../util/selection/ordering/RandomOrdering.java | 6 +-
.../util/selection/ordering/ReverseOrdering.java | 5 +-
.../util/selection/ordering/RingBasedOrdering.java | 27 +++-
.../util/selection/ordering/SortedOrdering.java | 5 +-
.../util/selection/recipes/ElitistSelection.java | 2 +-
.../util/selection/recipes/RandomSelection.java | 4 +-
.../util/selection/recipes/RankBasedSelection.java | 2 +-
.../recipes/RingBasedPopulationSelection.java | 30 ++++-
.../selection/recipes/RouletteWheelSelection.java | 4 +-
.../selection/recipes/TournamentSelection.java | 4 +-
.../util/selection/weighing/FixedWeighing.java | 23 ++--
.../util/selection/weighing/LinearWeighing.java | 23 ++--
.../cilib/util/selection/weighing/Weighing.java | 6 +-
.../selection/weighing/entity/EntityWeighing.java | 23 ++--
.../RouletteWheelSelectionStrategyTest.java | 3 +-
.../solutionweighing/SolutionWeighingTest.java | 22 ++--
.../util/selection/ordering/OrderingTest.java | 37 +++--
.../selection/recipes/SelectionRecipeTest.java | 2 +-
.../util/selection/weighing/WeighingTest.java | 59 -------
.../weighing/entity/EntityWeighingTest.java | 13 +-
30 files changed, 331 insertions(+), 372 deletions(-)
delete mode 100644 src/main/java/net/sourceforge/cilib/util/selection/OrderingSyntax.java
delete mode 100644 src/main/java/net/sourceforge/cilib/util/selection/WeighedSelection.java
delete mode 100644 src/main/java/net/sourceforge/cilib/util/selection/WeighingSyntax.java
delete mode 100644 src/test/java/net/sourceforge/cilib/util/selection/weighing/WeighingTest.java
diff --git a/src/main/java/net/sourceforge/cilib/entity/operators/selection/ElitistSelectionStrategy.java b/src/main/java/net/sourceforge/cilib/entity/operators/selection/ElitistSelectionStrategy.java
index 2a2cea5..29fe94f 100644
--- a/src/main/java/net/sourceforge/cilib/entity/operators/selection/ElitistSelectionStrategy.java
+++ b/src/main/java/net/sourceforge/cilib/entity/operators/selection/ElitistSelectionStrategy.java
@@ -60,7 +60,7 @@ public class ElitistSelectionStrategy extends SelectionStrategy {
*/
@Override
public <T extends Entity> T select(Topology<T> population) {
- return new ElitistSelection<T>(new DescendingFitnessComparator<T>()).select(population);
+ return new ElitistSelection<T>().select(population);
}
/**
diff --git a/src/main/java/net/sourceforge/cilib/moo/archive/solutionweighing/AntiClusterWeighing.java b/src/main/java/net/sourceforge/cilib/moo/archive/solutionweighing/AntiClusterWeighing.java
index 184cf5a..ce39f43 100644
--- a/src/main/java/net/sourceforge/cilib/moo/archive/solutionweighing/AntiClusterWeighing.java
+++ b/src/main/java/net/sourceforge/cilib/moo/archive/solutionweighing/AntiClusterWeighing.java
@@ -21,16 +21,16 @@
*/
package net.sourceforge.cilib.moo.archive.solutionweighing;
-import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
-import net.sourceforge.cilib.container.Pair;
import net.sourceforge.cilib.moo.archive.Archive;
import net.sourceforge.cilib.problem.Fitness;
import net.sourceforge.cilib.problem.MOFitness;
import net.sourceforge.cilib.problem.OptimisationSolution;
import net.sourceforge.cilib.pso.moo.guideselectionstrategies.GuideSelectionStrategy;
+import net.sourceforge.cilib.util.selection.Selection;
+import net.sourceforge.cilib.util.selection.Selection.Entry;
/**
* <p>
@@ -64,11 +64,11 @@ public class AntiClusterWeighing implements SolutionWeighing {
}
@Override
- public List<Pair<Double, OptimisationSolution>> weigh(List<OptimisationSolution> solutions) {
+ public boolean weigh(List<Selection.Entry<OptimisationSolution>> solutions) {
// Get first fitness as dummy fitness to set size of initial min and max fitness
// arrays as well as populating these arrays.
- Iterator<? extends OptimisationSolution> solutionIterator = solutions.iterator();
- MOFitness tempFitness = (MOFitness) solutionIterator.next().getFitness();
+ Iterator<? extends Entry<OptimisationSolution>> solutionIterator = solutions.iterator();
+ MOFitness tempFitness = (MOFitness) solutionIterator.next().getElement().getFitness();
Fitness[] minFitnesses = new Fitness[tempFitness.getDimension()];
Fitness[] maxFitnesses = new Fitness[tempFitness.getDimension()];
for (int i = 0; i < tempFitness.getDimension(); ++i) {
@@ -78,8 +78,8 @@ public class AntiClusterWeighing implements SolutionWeighing {
// Iterate over all remaining optimisation solutions and find the min and max fitness values.
while (solutionIterator.hasNext()) {
- OptimisationSolution optimisationSolution = solutionIterator.next();
- MOFitness fitnesses = (MOFitness) optimisationSolution.getFitness();
+ Entry<OptimisationSolution> optimisationSolution = solutionIterator.next();
+ MOFitness fitnesses = (MOFitness) optimisationSolution.getElement().getFitness();
for (int i = 0; i < fitnesses.getDimension(); ++i) {
Double fitnessValue = fitnesses.getFitness(i).getValue();
if (fitnessValue < minFitnesses[i].getValue()) {
@@ -92,14 +92,14 @@ public class AntiClusterWeighing implements SolutionWeighing {
// Now, iterate over all solutions again, but calculate the distance from each solution to every other
// solution and store the results in a list. Each solution in the list contains the distance as weight value.
- List<Pair<Double, OptimisationSolution>> weighedOptimisationSolutions = new ArrayList<Pair<Double, OptimisationSolution>>();
- for (OptimisationSolution fromSolution : solutions) {
+// List<Pair<Double, OptimisationSolution>> weighedOptimisationSolutions = new ArrayList<Pair<Double, OptimisationSolution>>();
+ for (Selection.Entry<OptimisationSolution> fromSolution : solutions) {
double totalDistance = 0.0;
- MOFitness fromFitnesses = (MOFitness) fromSolution.getFitness();
- for (OptimisationSolution toSolution : solutions) {
+ MOFitness fromFitnesses = (MOFitness) fromSolution.getElement().getFitness();
+ for (Selection.Entry<OptimisationSolution> toSolution : solutions) {
if (fromSolution != toSolution) {
double distance = 0.0;
- MOFitness toFitnesses = (MOFitness) toSolution.getFitness();
+ MOFitness toFitnesses = (MOFitness) toSolution.getElement().getFitness();
for (int i = 0; i < fromFitnesses.getDimension(); ++i) {
distance += Math.pow((fromFitnesses.getFitness(i).getValue() - toFitnesses.getFitness(i).getValue()) /
(maxFitnesses[i].getValue() - minFitnesses[i].getValue()), 2.0);
@@ -107,9 +107,11 @@ public class AntiClusterWeighing implements SolutionWeighing {
totalDistance += Math.sqrt(distance);
}
}
- weighedOptimisationSolutions.add(new Pair<Double, OptimisationSolution>((totalDistance != 0.0) ? 1.0 / totalDistance : Double.MAX_VALUE, fromSolution));
+ fromSolution.setWeight((totalDistance != 0.0) ? 1.0 / totalDistance : Double.MAX_VALUE);
+// weighedOptimisationSolutions.add(new Pair<Double, OptimisationSolution>((totalDistance != 0.0) ? 1.0 / totalDistance : Double.MAX_VALUE, fromSolution));
}
- return weighedOptimisationSolutions;
+// return weighedOptimisationSolutions;
+ return true;
}
}
diff --git a/src/main/java/net/sourceforge/cilib/moo/archive/solutionweighing/SolutionWeighing.java b/src/main/java/net/sourceforge/cilib/moo/archive/solutionweighing/SolutionWeighing.java
index 39f509d..b46b8ba 100644
--- a/src/main/java/net/sourceforge/cilib/moo/archive/solutionweighing/SolutionWeighing.java
+++ b/src/main/java/net/sourceforge/cilib/moo/archive/solutionweighing/SolutionWeighing.java
@@ -4,17 +4,17 @@
* Department of Computer Science
* University of Pretoria
* South Africa
- *
+ *
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
- *
+ *
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
- *
+ *
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
@@ -23,10 +23,10 @@ package net.sourceforge.cilib.moo.archive.solutionweighing;
import java.util.List;
-import net.sourceforge.cilib.container.Pair;
import net.sourceforge.cilib.problem.OptimisationSolution;
import net.sourceforge.cilib.util.selection.weighing.Weighing;
import net.sourceforge.cilib.util.Cloneable;
+import net.sourceforge.cilib.util.selection.Selection;
/**
* <p>
@@ -42,5 +42,5 @@ public interface SolutionWeighing extends Weighing<OptimisationSolution>, Clonea
public abstract SolutionWeighing getClone();
@Override
- public List<Pair<Double, OptimisationSolution>> weigh(List<OptimisationSolution> elements);
+ public boolean weigh(List<Selection.Entry<OptimisationSolution>> elements);
}
diff --git a/src/main/java/net/sourceforge/cilib/util/selection/OrderingSyntax.java b/src/main/java/net/sourceforge/cilib/util/selection/OrderingSyntax.java
deleted file mode 100644
index de29db5..0000000
--- a/src/main/java/net/sourceforge/cilib/util/selection/OrderingSyntax.java
+++ /dev/null
@@ -1,33 +0,0 @@
-/**
- * Copyright (C) 2003 - 2008
- * Computational Intelligence Research Group (CIRG@UP)
- * Department of Computer Science
- * University of Pretoria
- * South Africa
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- */
-package net.sourceforge.cilib.util.selection;
-
-import net.sourceforge.cilib.util.selection.ordering.Ordering;
-
-/**
- * @author Wiehann Matthysen
- * @param <E>
- */
-public interface OrderingSyntax<E> {
-
- public SelectionSyntax<E> apply(Ordering<E> ordering);
-}
diff --git a/src/main/java/net/sourceforge/cilib/util/selection/Selection.java b/src/main/java/net/sourceforge/cilib/util/selection/Selection.java
index bdad64b..f4ac2f1 100644
--- a/src/main/java/net/sourceforge/cilib/util/selection/Selection.java
+++ b/src/main/java/net/sourceforge/cilib/util/selection/Selection.java
@@ -24,7 +24,6 @@ package net.sourceforge.cilib.util.selection;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
-import net.sourceforge.cilib.container.Pair;
import net.sourceforge.cilib.util.selection.ordering.Ordering;
import net.sourceforge.cilib.util.selection.weighing.Weighing;
@@ -43,8 +42,8 @@ import net.sourceforge.cilib.util.selection.weighing.Weighing;
* would be done as follows:
* </p>
* <pre>
- * List<T> selection = Selection.from(population).apply(new RandomOrdering<T>()).first(tournamentSize)
- * .apply(new SortedOrdering<T>()).last().select();
+ * List<T> selection = Selection.from(population).orderBy(new RandomOrdering<T>()).first(tournamentSize)
+ * .orderBy(new SortedOrdering<T>()).last().select();
*
* return selection.get(0);
* </pre>
@@ -60,9 +59,9 @@ import net.sourceforge.cilib.util.selection.weighing.Weighing;
* @param <E> The comparable type.
* @author gpampara
*/
-public final class Selection<E> implements SelectionSyntax<E>, OrderingSyntax<E>, WeighingSyntax<E> {
+public final class Selection<E extends Comparable> implements SelectionSyntax<E> {
- private List<E> elements;
+ private List<Entry<E>> elements;
/**
* Assign the Selection to take palce on the porvided collection. The
@@ -71,7 +70,10 @@ public final class Selection<E> implements SelectionSyntax<E>, OrderingSyntax<E>
* @param elements The elements on which the selection should take place.
*/
private Selection(Collection<? extends E> elements) {
- this.elements = new ArrayList(elements);
+ this.elements = new ArrayList<Entry<E>>(elements.size());
+
+ for (E element : elements)
+ this.elements.add(new Entry<E>(element));
}
/**
@@ -80,24 +82,23 @@ public final class Selection<E> implements SelectionSyntax<E>, OrderingSyntax<E>
* @param elements The collection of elements to operate on.
* @return A selection based on the provided collection.
*/
- public static <T> Selection<T> from(Collection<? extends T> elements) {
+ public static <T extends Comparable> Selection<T> from(Collection<? extends T> elements) {
return new Selection<T>(elements);
}
/**
* Apply the provided ordering on the current selection. The result of the
* operation will result in a modified selection.
- * @param ordering The ordering to apply.
+ * @param ordering The ordering to orderBy.
* @return A selection upon which the ordering has be applied.
* @throws UnsupportedOperationException if the ordering cannot be applied.
*/
@Override
- public Selection<E> apply(Ordering<E> ordering) {
+ public SelectionSyntax<E> orderBy(Ordering<E> ordering) {
boolean result = ordering.order(this.elements);
- if (result) {
+ if (result)
return this;
- }
throw new UnsupportedOperationException("The ordering [" + ordering.getClass().getSimpleName() + "] " +
"cannot be applied to the selection. Please ensure that the intention of the ordering is correct.");
@@ -106,13 +107,18 @@ public final class Selection<E> implements SelectionSyntax<E>, OrderingSyntax<E>
/**
* Apply the provided weighing on the current selection. The result of the
* operation will result in new weighed selection.
- * @param weighing The weighing to apply.
+ * @param weighing The weighing to orderBy.
* @return A selection upon which the weighing has be applied.
*/
@Override
- public WeighedSelection<E> apply(Weighing<E> weighing) {
- List<Pair<Double, E>> weighedElements = weighing.weigh(this.elements);
- return new WeighedSelection<E>(weighedElements);
+ public SelectionSyntax<E> weigh(Weighing<E> weighing) {
+ boolean result = weighing.weigh(this.elements);
+
+ if (result)
+ return this;
+
+ throw new UnsupportedOperationException("The weighing [" + weighing.getClass().getSimpleName() + "]" +
+ "cannot be applied to the selection. Please ensure that the intention of the weighing is correct.");
}
/**
@@ -121,7 +127,7 @@ public final class Selection<E> implements SelectionSyntax<E>, OrderingSyntax<E>
* @return A selection containing the first element.
*/
@Override
- public Selection<E> first() {
+ public SelectionSyntax<E> first() {
this.elements = this.elements.subList(0, 1);
return this;
}
@@ -133,7 +139,7 @@ public final class Selection<E> implements SelectionSyntax<E>, OrderingSyntax<E>
* @return A selection containing the first {@code number} elements.
*/
@Override
- public Selection<E> first(int number) {
+ public SelectionSyntax<E> first(int number) {
this.elements = this.elements.subList(0, number);
return this;
}
@@ -143,7 +149,7 @@ public final class Selection<E> implements SelectionSyntax<E>, OrderingSyntax<E>
* @return A selection containing the last element.
*/
@Override
- public Selection<E> last() {
+ public SelectionSyntax<E> last() {
this.elements = this.elements.subList(this.elements.size() - 1, this.elements.size());
return this;
}
@@ -154,7 +160,7 @@ public final class Selection<E> implements SelectionSyntax<E>, OrderingSyntax<E>
* @return A selection containing the last {@code number} of elements.
*/
@Override
- public Selection<E> last(int number) {
+ public SelectionSyntax<E> last(int number) {
this.elements = this.elements.subList(this.elements.size() - number, this.elements.size());
return this;
}
@@ -165,6 +171,121 @@ public final class Selection<E> implements SelectionSyntax<E>, OrderingSyntax<E>
*/
@Override
public List<E> select() {
+ List<E> result = new ArrayList<E>();
+
+ for (Entry<E> entry : elements)
+ result.add(entry.getElement());
+
+ return result;
+ }
+
+ @Override
+ public E singleSelect() {
+ return this.elements.get(0).getElement();
+ }
+
+ @Override
+ public List<Selection.Entry<E>> entries() {
return this.elements;
}
+
+
+ /**
+ * This class provides the notion of an entry within a list
+ * for the selection process.
+ * <p>
+ * This class is is final and non-instantiable to ensure that the
+ * operations are allowed to be applied, however, additional metadata
+ * can be recored and used during the selection process.
+ * @param <E> The {@see Comparable} type.
+ */
+ public final static class Entry<E extends Comparable> implements Comparable {
+ private final E element;
+ private double weight;
+
+ /**
+ * Create a new {@code Entry}. This constructor is private intentionall
+ * @param element The element to decorate. + */
+ private Entry(E element) {
+ this.element = element;
+ this.weight = 0.0;
+ }
+
+ /**
+ * Get the {@code element} that this {@code Entry} represents.
+ * @return The decorated {@code element}.
+ */
+ public E getElement() {
+ return element;
+ }
+
+ /**
+ * Obtain the weight value associated with this {@code Entry}.
+ * <p>
+ * The weight value need not be set. It is not always used.
+ *
+ * @return The {@code weight} value.
+ */
+ public double getWeight() {
+ return this.weight;
+ }
+
+ /**
+ * Set the {@code weight} value for the current {@code Entry}
+ * within the {@code Selection}.
+ * @param weight The {@code weight} value to set.
+ */
+ public void setWeight(double weight) {
+ this.weight = weight;
+ }
+
+ /**
+ * Determine if the provided {@code obj} is equal to the currently
+ * decorated element within this {@code Entry}.
+ * @param obj The object instance to compare.
+ * @return {@code true} if the objects are equal, {@code false} otherwi
+ */
+ @Override
+ public boolean equals(Object obj) {
+ if (obj == this)
+ return true;
+
+ if ((obj == null) || (this.getClass() != obj.getClass()))
+ return false;
+
+ Entry<E> other = (Entry<E>) obj;
+ return this.element.equals(other.element);
+ }
+
+ /**
+ * Obtain the hash of the decorated {@code element}.
+ * @return The decorated instance's hash value.
+ */
+ @Override
+ public int hashCode() {
+ return element.hashCode();
+ }
+
+ /**
+ * Obtain the {@code String} of the decorated {@code element}.
+ * @return The {@code toString()} of the decorated element.
+ */
+ @Override
+ public String toString() {
+ return this.element.toString();
+ }
+
+ /**
+ * Compare the current decorated {@code element} with the provided
+ * object instance.
+ * @param o The instance to test.
+ * @return {@code 0} if equal, {@code -1} if the current element
+ * is less than the provided instance, {@code 1} otherwise.
+ */
+ @Override
+ public int compareTo(Object o) {
+ Entry<E> other = (Entry<E>) o;
+ return this.element.compareTo(other.element);
+ }
+ }
}
diff --git a/src/main/java/net/sourceforge/cilib/util/selection/SelectionSyntax.java b/src/main/java/net/sourceforge/cilib/util/selection/SelectionSyntax.java
index 846a186..3ba5613 100644
--- a/src/main/java/net/sourceforge/cilib/util/selection/SelectionSyntax.java
+++ b/src/main/java/net/sourceforge/cilib/util/selection/SelectionSyntax.java
@@ -22,12 +22,18 @@
package net.sourceforge.cilib.util.selection;
import java.util.List;
+import net.sourceforge.cilib.util.selection.ordering.Ordering;
+import net.sourceforge.cilib.util.selection.weighing.Weighing;
/**
* @author Wiehann Matthysen
* @param <E>
*/
-public interface SelectionSyntax<E> {
+public interface SelectionSyntax<E extends Comparable> {
+
+ public SelectionSyntax<E> orderBy(Ordering<E> ordering);
+
+ public SelectionSyntax<E> weigh(Weighing<E> weighing);
public SelectionSyntax<E> first();
@@ -38,4 +44,8 @@ public interface SelectionSyntax<E> {
public SelectionSyntax<E> last(int number);
public List<E> select();
+
+ List<Selection.Entry<E>> entries();
+
+ E singleSelect();
}
diff --git a/src/main/java/net/sourceforge/cilib/util/selection/WeighedSelection.java b/src/main/java/net/sourceforge/cilib/util/selection/WeighedSelection.java
deleted file mode 100644
index 788c2d4..0000000
--- a/src/main/java/net/sourceforge/cilib/util/selection/WeighedSelection.java
+++ /dev/null
@@ -1,102 +0,0 @@
-/**
- * Copyright (C) 2003 - 2008
- * Computational Intelligence Research Group (CIRG@UP)
- * Department of Computer Science
- * University of Pretoria
- * South Africa
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- */
-package net.sourceforge.cilib.util.selection;
-
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.List;
-import net.sourceforge.cilib.container.Pair;
-import net.sourceforge.cilib.util.selection.ordering.Ordering;
-import net.sourceforge.cilib.util.selection.weighing.Weighing;
-
-/**
- * @author Wiehann Matthysen
- * @param <E>
- */
-public final class WeighedSelection<E>
- implements SelectionSyntax<Pair<Double, E>>, OrderingSyntax<Pair<Double, E>>, WeighingSyntax<E> {
-
- private List<Pair<Double, E>> elements;
-
- protected WeighedSelection(Collection<Pair<Double, E>> elements) {
- this.elements = new ArrayList<Pair<Double, E>>(elements);
- }
-
- public static <T> List<T> unpack(Collection<Pair<Double, T>> elements) {
- List<T> unpackedElements = new ArrayList<T>(elements.size());
- for (Pair<Double, T> element : elements) {
- unpackedElements.add(element.getValue());
- }
- return unpackedElements;
- }
-
- @Override
- public WeighedSelection<E> apply(Ordering<Pair<Double, E>> ordering) {
- boolean result = ordering.order(this.elements);
-
- if (result) {
- return this;
- }
-
- throw new UnsupportedOperationException("The ordering [" + ordering.getClass().getSimpleName() + "] " +
- "cannot be applied to the selection. Please ensure that the intention of the ordering is correct.");
- }
-
- @Override
- public WeighedSelection<E> apply(Weighing<E> weighing) {
- this.elements = weighing.weigh(unpack(this.elements));
- return this;
- }
-
- @Override
- public WeighedSelection<E> first() {
- this.elements = this.elements.subList(0, 1);
- return this;
- }
-
- @Override
- public WeighedSelection<E> first(int number) {
- this.elements = this.elements.subList(0, number);
- return this;
- }
-
- @Override
- public WeighedSelection<E> last() {
- this.elements = this.elements.subList(this.elements.size() - 1, this.elements.size());
- return this;
- }
-
- @Override
- public WeighedSelection<E> last(int number) {
- this.elements = this.elements.subList(this.elements.size() - number, this.elements.size());
- return this;
- }
-
- @Override
- public List<Pair<Double, E>> select() {
- return this.elements;
- }
-
- public List<E> selectOriginal() {
- return unpack(this.elements);
- }
-}
diff --git a/src/main/java/net/sourceforge/cilib/util/selection/WeighingSyntax.java b/src/main/java/net/sourceforge/cilib/util/selection/WeighingSyntax.java
deleted file mode 100644
index 80abb79..0000000
--- a/src/main/java/net/sourceforge/cilib/util/selection/WeighingSyntax.java
+++ /dev/null
@@ -1,34 +0,0 @@
-/**
- * Copyright (C) 2003 - 2008
- * Computational Intelligence Research Group (CIRG@UP)
- * Department of Computer Science
- * University of Pretoria
- * South Africa
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- */
-package net.sourceforge.cilib.util.selection;
-
-import net.sourceforge.cilib.container.Pair;
-import net.sourceforge.cilib.util.selection.weighing.Weighing;
-
-/**
- * @author Wiehann Matthysen
- * @param <E>
- */
-public interface WeighingSyntax<E> {
-
- public SelectionSyntax<Pair<Double, E>> apply(Weighing<E> weighing);
-}
diff --git a/src/main/java/net/sourceforge/cilib/util/selection/ordering/Ordering.java b/src/main/java/net/sourceforge/cilib/util/selection/ordering/Ordering.java
index 631fc79..00f6b0d 100644
--- a/src/main/java/net/sourceforge/cilib/util/selection/ordering/Ordering.java
+++ b/src/main/java/net/sourceforge/cilib/util/selection/ordering/Ordering.java
@@ -22,6 +22,7 @@
package net.sourceforge.cilib.util.selection.ordering;
import java.util.List;
+import net.sourceforge.cilib.util.selection.Selection;
/**
* An ordering is a construct to define how a list of elements should be ordered.
@@ -29,12 +30,12 @@ import java.util.List;
* @param <E> The type to apply the ordering to.
* @author gpampara
*/
-public interface Ordering<E> {
+public interface Ordering<E extends Comparable> {
/**
* Apply the ordering on the provided list.
* @param elements The list to be ordered.
* @return {@code true} if successful, {@code false} otherwise.
*/
- public boolean order(List<E> elements);
+ public boolean order(List<Selection.Entry<E>> elements);
}
diff --git a/src/main/java/net/sourceforge/cilib/util/selection/ordering/ProportionalOrdering.java b/src/main/java/net/sourceforge/cilib/util/selection/ordering/ProportionalOrdering.java
index 1b326cc..04dd378 100644
--- a/src/main/java/net/sourceforge/cilib/util/selection/ordering/ProportionalOrdering.java
+++ b/src/main/java/net/sourceforge/cilib/util/selection/ordering/ProportionalOrdering.java
@@ -23,14 +23,14 @@ package net.sourceforge.cilib.util.selection.ordering;
import java.util.List;
import java.util.Random;
-import net.sourceforge.cilib.container.Pair;
import net.sourceforge.cilib.math.random.generator.MersenneTwister;
+import net.sourceforge.cilib.util.selection.Selection;
/**
* @author Wiehann Matthysen
* @param <E>
*/
-public class ProportionalOrdering<E> implements Ordering<Pair<Double, E>> {
+public class ProportionalOrdering<E extends Comparable> implements Ordering<E> {
private Random generator;
@@ -47,10 +47,10 @@ public class ProportionalOrdering<E> implements Ordering<Pair<Double, E>> {
* @return {@code true} if successful, {@code false} otherwise.
*/
@Override
- public boolean order(List<Pair<Double, E>> elements) {
+ public boolean order(List<Selection.Entry<E>> elements) {
double total = 0.0;
- for (Pair<Double, E> weighedObject : elements) {
- total += weighedObject.getKey();
+ for (Selection.Entry<E> weighedObject : elements) {
+ total += weighedObject.getWeight();
}
if (Double.compare(total, 0.0) == 0) {
@@ -62,11 +62,11 @@ public class ProportionalOrdering<E> implements Ordering<Pair<Double, E>> {
double marker = 0.0;
int j = i;
for (; j < elements.size() - 1 && marker < randomValue; ++j) {
- marker += elements.get(j).getKey();
+ marker += elements.get(j).getWeight();
}
// Swap elements i and j.
- Pair<Double, E> elementJ = elements.set(j, elements.set(i, elements.get(j)));
- total -= elementJ.getKey();
+ Selection.Entry<E> elementJ = elements.set(j, elements.set(i, elements.get(j)));
+ total -= elementJ.getWeight();
}
return true;
diff --git a/src/main/java/net/sourceforge/cilib/util/selection/ordering/RandomOrdering.java b/src/main/java/net/sourceforge/cilib/util/selection/ordering/RandomOrdering.java
index e8f3d84..202a962 100644
--- a/src/main/java/net/sourceforge/cilib/util/selection/ordering/RandomOrdering.java
+++ b/src/main/java/net/sourceforge/cilib/util/selection/ordering/RandomOrdering.java
@@ -25,6 +25,7 @@ import java.util.Collections;
import java.util.List;
import java.util.Random;
import net.sourceforge.cilib.math.random.generator.MersenneTwister;
+import net.sourceforge.cilib.util.selection.Selection;
/**
* Apply a random ordering to the provided list. This class defines that the
@@ -32,7 +33,7 @@ import net.sourceforge.cilib.math.random.generator.MersenneTwister;
* @param <E> The comparable type.
* @author gpampara
*/
-public class RandomOrdering<E> implements Ordering<E> {
+public class RandomOrdering<E extends Comparable> implements Ordering<E> {
private Random generator;
@@ -50,8 +51,9 @@ public class RandomOrdering<E> implements Ordering<E> {
* @return {@code true} if successful, {@code false} otherwise.
*/
@Override
- public boolean order(List<E> elements) {
+ public boolean order(List<Selection.Entry<E>> elements) {
Collections.shuffle(elements, this.generator);
return true;
}
+
}
diff --git a/src/main/java/net/sourceforge/cilib/util/selection/ordering/ReverseOrdering.java b/src/main/java/net/sourceforge/cilib/util/selection/ordering/ReverseOrdering.java
index 3222874..6a85fae 100644
--- a/src/main/java/net/sourceforge/cilib/util/selection/ordering/ReverseOrdering.java
+++ b/src/main/java/net/sourceforge/cilib/util/selection/ordering/ReverseOrdering.java
@@ -23,15 +23,16 @@ package net.sourceforge.cilib.util.selection.ordering;
import java.util.Collections;
import java.util.List;
+import net.sourceforge.cilib.util.selection.Selection;
/**
* @author Wiehann Matthysen
* @param <E>
*/
-public class ReverseOrdering<E> implements Ordering<E> {
+public class ReverseOrdering<E extends Comparable> implements Ordering<E> {
@Override
- public boolean order(List<E> elements) {
+ public boolean order(List<Selection.Entry<E>> elements) {
Collections.reverse(elements);
return true;
}
diff --git a/src/main/java/net/sourceforge/cilib/util/selection/ordering/RingBasedOrdering.java b/src/main/java/net/sourceforge/cilib/util/selection/ordering/RingBasedOrdering.java
index cd6482b..8927f8a 100644
--- a/src/main/java/net/sourceforge/cilib/util/selection/ordering/RingBasedOrdering.java
+++ b/src/main/java/net/sourceforge/cilib/util/selection/ordering/RingBasedOrdering.java
@@ -23,33 +23,44 @@ package net.sourceforge.cilib.util.selection.ordering;
import java.util.ArrayList;
import java.util.List;
+import net.sourceforge.cilib.util.selection.Selection;
/**
* @author Wiehann Matthysen
* @param <E>
*/
-public class RingBasedOrdering<E> implements Ordering<E> {
+public class RingBasedOrdering<E extends Comparable> implements Ordering<E> {
private E marker;
- public RingBasedOrdering(E marker) {
- this.marker = marker;
- }
-
public RingBasedOrdering() {
this.marker = null;
}
+ public RingBasedOrdering(E marker) {
+ this.marker = marker;
+ }
+
/**
* @param elements
* @return {@code true} if successful, {@code false} otherwise.
*/
@Override
- public boolean order(List<E> elements) {
- List<E> temp = new ArrayList<E>(elements);
+ public boolean order(List<Selection.Entry<E>> elements) {
+ List<Selection.Entry<E>> tmp = new ArrayList<Selection.Entry<E>>(elements);
+
+ int position = 0;
+ for (Selection.Entry<E> entry : elements) {
+ if (this.marker.compareTo(entry.getElement()) == 0)
+ break;
+
+ position++;
+ }
+
for (int i = 0; i < elements.size(); ++i) {
- elements.set(i, temp.get((temp.indexOf(this.marker) + 1 + i) % elements.size()));
+ elements.set(i, tmp.get((position + 1 + i) % elements.size()));
}
+
return true;
}
}
diff --git a/src/main/java/net/sourceforge/cilib/util/selection/ordering/SortedOrdering.java b/src/main/java/net/sourceforge/cilib/util/selection/ordering/SortedOrdering.java
index 813f88e..e080c07 100644
--- a/src/main/java/net/sourceforge/cilib/util/selection/ordering/SortedOrdering.java
+++ b/src/main/java/net/sourceforge/cilib/util/selection/ordering/SortedOrdering.java
@@ -24,6 +24,7 @@ package net.sourceforge.cilib.util.selection.ordering;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
+import net.sourceforge.cilib.util.selection.Selection;
/**
* Apply a sorting operation to the provided list, ordering the list naturally
@@ -49,8 +50,8 @@ public class SortedOrdering<E extends Comparable> implements Ordering<E> {
* @return {@code true} if successful, {@code false} otherwise.
*/
@Override
- public boolean order(List<E> element) {
- Collections.sort(element, this.comparator);
+ public boolean order(List<Selection.Entry<E>> element) {
+ Collections.sort(element);
return true;
}
}
diff --git a/src/main/java/net/sourceforge/cilib/util/selection/recipes/ElitistSelection.java b/src/main/java/net/sourceforge/cilib/util/selection/recipes/ElitistSelection.java
index 166bea2..2e92e16 100644
--- a/src/main/java/net/sourceforge/cilib/util/selection/recipes/ElitistSelection.java
+++ b/src/main/java/net/sourceforge/cilib/util/selection/recipes/ElitistSelection.java
@@ -64,7 +64,7 @@ public class ElitistSelection<E extends Comparable> implements SelectionRecipe<E
@Override
public E select(Collection<? extends E> elements) {
- List<E> selection = Selection.from(elements).apply(new SortedOrdering(this.comparator)).first().select();
+ List<E> selection = Selection.from(elements).orderBy(new SortedOrdering(this.comparator)).last().select();
return selection.get(0);
}
}
diff --git a/src/main/java/net/sourceforge/cilib/util/selection/recipes/RandomSelection.java b/src/main/java/net/sourceforge/cilib/util/selection/recipes/RandomSelection.java
index 223cb7a..d30d1de 100644
--- a/src/main/java/net/sourceforge/cilib/util/selection/recipes/RandomSelection.java
+++ b/src/main/java/net/sourceforge/cilib/util/selection/recipes/RandomSelection.java
@@ -32,7 +32,7 @@ import net.sourceforge.cilib.util.selection.ordering.RandomOrdering;
* @author Wiehann Matthysen
* @param <E>
*/
-public class RandomSelection<E> implements SelectionRecipe<E> {
+public class RandomSelection<E extends Comparable> implements SelectionRecipe<E> {
private static final long serialVersionUID = -5099663528040315048L;
private Random random;
@@ -56,7 +56,7 @@ public class RandomSelection<E> implements SelectionRecipe<E> {
@Override
public E select(Collection<? extends E> elements) {
- List<E> selection = Selection.from(elements).apply(new RandomOrdering<E>(this.random)).first().select();
+ List<E> selection = Selection.from(elements).orderBy(new RandomOrdering<E>(this.random)).first().select();
return selection.get(0);
}
}
diff --git a/src/main/java/net/sourceforge/cilib/util/selection/recipes/RankBasedSelection.java b/src/main/java/net/sourceforge/cilib/util/selection/recipes/RankBasedSelection.java
index b8bbe02..75b13bf 100644
--- a/src/main/java/net/sourceforge/cilib/util/selection/recipes/RankBasedSelection.java
+++ b/src/main/java/net/sourceforge/cilib/util/selection/recipes/RankBasedSelection.java
@@ -74,7 +74,7 @@ public class RankBasedSelection<E extends Comparable> implements SelectionRecipe
@Override
public E select(Collection<? extends E> elements) {
- List<E> selection = Selection.from(elements).apply(new SortedOrdering<E>(this.comparator)).first(this.random.nextInt(elements.size())).apply(new RandomOrdering<E>(this.random)).first().select();
+ List<E> selection = Selection.from(elements).orderBy(new SortedOrdering<E>(this.comparator)).first(this.random.nextInt(elements.size())).orderBy(new RandomOrdering<E>(this.random)).first().select();
return selection.get(0);
}
}
diff --git a/src/main/java/net/sourceforge/cilib/util/selection/recipes/RingBasedPopulationSelection.java b/src/main/java/net/sourceforge/cilib/util/selection/recipes/RingBasedPopulationSelection.java
index 3eb5ee4..5da4287 100644
--- a/src/main/java/net/sourceforge/cilib/util/selection/recipes/RingBasedPopulationSelection.java
+++ b/src/main/java/net/sourceforge/cilib/util/selection/recipes/RingBasedPopulationSelection.java
@@ -21,7 +21,9 @@
*/
package net.sourceforge.cilib.util.selection.recipes;
+import java.util.ArrayList;
import java.util.Collection;
+import java.util.List;
import net.sourceforge.cilib.algorithm.Algorithm;
import net.sourceforge.cilib.algorithm.population.PopulationBasedAlgorithm;
import net.sourceforge.cilib.util.selection.Selection;
@@ -46,7 +48,33 @@ public class RingBasedPopulationSelection implements SelectionRecipe<PopulationB
@Override
public PopulationBasedAlgorithm select(Collection<? extends PopulationBasedAlgorithm> elements) {
- return Selection.from(elements).apply(new RingBasedOrdering<PopulationBasedAlgorithm>((PopulationBasedAlgorithm)Algorithm.get())).first().select().get(0);
+ PopulationBasedAlgorithmWrapper targetPBA = new PopulationBasedAlgorithmWrapper((PopulationBasedAlgorithm)Algorithm.get());
+
+ List<PopulationBasedAlgorithmWrapper> list = new ArrayList<PopulationBasedAlgorithmWrapper>();
+ for (PopulationBasedAlgorithm p : elements) {
+ PopulationBasedAlgorithmWrapper wrapper = new PopulationBasedAlgorithmWrapper(p);
+ list.add(wrapper);
+ }
+
+ PopulationBasedAlgorithmWrapper wrapper = Selection.from(list).orderBy(new RingBasedOrdering<PopulationBasedAlgorithmWrapper>(targetPBA)).first().singleSelect();
+ return wrapper.get();
+ }
+
+ private class PopulationBasedAlgorithmWrapper implements Comparable {
+ private final PopulationBasedAlgorithm algorithm;
+
+ public PopulationBasedAlgorithmWrapper(PopulationBasedAlgorithm algorithm) {
+ this.algorithm = algorithm;
+ }
+
+ public PopulationBasedAlgorithm get() {
+ return algorithm;
+ }
+
+ @Override
+ public int compareTo(Object o) {
+ return 0;
+ }
}
}
diff --git a/src/main/java/net/sourceforge/cilib/util/selection/recipes/RouletteWheelSelection.java b/src/main/java/net/sourceforge/cilib/util/selection/recipes/RouletteWheelSelection.java
index a344eb1..8b4bf1f 100644
--- a/src/main/java/net/sourceforge/cilib/util/selection/recipes/RouletteWheelSelection.java
+++ b/src/main/java/net/sourceforge/cilib/util/selection/recipes/RouletteWheelSelection.java
@@ -73,7 +73,7 @@ public class RouletteWheelSelection<E extends Comparable> implements SelectionRe
@Override
public E select(Collection<? extends E> elements) {
- List<E> selection = Selection.from(elements).apply(this.weighing).apply(new ProportionalOrdering<E>(this.random)).first().selectOriginal();
- return selection.get(0);
+ E selection = Selection.from(elements).weigh(this.weighing).orderBy(new ProportionalOrdering<E>(this.random)).first().singleSelect();
+ return selection;
}
}
diff --git a/src/main/java/net/sourceforge/cilib/util/selection/recipes/TournamentSelection.java b/src/main/java/net/sourceforge/cilib/util/selection/recipes/TournamentSelection.java
index 78621e8..f030665 100644
--- a/src/main/java/net/sourceforge/cilib/util/selection/recipes/TournamentSelection.java
+++ b/src/main/java/net/sourceforge/cilib/util/selection/recipes/TournamentSelection.java
@@ -79,7 +79,7 @@ public class TournamentSelection<E extends Comparable> implements SelectionRecip
@Override
public E select(Collection<? extends E> elements) {
int tournamentSize = Double.valueOf(this.tournamentProportion.getParameter() * elements.size()).intValue();
- return Selection.from(elements).apply(new RandomOrdering<E>(this.random)).
- first(tournamentSize).apply(new SortedOrdering<E>(this.comparator)).first().select().get(0);
+ return Selection.from(elements).orderBy(new RandomOrdering<E>(this.random)).
+ first(tournamentSize).orderBy(new SortedOrdering<E>(this.comparator)).first().select().get(0);
}
}
diff --git a/src/main/java/net/sourceforge/cilib/util/selection/weighing/FixedWeighing.java b/src/main/java/net/sourceforge/cilib/util/selection/weighing/FixedWeighing.java
index f06215d..5152de4 100644
--- a/src/main/java/net/sourceforge/cilib/util/selection/weighing/FixedWeighing.java
+++ b/src/main/java/net/sourceforge/cilib/util/selection/weighing/FixedWeighing.java
@@ -21,27 +21,26 @@
*/
package net.sourceforge.cilib.util.selection.weighing;
-import java.util.ArrayList;
import java.util.List;
-import net.sourceforge.cilib.container.Pair;
+import net.sourceforge.cilib.util.selection.Selection;
/**
* @author Wiehann Matthysen
* @param <E>
*/
-public class FixedWeighing<E> implements Weighing<E> {
+public class FixedWeighing<E extends Comparable> implements Weighing<E> {
private static final long serialVersionUID = -6990220691744842964L;
private double weight;
+ public FixedWeighing() {
+ this(0.0);
+ }
+
public FixedWeighing(double weight) {
this.weight = weight;
}
- public FixedWeighing() {
- this(0.0);
- }
-
public FixedWeighing(FixedWeighing<E> copy) {
this.weight = copy.weight;
}
@@ -60,11 +59,11 @@ public class FixedWeighing<E> implements Weighing<E> {
}
@Override
- public List<Pair<Double, E>> weigh(List<E> elements) {
- List<Pair<Double, E>> weighedElements = new ArrayList<Pair<Double, E>>(elements.size());
- for (E object : elements) {
- weighedElements.add(new Pair<Double, E>(this.weight, object));
+ public boolean weigh(List<Selection.Entry<E>> elements) {
+ for (Selection.Entry<E> object : elements) {
+ object.setWeight(this.weight);
}
- return weighedElements;
+
+ return true;
}
}
diff --git a/src/main/java/net/sourceforge/cilib/util/selection/weighing/LinearWeighing.java b/src/main/java/net/sourceforge/cilib/util/selection/weighing/LinearWeighing.java
index 673af90..21d1adc 100644
--- a/src/main/java/net/sourceforge/cilib/util/selection/weighing/LinearWeighing.java
+++ b/src/main/java/net/sourceforge/cilib/util/selection/weighing/LinearWeighing.java
@@ -21,29 +21,28 @@
*/
package net.sourceforge.cilib.util.selection.weighing;
-import java.util.ArrayList;
import java.util.List;
-import net.sourceforge.cilib.container.Pair;
+import net.sourceforge.cilib.util.selection.Selection;
/**
* @author Wiehann Matthysen
* @param <E>
*/
-public class LinearWeighing<E> implements Weighing<E> {
+public class LinearWeighing<E extends Comparable> implements Weighing<E> {
private static final long serialVersionUID = 3294682425241945584L;
private double min;
private double max;
+ public LinearWeighing() {
+ this(0.0, 1.0);
+ }
+
public LinearWeighing(double min, double max) {
this.min = min;
this.max = max;
}
- public LinearWeighing() {
- this(0.0, 1.0);
- }
-
public LinearWeighing(LinearWeighing<E> copy) {
this.min = copy.min;
this.max = copy.max;
@@ -71,13 +70,13 @@ public class LinearWeighing<E> implements Weighing<E> {
}
@Override
- public List<Pair<Double, E>> weigh(List<E> elements) {
- List<Pair<Double, E>> weighedElements = new ArrayList<Pair<Double, E>>(elements.size());
+ public boolean weigh(List<Selection.Entry<E>> elements) {
double stepSize = (this.max - this.min) / (elements.size() - 1);
int objectIndex = 0;
- for (E element : elements) {
- weighedElements.add(new Pair<Double, E>(objectIndex++ * stepSize + this.min, element));
+ for (Selection.Entry<E> element : elements) {
+ element.setWeight(objectIndex++ * stepSize + this.min);
}
- return weighedElements;
+
+ return true;
}
}
diff --git a/src/main/java/net/sourceforge/cilib/util/selection/weighing/Weighing.java b/src/main/java/net/sourceforge/cilib/util/selection/weighing/Weighing.java
index 61d6dcf..158f931 100644
--- a/src/main/java/net/sourceforge/cilib/util/selection/weighing/Weighing.java
+++ b/src/main/java/net/sourceforge/cilib/util/selection/weighing/Weighing.java
@@ -22,17 +22,17 @@
package net.sourceforge.cilib.util.selection.weighing;
import java.util.List;
-import net.sourceforge.cilib.container.Pair;
import net.sourceforge.cilib.util.Cloneable;
+import net.sourceforge.cilib.util.selection.Selection;
/**
* @author Wiehann Matthysen
* @param <E>
*/
-public interface Weighing<E> extends Cloneable {
+public interface Weighing<E extends Comparable> extends Cloneable {
@Override
public Weighing<E> getClone();
- public List<Pair<Double, E>> weigh(List<E> elements);
+ public boolean weigh(List<Selection.Entry<E>> elements);
}
diff --git a/src/main/java/net/sourceforge/cilib/util/selection/weighing/entity/EntityWeighing.java b/src/main/java/net/sourceforge/cilib/util/selection/weighing/entity/EntityWeighing.java
index 2bf3565..0feb33c 100644
--- a/src/main/java/net/sourceforge/cilib/util/selection/weighing/entity/EntityWeighing.java
+++ b/src/main/java/net/sourceforge/cilib/util/selection/weighing/entity/EntityWeighing.java
@@ -22,13 +22,13 @@
package net.sourceforge.cilib.util.selection.weighing.entity;
import net.sourceforge.cilib.util.selection.weighing.*;
-import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import net.sourceforge.cilib.container.Pair;
import net.sourceforge.cilib.entity.Entity;
import net.sourceforge.cilib.problem.Fitness;
import net.sourceforge.cilib.problem.InferiorFitness;
+import net.sourceforge.cilib.util.selection.Selection;
/**
* @author Wiehann Matthysen
@@ -56,10 +56,10 @@ public class EntityWeighing<E extends Entity> implements Weighing<E> {
return new EntityWeighing(this);
}
- private Pair<Fitness, Fitness> getMinMaxFitness(Collection<E> entities) {
+ private Pair<Fitness, Fitness> getMinMaxFitness(Collection<Selection.Entry<E>> entities) {
Pair<Fitness, Fitness> minMaxFitness = new Pair<Fitness, Fitness>(InferiorFitness.instance(), InferiorFitness.instance());
- for (E entity : entities) {
- Fitness fitness = this.entityFitness.getFitness(entity);
+ for (Selection.Entry<E> entity : entities) {
+ Fitness fitness = this.entityFitness.getFitness(entity.getElement());
if (minMaxFitness.getKey() == InferiorFitness.instance() || fitness.compareTo(minMaxFitness.getKey()) < 0) {
minMaxFitness.setKey(fitness);
}
@@ -71,8 +71,8 @@ public class EntityWeighing<E extends Entity> implements Weighing<E> {
}
@Override
- public List<Pair<Double, E>> weigh(List<E> entities) {
- List<Pair<Double, E>> weighedEntities = new ArrayList<Pair<Double, E>>(entities.size());
+ public boolean weigh(List<Selection.Entry<E>> entities) {
+// List<Pair<Double, E>> weighedEntities = new ArrayList<Pair<Double, E>>(entities.size());
Pair<Fitness, Fitness> minMaxFitness = getMinMaxFitness(entities);
if (minMaxFitness.getKey() == InferiorFitness.instance() ||
@@ -82,11 +82,12 @@ public class EntityWeighing<E extends Entity> implements Weighing<E> {
double minMaxDifference = minMaxFitness.getValue().getValue() - minMaxFitness.getKey().getValue();
- for (E entity : entities) {
- double weight = (this.entityFitness.getFitness(entity).getValue() - minMaxFitness.getKey().getValue()) / minMaxDifference;
- weighedEntities.add(new Pair<Double, E>(weight, entity));
+ for (Selection.Entry<E> entity : entities) {
+ double weight = (this.entityFitness.getFitness(entity.getElement()).getValue() - minMaxFitness.getKey().getValue()) / minMaxDifference;
+ entity.setWeight(weight);
+// weighedEntities.add(new Pair<Double, E>(weight, entity));
}
-
- return weighedEntities;
+
+ return true;
}
}
diff --git a/src/test/java/net/sourceforge/cilib/entity/operators/selection/RouletteWheelSelectionStrategyTest.java b/src/test/java/net/sourceforge/cilib/entity/operators/selection/RouletteWheelSelectionStrategyTest.java
index 20f0699..4b69fc7 100644
--- a/src/test/java/net/sourceforge/cilib/entity/operators/selection/RouletteWheelSelectionStrategyTest.java
+++ b/src/test/java/net/sourceforge/cilib/entity/operators/selection/RouletteWheelSelectionStrategyTest.java
@@ -34,6 +34,7 @@ import net.sourceforge.cilib.problem.MaximisationFitness;
import net.sourceforge.cilib.problem.MinimisationFitness;
import org.junit.Before;
+import org.junit.Ignore;
import org.junit.Test;
/**
@@ -70,7 +71,7 @@ public class RouletteWheelSelectionStrategyTest {
RouletteWheelSelectionStrategy rouletteWheelSelectionStrategy = new RouletteWheelSelectionStrategy();
rouletteWheelSelectionStrategy.select(topology);
}
-
+
@Test
public void minimizationSelection() {
individual1.getProperties().put(EntityType.FITNESS, new MinimisationFitness(10000.0));
diff --git a/src/test/java/net/sourceforge/cilib/moo/archive/solutionweighing/SolutionWeighingTest.java b/src/test/java/net/sourceforge/cilib/moo/archive/solutionweighing/SolutionWeighingTest.java
index 4b36ce2..7be7d48 100644
--- a/src/test/java/net/sourceforge/cilib/moo/archive/solutionweighing/SolutionWeighingTest.java
+++ b/src/test/java/net/sourceforge/cilib/moo/archive/solutionweighing/SolutionWeighingTest.java
@@ -21,15 +21,15 @@
*/
package net.sourceforge.cilib.moo.archive.solutionweighing;
+import java.util.List;
+import net.sourceforge.cilib.util.selection.Selection.Entry;
import static org.hamcrest.core.Is.is;
-import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
-import net.sourceforge.cilib.container.Pair;
import net.sourceforge.cilib.problem.Fitness;
import net.sourceforge.cilib.problem.MOOptimisationProblem;
import net.sourceforge.cilib.problem.MinimisationFitness;
@@ -43,6 +43,7 @@ import net.sourceforge.cilib.type.types.Type;
import net.sourceforge.cilib.type.types.container.Vector;
import net.sourceforge.cilib.util.selection.Selection;
+import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
@@ -148,24 +149,23 @@ public class SolutionWeighingTest {
@Test
public void testAntiClusteringWeighingStrategy() {
- Collection<Pair<Double, OptimisationSolution>> weighedSolutions =
- Selection.from(solutions).apply(new AntiClusterWeighing()).select();
+ List<Entry<OptimisationSolution>> weighedSolutions = Selection.from(solutions).weigh(new AntiClusterWeighing()).entries();
- Iterator<Pair<Double, OptimisationSolution>> weighedSolutionIterator = weighedSolutions.iterator();
- Pair<Double, OptimisationSolution> weighedSolution = weighedSolutionIterator.next();
- assertEquals(0.290823093508, weighedSolution.getKey(), EPSILON);
+ Iterator<Entry<OptimisationSolution>> weighedSolutionIterator = weighedSolutions.iterator();
+ Selection.Entry<OptimisationSolution> weighedSolution = weighedSolutionIterator.next();
+ Assert.assertEquals(0.290823093508, weighedSolution.getWeight(), EPSILON);
weighedSolution = weighedSolutionIterator.next();
- assertEquals(0.367312140917, weighedSolution.getKey(), EPSILON);
+ Assert.assertEquals(0.367312140917, weighedSolution.getWeight(), EPSILON);
weighedSolution = weighedSolutionIterator.next();
- assertEquals(0.418956131219, weighedSolution.getKey(), EPSILON);
+ Assert.assertEquals(0.418956131219, weighedSolution.getWeight(), EPSILON);
weighedSolution = weighedSolutionIterator.next();
- assertEquals(0.266011104583, weighedSolution.getKey(), EPSILON);
+ Assert.assertEquals(0.266011104583, weighedSolution.getWeight(), EPSILON);
weighedSolution = weighedSolutionIterator.next();
- assertEquals(0.283305258318, weighedSolution.getKey(), EPSILON);
+ Assert.assertEquals(0.283305258318, weighedSolution.getWeight(), EPSILON);
assertThat(weighedSolutionIterator.hasNext(), is(false));
}
diff --git a/src/test/java/net/sourceforge/cilib/util/selection/ordering/OrderingTest.java b/src/test/java/net/sourceforge/cilib/util/selection/ordering/OrderingTest.java
index c92ab5c..5dc48e3 100644
--- a/src/test/java/net/sourceforge/cilib/util/selection/ordering/OrderingTest.java
+++ b/src/test/java/net/sourceforge/cilib/util/selection/ordering/OrderingTest.java
@@ -23,6 +23,7 @@ package net.sourceforge.cilib.util.selection.ordering;
import java.util.Arrays;
import java.util.List;
+import net.sourceforge.cilib.util.selection.Selection;
import org.junit.Assert;
import org.junit.Test;
@@ -39,7 +40,8 @@ public class OrderingTest {
@Test
public void randomOrdering() {
List<Integer> elements = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
- boolean ordered = new RandomOrdering<Integer>().order(elements);
+ List<Selection.Entry<Integer>> entries = Selection.from(elements).entries();
+ boolean ordered = new RandomOrdering<Integer>().order(entries);
Assert.assertTrue(ordered);
// TODO: We need to implement this.
}
@@ -47,36 +49,41 @@ public class OrderingTest {
@Test
public void reverseOrdering() {
List<Integer> elements = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
- boolean ordered = new ReverseOrdering<Integer>().order(elements);
+ List<Selection.Entry<Integer>> entries = Selection.from(elements).entries();
+ boolean ordered = new ReverseOrdering<Integer>().order(entries);
Assert.assertTrue(ordered);
+
for (int i = 0; i < 9; ++i) {
- Assert.assertEquals(elements.size() - i, elements.get(i).intValue());
+ Assert.assertEquals(elements.size() - i, entries.get(i).getElement().intValue());
}
}
@Test
public void ringBasedOrdering() {
List<Integer> elements = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
- boolean ordered = new RingBasedOrdering<Integer>(5).order(elements);
+ List<Selection.Entry<Integer>> entries = Selection.from(elements).entries();
+ boolean ordered = new RingBasedOrdering<Integer>(5).order(entries);
Assert.assertTrue(ordered);
- Assert.assertEquals(6, elements.get(0).intValue());
- Assert.assertEquals(7, elements.get(1).intValue());
- Assert.assertEquals(8, elements.get(2).intValue());
- Assert.assertEquals(9, elements.get(3).intValue());
- Assert.assertEquals(1, elements.get(4).intValue());
- Assert.assertEquals(2, elements.get(5).intValue());
- Assert.assertEquals(3, elements.get(6).intValue());
- Assert.assertEquals(4, elements.get(7).intValue());
- Assert.assertEquals(5, elements.get(8).intValue());
+ System.out.println("entries: " + entries);
+ Assert.assertEquals(6, entries.get(0).getElement().intValue());
+ Assert.assertEquals(7, entries.get(1).getElement().intValue());
+ Assert.assertEquals(8, entries.get(2).getElement().intValue());
+ Assert.assertEquals(9, entries.get(3).getElement().intValue());
+ Assert.assertEquals(1, entries.get(4).getElement().intValue());
+ Assert.assertEquals(2, entries.get(5).getElement().intValue());
+ Assert.assertEquals(3, entries.get(6).getElement().intValue());
+ Assert.assertEquals(4, entries.get(7).getElement().intValue());
+ Assert.assertEquals(5, entries.get(8).getElement().intValue());
}
@Test
public void sortedOrdering() {
List<Integer> elements = Arrays.asList(9, 8, 7, 6, 5, 4, 3, 2, 1);
- boolean ordered = new SortedOrdering().order(elements);
+ List<Selection.Entry<Integer>> entries = Selection.from(elements).entries();
+ boolean ordered = new SortedOrdering().order(entries);
Assert.assertTrue(ordered);
for (int i = 0; i < 9; ++i) {
- Assert.assertEquals(i + 1, elements.get(i).intValue());
+ Assert.assertEquals(i + ...
[truncated message content] |