|
From: Wiehann M. <wcm...@gm...> - 2009-05-23 02:44:50
|
This patch-set is the collection of patches that Gary and I worked on the last couple of days. It defines the new selection EDSL specification as well as utility classes (called recipes) that can be conveniently used in XML to achieve Tournament selection, Roulette Wheel Selection, Random Selection, Rank-based Selection and Ring-based Selection. |
|
From: Wiehann M. <wcm...@gm...> - 2009-05-23 00:47:08
|
From: Gary Pampara <gpa...@gm...>
The need to provide a wide variety of selection mechanisms is very
important. There is, however, a problem that is associated with this
concept and that is that it has resulted in a plethora of classes that
have been implemented to achieve this.
This patch proposes a new manner to do such a selection mechanism,
however, the selections are all generic and the process is fluent and
obvious.
More additions will be added - this is a positive step though!
Signed-off-by: Gary Pampara <gpa...@gm...>
---
.../selection/TournamentSelectionStrategy.java | 20 +--
.../selectionstrategies/generic/Ordering.java | 41 +++++
.../generic/RandomOrdering.java | 46 ++++++
.../selectionstrategies/generic/Selection.java | 156 ++++++++++++++++++++
.../generic/SortedOrdering.java | 46 ++++++
.../selectionstrategies/generic/SelectionTest.java | 107 +++++++++++++
6 files changed, 402 insertions(+), 14 deletions(-)
create mode 100644 src/main/java/net/sourceforge/cilib/util/selection/selectionstrategies/generic/Ordering.java
create mode 100644 src/main/java/net/sourceforge/cilib/util/selection/selectionstrategies/generic/RandomOrdering.java
create mode 100644 src/main/java/net/sourceforge/cilib/util/selection/selectionstrategies/generic/Selection.java
create mode 100644 src/main/java/net/sourceforge/cilib/util/selection/selectionstrategies/generic/SortedOrdering.java
create mode 100644 src/test/java/net/sourceforge/cilib/util/selection/selectionstrategies/generic/SelectionTest.java
diff --git a/src/main/java/net/sourceforge/cilib/entity/operators/selection/TournamentSelectionStrategy.java b/src/main/java/net/sourceforge/cilib/entity/operators/selection/TournamentSelectionStrategy.java
index 96f77bf..239f783 100644
--- a/src/main/java/net/sourceforge/cilib/entity/operators/selection/TournamentSelectionStrategy.java
+++ b/src/main/java/net/sourceforge/cilib/entity/operators/selection/TournamentSelectionStrategy.java
@@ -21,8 +21,6 @@
*/
package net.sourceforge.cilib.entity.operators.selection;
-import java.util.ArrayList;
-import java.util.Collections;
import java.util.List;
import net.sourceforge.cilib.controlparameter.ControlParameter;
@@ -31,6 +29,9 @@ import net.sourceforge.cilib.entity.Entity;
import net.sourceforge.cilib.entity.Topology;
import net.sourceforge.cilib.entity.topologies.TopologyHolder;
import net.sourceforge.cilib.math.random.RandomNumber;
+import net.sourceforge.cilib.util.selection.selectionstrategies.generic.RandomOrdering;
+import net.sourceforge.cilib.util.selection.selectionstrategies.generic.Selection;
+import net.sourceforge.cilib.util.selection.selectionstrategies.generic.SortedOrdering;
/**
* Perform a tournament selection process on the provided {@linkplain Topology}
@@ -72,19 +73,10 @@ public class TournamentSelectionStrategy extends SelectionStrategy {
*/
public <T extends Entity> T select(Topology<T> population) {
int tournamentSize = Double.valueOf(this.tournamentProportion.getParameter()*population.size()).intValue();
+ List<T> selection = Selection.from(population.asList()).apply(new RandomOrdering<T>()).first(tournamentSize)
+ .apply(new SortedOrdering<T>()).last().select();
- List<T> tournamentEntities = new ArrayList<T>();
-
- for (int i = 0; i < tournamentSize; i++) {
- double random = randomNumber.getUniform(0, population.size());
- T tmp = population.get(Double.valueOf(random).intValue());
-
- tournamentEntities.add(tmp);
- }
-
- Collections.sort(tournamentEntities, tournamentEntities.get(0).getComparator());
-
- return tournamentEntities.get(0);
+ return selection.get(0);
}
/**
diff --git a/src/main/java/net/sourceforge/cilib/util/selection/selectionstrategies/generic/Ordering.java b/src/main/java/net/sourceforge/cilib/util/selection/selectionstrategies/generic/Ordering.java
new file mode 100644
index 0000000..46e2a50
--- /dev/null
+++ b/src/main/java/net/sourceforge/cilib/util/selection/selectionstrategies/generic/Ordering.java
@@ -0,0 +1,41 @@
+/**
+ * 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.selectionstrategies.generic;
+
+import java.util.List;
+
+/**
+ * An ordering is a construct to define how a list of elements should be ordered.
+ * The ordering is a simple function that does a single action.
+ * @param <E> The type to apply the ordering to.
+ * @author gpampara
+ */
+public interface Ordering<E> {
+
+ /**
+ * Apply the ordering on the provided list.
+ * @param elements The list to be ordered.
+ * @return {@code true} if successful, {@code false} otherwise.
+ */
+ public boolean apply(List<E> elements);
+
+}
diff --git a/src/main/java/net/sourceforge/cilib/util/selection/selectionstrategies/generic/RandomOrdering.java b/src/main/java/net/sourceforge/cilib/util/selection/selectionstrategies/generic/RandomOrdering.java
new file mode 100644
index 0000000..6d307c6
--- /dev/null
+++ b/src/main/java/net/sourceforge/cilib/util/selection/selectionstrategies/generic/RandomOrdering.java
@@ -0,0 +1,46 @@
+/**
+ * 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.selectionstrategies.generic;
+
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * Apply a random ordering to the provided list. This class defines that the
+ * list instance will have it's internal order randomly shuffled.
+ * @param <E> The comparable type.
+ * @author gpampara
+ */
+public class RandomOrdering<E extends Comparable> implements Ordering<E> {
+
+ /**
+ * Randomly arrange the provided list.
+ * @param elements The list to randomly order.
+ * @return {@code true} if successful, {@code false} otherwise.
+ */
+ @Override
+ public boolean apply(List<E> elements) {
+ Collections.shuffle(elements);
+ return true;
+ }
+
+}
diff --git a/src/main/java/net/sourceforge/cilib/util/selection/selectionstrategies/generic/Selection.java b/src/main/java/net/sourceforge/cilib/util/selection/selectionstrategies/generic/Selection.java
new file mode 100644
index 0000000..04cb360
--- /dev/null
+++ b/src/main/java/net/sourceforge/cilib/util/selection/selectionstrategies/generic/Selection.java
@@ -0,0 +1,156 @@
+/**
+ * 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.selectionstrategies.generic;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * <p>
+ * A {@code Selection} is an abstraction that allows operations to be applied to
+ * a list instace that result in a selection of list elements, based on a varied of
+ * potential combination of operators.
+ * </p>
+ * <p>
+ * The {@code Selection} is implemented to be a fluent interface that is easily
+ * understandable and for which the intent of the selection is clear.
+ * </p>
+ * <p>
+ * As an example, applying tournament selection on a list of {@code Entity} instances
+ * would be done as follows:
+ * </p>
+ * <pre>
+ * List<T> selection = Selection.from(population.asList()).apply(new RandomOrdering<T>()).first(tournamentSize)
+ * .apply(new SortedOrdering<T>()).last().select();
+ *
+ * return selection.get(0);
+ * </pre>
+ * <p>
+ * where {@code T} is a {@link Comparable} type. The above performs the following steps:
+ * </p>
+ * <ol>
+ * <li>From the provided list of entities, order the entities randomly.</li>
+ * <li>From the randomized list, select the first {@code tournamentSize} entities.</li>
+ * <li>From the subset of elements, order them from smallest to largest, based on fitness.</li>
+ * <li>Finally, select the entity that is the "most fit" and then return it as the winner of the tournament.</li>
+ * </ol>
+ * @param <E> The comparable type.
+ * @author gpampara
+ */
+public final class Selection<E extends Comparable> {
+
+ private final List<E> elements;
+
+ /**
+ * Assign the Selection to take palce on the porvided list. The list
+ * is copied to ensure that the original list reference is not altered.
+ * @param elements The elements on which the selection should take place.
+ */
+ private Selection(List<E> elements) {
+ this.elements = new ArrayList(elements);
+ }
+
+ /**
+ * Create a selection that will operate from the provided list.
+ * @param <T> The comparable type.
+ * @param elements The list to operate on.
+ * @return A selection based on the provided list.
+ */
+ public static <T extends Comparable> Selection<T> from(List<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.
+ * @return A selection upon which the ordering has be applied.
+ * @throws UnsupportedOperationException if the ordering cannot be applied.
+ */
+ public Selection<E> apply(Ordering<E> ordering) {
+ boolean result = ordering.apply(elements);
+
+ if (result)
+ return new Selection<E>(elements);
+
+ throw new UnsupportedOperationException("The ordering [" + ordering.getClass().getSimpleName()
+ + "] cannot be applied to the selection. Please ensure that the intention of the ordering is correct.");
+ }
+
+ /**
+ * Obtain the first result from the current selection. These elements are returned
+ * from the front of the current selection.
+ * @return A selection containing the first element.
+ */
+ public Selection<E> first() {
+ return new Selection<E>(Arrays.asList(elements.get(0)));
+ }
+
+ /**
+ * Obtain the frist {@code number} of elements from the current selection. These
+ * elements are returned from the front of the current selection.
+ * @param number The number of elements to return.
+ * @return A selection containing the first {@code number} elements.
+ */
+ public Selection<E> first(int number) {
+ return new Selection<E>(elements.subList(0, number));
+ }
+
+ /**
+ * Obtain the last element contained within the current selection.
+ * @return A selection containing the last element.
+ */
+ public Selection<E> last() {
+ return new Selection<E>(Arrays.asList(elements.get(elements.size()-1)));
+ }
+
+ /**
+ * Obtain the last {@code number} of elements from the current selection.
+ * @param number The number of elements to select.
+ * @return A selection containing the last {@code number} of elements.
+ */
+ public Selection<E> last(int number) {
+ return new Selection<E>(elements.subList(elements.size()-number, elements.size()));
+ }
+
+ /**
+ * Return the reverse ordering of the provided selection.
+ * @return The selection in reverse.
+ */
+ public Selection<E> reverse() {
+ List<E> list = new ArrayList<E>(elements);
+ Collections.reverse(list);
+ return new Selection<E>(list);
+ }
+
+ /**
+ * Obtain the result of the selection.
+ * @return A list of elements that the selection has selected.
+ */
+ public List<E> select() {
+ return this.elements;
+ }
+
+}
diff --git a/src/main/java/net/sourceforge/cilib/util/selection/selectionstrategies/generic/SortedOrdering.java b/src/main/java/net/sourceforge/cilib/util/selection/selectionstrategies/generic/SortedOrdering.java
new file mode 100644
index 0000000..a89ec10
--- /dev/null
+++ b/src/main/java/net/sourceforge/cilib/util/selection/selectionstrategies/generic/SortedOrdering.java
@@ -0,0 +1,46 @@
+/**
+ * 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.selectionstrategies.generic;
+
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * Apply a sorting operation to the provided list, ordering the list naturally
+ * from smallest to largest.
+ * @param <E> The comparable type.
+ * @author gpampara
+ */
+public class SortedOrdering<E extends Comparable> implements Ordering<E> {
+
+ /**
+ * Sort the provided list in a natural order.
+ * @param element The list to sort.
+ * @return {@code true} if successful, {@code false} otherwise.
+ */
+ @Override
+ public boolean apply(List<E> element) {
+ Collections.sort(element);
+ return true;
+ }
+
+}
diff --git a/src/test/java/net/sourceforge/cilib/util/selection/selectionstrategies/generic/SelectionTest.java b/src/test/java/net/sourceforge/cilib/util/selection/selectionstrategies/generic/SelectionTest.java
new file mode 100644
index 0000000..2191cec
--- /dev/null
+++ b/src/test/java/net/sourceforge/cilib/util/selection/selectionstrategies/generic/SelectionTest.java
@@ -0,0 +1,107 @@
+/**
+ * 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.selectionstrategies.generic;
+
+import java.util.Arrays;
+import java.util.List;
+import net.sourceforge.cilib.ec.Individual;
+import net.sourceforge.cilib.entity.EntityType;
+import net.sourceforge.cilib.problem.Fitness;
+import net.sourceforge.cilib.problem.MaximisationFitness;
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ *
+ * @author gpampara
+ */
+public class SelectionTest {
+
+ @Test
+ public void randomSelectionOfFive() {
+ List<Integer> elements = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
+
+ List<Integer> selection = Selection.from(elements).apply(new RandomOrdering()).first(5).select();
+
+ Assert.assertEquals(5, selection.size());
+ }
+
+
+ @Test
+ public void tournamentSelection() {
+ List<Integer> elements = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
+ List<Integer> selection = Selection.from(elements).apply(new RandomOrdering()).first(4).apply(new SortedOrdering()).last().select();
+
+ Assert.assertEquals(1, selection.size());
+ }
+
+ @Test
+ public void lastSelection() {
+ List<Integer> elements = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
+ List<Integer> selection = Selection.from(elements).last(3).select();
+
+ Assert.assertEquals(3, selection.size());
+ Assert.assertTrue(selection.contains(7));
+ Assert.assertTrue(selection.contains(8));
+ Assert.assertTrue(selection.contains(9));
+
+ selection = Selection.from(elements).last().select();
+ Assert.assertEquals(1, selection.size());
+ Assert.assertEquals(9, selection.get(0).intValue());
+ }
+
+ @Test
+ public void firstSelection() {
+ List<Integer> elements = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
+ List<Integer> selection = Selection.from(elements).first(3).select();
+
+ Assert.assertEquals(3, selection.size());
+ Assert.assertEquals(1, selection.get(0).intValue());
+ Assert.assertEquals(2, selection.get(1).intValue());
+ Assert.assertEquals(3, selection.get(2).intValue());
+
+ selection = Selection.from(elements).first().select();
+ Assert.assertEquals(1, selection.get(0).intValue());
+ Assert.assertEquals(1, selection.size());
+ }
+
+ @Test
+ public void entitySelection() {
+ Individual i1 = createIndividual(new MaximisationFitness(1.0));
+ Individual i2 = createIndividual(new MaximisationFitness(2.0));
+ Individual i3 = createIndividual(new MaximisationFitness(3.0));
+
+ List<Individual> individuals = Arrays.asList(i1, i2, i3);
+ List<Individual> selection = Selection.from(individuals).apply(new SortedOrdering<Individual>()).first().select();
+
+ Assert.assertEquals(1, selection.size());
+ Assert.assertSame(i1, selection.get(0));
+ }
+
+ private Individual createIndividual(Fitness fitness) {
+ Individual i = new Individual();
+ i.getProperties().put(EntityType.FITNESS, fitness);
+ return i;
+ }
+
+}
--
1.6.0.6
|
|
From: Wiehann M. <wcm...@gm...> - 2009-05-23 00:54:27
|
Firstly, all of the SelectionStrategy classes as well as the various
interface definitions got removed. Also, related classes like the
WeighingStrategy classes with related interfaces got removed. ALL of
these classes got replaced with the new fluent selection classes. Some
of the implementations of these classes (like the
AntiClusteringWeighingStrategy class) got modified to comply to the new
fluent selection interface.
== Continued from previous patch ==
Implemented the concept of various syntax interfaces: SelectionSyntax,
OrderingSyntax as well as WeighingSyntax that defines the grammar of the
embedded domain-specific language (EDSL). Changed the Selection class
(that represents the builder in the builder-design pattern) to implement
these interfaces and also changed what the Selection class does during
the selection process: it changes state when one of the syntax-specific
methods gets called and returns either itself or another class to
specify state transitions in the language (a state machine that
specifies what methods can get called in sequence).
Forming part of the abovementioned changes is a new builder that gets
created when a selection gets weighed. This builder implements a
slightly different syntax: it can return a weighed selection when the
select() method gets called, or it can return the original (unpacked)
selection when the selectOriginal() method gets called.
In addition to the abovementioned changes is a collection of Weighing
classes that receives a collection of elements and weighs it in a
specific manner (for example: a collection of entities can get
normalised weights with respect to their fitness values).
Some additional Ordering classes were implemented like the Proportional
ordering class to select an element probabilistically from a list
depending on an element's weight. Also removed the reverse() method
from the Selection class and replaced it with a new ReverseOrdering
class to get the same effect.
To tie these classes together the concept of a SelectionRecipe is
introduced. These classes make use of the EDSL to select a single
element from a collection of elements. Eg. look at the
TournamentSelection class containing a one-liner selection statement.
They store state and can be used in XML - the EDSL is not XML-able.
Then, last but not least: a couple of small bugs were sorted out:
The ProportionalControlParameter class had a faulty getClone() method.
The Invididual and AbstractParticle classes now use the getComparator()
in the compareTo(...) methods to achieve the correct ordering. This was
an issue with the ElitistSelection class (used in the
ElitistSelectionStrategy class) as entities were sorted based on fitness
(from least fit to most fit) instead of using the
DescendingFitnessComparator logic to order them correctly.
---
.../KnowledgeTransferStrategy.java | 3 +-
.../SelectiveKnowledgeTransferStrategy.java | 44 ++--
.../ProportionalControlParameter.java | 11 +-
.../java/net/sourceforge/cilib/ec/Individual.java | 2 +-
.../selection/ElitistSelectionStrategy.java | 23 +-
.../selection/RandomSelectionStrategy.java | 13 +-
.../selection/RouletteWheelSelectionStrategy.java | 121 +---------
.../selection/TournamentSelectionStrategy.java | 39 +---
.../constrained/SetBasedConstrainedArchive.java | 27 +-
.../solutionweighing/AntiClusterWeighing.java | 115 +++++++++
.../archive/solutionweighing/SolutionWeighing.java | 46 ++++
.../AntiClusterWeighingStrategy.java | 119 ---------
.../SolutionWeighingStrategy.java | 48 ----
.../cilib/pso/particle/AbstractParticle.java | 2 +-
.../cilib/util/selection/OrderingSyntax.java | 33 +++
.../cilib/util/selection/Selection.java | 170 ++++++++++++
.../cilib/util/selection/SelectionSyntax.java | 41 +++
.../cilib/util/selection/WeighedSelection.java | 102 ++++++++
.../cilib/util/selection/WeighingSyntax.java | 34 +++
.../cilib/util/selection/ordering/Ordering.java | 40 +++
.../selection/ordering/ProportionalOrdering.java | 74 ++++++
.../util/selection/ordering/RandomOrdering.java | 57 ++++
.../util/selection/ordering/ReverseOrdering.java | 38 +++
.../util/selection/ordering/RingBasedOrdering.java | 55 ++++
.../util/selection/ordering/SortedOrdering.java | 64 +++++
.../util/selection/recipes/ElitistSelection.java | 52 ++++
.../util/selection/recipes/RandomSelection.java | 58 +++++
.../util/selection/recipes/RankBasedSelection.java | 59 +++++
.../recipes/RingBasedPopulationSelection.java | 52 ++++
.../selection/recipes/RouletteWheelSelection.java | 71 +++++
.../util/selection/recipes/SelectionRecipe.java | 37 +++
.../selection/recipes/TournamentSelection.java | 80 ++++++
.../IndexedSelectionStrategy.java | 79 ------
.../NormalisedProbabilisticSelectionStrategy.java | 72 -----
.../PopulationSelectionStrategy.java | 40 ---
.../ProbabilisticSelectionStrategy.java | 99 -------
.../RandomSelectionStrategy.java | 68 -----
.../RankBasedSelectionStrategy.java | 66 -----
.../RingBasedPopulationSelectionStrategy.java | 62 -----
.../selectionstrategies/SelectionStrategy.java | 44 ----
.../SimpleSelectionStrategy.java | 51 ----
.../TournamentSelectionStrategy.java | 87 -------
.../WeighedSelectionStrategy.java | 87 -------
.../selectionstrategies/generic/Ordering.java | 41 ---
.../generic/RandomOrdering.java | 46 ----
.../selectionstrategies/generic/Selection.java | 156 -----------
.../generic/SortedOrdering.java | 46 ----
.../util/selection/weighing/FixedWeighing.java | 70 +++++
.../util/selection/weighing/LinearWeighing.java | 83 ++++++
.../cilib/util/selection/weighing/Weighing.java | 38 +++
.../selection/weighing/entity/CurrentFitness.java | 37 +++
.../selection/weighing/entity/EntityFitness.java | 34 +++
.../selection/weighing/entity/EntityWeighing.java | 92 +++++++
.../weighing/entity/SocialBestFitness.java | 36 +++
.../weighingstrategies/EntityWeighingStrategy.java | 41 ---
.../FitnessBasedEntityWeighingStrategy.java | 96 -------
.../InverseWeighingStrategyDecorator.java | 68 -----
.../weighingstrategies/LinearWeighingStrategy.java | 86 ------
.../UniformWeighingStrategy.java | 70 -----
.../weighingstrategies/WeighingStrategy.java | 42 ---
.../cilib/moo/archive/constrained/ArchiveTest.java | 3 +
.../constrained/ConstrainedArchiveTest.java | 24 +-
.../solutionweighing/SolutionWeighingTest.java | 172 +++++++++++++
.../SolutionWeighingStrategyTest.java | 170 ------------
.../cilib/util/selection/SelectionTest.java | 65 +++++
.../util/selection/ordering/OrderingTest.java | 82 ++++++
.../selection/recipes/SelectionRecipeTest.java | 61 +++++
.../selectionstrategies/SelectionStrategyTest.java | 271 --------------------
.../selectionstrategies/generic/SelectionTest.java | 107 --------
.../util/selection/weighing/WeighingTest.java | 59 +++++
.../weighing/entity/EntityWeighingTest.java | 58 +++++
.../weighingstrategies/WeighingStrategyTest.java | 75 ------
xml/dynamic-vepso.xml | 8 +-
xml/vepso.xml | 104 ++++----
74 files changed, 2308 insertions(+), 2518 deletions(-)
create mode 100644 src/main/java/net/sourceforge/cilib/moo/archive/solutionweighing/AntiClusterWeighing.java
create mode 100644 src/main/java/net/sourceforge/cilib/moo/archive/solutionweighing/SolutionWeighing.java
delete mode 100644 src/main/java/net/sourceforge/cilib/moo/archive/solutionweighingstrategies/AntiClusterWeighingStrategy.java
delete mode 100644 src/main/java/net/sourceforge/cilib/moo/archive/solutionweighingstrategies/SolutionWeighingStrategy.java
create mode 100644 src/main/java/net/sourceforge/cilib/util/selection/OrderingSyntax.java
create mode 100644 src/main/java/net/sourceforge/cilib/util/selection/Selection.java
create mode 100644 src/main/java/net/sourceforge/cilib/util/selection/SelectionSyntax.java
create mode 100644 src/main/java/net/sourceforge/cilib/util/selection/WeighedSelection.java
create mode 100644 src/main/java/net/sourceforge/cilib/util/selection/WeighingSyntax.java
create mode 100644 src/main/java/net/sourceforge/cilib/util/selection/ordering/Ordering.java
create mode 100644 src/main/java/net/sourceforge/cilib/util/selection/ordering/ProportionalOrdering.java
create mode 100644 src/main/java/net/sourceforge/cilib/util/selection/ordering/RandomOrdering.java
create mode 100644 src/main/java/net/sourceforge/cilib/util/selection/ordering/ReverseOrdering.java
create mode 100644 src/main/java/net/sourceforge/cilib/util/selection/ordering/RingBasedOrdering.java
create mode 100644 src/main/java/net/sourceforge/cilib/util/selection/ordering/SortedOrdering.java
create mode 100644 src/main/java/net/sourceforge/cilib/util/selection/recipes/ElitistSelection.java
create mode 100644 src/main/java/net/sourceforge/cilib/util/selection/recipes/RandomSelection.java
create mode 100644 src/main/java/net/sourceforge/cilib/util/selection/recipes/RankBasedSelection.java
create mode 100644 src/main/java/net/sourceforge/cilib/util/selection/recipes/RingBasedPopulationSelection.java
create mode 100644 src/main/java/net/sourceforge/cilib/util/selection/recipes/RouletteWheelSelection.java
create mode 100644 src/main/java/net/sourceforge/cilib/util/selection/recipes/SelectionRecipe.java
create mode 100644 src/main/java/net/sourceforge/cilib/util/selection/recipes/TournamentSelection.java
delete mode 100644 src/main/java/net/sourceforge/cilib/util/selection/selectionstrategies/IndexedSelectionStrategy.java
delete mode 100644 src/main/java/net/sourceforge/cilib/util/selection/selectionstrategies/NormalisedProbabilisticSelectionStrategy.java
delete mode 100644 src/main/java/net/sourceforge/cilib/util/selection/selectionstrategies/PopulationSelectionStrategy.java
delete mode 100644 src/main/java/net/sourceforge/cilib/util/selection/selectionstrategies/ProbabilisticSelectionStrategy.java
delete mode 100644 src/main/java/net/sourceforge/cilib/util/selection/selectionstrategies/RandomSelectionStrategy.java
delete mode 100644 src/main/java/net/sourceforge/cilib/util/selection/selectionstrategies/RankBasedSelectionStrategy.java
delete mode 100644 src/main/java/net/sourceforge/cilib/util/selection/selectionstrategies/RingBasedPopulationSelectionStrategy.java
delete mode 100644 src/main/java/net/sourceforge/cilib/util/selection/selectionstrategies/SelectionStrategy.java
delete mode 100644 src/main/java/net/sourceforge/cilib/util/selection/selectionstrategies/SimpleSelectionStrategy.java
delete mode 100644 src/main/java/net/sourceforge/cilib/util/selection/selectionstrategies/TournamentSelectionStrategy.java
delete mode 100644 src/main/java/net/sourceforge/cilib/util/selection/selectionstrategies/WeighedSelectionStrategy.java
delete mode 100644 src/main/java/net/sourceforge/cilib/util/selection/selectionstrategies/generic/Ordering.java
delete mode 100644 src/main/java/net/sourceforge/cilib/util/selection/selectionstrategies/generic/RandomOrdering.java
delete mode 100644 src/main/java/net/sourceforge/cilib/util/selection/selectionstrategies/generic/Selection.java
delete mode 100644 src/main/java/net/sourceforge/cilib/util/selection/selectionstrategies/generic/SortedOrdering.java
create mode 100644 src/main/java/net/sourceforge/cilib/util/selection/weighing/FixedWeighing.java
create mode 100644 src/main/java/net/sourceforge/cilib/util/selection/weighing/LinearWeighing.java
create mode 100644 src/main/java/net/sourceforge/cilib/util/selection/weighing/Weighing.java
create mode 100644 src/main/java/net/sourceforge/cilib/util/selection/weighing/entity/CurrentFitness.java
create mode 100644 src/main/java/net/sourceforge/cilib/util/selection/weighing/entity/EntityFitness.java
create mode 100644 src/main/java/net/sourceforge/cilib/util/selection/weighing/entity/EntityWeighing.java
create mode 100644 src/main/java/net/sourceforge/cilib/util/selection/weighing/entity/SocialBestFitness.java
delete mode 100644 src/main/java/net/sourceforge/cilib/util/selection/weighingstrategies/EntityWeighingStrategy.java
delete mode 100644 src/main/java/net/sourceforge/cilib/util/selection/weighingstrategies/FitnessBasedEntityWeighingStrategy.java
delete mode 100644 src/main/java/net/sourceforge/cilib/util/selection/weighingstrategies/InverseWeighingStrategyDecorator.java
delete mode 100644 src/main/java/net/sourceforge/cilib/util/selection/weighingstrategies/LinearWeighingStrategy.java
delete mode 100644 src/main/java/net/sourceforge/cilib/util/selection/weighingstrategies/UniformWeighingStrategy.java
delete mode 100644 src/main/java/net/sourceforge/cilib/util/selection/weighingstrategies/WeighingStrategy.java
create mode 100644 src/test/java/net/sourceforge/cilib/moo/archive/solutionweighing/SolutionWeighingTest.java
delete mode 100644 src/test/java/net/sourceforge/cilib/moo/archive/solutionweighingstrategies/SolutionWeighingStrategyTest.java
create mode 100644 src/test/java/net/sourceforge/cilib/util/selection/SelectionTest.java
create mode 100644 src/test/java/net/sourceforge/cilib/util/selection/ordering/OrderingTest.java
create mode 100644 src/test/java/net/sourceforge/cilib/util/selection/recipes/SelectionRecipeTest.java
delete mode 100644 src/test/java/net/sourceforge/cilib/util/selection/selectionstrategies/SelectionStrategyTest.java
delete mode 100644 src/test/java/net/sourceforge/cilib/util/selection/selectionstrategies/generic/SelectionTest.java
create mode 100644 src/test/java/net/sourceforge/cilib/util/selection/weighing/WeighingTest.java
create mode 100644 src/test/java/net/sourceforge/cilib/util/selection/weighing/entity/EntityWeighingTest.java
delete mode 100644 src/test/java/net/sourceforge/cilib/util/selection/weighingstrategies/WeighingStrategyTest.java
diff --git a/src/main/java/net/sourceforge/cilib/algorithm/population/knowledgetransferstrategies/KnowledgeTransferStrategy.java b/src/main/java/net/sourceforge/cilib/algorithm/population/knowledgetransferstrategies/KnowledgeTransferStrategy.java
index 6626c16..6d6ba98 100644
--- a/src/main/java/net/sourceforge/cilib/algorithm/population/knowledgetransferstrategies/KnowledgeTransferStrategy.java
+++ b/src/main/java/net/sourceforge/cilib/algorithm/population/knowledgetransferstrategies/KnowledgeTransferStrategy.java
@@ -23,6 +23,7 @@ package net.sourceforge.cilib.algorithm.population.knowledgetransferstrategies;
import java.util.List;
+import net.sourceforge.cilib.algorithm.population.MultiPopulationBasedAlgorithm;
import net.sourceforge.cilib.algorithm.population.PopulationBasedAlgorithm;
import net.sourceforge.cilib.type.types.Type;
import net.sourceforge.cilib.util.Cloneable;
@@ -44,7 +45,7 @@ public interface KnowledgeTransferStrategy extends Cloneable {
/**
* Returns knowledge that was gained from an entity within the list of populations.
* @param allPopulations The list of populations that will be used to select an
- * entity from who's knowledge will be used.
+ * entity who's knowledge will be used.
* @return The knowledge that was gained.
*/
public Type transferKnowledge(List<PopulationBasedAlgorithm> allPopulations);
diff --git a/src/main/java/net/sourceforge/cilib/algorithm/population/knowledgetransferstrategies/SelectiveKnowledgeTransferStrategy.java b/src/main/java/net/sourceforge/cilib/algorithm/population/knowledgetransferstrategies/SelectiveKnowledgeTransferStrategy.java
index 6111a8e..fd73ff9 100644
--- a/src/main/java/net/sourceforge/cilib/algorithm/population/knowledgetransferstrategies/SelectiveKnowledgeTransferStrategy.java
+++ b/src/main/java/net/sourceforge/cilib/algorithm/population/knowledgetransferstrategies/SelectiveKnowledgeTransferStrategy.java
@@ -27,16 +27,17 @@ import net.sourceforge.cilib.algorithm.population.MultiPopulationBasedAlgorithm;
import net.sourceforge.cilib.algorithm.population.PopulationBasedAlgorithm;
import net.sourceforge.cilib.entity.Entity;
import net.sourceforge.cilib.type.types.Type;
-import net.sourceforge.cilib.util.selection.selectionstrategies.RandomSelectionStrategy;
-import net.sourceforge.cilib.util.selection.selectionstrategies.RingBasedPopulationSelectionStrategy;
-import net.sourceforge.cilib.util.selection.selectionstrategies.SelectionStrategy;
+import net.sourceforge.cilib.util.selection.Selection;
+import net.sourceforge.cilib.util.selection.recipes.RankBasedSelection;
+import net.sourceforge.cilib.util.selection.recipes.RingBasedPopulationSelection;
+import net.sourceforge.cilib.util.selection.recipes.SelectionRecipe;
/**
* <p>
- * An implementation of {@link KnowledgeTransferStrategy} where two {@link SelectionStrategy}
+ * An implementation of {@link KnowledgeTransferStrategy} where two {@link Selection}
* instances are used to first select a sub-population ({@link PopulationBasedAlgorithm}) from
* a collection of population-based algorithms (see {@link MultiPopulationBasedAlgorithm) and then
- * within this sub-population's {@link Topology} , which entity's knowledge is to be transfered
+ * within this sub-population's {@link Topology}, which entity's knowledge is to be transfered
* to the caller requesting it.
* </p>
*
@@ -45,17 +46,18 @@ import net.sourceforge.cilib.util.selection.selectionstrategies.SelectionStrateg
public class SelectiveKnowledgeTransferStrategy implements KnowledgeTransferStrategy {
private static final long serialVersionUID = 402688951924934682L;
- private SelectionStrategy<PopulationBasedAlgorithm> populationSelectionStrategy;
- private SelectionStrategy<Entity> entitySelectionStrategy;
+
+ private SelectionRecipe<PopulationBasedAlgorithm> populationSelection;
+ private SelectionRecipe<Entity> entitySelection;
public SelectiveKnowledgeTransferStrategy() {
- this.populationSelectionStrategy = new RingBasedPopulationSelectionStrategy();
- this.entitySelectionStrategy = new RandomSelectionStrategy<Entity>();
+ this.populationSelection = new RingBasedPopulationSelection();
+ this.entitySelection = new RankBasedSelection<Entity>();
}
public SelectiveKnowledgeTransferStrategy(SelectiveKnowledgeTransferStrategy copy) {
- this.populationSelectionStrategy = copy.populationSelectionStrategy.getClone();
- this.entitySelectionStrategy = copy.entitySelectionStrategy.getClone();
+ this.populationSelection = copy.populationSelection.getClone();
+ this.entitySelection = copy.entitySelection.getClone();
}
@Override
@@ -63,26 +65,26 @@ public class SelectiveKnowledgeTransferStrategy implements KnowledgeTransferStra
return new SelectiveKnowledgeTransferStrategy(this);
}
- public void setPopulationSelectionStrategy(SelectionStrategy<PopulationBasedAlgorithm> populationSelectionStrategy) {
- this.populationSelectionStrategy = populationSelectionStrategy;
+ public void setPopulationSelection(SelectionRecipe<PopulationBasedAlgorithm> populationSelection) {
+ this.populationSelection = populationSelection;
}
- public SelectionStrategy<PopulationBasedAlgorithm> getPopulationBasedAlgorithm() {
- return this.populationSelectionStrategy;
+ public SelectionRecipe<PopulationBasedAlgorithm> getPopulationSelection() {
+ return this.populationSelection;
}
- public void setEntitySelectionStrategy(SelectionStrategy<Entity> entitySelectionStrategy) {
- this.entitySelectionStrategy = entitySelectionStrategy;
+ public void setEntitySelection(SelectionRecipe<Entity> entitySelection) {
+ this.entitySelection = entitySelection;
}
- public SelectionStrategy<Entity> getEntitySelectionStrategy() {
- return this.entitySelectionStrategy;
+ public SelectionRecipe<Entity> getEntitySelection() {
+ return this.entitySelection;
}
@Override
public Type transferKnowledge(List<PopulationBasedAlgorithm> allPopulations) {
- PopulationBasedAlgorithm population = this.populationSelectionStrategy.select(allPopulations);
- Entity entity = this.entitySelectionStrategy.select(population.getTopology());
+ PopulationBasedAlgorithm population = this.populationSelection.select(allPopulations);
+ Entity entity = this.entitySelection.select(population.getTopology());
return entity.getProperties();
}
}
diff --git a/src/main/java/net/sourceforge/cilib/controlparameter/ProportionalControlParameter.java b/src/main/java/net/sourceforge/cilib/controlparameter/ProportionalControlParameter.java
index f00eba1..816c998 100644
--- a/src/main/java/net/sourceforge/cilib/controlparameter/ProportionalControlParameter.java
+++ b/src/main/java/net/sourceforge/cilib/controlparameter/ProportionalControlParameter.java
@@ -38,16 +38,22 @@ public class ProportionalControlParameter implements ControlParameter {
this.proportion = 0.1;
}
+ public ProportionalControlParameter(ProportionalControlParameter copy) {
+ this.proportion = copy.proportion;
+ }
+
/**
* {@inheritDoc}
*/
+ @Override
public ProportionalControlParameter getClone() {
- return null;
+ return new ProportionalControlParameter(this);
}
/**
* {@inheritDoc}
*/
+ @Override
public double getParameter() {
return this.proportion;
}
@@ -55,6 +61,7 @@ public class ProportionalControlParameter implements ControlParameter {
/**
* {@inheritDoc}
*/
+ @Override
public double getParameter(double min, double max) {
double diff = max - min;
return this.proportion * diff;
@@ -63,6 +70,7 @@ public class ProportionalControlParameter implements ControlParameter {
/**
* {@inheritDoc}
*/
+ @Override
public void setParameter(double value) {
if (value < 0)
throw new IllegalArgumentException("The proportion must be positive");
@@ -73,6 +81,7 @@ public class ProportionalControlParameter implements ControlParameter {
/**
* {@inheritDoc}
*/
+ @Override
public void updateParameter() {
}
}
diff --git a/src/main/java/net/sourceforge/cilib/ec/Individual.java b/src/main/java/net/sourceforge/cilib/ec/Individual.java
index 5557fbe..15dfd0a 100644
--- a/src/main/java/net/sourceforge/cilib/ec/Individual.java
+++ b/src/main/java/net/sourceforge/cilib/ec/Individual.java
@@ -126,7 +126,7 @@ public class Individual extends AbstractEntity {
*/
@Override
public int compareTo(Entity o) {
- return this.getFitness().compareTo(o.getFitness());
+ return getComparator().compare(this, o);
}
/**
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 f594e12..4936a9e 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
@@ -19,33 +19,34 @@
* 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.entity.operators.selection;
-import java.util.Collections;
-
import net.sourceforge.cilib.controlparameter.ControlParameter;
import net.sourceforge.cilib.controlparameter.ProportionalControlParameter;
import net.sourceforge.cilib.entity.Entity;
import net.sourceforge.cilib.entity.Topology;
-import net.sourceforge.cilib.entity.topologies.GBestTopology;
import net.sourceforge.cilib.entity.topologies.TopologyHolder;
+import net.sourceforge.cilib.util.selection.recipes.ElitistSelection;
public class ElitistSelectionStrategy extends SelectionStrategy {
private static final long serialVersionUID = -3055600262753819388L;
-
+
private ControlParameter selectionPercentage;
public ElitistSelectionStrategy() {
this.selectionPercentage = new ProportionalControlParameter();
}
+ public ElitistSelectionStrategy(ElitistSelectionStrategy copy) {
+ this.selectionPercentage = copy.selectionPercentage.getClone();
+ }
+
/**
* {@inheritDoc}
*/
@Override
- public SelectionStrategy getClone() {
- return this;
+ public ElitistSelectionStrategy getClone() {
+ return new ElitistSelectionStrategy(this);
}
/**
@@ -58,12 +59,7 @@ public class ElitistSelectionStrategy extends SelectionStrategy {
*/
@Override
public <T extends Entity> T select(Topology<T> population) {
- Topology<T> tmp = new GBestTopology<T>();
- tmp.addAll(population);
-
- Collections.sort(tmp, tmp.get(0).getComparator());
-
- return tmp.get(0);
+ return new ElitistSelection<T>().select(population);
}
/**
@@ -98,5 +94,4 @@ public class ElitistSelectionStrategy extends SelectionStrategy {
public void setSelectionPercentage(ControlParameter selectionPercentage) {
this.selectionPercentage = selectionPercentage;
}
-
}
diff --git a/src/main/java/net/sourceforge/cilib/entity/operators/selection/RandomSelectionStrategy.java b/src/main/java/net/sourceforge/cilib/entity/operators/selection/RandomSelectionStrategy.java
index 0b4728e..7b522bc 100644
--- a/src/main/java/net/sourceforge/cilib/entity/operators/selection/RandomSelectionStrategy.java
+++ b/src/main/java/net/sourceforge/cilib/entity/operators/selection/RandomSelectionStrategy.java
@@ -21,12 +21,10 @@
*/
package net.sourceforge.cilib.entity.operators.selection;
-import java.util.Random;
-
import net.sourceforge.cilib.entity.Entity;
import net.sourceforge.cilib.entity.Topology;
import net.sourceforge.cilib.entity.topologies.TopologyHolder;
-import net.sourceforge.cilib.math.random.generator.MersenneTwister;
+import net.sourceforge.cilib.util.selection.recipes.RandomSelection;
/**
*
@@ -36,21 +34,17 @@ import net.sourceforge.cilib.math.random.generator.MersenneTwister;
public class RandomSelectionStrategy extends SelectionStrategy {
private static final long serialVersionUID = -216894674927488180L;
- private Random random;
-
public RandomSelectionStrategy() {
- this.random = new MersenneTwister();
}
@Override
- public SelectionStrategy getClone() {
+ public RandomSelectionStrategy getClone() {
return new RandomSelectionStrategy();
}
@Override
public <T extends Entity> T select(Topology<T> population) {
- int randomNumber = random.nextInt(population.size());
- return population.get(randomNumber);
+ return new RandomSelection<T>().select(population);
}
@Override
@@ -62,5 +56,4 @@ public class RandomSelectionStrategy extends SelectionStrategy {
holder.add(select(topology));
// offspring.add(select(topology));
}
-
}
diff --git a/src/main/java/net/sourceforge/cilib/entity/operators/selection/RouletteWheelSelectionStrategy.java b/src/main/java/net/sourceforge/cilib/entity/operators/selection/RouletteWheelSelectionStrategy.java
index e44471b..08b8713 100644
--- a/src/main/java/net/sourceforge/cilib/entity/operators/selection/RouletteWheelSelectionStrategy.java
+++ b/src/main/java/net/sourceforge/cilib/entity/operators/selection/RouletteWheelSelectionStrategy.java
@@ -24,10 +24,8 @@ package net.sourceforge.cilib.entity.operators.selection;
import net.sourceforge.cilib.entity.Entity;
import net.sourceforge.cilib.entity.Topology;
import net.sourceforge.cilib.entity.topologies.TopologyHolder;
-import net.sourceforge.cilib.math.random.generator.MersenneTwister;
-import net.sourceforge.cilib.math.random.generator.Random;
-import net.sourceforge.cilib.problem.InferiorFitness;
-import net.sourceforge.cilib.problem.MinimisationFitness;
+import net.sourceforge.cilib.util.selection.recipes.RouletteWheelSelection;
+import net.sourceforge.cilib.util.selection.weighing.entity.EntityWeighing;
/**
* This class implements Roulette Wheel selection, also known as proportionate
@@ -44,13 +42,11 @@ import net.sourceforge.cilib.problem.MinimisationFitness;
*/
public class RouletteWheelSelectionStrategy extends SelectionStrategy {
private static final long serialVersionUID = 6827649649373047787L;
- private Random random;
/**
* Create an instance of the {@linkplain RouletteWheelSelectionStrategy}.
*/
public RouletteWheelSelectionStrategy() {
- this.random = new MersenneTwister();
}
/**
@@ -58,7 +54,6 @@ public class RouletteWheelSelectionStrategy extends SelectionStrategy {
* @param copy The instance to copy.
*/
public RouletteWheelSelectionStrategy(final RouletteWheelSelectionStrategy copy) {
- this.random = copy.random.getClone();
}
/**
@@ -74,32 +69,9 @@ public class RouletteWheelSelectionStrategy extends SelectionStrategy {
*/
@Override
public <T extends Entity> T select(final Topology<T> population) {
- double minimumFitness = getMinimumFitness(population);
- double maximumFitness = getMaximumFitness(population);
-
- double totalFitness = getTotalFitness(population, minimumFitness, maximumFitness);
- double cumulativeProb = 0.0;
- double valueToPick = random.nextDouble();
-
- // If the fitness' have not been calculated return a random entity. This should NEVER happen.
- if (Double.compare(totalFitness, InferiorFitness.instance().getValue()) == 0)
- throw new UnsupportedOperationException("Cannot perform selection operator on Topology of Entity. Each Entity is incorrectly defined to have an InferiorFitness. Initial Fitness' need to...
[truncated message content] |
|
From: Wiehann M. <wcm...@gm...> - 2009-05-23 00:47:34
|
Changed the compareTo methods of the Individual and AbstractParticle
back to the previous state. However, this required additional state to
be added in the form of comparators to the various selection recipe
classes. Modified the ElitistSelectionStrategy class to pass a
DescendingFitnessComparator to the selection recipe as well as the
TournamentSelectionStrategy class. Some changes were added to the
vepso.xml and dynamic-vepso.xml files to use
DescendingFitnessComparators for the selection recipes.
---
.../java/net/sourceforge/cilib/ec/Individual.java | 2 +-
.../comparator/AscendingFitnessComparator.java | 5 ++-
.../comparator/DescendingFitnessComparator.java | 5 ++-
.../comparator/SocialBestFitnessComparator.java | 4 +-
.../selection/ElitistSelectionStrategy.java | 3 +-
.../selection/RouletteWheelSelectionStrategy.java | 4 +--
.../selection/TournamentSelectionStrategy.java | 2 +
.../cilib/pso/particle/AbstractParticle.java | 2 +-
.../util/selection/ordering/DefaultComparator.java | 32 ++++++++++++++++++++
.../util/selection/ordering/SortedOrdering.java | 8 -----
.../util/selection/recipes/ElitistSelection.java | 20 ++++++++++++-
.../util/selection/recipes/RandomSelection.java | 4 ++
.../util/selection/recipes/RankBasedSelection.java | 25 ++++++++++++++-
.../selection/recipes/RouletteWheelSelection.java | 12 ++++++-
.../selection/recipes/TournamentSelection.java | 25 +++++++++------
xml/dynamic-vepso.xml | 4 ++-
xml/vepso.xml | 4 ++-
17 files changed, 124 insertions(+), 37 deletions(-)
create mode 100644 src/main/java/net/sourceforge/cilib/util/selection/ordering/DefaultComparator.java
diff --git a/src/main/java/net/sourceforge/cilib/ec/Individual.java b/src/main/java/net/sourceforge/cilib/ec/Individual.java
index 15dfd0a..5557fbe 100644
--- a/src/main/java/net/sourceforge/cilib/ec/Individual.java
+++ b/src/main/java/net/sourceforge/cilib/ec/Individual.java
@@ -126,7 +126,7 @@ public class Individual extends AbstractEntity {
*/
@Override
public int compareTo(Entity o) {
- return getComparator().compare(this, o);
+ return this.getFitness().compareTo(o.getFitness());
}
/**
diff --git a/src/main/java/net/sourceforge/cilib/entity/comparator/AscendingFitnessComparator.java b/src/main/java/net/sourceforge/cilib/entity/comparator/AscendingFitnessComparator.java
index af05755..8c5ee29 100644
--- a/src/main/java/net/sourceforge/cilib/entity/comparator/AscendingFitnessComparator.java
+++ b/src/main/java/net/sourceforge/cilib/entity/comparator/AscendingFitnessComparator.java
@@ -33,7 +33,7 @@ import net.sourceforge.cilib.problem.Fitness;
*
* @author Gary Pampara
*/
-public class AscendingFitnessComparator implements Comparator<Entity> {
+public class AscendingFitnessComparator<E extends Entity> implements Comparator<E> {
/**
* Compare the {@linkplain Entity} objects returning the desired ordering.
@@ -43,7 +43,8 @@ public class AscendingFitnessComparator implements Comparator<Entity> {
* 0 if e1 and e2 are equal
* 1 if e2 is greater than e1
*/
- public int compare(Entity e1, Entity e2) {
+ @Override
+ public int compare(E e1, E e2) {
Fitness f1 = e1.getFitness();
Fitness f2 = e2.getFitness();
diff --git a/src/main/java/net/sourceforge/cilib/entity/comparator/DescendingFitnessComparator.java b/src/main/java/net/sourceforge/cilib/entity/comparator/DescendingFitnessComparator.java
index 8c6b7c6..9b9530f 100644
--- a/src/main/java/net/sourceforge/cilib/entity/comparator/DescendingFitnessComparator.java
+++ b/src/main/java/net/sourceforge/cilib/entity/comparator/DescendingFitnessComparator.java
@@ -33,7 +33,7 @@ import net.sourceforge.cilib.problem.Fitness;
*
* @author Gary Pampara
*/
-public class DescendingFitnessComparator implements Comparator<Entity> {
+public class DescendingFitnessComparator<E extends Entity> implements Comparator<E> {
/**
* Compare the {@linkplain Entity} objects returning the desired ordering.
@@ -43,7 +43,8 @@ public class DescendingFitnessComparator implements Comparator<Entity> {
* 0 if e1 and e2 are equal
* 1 if e2 is greater than e2
*/
- public int compare(Entity e1, Entity e2) {
+ @Override
+ public int compare(E e1, E e2) {
Fitness f1 = e1.getFitness();
Fitness f2 = e2.getFitness();
diff --git a/src/main/java/net/sourceforge/cilib/entity/comparator/SocialBestFitnessComparator.java b/src/main/java/net/sourceforge/cilib/entity/comparator/SocialBestFitnessComparator.java
index a0d5e1f..5db1c32 100644
--- a/src/main/java/net/sourceforge/cilib/entity/comparator/SocialBestFitnessComparator.java
+++ b/src/main/java/net/sourceforge/cilib/entity/comparator/SocialBestFitnessComparator.java
@@ -32,13 +32,13 @@ import net.sourceforge.cilib.problem.Fitness;
* @see SocialEntity#getSocialBestFitness()
* @author gpampara
*/
-public class SocialBestFitnessComparator implements Comparator<SocialEntity> {
+public class SocialBestFitnessComparator<E extends SocialEntity> implements Comparator<E> {
/**
* {@inheritDoc}
*/
@Override
- public int compare(SocialEntity o1, SocialEntity o2) {
+ public int compare(E o1, E o2) {
Fitness f1 = o1.getSocialBestFitness();
Fitness f2 = o2.getSocialBestFitness();
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 4936a9e..2a2cea5 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
@@ -25,6 +25,7 @@ import net.sourceforge.cilib.controlparameter.ControlParameter;
import net.sourceforge.cilib.controlparameter.ProportionalControlParameter;
import net.sourceforge.cilib.entity.Entity;
import net.sourceforge.cilib.entity.Topology;
+import net.sourceforge.cilib.entity.comparator.DescendingFitnessComparator;
import net.sourceforge.cilib.entity.topologies.TopologyHolder;
import net.sourceforge.cilib.util.selection.recipes.ElitistSelection;
@@ -59,7 +60,7 @@ public class ElitistSelectionStrategy extends SelectionStrategy {
*/
@Override
public <T extends Entity> T select(Topology<T> population) {
- return new ElitistSelection<T>().select(population);
+ return new ElitistSelection<T>(new DescendingFitnessComparator<T>()).select(population);
}
/**
diff --git a/src/main/java/net/sourceforge/cilib/entity/operators/selection/RouletteWheelSelectionStrategy.java b/src/main/java/net/sourceforge/cilib/entity/operators/selection/RouletteWheelSelectionStrategy.java
index 08b8713..dcf6de7 100644
--- a/src/main/java/net/sourceforge/cilib/entity/operators/selection/RouletteWheelSelectionStrategy.java
+++ b/src/main/java/net/sourceforge/cilib/entity/operators/selection/RouletteWheelSelectionStrategy.java
@@ -69,9 +69,7 @@ public class RouletteWheelSelectionStrategy extends SelectionStrategy {
*/
@Override
public <T extends Entity> T select(final Topology<T> population) {
- RouletteWheelSelection<T> selection = new RouletteWheelSelection<T>();
- selection.setWeighing(new EntityWeighing<T>());
- return selection.select(population);
+ return new RouletteWheelSelection<T>(new EntityWeighing<T>()).select(population);
}
/**
diff --git a/src/main/java/net/sourceforge/cilib/entity/operators/selection/TournamentSelectionStrategy.java b/src/main/java/net/sourceforge/cilib/entity/operators/selection/TournamentSelectionStrategy.java
index 2c0129d..8d615e0 100644
--- a/src/main/java/net/sourceforge/cilib/entity/operators/selection/TournamentSelectionStrategy.java
+++ b/src/main/java/net/sourceforge/cilib/entity/operators/selection/TournamentSelectionStrategy.java
@@ -25,6 +25,7 @@ import net.sourceforge.cilib.controlparameter.ControlParameter;
import net.sourceforge.cilib.controlparameter.ProportionalControlParameter;
import net.sourceforge.cilib.entity.Entity;
import net.sourceforge.cilib.entity.Topology;
+import net.sourceforge.cilib.entity.comparator.DescendingFitnessComparator;
import net.sourceforge.cilib.entity.topologies.TopologyHolder;
import net.sourceforge.cilib.util.selection.recipes.TournamentSelection;
@@ -69,6 +70,7 @@ public class TournamentSelectionStrategy extends SelectionStrategy {
public <T extends Entity> T select(Topology<T> population) {
TournamentSelection<T> selection = new TournamentSelection<T>();
selection.setTournamentSize(this.tournamentProportion);
+ selection.setComparator(new DescendingFitnessComparator<T>());
return selection.select(population);
}
diff --git a/src/main/java/net/sourceforge/cilib/pso/particle/AbstractParticle.java b/src/main/java/net/sourceforge/cilib/pso/particle/AbstractParticle.java
index 0a2713c..647a077 100644
--- a/src/main/java/net/sourceforge/cilib/pso/particle/AbstractParticle.java
+++ b/src/main/java/net/sourceforge/cilib/pso/particle/AbstractParticle.java
@@ -307,7 +307,7 @@ public abstract class AbstractParticle extends AbstractEntity implements Particl
*/
@Override
public int compareTo(Entity o) {
- return getComparator().compare(this, o);
+ return this.getFitness().compareTo(o.getFitness());
}
/**
diff --git a/src/main/java/net/sourceforge/cilib/util/selection/ordering/DefaultComparator.java b/src/main/java/net/sourceforge/cilib/util/selection/ordering/DefaultComparator.java
new file mode 100644
index 0000000..9b80982
--- /dev/null
+++ b/src/main/java/net/sourceforge/cilib/util/selection/ordering/DefaultComparator.java
@@ -0,0 +1,32 @@
+/**
+ * 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.ordering;
+
+import java.util.Comparator;
+
+public class DefaultComparator<E extends Comparable> implements Comparator<E> {
+
+ @Override
+ public int compare(E o1, E o2) {
+ return o1.compareTo(o2);
+ }
+}
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 40224b8..813f88e 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
@@ -35,14 +35,6 @@ public class SortedOrdering<E extends Comparable> implements Ordering<E> {
private Comparator<E> comparator;
- private class DefaultComparator<E extends Comparable> implements Comparator<E> {
-
- @Override
- public int compare(E o1, E o2) {
- return o1.compareTo(o2);
- }
- }
-
public SortedOrdering(Comparator<E> comparator) {
this.comparator = comparator;
}
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 3fcadd8..166bea2 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
@@ -22,8 +22,10 @@
package net.sourceforge.cilib.util.selection.recipes;
import java.util.Collection;
+import java.util.Comparator;
import java.util.List;
import net.sourceforge.cilib.util.selection.Selection;
+import net.sourceforge.cilib.util.selection.ordering.DefaultComparator;
import net.sourceforge.cilib.util.selection.ordering.SortedOrdering;
/**
@@ -33,10 +35,26 @@ import net.sourceforge.cilib.util.selection.ordering.SortedOrdering;
public class ElitistSelection<E extends Comparable> implements SelectionRecipe<E> {
private static final long serialVersionUID = -5432603299031620114L;
+ private Comparator<E> comparator;
+
+ public ElitistSelection(Comparator<E> comparator) {
+ this.comparator = comparator;
+ }
+
public ElitistSelection() {
+ this.comparator = new DefaultComparator<E>();
}
public ElitistSelection(ElitistSelection copy) {
+ this.comparator = copy.comparator;
+ }
+
+ public void setComparator(Comparator<E> comparator) {
+ this.comparator = comparator;
+ }
+
+ public Comparator<E> getComparator() {
+ return this.comparator;
}
@Override
@@ -46,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()).first().select();
+ List<E> selection = Selection.from(elements).apply(new SortedOrdering(this.comparator)).first().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 556e676..223cb7a 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
@@ -37,6 +37,10 @@ public class RandomSelection<E> implements SelectionRecipe<E> {
private Random random;
+ public RandomSelection(Random random) {
+ this.random = random;
+ }
+
public RandomSelection() {
this.random = new MersenneTwister();
}
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 661c09f..b8bbe02 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
@@ -22,10 +22,12 @@
package net.sourceforge.cilib.util.selection.recipes;
import java.util.Collection;
+import java.util.Comparator;
import java.util.List;
import net.sourceforge.cilib.math.random.generator.MersenneTwister;
import net.sourceforge.cilib.math.random.generator.Random;
import net.sourceforge.cilib.util.selection.Selection;
+import net.sourceforge.cilib.util.selection.ordering.DefaultComparator;
import net.sourceforge.cilib.util.selection.ordering.RandomOrdering;
import net.sourceforge.cilib.util.selection.ordering.SortedOrdering;
@@ -36,16 +38,35 @@ import net.sourceforge.cilib.util.selection.ordering.SortedOrdering;
public class RankBasedSelection<E extends Comparable> implements SelectionRecipe<E> {
private static final long serialVersionUID = -2387196820773731607L;
+ private Comparator<E> comparator;
private Random random;
+ public RankBasedSelection(Comparator<E> comparator, Random random) {
+ this.comparator = comparator;
+ this.random = random;
+ }
+
+ public RankBasedSelection(Comparator<E> comparator) {
+ this(comparator, new MersenneTwister());
+ }
+
public RankBasedSelection() {
- this.random = new MersenneTwister();
+ this(new DefaultComparator<E>());
}
public RankBasedSelection(RankBasedSelection copy) {
+ this.comparator = copy.comparator;
this.random = copy.random.getClone();
}
+ public void setComparator(Comparator<E> comparator) {
+ this.comparator = comparator;
+ }
+
+ public Comparator<E> getComparator() {
+ return this.comparator;
+ }
+
@Override
public RankBasedSelection getClone() {
return new RankBasedSelection(this);
@@ -53,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>()).first(this.random.nextInt(elements.size())).apply(new RandomOrdering<E>()).first().select();
+ 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();
return selection.get(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 2a4e607..a344eb1 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
@@ -40,9 +40,17 @@ public class RouletteWheelSelection<E extends Comparable> implements SelectionRe
private Weighing<E> weighing;
private Random random;
+ public RouletteWheelSelection(Weighing<E> weighing, Random random) {
+ this.weighing = weighing;
+ this.random = random;
+ }
+
+ public RouletteWheelSelection(Weighing<E> weighing) {
+ this(weighing, new MersenneTwister());
+ }
+
public RouletteWheelSelection() {
- this.weighing = new LinearWeighing<E>();
- this.random = new MersenneTwister();
+ this(new LinearWeighing<E>());
}
public RouletteWheelSelection(RouletteWheelSelection copy) {
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 680c9d6..78621e8 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
@@ -22,11 +22,13 @@
package net.sourceforge.cilib.util.selection.recipes;
import java.util.Collection;
+import java.util.Comparator;
import net.sourceforge.cilib.controlparameter.ControlParameter;
import net.sourceforge.cilib.controlparameter.ProportionalControlParameter;
import net.sourceforge.cilib.math.random.generator.MersenneTwister;
import net.sourceforge.cilib.math.random.generator.Random;
import net.sourceforge.cilib.util.selection.Selection;
+import net.sourceforge.cilib.util.selection.ordering.DefaultComparator;
import net.sourceforge.cilib.util.selection.ordering.RandomOrdering;
import net.sourceforge.cilib.util.selection.ordering.SortedOrdering;
@@ -38,15 +40,18 @@ public class TournamentSelection<E extends Comparable> implements SelectionRecip
private static final long serialVersionUID = -6689673224380247931L;
private ControlParameter tournamentProportion;
+ private Comparator<E> comparator;
private Random random;
public TournamentSelection() {
this.tournamentProportion = new ProportionalControlParameter();
+ this.comparator = new DefaultComparator<E>();
this.random = new MersenneTwister();
}
public TournamentSelection(TournamentSelection copy) {
this.tournamentProportion = copy.tournamentProportion.getClone();
+ this.comparator = copy.comparator;
this.random = copy.random.getClone();
}
@@ -54,27 +59,27 @@ public class TournamentSelection<E extends Comparable> implements SelectionRecip
public TournamentSelection getClone() {
return new TournamentSelection(this);
}
-
- /**
- * Get the defined size of the tournament.
- * @return The size of the tournament.
- */
+
public ControlParameter getTournamentSize() {
return this.tournamentProportion;
}
- /**
- * Set the size of the tournament.
- * @param tournamanetSize The size of the tournament to set.
- */
public void setTournamentSize(ControlParameter tournamanetSize) {
this.tournamentProportion = tournamanetSize;
}
+ public void setComparator(Comparator<E> comparator) {
+ this.comparator = comparator;
+ }
+
+ public Comparator<E> getComparator() {
+ return this.comparator;
+ }
+
@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>()).first().select().get(0);
+ first(tournamentSize).apply(new SortedOrdering<E>(this.comparator)).first().select().get(0);
}
}
diff --git a/xml/dynamic-vepso.xml b/xml/dynamic-vepso.xml
index 0fc7941..7d83de6 100644
--- a/xml/dynamic-vepso.xml
+++ b/xml/dynamic-vepso.xml
@@ -16,7 +16,9 @@
<globalGuideSelectionStrategy class="pso.moo.guideselectionstrategies.VEPSOGuideSelectionStrategy">
<knowledgeTransferStrategy class="algorithm.population.knowledgetransferstrategies.SelectiveKnowledgeTransferStrategy">
<populationSelection class="util.selection.recipes.RingBasedPopulationSelection"/>
- <entitySelection class="util.selection.recipes.TournamentSelection"/>
+ <entitySelection class="util.selection.recipes.TournamentSelection">
+ <comparator class="entity.comparator.DescendingFitnessComparator"/>
+ </entitySelection>
</knowledgeTransferStrategy>
</globalGuideSelectionStrategy>
<globalGuideUpdateStrategy class="pso.moo.guideupdatestrategies.StandardGuideUpdateStrategy"/>
diff --git a/xml/vepso.xml b/xml/vepso.xml
index 58f41c9..a56d454 100644
--- a/xml/vepso.xml
+++ b/xml/vepso.xml
@@ -15,7 +15,9 @@
<globalGuideSelectionStrategy class="pso.moo.guideselectionstrategies.VEPSOGuideSelectionStrategy">
<knowledgeTransferStrategy class="algorithm.population.knowledgetransferstrategies.SelectiveKnowledgeTransferStrategy">
<populationSelection class="util.selection.recipes.RingBasedPopulationSelection"/>
- <entitySelection class="util.selection.recipes.TournamentSelection"/>
+ <entitySelection class="util.selection.recipes.TournamentSelection">
+ <comparator class="entity.comparator.DescendingFitnessComparator"/>
+ </entitySelection>
</knowledgeTransferStrategy>
</globalGuideSelectionStrategy>
<globalGuideUpdateStrategy class="pso.moo.guideupdatestrategies.StandardGuideUpdateStrategy"/>
--
1.6.0.6
|
|
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> {...
[truncated message content] |
|
From: Wiehann M. <wcm...@gm...> - 2009-05-23 00:47:37
|
Fixed the EntityWeighing class by removing the erranous else clause
that caused one of the fitness values to be equal to InferiorFitness.
This broke the RouletteWheelSelection class and all of its dependants
such as RouletteWheelSelectionStrategy.
Fixed the ProportionateOrdering class to correctly order the elements.
Also swapped the ordering around such that most fit entities that were
selected (or entities with higher probability of selection) are located
at the end of the selection (conforms to natural number ordering).
Then, changes were made to the core selection classes such as Selection
and SelectionSyntax as well as Ordering and Weighing to allow for
selections to be performed on elements that are not necessarily
comparable elements. This is usefull for when you want to select a
random population from a list of populations (and population -- or
algorithm -- is not comparable). However, this meant that changes had to
be made to all of the Ordering as well as Weighing classes to remove
comparable type requirement. This requirement were also removed from
some of the recipe classes and were left untouched in classes such as
TournamentSelection where it is required (an ordering of elements is
required). The DefaultComparator class accesses the Entry class and
performs the comparison if it is required in classes such as
SortedOrdering.
Some of the tests were modified to conform to the abovementioned
changes. Also, the WeighingTest got reintroduced and some changes were
made to the vepso.xml and dynamic-vepso.xml files to not make use of the
DescendingFitnessComparator class as all entities in cilib should be
ordered from least fit to most fit (the selection classes were changed
to take that into account).
---
.../selection/ElitistSelectionStrategy.java | 1 -
.../selection/TournamentSelectionStrategy.java | 2 -
.../cilib/util/selection/Selection.java | 45 ++++++---------
.../cilib/util/selection/SelectionSyntax.java | 2 +-
.../util/selection/ordering/DefaultComparator.java | 11 +++-
.../cilib/util/selection/ordering/Ordering.java | 2 +-
.../selection/ordering/ProportionalOrdering.java | 13 ++--
.../util/selection/ordering/RandomOrdering.java | 2 +-
.../util/selection/ordering/ReverseOrdering.java | 2 +-
.../util/selection/ordering/RingBasedOrdering.java | 5 +-
.../util/selection/ordering/SortedOrdering.java | 9 ++-
.../util/selection/recipes/ElitistSelection.java | 11 ++--
.../util/selection/recipes/RandomSelection.java | 4 +-
.../util/selection/recipes/RankBasedSelection.java | 13 ++--
.../recipes/RingBasedPopulationSelection.java | 31 +----------
.../selection/recipes/RouletteWheelSelection.java | 3 +-
.../selection/recipes/TournamentSelection.java | 9 ++-
.../util/selection/weighing/FixedWeighing.java | 2 +-
.../util/selection/weighing/LinearWeighing.java | 5 +-
.../cilib/util/selection/weighing/Weighing.java | 2 +-
.../selection/weighing/entity/EntityWeighing.java | 6 +-
.../RouletteWheelSelectionStrategyTest.java | 15 ++---
.../util/selection/ordering/OrderingTest.java | 4 +-
.../util/selection/weighing/WeighingTest.java | 59 ++++++++++++++++++++
.../weighing/entity/EntityWeighingTest.java | 19 ++++++-
xml/dynamic-vepso.xml | 4 +-
xml/vepso.xml | 4 +-
27 files changed, 158 insertions(+), 127 deletions(-)
create 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 29fe94f..4936a9e 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
@@ -25,7 +25,6 @@ import net.sourceforge.cilib.controlparameter.ControlParameter;
import net.sourceforge.cilib.controlparameter.ProportionalControlParameter;
import net.sourceforge.cilib.entity.Entity;
import net.sourceforge.cilib.entity.Topology;
-import net.sourceforge.cilib.entity.comparator.DescendingFitnessComparator;
import net.sourceforge.cilib.entity.topologies.TopologyHolder;
import net.sourceforge.cilib.util.selection.recipes.ElitistSelection;
diff --git a/src/main/java/net/sourceforge/cilib/entity/operators/selection/TournamentSelectionStrategy.java b/src/main/java/net/sourceforge/cilib/entity/operators/selection/TournamentSelectionStrategy.java
index 8d615e0..2c0129d 100644
--- a/src/main/java/net/sourceforge/cilib/entity/operators/selection/TournamentSelectionStrategy.java
+++ b/src/main/java/net/sourceforge/cilib/entity/operators/selection/TournamentSelectionStrategy.java
@@ -25,7 +25,6 @@ import net.sourceforge.cilib.controlparameter.ControlParameter;
import net.sourceforge.cilib.controlparameter.ProportionalControlParameter;
import net.sourceforge.cilib.entity.Entity;
import net.sourceforge.cilib.entity.Topology;
-import net.sourceforge.cilib.entity.comparator.DescendingFitnessComparator;
import net.sourceforge.cilib.entity.topologies.TopologyHolder;
import net.sourceforge.cilib.util.selection.recipes.TournamentSelection;
@@ -70,7 +69,6 @@ public class TournamentSelectionStrategy extends SelectionStrategy {
public <T extends Entity> T select(Topology<T> population) {
TournamentSelection<T> selection = new TournamentSelection<T>();
selection.setTournamentSize(this.tournamentProportion);
- selection.setComparator(new DescendingFitnessComparator<T>());
return selection.select(population);
}
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 f4ac2f1..2da72ac 100644
--- a/src/main/java/net/sourceforge/cilib/util/selection/Selection.java
+++ b/src/main/java/net/sourceforge/cilib/util/selection/Selection.java
@@ -59,7 +59,7 @@ import net.sourceforge.cilib.util.selection.weighing.Weighing;
* @param <E> The comparable type.
* @author gpampara
*/
-public final class Selection<E extends Comparable> implements SelectionSyntax<E> {
+public final class Selection<E> implements SelectionSyntax<E> {
private List<Entry<E>> elements;
@@ -72,8 +72,9 @@ public final class Selection<E extends Comparable> implements SelectionSyntax<E>
private Selection(Collection<? extends E> elements) {
this.elements = new ArrayList<Entry<E>>(elements.size());
- for (E element : elements)
+ for (E element : elements) {
this.elements.add(new Entry<E>(element));
+ }
}
/**
@@ -82,7 +83,7 @@ public final class Selection<E extends Comparable> implements SelectionSyntax<E>
* @param elements The collection of elements to operate on.
* @return A selection based on the provided collection.
*/
- public static <T extends Comparable> Selection<T> from(Collection<? extends T> elements) {
+ public static <T> Selection<T> from(Collection<? extends T> elements) {
return new Selection<T>(elements);
}
@@ -90,15 +91,16 @@ public final class Selection<E extends Comparable> implements SelectionSyntax<E>
* Apply the provided ordering on the current selection. The result of the
* operation will result in a modified selection.
* @param ordering The ordering to orderBy.
- * @return A selection upon which the ordering has be applied.
+ * @return A selection upon which the ordering has been applied.
* @throws UnsupportedOperationException if the ordering cannot be applied.
*/
@Override
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.");
@@ -107,15 +109,16 @@ public final class Selection<E extends Comparable> implements SelectionSyntax<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 orderBy.
- * @return A selection upon which the weighing has be applied.
+ * @param weighing The weighing to weighWith.
+ * @return A selection upon which the weighing has been applied.
*/
@Override
public SelectionSyntax<E> weigh(Weighing<E> weighing) {
boolean result = weighing.weigh(this.elements);
- if (result)
+ 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.");
@@ -173,8 +176,9 @@ public final class Selection<E extends Comparable> implements SelectionSyntax<E>
public List<E> select() {
List<E> result = new ArrayList<E>();
- for (Entry<E> entry : elements)
+ for (Entry<E> entry : elements) {
result.add(entry.getElement());
+ }
return result;
}
@@ -189,7 +193,6 @@ public final class Selection<E extends Comparable> implements SelectionSyntax<E>
return this.elements;
}
-
/**
* This class provides the notion of an entry within a list
* for the selection process.
@@ -199,7 +202,8 @@ public final class Selection<E extends Comparable> implements SelectionSyntax<E>
* 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 {
+ public final static class Entry<E> {
+
private final E element;
private double weight;
@@ -247,11 +251,13 @@ public final class Selection<E extends Comparable> implements SelectionSyntax<E>
*/
@Override
public boolean equals(Object obj) {
- if (obj == this)
+ if (obj == this) {
return true;
+ }
- if ((obj == null) || (this.getClass() != obj.getClass()))
+ if ((obj == null) || (this.getClass() != obj.getClass())) {
return false;
+ }
Entry<E> other = (Entry<E>) obj;
return this.element.equals(other.element);
@@ -274,18 +280,5 @@ public final class Selection<E extends Comparable> implements SelectionSyntax<E>
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 3ba5613..1bb1f8b 100644
--- a/src/main/java/net/sourceforge/cilib/util/selection/SelectionSyntax.java
+++ b/src/main/java/net/sourceforge/cilib/util/selection/SelectionSyntax.java
@@ -29,7 +29,7 @@ import net.sourceforge.cilib.util.selection.weighing.Weighing;
* @author Wiehann Matthysen
* @param <E>
*/
-public interface SelectionSyntax<E extends Comparable> {
+public interface SelectionSyntax<E> {
public SelectionSyntax<E> orderBy(Ordering<E> ordering);
diff --git a/src/main/java/net/sourceforge/cilib/util/selection/ordering/DefaultComparator.java b/src/main/java/net/sourceforge/cilib/util/selection/ordering/DefaultComparator.java
index 9b80982..4a44798 100644
--- a/src/main/java/net/sourceforge/cilib/util/selection/ordering/DefaultComparator.java
+++ b/src/main/java/net/sourceforge/cilib/util/selection/ordering/DefaultComparator.java
@@ -22,11 +22,16 @@
package net.sourceforge.cilib.util.selection.ordering;
import java.util.Comparator;
+import net.sourceforge.cilib.util.selection.Selection.Entry;
-public class DefaultComparator<E extends Comparable> implements Comparator<E> {
+/**
+ * @author Wiehann Matthysen
+ * @param <E>
+ */
+public class DefaultComparator<E extends Comparable> implements Comparator<Entry<E>> {
@Override
- public int compare(E o1, E o2) {
- return o1.compareTo(o2);
+ public int compare(Entry<E> o1, Entry<E> o2) {
+ return o1.getElement().compareTo(o2.getElement());
}
}
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 00f6b0d..ad34630 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
@@ -30,7 +30,7 @@ import net.sourceforge.cilib.util.selection.Selection;
* @param <E> The type to apply the ordering to.
* @author gpampara
*/
-public interface Ordering<E extends Comparable> {
+public interface Ordering<E> {
/**
* Apply the ordering on the provided list.
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 04dd378..538f06c 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
@@ -30,7 +30,7 @@ import net.sourceforge.cilib.util.selection.Selection;
* @author Wiehann Matthysen
* @param <E>
*/
-public class ProportionalOrdering<E extends Comparable> implements Ordering<E> {
+public class ProportionalOrdering<E> implements Ordering<E> {
private Random generator;
@@ -61,14 +61,13 @@ public class ProportionalOrdering<E extends Comparable> implements Ordering<E> {
double randomValue = this.generator.nextDouble() * total;
double marker = 0.0;
int j = i;
- for (; j < elements.size() - 1 && marker < randomValue; ++j) {
- marker += elements.get(j).getWeight();
- }
- // Swap elements i and j.
- Selection.Entry<E> elementJ = elements.set(j, elements.set(i, elements.get(j)));
+ do {
+ marker += elements.get(j++).getWeight();
+ } while (j < elements.size() && marker >= randomValue);
+ // Swap elements i and j - 1.
+ Selection.Entry<E> elementJ = elements.set(j - 1, elements.set(i, elements.get(j - 1)));
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 202a962..32a76f0 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
@@ -33,7 +33,7 @@ import net.sourceforge.cilib.util.selection.Selection;
* @param <E> The comparable type.
* @author gpampara
*/
-public class RandomOrdering<E extends Comparable> implements Ordering<E> {
+public class RandomOrdering<E> implements Ordering<E> {
private Random generator;
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 6a85fae..3b00ad6 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
@@ -29,7 +29,7 @@ import net.sourceforge.cilib.util.selection.Selection;
* @author Wiehann Matthysen
* @param <E>
*/
-public class ReverseOrdering<E extends Comparable> implements Ordering<E> {
+public class ReverseOrdering<E> implements Ordering<E> {
@Override
public boolean order(List<Selection.Entry<E>> elements) {
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 8927f8a..0cd8395 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
@@ -29,7 +29,7 @@ import net.sourceforge.cilib.util.selection.Selection;
* @author Wiehann Matthysen
* @param <E>
*/
-public class RingBasedOrdering<E extends Comparable> implements Ordering<E> {
+public class RingBasedOrdering<E> implements Ordering<E> {
private E marker;
@@ -51,9 +51,8 @@ public class RingBasedOrdering<E extends Comparable> implements Ordering<E> {
int position = 0;
for (Selection.Entry<E> entry : elements) {
- if (this.marker.compareTo(entry.getElement()) == 0)
+ if (this.marker.equals(entry.getElement()))
break;
-
position++;
}
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 e080c07..4787233 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
@@ -25,6 +25,7 @@ import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import net.sourceforge.cilib.util.selection.Selection;
+import net.sourceforge.cilib.util.selection.Selection.Entry;
/**
* Apply a sorting operation to the provided list, ordering the list naturally
@@ -34,9 +35,9 @@ import net.sourceforge.cilib.util.selection.Selection;
*/
public class SortedOrdering<E extends Comparable> implements Ordering<E> {
- private Comparator<E> comparator;
+ private Comparator<Entry<E>> comparator;
- public SortedOrdering(Comparator<E> comparator) {
+ public SortedOrdering(Comparator<Entry<E>> comparator) {
this.comparator = comparator;
}
@@ -50,8 +51,8 @@ public class SortedOrdering<E extends Comparable> implements Ordering<E> {
* @return {@code true} if successful, {@code false} otherwise.
*/
@Override
- public boolean order(List<Selection.Entry<E>> element) {
- Collections.sort(element);
+ public boolean order(List<Selection.Entry<E>> elements) {
+ Collections.sort(elements, this.comparator);
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 2e92e16..f9c9c6f 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
@@ -25,6 +25,7 @@ import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import net.sourceforge.cilib.util.selection.Selection;
+import net.sourceforge.cilib.util.selection.Selection.Entry;
import net.sourceforge.cilib.util.selection.ordering.DefaultComparator;
import net.sourceforge.cilib.util.selection.ordering.SortedOrdering;
@@ -35,9 +36,9 @@ import net.sourceforge.cilib.util.selection.ordering.SortedOrdering;
public class ElitistSelection<E extends Comparable> implements SelectionRecipe<E> {
private static final long serialVersionUID = -5432603299031620114L;
- private Comparator<E> comparator;
+ private Comparator<Entry<E>> comparator;
- public ElitistSelection(Comparator<E> comparator) {
+ public ElitistSelection(Comparator<Entry<E>> comparator) {
this.comparator = comparator;
}
@@ -49,11 +50,11 @@ public class ElitistSelection<E extends Comparable> implements SelectionRecipe<E
this.comparator = copy.comparator;
}
- public void setComparator(Comparator<E> comparator) {
+ public void setComparator(Comparator<Entry<E>> comparator) {
this.comparator = comparator;
}
- public Comparator<E> getComparator() {
+ public Comparator<Entry<E>> getComparator() {
return this.comparator;
}
@@ -64,7 +65,7 @@ public class ElitistSelection<E extends Comparable> implements SelectionRecipe<E
@Override
public E select(Collection<? extends E> elements) {
- List<E> selection = Selection.from(elements).orderBy(new SortedOrdering(this.comparator)).last().select();
+ List<E> selection = Selection.from(elements).orderBy(new SortedOrdering<E>(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 d30d1de..0eb1401 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 extends Comparable> implements SelectionRecipe<E> {
+public class RandomSelection<E> implements SelectionRecipe<E> {
private static final long serialVersionUID = -5099663528040315048L;
private Random random;
@@ -56,7 +56,7 @@ public class RandomSelection<E extends Comparable> implements SelectionRecipe<E>
@Override
public E select(Collection<? extends E> elements) {
- List<E> selection = Selection.from(elements).orderBy(new RandomOrdering<E>(this.random)).first().select();
+ List<E> selection = Selection.from(elements).orderBy(new RandomOrdering<E>(this.random)).last().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 75b13bf..779a855 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
@@ -27,6 +27,7 @@ import java.util.List;
import net.sourceforge.cilib.math.random.generator.MersenneTwister;
import net.sourceforge.cilib.math.random.generator.Random;
import net.sourceforge.cilib.util.selection.Selection;
+import net.sourceforge.cilib.util.selection.Selection.Entry;
import net.sourceforge.cilib.util.selection.ordering.DefaultComparator;
import net.sourceforge.cilib.util.selection.ordering.RandomOrdering;
import net.sourceforge.cilib.util.selection.ordering.SortedOrdering;
@@ -38,15 +39,15 @@ import net.sourceforge.cilib.util.selection.ordering.SortedOrdering;
public class RankBasedSelection<E extends Comparable> implements SelectionRecipe<E> {
private static final long serialVersionUID = -2387196820773731607L;
- private Comparator<E> comparator;
+ private Comparator<Entry<E>> comparator;
private Random random;
- public RankBasedSelection(Comparator<E> comparator, Random random) {
+ public RankBasedSelection(Comparator<Entry<E>> comparator, Random random) {
this.comparator = comparator;
this.random = random;
}
- public RankBasedSelection(Comparator<E> comparator) {
+ public RankBasedSelection(Comparator<Entry<E>> comparator) {
this(comparator, new MersenneTwister());
}
@@ -59,11 +60,11 @@ public class RankBasedSelection<E extends Comparable> implements SelectionRecipe
this.random = copy.random.getClone();
}
- public void setComparator(Comparator<E> comparator) {
+ public void setComparator(Comparator<Entry<E>> comparator) {
this.comparator = comparator;
}
- public Comparator<E> getComparator() {
+ public Comparator<Entry<E>> getComparator() {
return this.comparator;
}
@@ -74,7 +75,7 @@ public class RankBasedSelection<E extends Comparable> implements SelectionRecipe
@Override
public E select(Collection<? extends E> elements) {
- 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();
+ List<E> selection = Selection.from(elements).orderBy(new SortedOrdering<E>(this.comparator)).last(this.random.nextInt(elements.size())).orderBy(new RandomOrdering<E>(this.random)).last().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 5da4287..4886855 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,9 +21,7 @@
*/
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;
@@ -48,33 +46,6 @@ public class RingBasedPopulationSelection implements SelectionRecipe<PopulationB
@Override
public PopulationBasedAlgorithm select(Collection<? extends PopulationBasedAlgorithm> elements) {
- 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();
+ return Selection.from(elements).orderBy(new RingBasedOrdering<PopulationBasedAlgorithm>((PopulationBasedAlgorithm)Algorithm.get())).first().singleSelect();
}
-
- 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 8b4bf1f..e0205e8 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
@@ -22,7 +22,6 @@
package net.sourceforge.cilib.util.selection.recipes;
import java.util.Collection;
-import java.util.List;
import net.sourceforge.cilib.math.random.generator.MersenneTwister;
import net.sourceforge.cilib.math.random.generator.Random;
import net.sourceforge.cilib.util.selection.Selection;
@@ -73,7 +72,7 @@ public class RouletteWheelSelection<E extends Comparable> implements SelectionRe
@Override
public E select(Collection<? extends E> elements) {
- E selection = Selection.from(elements).weigh(this.weighing).orderBy(new ProportionalOrdering<E>(this.random)).first().singleSelect();
+ E selection = Selection.from(elements).weigh(this.weighing).orderBy(new ProportionalOrdering<E>(this.random)).last().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 f030665..093e569 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
@@ -28,6 +28,7 @@ import net.sourceforge.cilib.controlparameter.ProportionalControlParameter;
import net.sourceforge.cilib.math.random.generator.MersenneTwister;
import net.sourceforge.cilib.math.random.generator.Random;
import net.sourceforge.cilib.util.selection.Selection;
+import net.sourceforge.cilib.util.selection.Selection.Entry;
import net.sourceforge.cilib.util.selection.ordering.DefaultComparator;
import net.sourceforge.cilib.util.selection.ordering.RandomOrdering;
import net.sourceforge.cilib.util.selection.ordering.SortedOrdering;
@@ -40,7 +41,7 @@ public class TournamentSelection<E extends Comparable> implements SelectionRecip
private static final long serialVersionUID = -6689673224380247931L;
private ControlParameter tournamentProportion;
- private Comparator<E> comparator;
+ private Comparator<Entry<E>> comparator;
private Random random;
public ...
[truncated message content] |
|
From: Gary P. <gpa...@gm...> - 2009-05-26 09:12:13
|
There are some optimisations that can be made that will make the Selection
class more complete. I'm applying these changes at the moment.
One issue is that to select a random element from the provided element list
originally needed to have an ordering applied to all the elements and then to
select the desired objects. This can be done in a simpler manner. I'm thinking
in a static method that terminates the selection process after it completes.
Having it continue allowing for a chaning on the Selection class would be
wrong.
Regards,
Gary
On Saturday 23 May 2009 02:45:47 Wiehann Matthysen wrote:
> Fixed the EntityWeighing class by removing the erranous else clause
> that caused one of the fitness values to be equal to InferiorFitness.
> This broke the RouletteWheelSelection class and all of its dependants
> such as RouletteWheelSelectionStrategy.
>
> Fixed the ProportionateOrdering class to correctly order the elements.
> Also swapped the ordering around such that most fit entities that were
> selected (or entities with higher probability of selection) are located
> at the end of the selection (conforms to natural number ordering).
>
> Then, changes were made to the core selection classes such as Selection
> and SelectionSyntax as well as Ordering and Weighing to allow for
> selections to be performed on elements that are not necessarily
> comparable elements. This is usefull for when you want to select a
> random population from a list of populations (and population -- or
> algorithm -- is not comparable). However, this meant that changes had to
> be made to all of the Ordering as well as Weighing classes to remove
> comparable type requirement. This requirement were also removed from
> some of the recipe classes and were left untouched in classes such as
> TournamentSelection where it is required (an ordering of elements is
> required). The DefaultComparator class accesses the Entry class and
> performs the comparison if it is required in classes such as
> SortedOrdering.
>
> Some of the tests were modified to conform to the abovementioned
> changes. Also, the WeighingTest got reintroduced and some changes were
> made to the vepso.xml and dynamic-vepso.xml files to not make use of the
> DescendingFitnessComparator class as all entities in cilib should be
> ordered from least fit to most fit (the selection classes were changed
> to take that into account).
> ---
> .../selection/ElitistSelectionStrategy.java | 1 -
> .../selection/TournamentSelectionStrategy.java | 2 -
> .../cilib/util/selection/Selection.java | 45 ++++++---------
> .../cilib/util/selection/SelectionSyntax.java | 2 +-
> .../util/selection/ordering/DefaultComparator.java | 11 +++-
> .../cilib/util/selection/ordering/Ordering.java | 2 +-
> .../selection/ordering/ProportionalOrdering.java | 13 ++--
> .../util/selection/ordering/RandomOrdering.java | 2 +-
> .../util/selection/ordering/ReverseOrdering.java | 2 +-
> .../util/selection/ordering/RingBasedOrdering.java | 5 +-
> .../util/selection/ordering/SortedOrdering.java | 9 ++-
> .../util/selection/recipes/ElitistSelection.java | 11 ++--
> .../util/selection/recipes/RandomSelection.java | 4 +-
> .../util/selection/recipes/RankBasedSelection.java | 13 ++--
> .../recipes/RingBasedPopulationSelection.java | 31 +----------
> .../selection/recipes/RouletteWheelSelection.java | 3 +-
> .../selection/recipes/TournamentSelection.java | 9 ++-
> .../util/selection/weighing/FixedWeighing.java | 2 +-
> .../util/selection/weighing/LinearWeighing.java | 5 +-
> .../cilib/util/selection/weighing/Weighing.java | 2 +-
> .../selection/weighing/entity/EntityWeighing.java | 6 +-
> .../RouletteWheelSelectionStrategyTest.java | 15 ++---
> .../util/selection/ordering/OrderingTest.java | 4 +-
> .../util/selection/weighing/WeighingTest.java | 59
> ++++++++++++++++++++ .../weighing/entity/EntityWeighingTest.java |
> 19 ++++++- xml/dynamic-vepso.xml | 4 +-
> xml/vepso.xml | 4 +-
> 27 files changed, 158 insertions(+), 127 deletions(-)
> create mode 100644
> src/test/java/net/sourceforge/cilib/util/selection/weighing/WeighingTest.ja
>va
>
> diff --git
> a/src/main/java/net/sourceforge/cilib/entity/operators/selection/ElitistSel
>ectionStrategy.java
> b/src/main/java/net/sourceforge/cilib/entity/operators/selection/ElitistSel
>ectionStrategy.java index 29fe94f..4936a9e 100644
> ---
> a/src/main/java/net/sourceforge/cilib/entity/operators/selection/ElitistSel
>ectionStrategy.java +++
> b/src/main/java/net/sourceforge/cilib/entity/operators/selection/ElitistSel
>ectionStrategy.java @@ -25,7 +25,6 @@ import
> net.sourceforge.cilib.controlparameter.ControlParameter; import
> net.sourceforge.cilib.controlparameter.ProportionalControlParameter; import
> net.sourceforge.cilib.entity.Entity;
> import net.sourceforge.cilib.entity.Topology;
> -import
> net.sourceforge.cilib.entity.comparator.DescendingFitnessComparator; import
> net.sourceforge.cilib.entity.topologies.TopologyHolder;
> import net.sourceforge.cilib.util.selection.recipes.ElitistSelection;
>
> diff --git
> a/src/main/java/net/sourceforge/cilib/entity/operators/selection/Tournament
>SelectionStrategy.java
> b/src/main/java/net/sourceforge/cilib/entity/operators/selection/Tournament
>SelectionStrategy.java index 8d615e0..2c0129d 100644
> ---
> a/src/main/java/net/sourceforge/cilib/entity/operators/selection/Tournament
>SelectionStrategy.java +++
> b/src/main/java/net/sourceforge/cilib/entity/operators/selection/Tournament
>SelectionStrategy.java @@ -25,7 +25,6 @@ import
> net.sourceforge.cilib.controlparameter.ControlParameter; import
> net.sourceforge.cilib.controlparameter.ProportionalControlParameter; import
> net.sourceforge.cilib.entity.Entity;
> import net.sourceforge.cilib.entity.Topology;
> -import
> net.sourceforge.cilib.entity.comparator.DescendingFitnessComparator; import
> net.sourceforge.cilib.entity.topologies.TopologyHolder;
> import net.sourceforge.cilib.util.selection.recipes.TournamentSelection;
>
> @@ -70,7 +69,6 @@ public class TournamentSelectionStrategy extends
> SelectionStrategy { public <T extends Entity> T select(Topology<T>
> population) {
> TournamentSelection<T> selection = new TournamentSelection<T>();
> selection.setTournamentSize(this.tournamentProportion);
> - selection.setComparator(new DescendingFitnessComparator<T>());
> return selection.select(population);
> }
>
> 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
> f4ac2f1..2da72ac 100644
> --- a/src/main/java/net/sourceforge/cilib/util/selection/Selection.java
> +++ b/src/main/java/net/sourceforge/cilib/util/selection/Selection.java
> @@ -59,7 +59,7 @@ import
> net.sourceforge.cilib.util.selection.weighing.Weighing; * @param <E> The
> comparable type.
> * @author gpampara
> */
> -public final class Selection<E extends Comparable> implements
> SelectionSyntax<E> { +public final class Selection<E> implements
> SelectionSyntax<E> {
>
> private List<Entry<E>> elements;
>
> @@ -72,8 +72,9 @@ public final class Selection<E extends Comparable>
> implements SelectionSyntax<E> private Selection(Collection<? extends E>
> elements) {
> this.elements = new ArrayList<Entry<E>>(elements.size());
>
> - for (E element : elements)
> + for (E element : elements) {
> this.elements.add(new Entry<E>(element));
> + }
> }
>
> /**
> @@ -82,7 +83,7 @@ public final class Selection<E extends Comparable>
> implements SelectionSyntax<E> * @param elements The collection of elements
> to operate on.
> * @return A selection based on the provided collection.
> */
> - public static <T extends Comparable> Selection<T> from(Collection<?
> extends T> elements) { + public static <T> Selection<T>
> from(Collection<? extends T> elements) { return new Selection<T>(elements);
> }
>
> @@ -90,15 +91,16 @@ public final class Selection<E extends Comparable>
> implements SelectionSyntax<E> * Apply the provided ordering on the current
> selection. The result of the * operation will result in a modified
> selection.
> * @param ordering The ordering to orderBy.
> - * @return A selection upon which the ordering has be applied.
> + * @return A selection upon which the ordering has been applied.
> * @throws UnsupportedOperationException if the ordering cannot be
> applied. */
> @Override
> 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.");
> @@ -107,15 +109,16 @@ public final class Selection<E extends Comparable>
> implements SelectionSyntax<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 orderBy.
> - * @return A selection upon which the weighing has be applied.
> + * @param weighing The weighing to weighWith.
> + * @return A selection upon which the weighing has been applied.
> */
> @Override
> public SelectionSyntax<E> weigh(Weighing<E> weighing) {
> boolean result = weighing.weigh(this.elements);
>
> - if (result)
> + 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.");
> @@ -173,8 +176,9 @@ public final class Selection<E extends Comparable>
> implements SelectionSyntax<E> public List<E> select() {
> List<E> result = new ArrayList<E>();
>
> - for (Entry<E> entry : elements)
> + for (Entry<E> entry : elements) {
> result.add(entry.getElement());
> + }
>
> return result;
> }
> @@ -189,7 +193,6 @@ public final class Selection<E extends Comparable>
> implements SelectionSyntax<E> return this.elements;
> }
>
> -
> /**
> * This class provides the notion of an entry within a list
> * for the selection process.
> @@ -199,7 +202,8 @@ public final class Selection<E extends Comparable>
> implements SelectionSyntax<E> * 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 { + public final static class Entry<E> {
> +
> private final E element;
> private double weight;
>
> @@ -247,11 +251,13 @@ public final class Selection<E extends Comparable>
> implements SelectionSyntax<E> */
> @Override
> public boolean equals(Object obj) {
> - if (obj == this)
> + if (obj == this) {
> return true;
> + }
>
> - if ((obj == null) || (this.getClass() != obj.getClass()))
> + if ((obj == null) || (this.getClass() != obj.getClass())) {
> return false;
> + }
>
> Entry<E> other = (Entry<E>) obj;
> return this.element.equals(other.element);
> @@ -274,18 +280,5 @@ public final class Selection<E extends Comparable>
> implements SelectionSyntax<E> 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 3ba5613..1bb1f8b 100644
> ---
> a/src/main/java/net/sourceforge/cilib/util/selection/SelectionSyntax.java
> +++
> b/src/main/java/net/sourceforge/cilib/util/selection/SelectionSyntax.java
> @@ -29,7 +29,7 @@ import
> net.sourceforge.cilib.util.selection.weighing.Weighing; * @author Wiehann
> Matthysen
> * @param <E>
> */
> -public interface SelectionSyntax<E extends Comparable> {
> +public interface SelectionSyntax<E> {
>
> public SelectionSyntax<E> orderBy(Ordering<E> ordering);
>
> diff --git
> a/src/main/java/net/sourceforge/cilib/util/selection/ordering/DefaultCompar
>ator.java
> b/src/main/java/net/sourceforge/cilib/util/selection/ordering/DefaultCompar
>ator.java index 9b80982..4a44798 100644
> ---
> a/src/main/java/net/sourceforge/cilib/util/selection/ordering/DefaultCompar
>ator.java +++
> b/src/main/java/net/sourceforge/cilib/util/selection/ordering/DefaultCompar
>ator.java @@ -22,11 +22,16 @@
> package net.sourceforge.cilib.util.selection.ordering;
>
> import java.util.Comparator;
> +import net.sourceforge.cilib.util.selection.Selection.Entry;
>
> -public class DefaultComparator<E extends Comparable> implements
> Comparator<E> { +/**
> + * @author Wiehann Matthysen
> + * @param <E>
> + */
> +public class DefaultComparator<E extends Comparable> implements
> Comparator<Entry<E>> {
>
> @Override
> - public int compare(E o1, E o2) {
> - return o1.compareTo(o2);
> + public int compare(Entry<E> o1, Entry<E> o2) {
> + return o1.getElement().compareTo(o2.getElement());
> }
> }
> 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 00f6b0d..ad34630 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
> @@ -30,7 +30,7 @@ import net.sourceforge.cilib.util.selection.Selection; *
> @param <E> The type to apply the ordering to.
> * @author gpampara
> */
> -public interface Ordering<E extends Comparable> {
> +public interface Ordering<E> {
>
> /**
> * Apply the ordering on the provided list.
> diff --git
> a/src/main/java/net/sourceforge/cilib/util/selection/ordering/ProportionalO
>rdering.java
> b/src/main/java/net/sourceforge/cilib/util/selection/ordering/ProportionalO
>rdering.java index 04dd378..538f06c 100644
> ---
> a/src/main/java/net/sourceforge/cilib/util/selection/ordering/ProportionalO
>rdering.java +++
> b/src/main/java/net/sourceforge/cilib/util/selection/ordering/ProportionalO
>rdering.java @@ -30,7 +30,7 @@ import
> net.sourceforge.cilib.util.selection.Selection; * @author Wiehann Matthysen
> * @param <E>
> */
> -public class ProportionalOrdering<E extends Comparable> implements
> Ordering<E> { +public class ProportionalOrdering<E> implements Ordering<E>
> {
>
> private Random generator;
>
> @@ -61,14 +61,13 @@ public class ProportionalOrdering<E extends Comparable>
> implements Ordering<E> { double randomValue = this.generator.nextDouble() *
> total; double marker = 0.0;
> int j = i;
> - for (; j < elements.size() - 1 && marker < randomValue; ++j) {
> - marker += elements.get(j).getWeight();
> - }
> - // Swap elements i and j.
> - Selection.Entry<E> elementJ = elements.set(j, elements.set(i,
> elements.get(j))); + do {
> + marker += elements.get(j++).getWeight();
> + } while (j < elements.size() && marker >= randomValue);
> + // Swap elements i and j - 1.
> + Selection.Entry<E> elementJ = elements.set(j - 1,
> elements.set(i, elements.get(j - 1))); total -= elementJ.getWeight();
> }
> -
> return true;
> }
> }
> diff --git
> a/src/main/java/net/sourceforge/cilib/util/selection/ordering/RandomOrderin
>g.java
> b/src/main/java/net/sourceforge/cilib/util/selection/ordering/RandomOrderin
>g.java index 202a962..32a76f0 100644
> ---
> a/src/main/java/net/sourceforge/cilib/util/selection/ordering/RandomOrderin
>g.java +++
> b/src/main/java/net/sourceforge/cilib/util/selection/ordering/RandomOrderin
>g.java @@ -33,7 +33,7 @@ import
> net.sourceforge.cilib.util.selection.Selection; * @param <E> The comparable
> type.
> * @author gpampara
> */
> -public class RandomOrdering<E extends Comparable> implements Ordering<E> {
> +public class RandomOrdering<E> implements Ordering<E> {
>
> private Random generator;
>
> diff --git
> a/src/main/java/net/sourceforge/cilib/util/selection/ordering/ReverseOrderi
>ng.java
> b/src/main/java/net/sourceforge/cilib/util/selection/ordering/ReverseOrderi
>ng.java index 6a85fae..3b00ad6 100644
> ---
> a/src/main/java/net/sourceforge/cilib/util/selection/ordering/ReverseOrderi
>ng.java +++
> b/src/main/java/net/sourceforge/cilib/util/selection/ordering/ReverseOrderi
>ng.java @@ -29,7 +29,7 @@ import
> net.sourceforge.cilib.util.selection.Selection; * @author Wiehann Matthysen
> * @param <E>
> */
> -public class ReverseOrdering<E extends Comparable> implements Ordering<E>
> { +public class ReverseOrdering<E> implements Ordering<E> {
>
> @Override
> public boolean order(List<Selection.Entry<E>> elements) {
> diff --git
> a/src/main/java/net/sourceforge/cilib/util/selection/ordering/RingBasedOrde
>ring.java
> b/src/main/java/net/sourceforge/cilib/util/selection/ordering/RingBasedOrde
>ring.java index 8927f8a..0cd8395 100644
> ---
> a/src/main/java/net/sourceforge/cilib/util/selection/ordering/RingBasedOrde
>ring.java +++
> b/src/main/java/net/sourceforge/cilib/util/selection/ordering/RingBasedOrde
>ring.java @@ -29,7 +29,7 @@ import
> net.sourceforge.cilib.util.selection.Selection; * @author Wiehann Matthysen
> * @param <E>
> */
> -public class RingBasedOrdering<E extends Comparable> implements
> Ordering<E> { +public class RingBasedOrdering<E> implements Ordering<E> {
>
> private E marker;
>
> @@ -51,9 +51,8 @@ public class RingBasedOrdering<E extends Comparable>
> implements Ordering<E> {
>
> int position = 0;
> for (Selection.Entry<E> entry : elements) {
> - if (this.marker.compareTo(entry.getElement()) == 0)
> + if (this.marker.equals(entry.getElement()))
> break;
> -
> position++;
> }
>
> diff --git
> a/src/main/java/net/sourceforge/cilib/util/selection/ordering/SortedOrderin
>g.java
> b/src/main/java/net/sourceforge/cilib/util/selection/ordering/SortedOrderin
>g.java index e080c07..4787233 100644
> ---
> a/src/main/java/net/sourceforge/cilib/util/selection/ordering/SortedOrderin
>g.java +++
> b/src/main/java/net/sourceforge/cilib/util/selection/ordering/SortedOrderin
>g.java @@ -25,6 +25,7 @@ import java.util.Collections;
> import java.util.Comparator;
> import java.util.List;
> import net.sourceforge.cilib.util.selection.Selection;
> +import net.sourceforge.cilib.util.selection.Selection.Entry;
>
> /**
> * Apply a sorting operation to the provided list, ordering the list
> naturally @@ -34,9 +35,9 @@ import
> net.sourceforge.cilib.util.selection.Selection; */
> public class SortedOrdering<E extends Comparable> implements Ordering<E> {
>
> - private Comparator<E> comparator;
> + private Comparator<Entry<E>> comparator;
>
> - public SortedOrdering(Comparator<E> comparator) {
> + public SortedOrdering(Comparator<Entry<E>> comparator) {
> this.comparator = comparator;
> }
>
> @@ -50,8 +51,8 @@ public class SortedOrdering<E extends Comparable>
> implements Ordering<E> { * @return {@code true} if successful, {@code
> false} otherwise. */
> @Override
> - public boolean order(List<Selection.Entry<E>> element) {
> - Collections.sort(element);
> + public boolean order(List<Selection.Entry<E>> elements) {
> + Collections.sort(elements, this.comparator);
> return true;
> }
> }
> diff --git
> a/src/main/java/net/sourceforge/cilib/util/selection/recipes/ElitistSelecti
>on.java
> b/src/main/java/net/sourceforge/cilib/util/selection/recipes/ElitistSelecti
>on.java index 2e92e16..f9c9c6f 100644
> ---
> a/src/main/java/net/sourceforge/cilib/util/selection/recipes/ElitistSelecti
>on.java +++
> b/src/main/java/net/sourceforge/cilib/util/selection/recipes/ElitistSelecti
>on.java @@ -25,6 +25,7 @@ import java.util.Collection;
> import java.util.Comparator;
> import java.util.List;
> import net.sourceforge.cilib.util.selection.Selection;
> +import net.sourceforge.cilib.util.selection.Selection.Entry;
> import net.sourceforge.cilib.util.selection.ordering.DefaultComparator;
> import net.sourceforge.cilib.util.selection.ordering.SortedOrdering;
>
> @@ -35,9 +36,9 @@ import
> net.sourceforge.cilib.util.selection.ordering.SortedOrdering; public class
> ElitistSelection<E extends Comparable> implements SelectionRecipe<E> {
> private static final long serialVersionUID = -5432603299031620114L;
>
> - private Comparator<E> comparator;
> + private Comparator<Entry<E>> comparator;
>
> - public ElitistSelection(Comparator<E> comparator) {
> + public ElitistSelection(Comparator<Entry<E>> comparator) {
> this.comparator = comparator;
> }
>
> @@ -49,11 +50,11 @@ public class ElitistSelection<E extends Comparable>
> implements SelectionRecipe<E this.comparator = copy.comparator;
> }
>
> - public void setComparator(Comparator<E> comparator) {
> + public void setComparator(Comparator<Entry<E>> comparator) {
> this.comparator = comparator;
> }
>
> - public Comparator<E> getComparator() {
> + public Comparator<Entry<E>> getComparator() {
> return this.comparator;
> }
>
> @@ -64,7 +65,7 @@ public class ElitistSelection<E extends Comparable>
> implements SelectionRecipe<E
>
> @Override
> public E select(Collection<? extends E> elements) {
> - List<E> selection = Selection.from(elements).orderBy(new
> SortedOrdering(this.comparator)).last().select(); + List<E>
> selection = Selection.from(elements).orderBy(new
> SortedOrdering<E>(this.comparator)).last().select(); return
> selection.get(0);
> }
> }
> diff --git
> a/src/main/java/net/sourceforge/cilib/util/selection/recipes/RandomSelectio
>n.java
> b/src/main/java/net/sourceforge/cilib/util/selection/recipes/RandomSelectio
>n.java index d30d1de..0eb1401 100644
> ---
> a/src/main/java/net/sourceforge/cilib/util/selection/recipes/RandomSelectio
>n.java +++
> b/src/main/java/net/sourceforge/cilib/util/selection/recipes/RandomSelectio
>n.java @@ -32,7 +32,7 @@ import
> net.sourceforge.cilib.util.selection.ordering.RandomOrdering; * @author
> Wiehann Matthysen
> * @param <E>
> */
> -public class RandomSelection<E extends Comparable> implements
> SelectionRecipe<E> { +public class RandomSelection<E> implements
> SelectionRecipe<E> {
> private static final long serialVersionUID = -5099663528040315048L;
>
> private Random random;
> @@ -56,7 +56,7 @@ public class RandomSelection<E extends Comparable>
> implements SelectionRecipe<E>
>
> @Override
> public E select(Collection<? extends E> elements) {
> - List<E> selection = Selection.from(elements).orderBy(new
> RandomOrdering<E>(this.random)).first().select(); + List<E>
> selection = Selection.from(elements).orderBy(new
> RandomOrdering<E>(this.random)).last().select(); return selection.get(0);
> }
> }
> diff --git
> a/src/main/java/net/sourceforge/cilib/util/selection/recipes/RankBasedSelec
>tion.java
> b/src/main/java/net/sourceforge/cilib/util/selection/recipes/RankBasedSelec
>tion.java index 75b13bf..779a855 100644
> ---
> a/src/main/java/net/sourceforge/cilib/util/selection/recipes/RankBasedSelec
>tion.java +++
> b/src/main/java/net/sourceforge/cilib/util/selection/recipes/RankBasedSelec
>tion.java @@ -27,6 +27,7 @@ import java.util.List;
> import net.sourceforge.cilib.math.random.generator.MersenneTwister;
> import net.sourceforge.cilib.math.random.generator.Random;
> import net.sourceforge.cilib.util.selection.Selection;
> +import net.sourceforge.cilib.util.selection.Selection.Entry;
> import net.sourceforge.cilib.util.selection.ordering.DefaultComparator;
> import net.sourceforge.cilib.util.selection.ordering.RandomOrdering;
> import net.sourceforge.cilib.util.selection.ordering.SortedOrdering;
> @@ -38,15 +39,15 @@ import
> net.sourceforge.cilib.util.selection.ordering.SortedOrdering; public class
> RankBasedSelection<E extends Comparable> implements SelectionRecipe<E> {
> private static final long serialVersionUID = -2387196820773731607L;
>
> - private Comparator<E> comparator;
> + private Comparator<Entry<E>> comparator;
> private Random random;
>
> - public RankBasedSelection(Comparator<E> comparator, Random random) {
> + public RankBasedSelection(Comparator<Entry<E>> comparator, Random
> random) { this.comparator = comparator;
> this.random = random;
> }
>
> - public RankBasedSelection(Comparator<E> comparator) {
> + public RankBasedSelection(Comparator<Entry<E>> comparator) {
> this(comparator, new MersenneTwister());
> }
>
> @@ -59,11 +60,11 @@ public class RankBasedSelection<E extends Comparable>
> implements SelectionRecipe this.random = copy.random.getClone();
> }
>
> - public void setComparator(Comparator<E> comparator) {
> + public void setComparator(Comparator<Entry<E>> comparator) {
> this.comparator = comparator;
> }
>
> - public Comparator<E> getComparator() {
> + public Comparator<Entry<E>> getComparator() {
> return this.comparator;
> }
>
> @@ -74,7 +75,7 @@ public class RankBasedSelection<E extends Comparable>
> implements SelectionRecipe
>
> @Override
> public E select(Collection<? extends E> elements) {
> - 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(); +
> List<E> selection = Selection.from(elements).orderBy(new
> SortedOrdering<E>(this.comparator)).last(this.random.nextInt(elements.size(
>))).orderBy(new RandomOrdering<E>(this.random)).last().select(); return
> selection.get(0);
> }
> }
> diff --git
> a/src/main/java/net/sourceforge/cilib/util/selection/recipes/RingBasedPopul
>ationSelection.java
> b/src/main/java/net/sourceforge/cilib/util/selection/recipes/RingBasedPopul
>ationSelection.java index 5da4287..4886855 100644
> ---
> a/src/main/java/net/sourceforge/cilib/util/selection/recipes/RingBasedPopul
>ationSelection.java +++
> b/src/main/java/net/sourceforge/cilib/util/selection/recipes/RingBasedPopul
>ationSelection.java @@ -21,9 +21,7 @@
> */
> 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;
> @@ -48,33 +46,6 @@ public class RingBasedPopulationSelection implements
> SelectionRecipe<PopulationB
>
> @Override
> public PopulationBasedAlgorithm select(Collection<? extends
> PopulationBasedAlgorithm> elements) { -
> 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().sing
>leSelect(); - return wrapper.get();
> + return Selection.from(elements).orderBy(new
> RingBasedOrdering<PopulationBasedAlgorithm>((PopulationBasedAlgorithm)Algor
>ithm.get())).first().singleSelect(); }
> -
> - 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/RouletteWheelS
>election.java
> b/src/main/java/net/sourceforge/cilib/util/selection/recipes/RouletteWheelS
>election.java index 8b4bf1f..e0205e8 100644
> ---
> a/src/main/java/net/sourceforge/cilib/util/selection/recipes/RouletteWheelS
>election.java +++
> b/src/main/java/net/sourceforge/cilib/util/selection/recipes/RouletteWheelS
>election.java @@ -22,7 +22,6 @@
> package net.sourceforge.cilib.util.selection.recipes;
>
> import java.util.Collection;
> -import java.util.List;
> import net.sourceforge.cilib.math.random.generator.MersenneTwister;
> import net.sourceforge.cilib.math.random.generator.Random;
> import net.sourceforge.cilib.util.selection.Selection;
> @@ -73,7 +72,7 @@ public class RouletteWheelSelection<E extends Comparable>
> implements SelectionRe
>
> @Override
> public E select(Collection<? extends E> elements) {
> - E...
[truncated message content] |
|
From: Gary P. <gpa...@gm...> - 2009-05-27 09:34:13
|
The changes have been applied. We will be merging the source into master as
soon as the javadocs are finalised. (Yes we are writing documentation!)
On Tuesday 26 May 2009 11:11:56 Gary Pampara wrote:
> There are some optimisations that can be made that will make the Selection
> class more complete. I'm applying these changes at the moment.
>
> One issue is that to select a random element from the provided element list
> originally needed to have an ordering applied to all the elements and then
> to select the desired objects. This can be done in a simpler manner. I'm
> thinking in a static method that terminates the selection process after it
> completes. Having it continue allowing for a chaning on the Selection class
> would be wrong.
>
> Regards,
> Gary
>
> On Saturday 23 May 2009 02:45:47 Wiehann Matthysen wrote:
> > Fixed the EntityWeighing class by removing the erranous else clause
> > that caused one of the fitness values to be equal to InferiorFitness.
> > This broke the RouletteWheelSelection class and all of its dependants
> > such as RouletteWheelSelectionStrategy.
> >
> > Fixed the ProportionateOrdering class to correctly order the elements.
> > Also swapped the ordering around such that most fit entities that were
> > selected (or entities with higher probability of selection) are located
> > at the end of the selection (conforms to natural number ordering).
> >
> > Then, changes were made to the core selection classes such as Selection
> > and SelectionSyntax as well as Ordering and Weighing to allow for
> > selections to be performed on elements that are not necessarily
> > comparable elements. This is usefull for when you want to select a
> > random population from a list of populations (and population -- or
> > algorithm -- is not comparable). However, this meant that changes had to
> > be made to all of the Ordering as well as Weighing classes to remove
> > comparable type requirement. This requirement were also removed from
> > some of the recipe classes and were left untouched in classes such as
> > TournamentSelection where it is required (an ordering of elements is
> > required). The DefaultComparator class accesses the Entry class and
> > performs the comparison if it is required in classes such as
> > SortedOrdering.
> >
> > Some of the tests were modified to conform to the abovementioned
> > changes. Also, the WeighingTest got reintroduced and some changes were
> > made to the vepso.xml and dynamic-vepso.xml files to not make use of the
> > DescendingFitnessComparator class as all entities in cilib should be
> > ordered from least fit to most fit (the selection classes were changed
> > to take that into account).
> > ---
> > .../selection/ElitistSelectionStrategy.java | 1 -
> > .../selection/TournamentSelectionStrategy.java | 2 -
> > .../cilib/util/selection/Selection.java | 45
> > ++++++--------- .../cilib/util/selection/SelectionSyntax.java | 2
> > +-
> > .../util/selection/ordering/DefaultComparator.java | 11 +++-
> > .../cilib/util/selection/ordering/Ordering.java | 2 +-
> > .../selection/ordering/ProportionalOrdering.java | 13 ++--
> > .../util/selection/ordering/RandomOrdering.java | 2 +-
> > .../util/selection/ordering/ReverseOrdering.java | 2 +-
> > .../util/selection/ordering/RingBasedOrdering.java | 5 +-
> > .../util/selection/ordering/SortedOrdering.java | 9 ++-
> > .../util/selection/recipes/ElitistSelection.java | 11 ++--
> > .../util/selection/recipes/RandomSelection.java | 4 +-
> > .../util/selection/recipes/RankBasedSelection.java | 13 ++--
> > .../recipes/RingBasedPopulationSelection.java | 31 +----------
> > .../selection/recipes/RouletteWheelSelection.java | 3 +-
> > .../selection/recipes/TournamentSelection.java | 9 ++-
> > .../util/selection/weighing/FixedWeighing.java | 2 +-
> > .../util/selection/weighing/LinearWeighing.java | 5 +-
> > .../cilib/util/selection/weighing/Weighing.java | 2 +-
> > .../selection/weighing/entity/EntityWeighing.java | 6 +-
> > .../RouletteWheelSelectionStrategyTest.java | 15 ++---
> > .../util/selection/ordering/OrderingTest.java | 4 +-
> > .../util/selection/weighing/WeighingTest.java | 59
> > ++++++++++++++++++++ .../weighing/entity/EntityWeighingTest.java |
> > 19 ++++++- xml/dynamic-vepso.xml | 4 +-
> > xml/vepso.xml | 4 +-
> > 27 files changed, 158 insertions(+), 127 deletions(-)
> > create mode 100644
> > src/test/java/net/sourceforge/cilib/util/selection/weighing/WeighingTest.
> >ja va
> >
> > diff --git
> > a/src/main/java/net/sourceforge/cilib/entity/operators/selection/ElitistS
> >el ectionStrategy.java
> > b/src/main/java/net/sourceforge/cilib/entity/operators/selection/ElitistS
> >el ectionStrategy.java index 29fe94f..4936a9e 100644
> > ---
> > a/src/main/java/net/sourceforge/cilib/entity/operators/selection/ElitistS
> >el ectionStrategy.java +++
> > b/src/main/java/net/sourceforge/cilib/entity/operators/selection/ElitistS
> >el ectionStrategy.java @@ -25,7 +25,6 @@ import
> > net.sourceforge.cilib.controlparameter.ControlParameter; import
> > net.sourceforge.cilib.controlparameter.ProportionalControlParameter;
> > import net.sourceforge.cilib.entity.Entity;
> > import net.sourceforge.cilib.entity.Topology;
> > -import
> > net.sourceforge.cilib.entity.comparator.DescendingFitnessComparator;
> > import net.sourceforge.cilib.entity.topologies.TopologyHolder;
> > import net.sourceforge.cilib.util.selection.recipes.ElitistSelection;
> >
> > diff --git
> > a/src/main/java/net/sourceforge/cilib/entity/operators/selection/Tourname
> >nt SelectionStrategy.java
> > b/src/main/java/net/sourceforge/cilib/entity/operators/selection/Tourname
> >nt SelectionStrategy.java index 8d615e0..2c0129d 100644
> > ---
> > a/src/main/java/net/sourceforge/cilib/entity/operators/selection/Tourname
> >nt SelectionStrategy.java +++
> > b/src/main/java/net/sourceforge/cilib/entity/operators/selection/Tourname
> >nt SelectionStrategy.java @@ -25,7 +25,6 @@ import
> > net.sourceforge.cilib.controlparameter.ControlParameter; import
> > net.sourceforge.cilib.controlparameter.ProportionalControlParameter;
> > import net.sourceforge.cilib.entity.Entity;
> > import net.sourceforge.cilib.entity.Topology;
> > -import
> > net.sourceforge.cilib.entity.comparator.DescendingFitnessComparator;
> > import net.sourceforge.cilib.entity.topologies.TopologyHolder;
> > import net.sourceforge.cilib.util.selection.recipes.TournamentSelection;
> >
> > @@ -70,7 +69,6 @@ public class TournamentSelectionStrategy extends
> > SelectionStrategy { public <T extends Entity> T select(Topology<T>
> > population) {
> > TournamentSelection<T> selection = new TournamentSelection<T>();
> > selection.setTournamentSize(this.tournamentProportion);
> > - selection.setComparator(new DescendingFitnessComparator<T>());
> > return selection.select(population);
> > }
> >
> > 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
> > f4ac2f1..2da72ac 100644
> > --- a/src/main/java/net/sourceforge/cilib/util/selection/Selection.java
> > +++ b/src/main/java/net/sourceforge/cilib/util/selection/Selection.java
> > @@ -59,7 +59,7 @@ import
> > net.sourceforge.cilib.util.selection.weighing.Weighing; * @param <E> The
> > comparable type.
> > * @author gpampara
> > */
> > -public final class Selection<E extends Comparable> implements
> > SelectionSyntax<E> { +public final class Selection<E> implements
> > SelectionSyntax<E> {
> >
> > private List<Entry<E>> elements;
> >
> > @@ -72,8 +72,9 @@ public final class Selection<E extends Comparable>
> > implements SelectionSyntax<E> private Selection(Collection<? extends E>
> > elements) {
> > this.elements = new ArrayList<Entry<E>>(elements.size());
> >
> > - for (E element : elements)
> > + for (E element : elements) {
> > this.elements.add(new Entry<E>(element));
> > + }
> > }
> >
> > /**
> > @@ -82,7 +83,7 @@ public final class Selection<E extends Comparable>
> > implements SelectionSyntax<E> * @param elements The collection of
> > elements to operate on.
> > * @return A selection based on the provided collection.
> > */
> > - public static <T extends Comparable> Selection<T> from(Collection<?
> > extends T> elements) { + public static <T> Selection<T>
> > from(Collection<? extends T> elements) { return new
> > Selection<T>(elements); }
> >
> > @@ -90,15 +91,16 @@ public final class Selection<E extends Comparable>
> > implements SelectionSyntax<E> * Apply the provided ordering on the
> > current selection. The result of the * operation will result in a
> > modified selection.
> > * @param ordering The ordering to orderBy.
> > - * @return A selection upon which the ordering has be applied.
> > + * @return A selection upon which the ordering has been applied.
> > * @throws UnsupportedOperationException if the ordering cannot be
> > applied. */
> > @Override
> > 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."); @@ -107,15 +109,16 @@ public final class Selection<E extends
> > Comparable> implements SelectionSyntax<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 orderBy.
> > - * @return A selection upon which the weighing has be applied.
> > + * @param weighing The weighing to weighWith.
> > + * @return A selection upon which the weighing has been applied.
> > */
> > @Override
> > public SelectionSyntax<E> weigh(Weighing<E> weighing) {
> > boolean result = weighing.weigh(this.elements);
> >
> > - if (result)
> > + 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."); @@ -173,8 +176,9 @@ public final class Selection<E extends
> > Comparable> implements SelectionSyntax<E> public List<E> select() {
> > List<E> result = new ArrayList<E>();
> >
> > - for (Entry<E> entry : elements)
> > + for (Entry<E> entry : elements) {
> > result.add(entry.getElement());
> > + }
> >
> > return result;
> > }
> > @@ -189,7 +193,6 @@ public final class Selection<E extends Comparable>
> > implements SelectionSyntax<E> return this.elements;
> > }
> >
> > -
> > /**
> > * This class provides the notion of an entry within a list
> > * for the selection process.
> > @@ -199,7 +202,8 @@ public final class Selection<E extends Comparable>
> > implements SelectionSyntax<E> * 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 { + public final static class Entry<E> {
> > +
> > private final E element;
> > private double weight;
> >
> > @@ -247,11 +251,13 @@ public final class Selection<E extends Comparable>
> > implements SelectionSyntax<E> */
> > @Override
> > public boolean equals(Object obj) {
> > - if (obj == this)
> > + if (obj == this) {
> > return true;
> > + }
> >
> > - if ((obj == null) || (this.getClass() != obj.getClass()))
> > + if ((obj == null) || (this.getClass() != obj.getClass())) {
> > return false;
> > + }
> >
> > Entry<E> other = (Entry<E>) obj;
> > return this.element.equals(other.element);
> > @@ -274,18 +280,5 @@ public final class Selection<E extends Comparable>
> > implements SelectionSyntax<E> 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 3ba5613..1bb1f8b 100644
> > ---
> > a/src/main/java/net/sourceforge/cilib/util/selection/SelectionSyntax.java
> > +++
> > b/src/main/java/net/sourceforge/cilib/util/selection/SelectionSyntax.java
> > @@ -29,7 +29,7 @@ import
> > net.sourceforge.cilib.util.selection.weighing.Weighing; * @author Wiehann
> > Matthysen
> > * @param <E>
> > */
> > -public interface SelectionSyntax<E extends Comparable> {
> > +public interface SelectionSyntax<E> {
> >
> > public SelectionSyntax<E> orderBy(Ordering<E> ordering);
> >
> > diff --git
> > a/src/main/java/net/sourceforge/cilib/util/selection/ordering/DefaultComp
> >ar ator.java
> > b/src/main/java/net/sourceforge/cilib/util/selection/ordering/DefaultComp
> >ar ator.java index 9b80982..4a44798 100644
> > ---
> > a/src/main/java/net/sourceforge/cilib/util/selection/ordering/DefaultComp
> >ar ator.java +++
> > b/src/main/java/net/sourceforge/cilib/util/selection/ordering/DefaultComp
> >ar ator.java @@ -22,11 +22,16 @@
> > package net.sourceforge.cilib.util.selection.ordering;
> >
> > import java.util.Comparator;
> > +import net.sourceforge.cilib.util.selection.Selection.Entry;
> >
> > -public class DefaultComparator<E extends Comparable> implements
> > Comparator<E> { +/**
> > + * @author Wiehann Matthysen
> > + * @param <E>
> > + */
> > +public class DefaultComparator<E extends Comparable> implements
> > Comparator<Entry<E>> {
> >
> > @Override
> > - public int compare(E o1, E o2) {
> > - return o1.compareTo(o2);
> > + public int compare(Entry<E> o1, Entry<E> o2) {
> > + return o1.getElement().compareTo(o2.getElement());
> > }
> > }
> > diff --git
> > a/src/main/java/net/sourceforge/cilib/util/selection/ordering/Ordering.ja
> >va
> > b/src/main/java/net/sourceforge/cilib/util/selection/ordering/Ordering.ja
> >va index 00f6b0d..ad34630 100644
> > ---
> > a/src/main/java/net/sourceforge/cilib/util/selection/ordering/Ordering.ja
> >va +++
> > b/src/main/java/net/sourceforge/cilib/util/selection/ordering/Ordering.ja
> >va @@ -30,7 +30,7 @@ import
> > net.sourceforge.cilib.util.selection.Selection; * @param <E> The type to
> > apply the ordering to.
> > * @author gpampara
> > */
> > -public interface Ordering<E extends Comparable> {
> > +public interface Ordering<E> {
> >
> > /**
> > * Apply the ordering on the provided list.
> > diff --git
> > a/src/main/java/net/sourceforge/cilib/util/selection/ordering/Proportiona
> >lO rdering.java
> > b/src/main/java/net/sourceforge/cilib/util/selection/ordering/Proportiona
> >lO rdering.java index 04dd378..538f06c 100644
> > ---
> > a/src/main/java/net/sourceforge/cilib/util/selection/ordering/Proportiona
> >lO rdering.java +++
> > b/src/main/java/net/sourceforge/cilib/util/selection/ordering/Proportiona
> >lO rdering.java @@ -30,7 +30,7 @@ import
> > net.sourceforge.cilib.util.selection.Selection; * @author Wiehann
> > Matthysen * @param <E>
> > */
> > -public class ProportionalOrdering<E extends Comparable> implements
> > Ordering<E> { +public class ProportionalOrdering<E> implements
> > Ordering<E> {
> >
> > private Random generator;
> >
> > @@ -61,14 +61,13 @@ public class ProportionalOrdering<E extends
> > Comparable> implements Ordering<E> { double randomValue =
> > this.generator.nextDouble() * total; double marker = 0.0;
> > int j = i;
> > - for (; j < elements.size() - 1 && marker < randomValue; ++j)
> > { - marker += elements.get(j).getWeight();
> > - }
> > - // Swap elements i and j.
> > - Selection.Entry<E> elementJ = elements.set(j,
> > elements.set(i, elements.get(j))); + do {
> > + marker += elements.get(j++).getWeight();
> > + } while (j < elements.size() && marker >= randomValue);
> > + // Swap elements i and j - 1.
> > + Selection.Entry<E> elementJ = elements.set(j - 1,
> > elements.set(i, elements.get(j - 1))); total -= elementJ.getWeight();
> > }
> > -
> > return true;
> > }
> > }
> > diff --git
> > a/src/main/java/net/sourceforge/cilib/util/selection/ordering/RandomOrder
> >in g.java
> > b/src/main/java/net/sourceforge/cilib/util/selection/ordering/RandomOrder
> >in g.java index 202a962..32a76f0 100644
> > ---
> > a/src/main/java/net/sourceforge/cilib/util/selection/ordering/RandomOrder
> >in g.java +++
> > b/src/main/java/net/sourceforge/cilib/util/selection/ordering/RandomOrder
> >in g.java @@ -33,7 +33,7 @@ import
> > net.sourceforge.cilib.util.selection.Selection; * @param <E> The
> > comparable type.
> > * @author gpampara
> > */
> > -public class RandomOrdering<E extends Comparable> implements Ordering<E>
> > { +public class RandomOrdering<E> implements Ordering<E> {
> >
> > private Random generator;
> >
> > diff --git
> > a/src/main/java/net/sourceforge/cilib/util/selection/ordering/ReverseOrde
> >ri ng.java
> > b/src/main/java/net/sourceforge/cilib/util/selection/ordering/ReverseOrde
> >ri ng.java index 6a85fae..3b00ad6 100644
> > ---
> > a/src/main/java/net/sourceforge/cilib/util/selection/ordering/ReverseOrde
> >ri ng.java +++
> > b/src/main/java/net/sourceforge/cilib/util/selection/ordering/ReverseOrde
> >ri ng.java @@ -29,7 +29,7 @@ import
> > net.sourceforge.cilib.util.selection.Selection; * @author Wiehann
> > Matthysen * @param <E>
> > */
> > -public class ReverseOrdering<E extends Comparable> implements
> > Ordering<E> { +public class ReverseOrdering<E> implements Ordering<E> {
> >
> > @Override
> > public boolean order(List<Selection.Entry<E>> elements) {
> > diff --git
> > a/src/main/java/net/sourceforge/cilib/util/selection/ordering/RingBasedOr
> >de ring.java
> > b/src/main/java/net/sourceforge/cilib/util/selection/ordering/RingBasedOr
> >de ring.java index 8927f8a..0cd8395 100644
> > ---
> > a/src/main/java/net/sourceforge/cilib/util/selection/ordering/RingBasedOr
> >de ring.java +++
> > b/src/main/java/net/sourceforge/cilib/util/selection/ordering/RingBasedOr
> >de ring.java @@ -29,7 +29,7 @@ import
> > net.sourceforge.cilib.util.selection.Selection; * @author Wiehann
> > Matthysen * @param <E>
> > */
> > -public class RingBasedOrdering<E extends Comparable> implements
> > Ordering<E> { +public class RingBasedOrdering<E> implements Ordering<E> {
> >
> > private E marker;
> >
> > @@ -51,9 +51,8 @@ public class RingBasedOrdering<E extends Comparable>
> > implements Ordering<E> {
> >
> > int position = 0;
> > for (Selection.Entry<E> entry : elements) {
> > - if (this.marker.compareTo(entry.getElement()) == 0)
> > + if (this.marker.equals(entry.getElement()))
> > break;
> > -
> > position++;
> > }
> >
> > diff --git
> > a/src/main/java/net/sourceforge/cilib/util/selection/ordering/SortedOrder
> >in g.java
> > b/src/main/java/net/sourceforge/cilib/util/selection/ordering/SortedOrder
> >in g.java index e080c07..4787233 100644
> > ---
> > a/src/main/java/net/sourceforge/cilib/util/selection/ordering/SortedOrder
> >in g.java +++
> > b/src/main/java/net/sourceforge/cilib/util/selection/ordering/SortedOrder
> >in g.java @@ -25,6 +25,7 @@ import java.util.Collections;
> > import java.util.Comparator;
> > import java.util.List;
> > import net.sourceforge.cilib.util.selection.Selection;
> > +import net.sourceforge.cilib.util.selection.Selection.Entry;
> >
> > /**
> > * Apply a sorting operation to the provided list, ordering the list
> > naturally @@ -34,9 +35,9 @@ import
> > net.sourceforge.cilib.util.selection.Selection; */
> > public class SortedOrdering<E extends Comparable> implements Ordering<E>
> > {
> >
> > - private Comparator<E> comparator;
> > + private Comparator<Entry<E>> comparator;
> >
> > - public SortedOrdering(Comparator<E> comparator) {
> > + public SortedOrdering(Comparator<Entry<E>> comparator) {
> > this.comparator = comparator;
> > }
> >
> > @@ -50,8 +51,8 @@ public class SortedOrdering<E extends Comparable>
> > implements Ordering<E> { * @return {@code true} if successful, {@code
> > false} otherwise. */
> > @Override
> > - public boolean order(List<Selection.Entry<E>> element) {
> > - Collections.sort(element);
> > + public boolean order(List<Selection.Entry<E>> elements) {
> > + Collections.sort(elements, this.comparator);
> > return true;
> > }
> > }
> > diff --git
> > a/src/main/java/net/sourceforge/cilib/util/selection/recipes/ElitistSelec
> >ti on.java
> > b/src/main/java/net/sourceforge/cilib/util/selection/recipes/ElitistSelec
> >ti on.java index 2e92e16..f9c9c6f 100644
> > ---
> > a/src/main/java/net/sourceforge/cilib/util/selection/recipes/ElitistSelec
> >ti on.java +++
> > b/src/main/java/net/sourceforge/cilib/util/selection/recipes/ElitistSelec
> >ti on.java @@ -25,6 +25,7 @@ import java.util.Collection;
> > import java.util.Comparator;
> > import java.util.List;
> > import net.sourceforge.cilib.util.selection.Selection;
> > +import net.sourceforge.cilib.util.selection.Selection.Entry;
> > import net.sourceforge.cilib.util.selection.ordering.DefaultComparator;
> > import net.sourceforge.cilib.util.selection.ordering.SortedOrdering;
> >
> > @@ -35,9 +36,9 @@ import
> > net.sourceforge.cilib.util.selection.ordering.SortedOrdering; public
> > class ElitistSelection<E extends Comparable> implements
> > SelectionRecipe<E> { private static final long serialVersionUID =
> > -5432603299031620114L;
> >
> > - private Comparator<E> comparator;
> > + private Comparator<Entry<E>> comparator;
> >
> > - public ElitistSelection(Comparator<E> comparator) {
> > + public ElitistSelection(Comparator<Entry<E>> comparator) {
> > this.comparator = comparator;
> > }
> >
> > @@ -49,11 +50,11 @@ public class ElitistSelection<E extends Comparable>
> > implements SelectionRecipe<E this.comparator = copy.comparator;
> > }
> >
> > - public void setComparator(Comparator<E> comparator) {
> > + public void setComparator(Comparator<Entry<E>> comparator) {
> > this.comparator = comparator;
> > }
> >
> > - public Comparator<E> getComparator() {
> > + public Comparator<Entry<E>> getComparator() {
> > return this.comparator;
> > }
> >
> > @@ -64,7 +65,7 @@ public class ElitistSelection<E extends Comparable>
> > implements SelectionRecipe<E
> >
> > @Override
> > public E select(Collection<? extends E> elements) {
> > - List<E> selection = Selection.from(elements).orderBy(new
> > SortedOrdering(this.comparator)).last().select(); + List<E>
> > selection = Selection.from(elements).orderBy(new
> > SortedOrdering<E>(this.comparator)).last().select(); return
> > selection.get(0);
> > }
> > }
> > diff --git
> > a/src/main/java/net/sourceforge/cilib/util/selection/recipes/RandomSelect
> >io n.java
> > b/src/main/java/net/sourceforge/cilib/util/selection/recipes/RandomSelect
> >io n.java index d30d1de..0eb1401 100644
> > ---
> > a/src/main/java/net/sourceforge/cilib/util/selection/recipes/RandomSelect
> >io n.java +++
> > b/src/main/java/net/sourceforge/cilib/util/selection/recipes/RandomSelect
> >io n.java @@ -32,7 +32,7 @@ import
> > net.sourceforge.cilib.util.selection.ordering.RandomOrdering; * @author
> > Wiehann Matthysen
> > * @param <E>
> > */
> > -public class RandomSelection<E extends Comparable> implements
> > SelectionRecipe<E> { +public class RandomSelection<E> implements
> > SelectionRecipe<E> {
> > private static final long serialVersionUID = -5099663528040315048L;
> >
> > private Random random;
> > @@ -56,7 +56,7 @@ public class RandomSelection<E extends Comparable>
> > implements SelectionRecipe<E>
> >
> > @Override
> > public E select(Collection<? extends E> elements) {
> > - List<E> selection = Selection.from(elements).orderBy(new
> > RandomOrdering<E>(this.random)).first().select(); + List<E>
> > selection = Selection.from(elements).orderBy(new
> > RandomOrdering<E>(this.random)).last().select(); return selection.get(0);
> > }
> > }
> > diff --git
> > a/src/main/java/net/sourceforge/cilib/util/selection/recipes/RankBasedSel
> >ec tion.java
> > b/src/main/java/net/sourceforge/cilib/util/selection/recipes/RankBasedSel
> >ec tion.java index 75b13bf..779a855 100644
> > ---
> > a/src/main/java/net/sourceforge/cilib/util/selection/recipes/RankBasedSel
> >ec tion.java +++
> > b/src/main/java/net/sourceforge/cilib/util/selection/recipes/RankBasedSel
> >ec tion.java @@ -27,6 +27,7 @@ import java.util.List;
> > import net.sourceforge.cilib.math.random.generator.MersenneTwister;
> > import net.sourceforge.cilib.math.random.generator.Random;
> > import net.sourceforge.cilib.util.selection.Selection;
> > +import net.sourceforge.cilib.util.selection.Selection.Entry;
> > import net.sourceforge.cilib.util.selection.ordering.DefaultComparator;
> > import net.sourceforge.cilib.util.selection.ordering.RandomOrdering;
> > import net.sourceforge.cilib.util.selection.ordering.SortedOrdering;
> > @@ -38,15 +39,15 @@ import
> > net.sourceforge.cilib.util.selection.ordering.SortedOrdering; public
> > class RankBasedSelection<E extends Comparable> implements
> > SelectionRecipe<E> { private static final long serialVersionUID =
> > -2387196820773731607L;
> >
> > - private Comparator<E> comparator;
> > + private Comparator<Entry<E>> comparator;
> > private Random random;
> >
> > - public RankBasedSelection(Comparator<E> comparator, Random random) {
> > + public RankBasedSelection(Comparator<Entry<E>> comparator, Random
> > random) { this.comparator = comparator;
> > this.random = random;
> > }
> >
> > - public RankBasedSelection(Comparator<E> comparator) {
> > + public RankBasedSelection(Comparator<Entry<E>> comparator) {
> > this(comparator, new MersenneTwister());
> > }
> >
> > @@ -59,11 +60,11 @@ public class RankBasedSelection<E extends Comparable>
> > implements SelectionRecipe this.random = copy.random.getClone();
> > }
> >
> > - public void setComparator(Comparator<E> comparator) {
> > + public void setComparator(Comparator<Entry<E>> comparator) {
> > this.comparator = comparator;
> > }
> >
> > - public Comparator<E> getComparator() {
> > + public Comparator<Entry<E>> getComparator() {
> > return this.comparator;
> > }
> >
> > @@ -74,7 +75,7 @@ public class RankBasedSelection<E extends Comparable>
> > implements SelectionRecipe
> >
> > @Override
> > public E select(Collection<? extends E> elements) {
> > - List<E> selection = Selection.from(elements).orderBy(new
> > SortedOrdering<E>(this.comparator)).first(this.random.nextInt(elements.si
> >ze ())).orderBy(new RandomOrdering<E>(this.random)).first().select(); +
> > List<E> selection = Selection.from(elements).orderBy(new
> > SortedOrdering<E>(this.comparator)).last(this.random.nextInt(elements.siz
> >e( ))).orderBy(new RandomOrdering<E>(this.random)).last().select(); return
> > selection.get(0);
> > }
> > }
> > diff --git
> > a/src/main/java/net/sourceforge/cilib/util/selection/recipes/RingBasedPop
> >ul ationSelection.java
> > b/src/main/java/net/sourceforge/cilib/util/selection/recipes/RingBasedPop
> >ul ationSelection.java index 5da4287..4886855 100644
> > ---
> > a/src/main/java/net/sourceforge/cilib/util/selection/recipes/RingBasedPop
> >ul ationSelection.java +++
> > b/src/main/java/net/sourceforge/cilib/util/selection/recipes/RingBasedPop
> >ul ationSelection.java @@ -21,9 +21,7 @@
> > */
> > 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;
> > @@ -48,33 +46,6 @@ public class RingBasedPopulationSelection implements
> > SelectionRecipe<PopulationB
> >
> > @Override
> > public PopulationBasedAlgorithm select(Collection<? extends
> > PopulationBasedAlgorithm> elements) { -
> > 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().si
> >ng leSelect(); - return wrapper.get();
> > + return Selection.from(elements).o...
[truncated message content] |