|
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 TournamentSelection() {
@@ -68,11 +69,11 @@ public class TournamentSelection<E extends Comparable> implements SelectionRecip
this.tournamentProportion = tournamanetSize;
}
- 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;
}
@@ -80,6 +81,6 @@ public class TournamentSelection<E extends Comparable> implements SelectionRecip
public E select(Collection<? extends E> elements) {
int tournamentSize = Double.valueOf(this.tournamentProportion.getParameter() * elements.size()).intValue();
return Selection.from(elements).orderBy(new RandomOrdering<E>(this.random)).
- first(tournamentSize).orderBy(new SortedOrdering<E>(this.comparator)).first().select().get(0);
+ last(tournamentSize).orderBy(new SortedOrdering<E>(this.comparator)).last().select().get(0);
}
}
diff --git a/src/main/java/net/sourceforge/cilib/util/selection/weighing/FixedWeighing.java b/src/main/java/net/sourceforge/cilib/util/selection/weighing/FixedWeighing.java
index 5152de4..7a6a217 100644
--- a/src/main/java/net/sourceforge/cilib/util/selection/weighing/FixedWeighing.java
+++ b/src/main/java/net/sourceforge/cilib/util/selection/weighing/FixedWeighing.java
@@ -28,7 +28,7 @@ import net.sourceforge.cilib.util.selection.Selection;
* @author Wiehann Matthysen
* @param <E>
*/
-public class FixedWeighing<E extends Comparable> implements Weighing<E> {
+public class FixedWeighing<E> implements Weighing<E> {
private static final long serialVersionUID = -6990220691744842964L;
private double weight;
diff --git a/src/main/java/net/sourceforge/cilib/util/selection/weighing/LinearWeighing.java b/src/main/java/net/sourceforge/cilib/util/selection/weighing/LinearWeighing.java
index 21d1adc..64ce536 100644
--- a/src/main/java/net/sourceforge/cilib/util/selection/weighing/LinearWeighing.java
+++ b/src/main/java/net/sourceforge/cilib/util/selection/weighing/LinearWeighing.java
@@ -28,7 +28,7 @@ import net.sourceforge.cilib.util.selection.Selection;
* @author Wiehann Matthysen
* @param <E>
*/
-public class LinearWeighing<E extends Comparable> implements Weighing<E> {
+public class LinearWeighing<E> implements Weighing<E> {
private static final long serialVersionUID = 3294682425241945584L;
private double min;
@@ -37,7 +37,7 @@ public class LinearWeighing<E extends Comparable> implements Weighing<E> {
public LinearWeighing() {
this(0.0, 1.0);
}
-
+
public LinearWeighing(double min, double max) {
this.min = min;
this.max = max;
@@ -76,7 +76,6 @@ public class LinearWeighing<E extends Comparable> implements Weighing<E> {
for (Selection.Entry<E> element : elements) {
element.setWeight(objectIndex++ * stepSize + this.min);
}
-
return true;
}
}
diff --git a/src/main/java/net/sourceforge/cilib/util/selection/weighing/Weighing.java b/src/main/java/net/sourceforge/cilib/util/selection/weighing/Weighing.java
index 158f931..9fd703d 100644
--- a/src/main/java/net/sourceforge/cilib/util/selection/weighing/Weighing.java
+++ b/src/main/java/net/sourceforge/cilib/util/selection/weighing/Weighing.java
@@ -29,7 +29,7 @@ import net.sourceforge.cilib.util.selection.Selection;
* @author Wiehann Matthysen
* @param <E>
*/
-public interface Weighing<E extends Comparable> extends Cloneable {
+public interface Weighing<E> extends Cloneable {
@Override
public Weighing<E> getClone();
diff --git a/src/main/java/net/sourceforge/cilib/util/selection/weighing/entity/EntityWeighing.java b/src/main/java/net/sourceforge/cilib/util/selection/weighing/entity/EntityWeighing.java
index 0feb33c..05ec7e7 100644
--- a/src/main/java/net/sourceforge/cilib/util/selection/weighing/entity/EntityWeighing.java
+++ b/src/main/java/net/sourceforge/cilib/util/selection/weighing/entity/EntityWeighing.java
@@ -35,8 +35,8 @@ import net.sourceforge.cilib.util.selection.Selection;
* @param <E>
*/
public class EntityWeighing<E extends Entity> implements Weighing<E> {
- private static final long serialVersionUID = 5906567326258195932L;
+ private static final long serialVersionUID = 5906567326258195932L;
private EntityFitness<E> entityFitness;
public EntityWeighing(EntityFitness<E> entityFitness) {
@@ -63,7 +63,7 @@ public class EntityWeighing<E extends Entity> implements Weighing<E> {
if (minMaxFitness.getKey() == InferiorFitness.instance() || fitness.compareTo(minMaxFitness.getKey()) < 0) {
minMaxFitness.setKey(fitness);
}
- else if (minMaxFitness.getValue() == InferiorFitness.instance() || fitness.compareTo(minMaxFitness.getValue()) > 0) {
+ if (minMaxFitness.getValue() == InferiorFitness.instance() || fitness.compareTo(minMaxFitness.getValue()) > 0) {
minMaxFitness.setValue(fitness);
}
}
@@ -72,7 +72,6 @@ public class EntityWeighing<E extends Entity> implements Weighing<E> {
@Override
public boolean weigh(List<Selection.Entry<E>> entities) {
-// List<Pair<Double, E>> weighedEntities = new ArrayList<Pair<Double, E>>(entities.size());
Pair<Fitness, Fitness> minMaxFitness = getMinMaxFitness(entities);
if (minMaxFitness.getKey() == InferiorFitness.instance() ||
@@ -85,7 +84,6 @@ public class EntityWeighing<E extends Entity> implements Weighing<E> {
for (Selection.Entry<E> entity : entities) {
double weight = (this.entityFitness.getFitness(entity.getElement()).getValue() - minMaxFitness.getKey().getValue()) / minMaxDifference;
entity.setWeight(weight);
-// weighedEntities.add(new Pair<Double, E>(weight, entity));
}
return true;
diff --git a/src/test/java/net/sourceforge/cilib/entity/operators/selection/RouletteWheelSelectionStrategyTest.java b/src/test/java/net/sourceforge/cilib/entity/operators/selection/RouletteWheelSelectionStrategyTest.java
index 4b69fc7..d030f6d 100644
--- a/src/test/java/net/sourceforge/cilib/entity/operators/selection/RouletteWheelSelectionStrategyTest.java
+++ b/src/test/java/net/sourceforge/cilib/entity/operators/selection/RouletteWheelSelectionStrategyTest.java
@@ -23,7 +23,6 @@
*/
package net.sourceforge.cilib.entity.operators.selection;
-
import junit.framework.Assert;
import net.sourceforge.cilib.ec.Individual;
import net.sourceforge.cilib.entity.Entity;
@@ -34,7 +33,6 @@ import net.sourceforge.cilib.problem.MaximisationFitness;
import net.sourceforge.cilib.problem.MinimisationFitness;
import org.junit.Before;
-import org.junit.Ignore;
import org.junit.Test;
/**
@@ -48,7 +46,7 @@ import org.junit.Test;
* </p>
*/
public class RouletteWheelSelectionStrategyTest {
-
+
private Topology<Individual> topology;
private Individual individual1;
private Individual individual2;
@@ -57,7 +55,7 @@ public class RouletteWheelSelectionStrategyTest {
@Before
public void createDummyTopology() {
topology = new GBestTopology<Individual>();
-
+
individual1 = new Individual();
individual2 = new Individual();
individual3 = new Individual();
@@ -83,20 +81,19 @@ public class RouletteWheelSelectionStrategyTest {
Assert.assertNotNull(entity);
Assert.assertTrue(topology.contains(entity));
- Assert.assertEquals(entity, individual3);
+ Assert.assertSame(entity, individual3);
}
@Test
public void maximizationSelection() {
- individual1.getProperties().put(EntityType.FITNESS, new MaximisationFitness(90000.0)); // Should be the best entity
- individual2.getProperties().put(EntityType.FITNESS, new MaximisationFitness(0.5));
+ individual1.getProperties().put(EntityType.FITNESS, new MaximisationFitness(0.5));
+ individual2.getProperties().put(EntityType.FITNESS, new MaximisationFitness(90000.0)); // Should be the best entity
individual3.getProperties().put(EntityType.FITNESS, new MaximisationFitness(0.5));
RouletteWheelSelectionStrategy rouletteWheelSelectionStrategy = new RouletteWheelSelectionStrategy();
Entity entity = rouletteWheelSelectionStrategy.select(topology);
Assert.assertNotNull(entity);
- Assert.assertTrue(entity.equals(individual1));
+ Assert.assertTrue(entity.equals(individual2));
}
-
}
diff --git a/src/test/java/net/sourceforge/cilib/util/selection/ordering/OrderingTest.java b/src/test/java/net/sourceforge/cilib/util/selection/ordering/OrderingTest.java
index 5dc48e3..6bdc981 100644
--- a/src/test/java/net/sourceforge/cilib/util/selection/ordering/OrderingTest.java
+++ b/src/test/java/net/sourceforge/cilib/util/selection/ordering/OrderingTest.java
@@ -52,7 +52,6 @@ public class OrderingTest {
List<Selection.Entry<Integer>> entries = Selection.from(elements).entries();
boolean ordered = new ReverseOrdering<Integer>().order(entries);
Assert.assertTrue(ordered);
-
for (int i = 0; i < 9; ++i) {
Assert.assertEquals(elements.size() - i, entries.get(i).getElement().intValue());
}
@@ -64,7 +63,6 @@ public class OrderingTest {
List<Selection.Entry<Integer>> entries = Selection.from(elements).entries();
boolean ordered = new RingBasedOrdering<Integer>(5).order(entries);
Assert.assertTrue(ordered);
- System.out.println("entries: " + entries);
Assert.assertEquals(6, entries.get(0).getElement().intValue());
Assert.assertEquals(7, entries.get(1).getElement().intValue());
Assert.assertEquals(8, entries.get(2).getElement().intValue());
@@ -75,7 +73,7 @@ public class OrderingTest {
Assert.assertEquals(4, entries.get(7).getElement().intValue());
Assert.assertEquals(5, entries.get(8).getElement().intValue());
}
-
+
@Test
public void sortedOrdering() {
List<Integer> elements = Arrays.asList(9, 8, 7, 6, 5, 4, 3, 2, 1);
diff --git a/src/test/java/net/sourceforge/cilib/util/selection/weighing/WeighingTest.java b/src/test/java/net/sourceforge/cilib/util/selection/weighing/WeighingTest.java
new file mode 100644
index 0000000..286b687
--- /dev/null
+++ b/src/test/java/net/sourceforge/cilib/util/selection/weighing/WeighingTest.java
@@ -0,0 +1,59 @@
+/**
+ * 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.weighing;
+
+import java.util.Arrays;
+import java.util.List;
+import net.sourceforge.cilib.util.selection.Selection;
+import net.sourceforge.cilib.util.selection.Selection.Entry;
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * @author Wiehann Matthysen
+ */
+public class WeighingTest {
+
+ @Test
+ public void fixedWeighing() {
+ List<Integer> elements = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
+ List<Entry<Integer>> weighedElements = Selection.from(elements).weigh(new FixedWeighing(1.0)).entries();
+ for (int i = 0; i < weighedElements.size(); ++i) {
+ Assert.assertEquals(1.0, weighedElements.get(i).getWeight(), 0.0001);
+ }
+ }
+
+ @Test
+ public void linearWeighing() {
+ List<Integer> elements = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
+ List<Entry<Integer>> weighedElements = Selection.from(elements).weigh(new LinearWeighing<Integer>(0.0,1.0)).entries();
+ Assert.assertEquals(0.0, weighedElements.get(0).getWeight(), 0.0001);
+ Assert.assertEquals(0.125, weighedElements.get(1).getWeight(), 0.0001);
+ Assert.assertEquals(0.25, weighedElements.get(2).getWeight(), 0.0001);
+ Assert.assertEquals(0.375, weighedElements.get(3).getWeight(), 0.0001);
+ Assert.assertEquals(0.5, weighedElements.get(4).getWeight(), 0.0001);
+ Assert.assertEquals(0.625, weighedElements.get(5).getWeight(), 0.0001);
+ Assert.assertEquals(0.75, weighedElements.get(6).getWeight(), 0.0001);
+ Assert.assertEquals(0.875, weighedElements.get(7).getWeight(), 0.0001);
+ Assert.assertEquals(1.0, weighedElements.get(8).getWeight(), 0.0001);
+ }
+}
diff --git a/src/test/java/net/sourceforge/cilib/util/selection/weighing/entity/EntityWeighingTest.java b/src/test/java/net/sourceforge/cilib/util/selection/weighing/entity/EntityWeighingTest.java
index c2d0c7a..f8d23ef 100644
--- a/src/test/java/net/sourceforge/cilib/util/selection/weighing/entity/EntityWeighingTest.java
+++ b/src/test/java/net/sourceforge/cilib/util/selection/weighing/entity/EntityWeighingTest.java
@@ -27,6 +27,7 @@ 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 net.sourceforge.cilib.problem.MinimisationFitness;
import net.sourceforge.cilib.util.selection.Selection;
import net.sourceforge.cilib.util.selection.Selection.Entry;
import org.junit.Assert;
@@ -38,7 +39,7 @@ import org.junit.Test;
public class EntityWeighingTest {
@Test
- public void entityWeighing() {
+ public void entityWeighingMaximise() {
Individual i1 = createIndividual(new MaximisationFitness(1.0));
Individual i2 = createIndividual(new MaximisationFitness(2.0));
Individual i3 = createIndividual(new MaximisationFitness(3.0));
@@ -53,6 +54,22 @@ public class EntityWeighingTest {
Assert.assertEquals(1.0, entries.get(2).getWeight(), 0.0001);
}
+ @Test
+ public void entityWeighingMinimise() {
+ Individual i1 = createIndividual(new MinimisationFitness(1.0));
+ Individual i2 = createIndividual(new MinimisationFitness(2.0));
+ Individual i3 = createIndividual(new MinimisationFitness(3.0));
+
+ List<Individual> individuals = Arrays.asList(i1, i2, i3);
+ List<Selection.Entry<Individual>> entries = Selection.from(individuals).entries();
+ EntityWeighing<Individual> weighing = new EntityWeighing<Individual>();
+ weighing.weigh(entries);
+
+ Assert.assertEquals(1.0, entries.get(0).getWeight(), 0.0001);
+ Assert.assertEquals(0.5, entries.get(1).getWeight(), 0.0001);
+ Assert.assertEquals(0.0, entries.get(2).getWeight(), 0.0001);
+ }
+
private Individual createIndividual(Fitness fitness) {
Individual i = new Individual();
i.getProperties().put(EntityType.FITNESS, fitness);
diff --git a/xml/dynamic-vepso.xml b/xml/dynamic-vepso.xml
index 7d83de6..0fc7941 100644
--- a/xml/dynamic-vepso.xml
+++ b/xml/dynamic-vepso.xml
@@ -16,9 +16,7 @@
<globalGuideSelectionStrategy class="pso.moo.guideselectionstrategies.VEPSOGuideSelectionStrategy">
<knowledgeTransferStrategy class="algorithm.population.knowledgetransferstrategies.SelectiveKnowledgeTransferStrategy">
<populationSelection class="util.selection.recipes.RingBasedPopulationSelection"/>
- <entitySelection class="util.selection.recipes.TournamentSelection">
- <comparator class="entity.comparator.DescendingFitnessComparator"/>
- </entitySelection>
+ <entitySelection class="util.selection.recipes.TournamentSelection"/>
</knowledgeTransferStrategy>
</globalGuideSelectionStrategy>
<globalGuideUpdateStrategy class="pso.moo.guideupdatestrategies.StandardGuideUpdateStrategy"/>
diff --git a/xml/vepso.xml b/xml/vepso.xml
index a56d454..58f41c9 100644
--- a/xml/vepso.xml
+++ b/xml/vepso.xml
@@ -15,9 +15,7 @@
<globalGuideSelectionStrategy class="pso.moo.guideselectionstrategies.VEPSOGuideSelectionStrategy">
<knowledgeTransferStrategy class="algorithm.population.knowledgetransferstrategies.SelectiveKnowledgeTransferStrategy">
<populationSelection class="util.selection.recipes.RingBasedPopulationSelection"/>
- <entitySelection class="util.selection.recipes.TournamentSelection">
- <comparator class="entity.comparator.DescendingFitnessComparator"/>
- </entitySelection>
+ <entitySelection class="util.selection.recipes.TournamentSelection"/>
</knowledgeTransferStrategy>
</globalGuideSelectionStrategy>
<globalGuideUpdateStrategy class="pso.moo.guideupdatestrategies.StandardGuideUpdateStrategy"/>
--
1.6.0.6
|