|
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
|