From: Andrich v. W. <avw...@gm...> - 2009-07-20 13:22:49
|
Fixed bug that caused the unit tests of VisualPositionUpdateStategy and ExplorerBee to fail, the issue was related to bees leaving the function domain, which should be allowed. The tests were also expanded. Removed dependecy of ExplorerBee on Algorithm.get(), enabling easier testing. Changed exactly when bees calculate fitnesses, new implementation is slightly more efficient. Changed a number of method names and made slight changes to the abc.xml file. Fixed a number of code style and reported PMD issues. --- src/main/java/net/sourceforge/cilib/boa/ABC.java | 141 +++++++++++++++++--- .../net/sourceforge/cilib/boa/bee/AbstractBee.java | 34 +++++- .../net/sourceforge/cilib/boa/bee/ExplorerBee.java | 82 ++++++++++-- .../net/sourceforge/cilib/boa/bee/HoneyBee.java | 2 + .../net/sourceforge/cilib/boa/bee/OnlookerBee.java | 8 +- .../net/sourceforge/cilib/boa/bee/WorkerBee.java | 28 +++- .../BeePositionUpdateStrategy.java | 7 + .../VisualPositionUpdateStategy.java | 8 +- .../java/net/sourceforge/cilib/boa/ABCTest.java | 8 +- .../cilib/boa/bees/ExplorerBeeTest.java | 51 +++---- .../VisualPositionUpdateStategyTest.java | 25 ++-- xml/abc.xml | 136 ++++++++----------- 12 files changed, 359 insertions(+), 171 deletions(-) diff --git a/src/main/java/net/sourceforge/cilib/boa/ABC.java b/src/main/java/net/sourceforge/cilib/boa/ABC.java index e35e0a6..614912a 100644 --- a/src/main/java/net/sourceforge/cilib/boa/ABC.java +++ b/src/main/java/net/sourceforge/cilib/boa/ABC.java @@ -63,19 +63,16 @@ import net.sourceforge.cilib.problem.OptimisationSolution; * */ public class ABC extends SinglePopulationBasedAlgorithm { - private static final long serialVersionUID = 7918711449442012960L; + private static final long serialVersionUID = 7918711449442012960L; private Topology<HoneyBee> workerBees; //keeps references to the worker bees private Topology<HoneyBee> onlookerBees; //keeps references to the onlooker bees private Topology<HoneyBee> hive; //keeps references to all the bees (workers and onlookers) - private ExplorerBee explorerBee; //explorer bee private SelectionStrategy dancingSelectionStrategy; //bee dancing selection strategy - private ControlParameter workerBeePercentage; //control parameter for number of worker bees private ControlParameter forageLimit; //control parameter for the forage limit private ControlParameter explorerBeeUpdateLimit; //control parameter to limit the explorer bee position updates per iteration - private HoneyBee bestBee; //reference to best solution found so far /** @@ -134,7 +131,7 @@ public class ABC extends SinglePopulationBasedAlgorithm { this.initialisationStrategy.initialise(hive, this.optimisationProblem); int i; - int numWorkerBees = (int) (workerBeePercentage.getParameter()*hive.size()); + int numWorkerBees = (int) (workerBeePercentage.getParameter() * hive.size()); for (i = 0; i < numWorkerBees; i++) { WorkerBee bee = (WorkerBee) hive.get(i); bee.setForageLimit(this.forageLimit.getClone()); @@ -158,11 +155,9 @@ public class ABC extends SinglePopulationBasedAlgorithm { protected void algorithmIteration() { for (HoneyBee bee : workerBees) { bee.updatePosition(); - bee.calculateFitness(); if (bestBee == null) { bestBee = bee.getClone(); - } - else if (bee.getBestFitness().compareTo(bestBee.getBestFitness()) > 0) { + } else if (bee.getBestFitness().compareTo(bestBee.getBestFitness()) > 0) { bestBee = bee.getClone(); } } @@ -171,11 +166,9 @@ public class ABC extends SinglePopulationBasedAlgorithm { HoneyBee selectedBee = dancingSelectionStrategy.select(workerBees); bee.setPosition(selectedBee.getPosition().getClone()); bee.updatePosition(); - bee.calculateFitness(); if (bestBee == null) { bestBee = bee; - } - else if (bee.getBestFitness().compareTo(bestBee.getBestFitness()) > 0) { + } else if (bee.getBestFitness().compareTo(bestBee.getBestFitness()) > 0) { bestBee = bee; } } @@ -186,8 +179,9 @@ public class ABC extends SinglePopulationBasedAlgorithm { */ @Override public OptimisationSolution getBestSolution() { - if (this.bestBee == null) + if (this.bestBee == null) { throw new InitialisationException("Best solution cannot be determined before algorithm is run"); + } return new OptimisationSolution(bestBee.getPosition(), bestBee.getFitness()); } @@ -208,14 +202,6 @@ public class ABC extends SinglePopulationBasedAlgorithm { return this.hive; } - public Topology<HoneyBee> getWorkerTopology() { - return this.workerBees; - } - - public Topology<HoneyBee> getOnlookerTopology() { - return this.onlookerBees; - } - /** * {@inheritDoc} */ @@ -224,41 +210,156 @@ public class ABC extends SinglePopulationBasedAlgorithm { throw new UnsupportedOperationException("Method not implemented"); } + /** + * Gets the bee dancing selection strategy. + * @return the bee dancing selection strategy. + */ public SelectionStrategy getDancingSelectionStrategy() { return dancingSelectionStrategy; } + /** + * Sets the bee dancinc selection strategy. + * @param dancingSelectionStrategy the new bee dancing selection strategy. + */ public void setDancingSelectionStrategy( SelectionStrategy dancingSelectionStrategy) { this.dancingSelectionStrategy = dancingSelectionStrategy; } + /** + * Gets the explorer bee. + * @return the explorer bee. + */ public ExplorerBee getExplorerBee() { return this.explorerBee; } + /** + * Sets the explorer bee. + * @param explorerBee the new explorer bee. + */ + public void setExplorerBee(ExplorerBee explorerBee) { + this.explorerBee = explorerBee; + } + + /** + * Gets the {@code ControlParameter} specifying the number of worker bees. + * @return the {@code ControlParameter} specifying the number of worker bees. + */ public ControlParameter getWorkerBeeNumber() { return workerBeePercentage; } + /** + * Gets the {@code ControlParameter} specifying the percentage of worker bees. + * @return the {@code ControlParameter} specifying the percentage of worker bees. + */ + public ControlParameter getWorkerBeePercentage() { + return workerBeePercentage; + } + + /** + * Sets the {@code ControlParameter} specifying the percentage of worker bees.. + * @param workerBeeNumber the new {@code ControlParameter} specifying the percentage of worker bees. + */ public void setWorkerBeePercentage(ControlParameter workerBeeNumber) { this.workerBeePercentage = workerBeeNumber; } + /** + * Gets the {@code ControlParameter} specifying the foraging limit. + * @return the {@code ControlParameter} specifying the foraging limit. + */ public ControlParameter getForageLimit() { return forageLimit; } + /** + * Sets the {@code ControlParameter} specifying the foraging limit. + * @param forageThreshold the new {@code ControlParameter} specifying the foraging limit. + */ public void setForageLimit(ControlParameter forageThreshold) { this.forageLimit = forageThreshold; } + /** + * Gets the {@code ControlParameter} specifying the limit to how many times the explorer bee can update positions. + * @return the {@code ControlParameter} specifying the limit to how many times the explorer bee can update positions. + */ public ControlParameter getExplorerBeeUpdateLimit() { return explorerBeeUpdateLimit; } + /** + * Sets the {@code ControlParameter} specifying the limit to how many times the explorer bee can update positions. + * @param explorerBeeUpdateLimit the {@code ControlParameter} specifying the limit to how many times the explorer bee can update positions. + */ public void setExplorerBeeUpdateLimit(ControlParameter explorerBeeUpdateLimit) { this.explorerBeeUpdateLimit = explorerBeeUpdateLimit; } + /** + * Gets the bee with the highest fitness during the algorithm execution. + * @return the bee with the highest fitness. + */ + public HoneyBee getBestBee() { + return bestBee; + } + + /** + * Sets the bee with the highest fitness during the algorithm execution. + * @param bestBee the new bee with the highest fitness. + */ + public void setBestBee(HoneyBee bestBee) { + this.bestBee = bestBee; + } + + /** + * Get the {@code Topology} containing all the bees in the hive. + * @return the {@code Topology} containing all bees in the hive. + */ + public Topology<HoneyBee> getHive() { + return hive; + } + + /** + * Set the {@code Topology} containing all the bees in the hive. + * @param hive the new {@code Topology} containing all bees in the hive. + */ + public void setHive(Topology<HoneyBee> hive) { + this.hive = hive; + } + + /** + * Get the {@code Topology} containing the onlooker bees. + * @return the {@code Topology} containing the onlooker bees. + */ + public Topology<HoneyBee> getOnlookerBees() { + return onlookerBees; + } + + /** + * Set the {@code Topology} containing the onlooker bees. + * @param onlookerBees the new {@code Topology} containing the onlooker bees. + */ + public void setOnlookerBees(Topology<HoneyBee> onlookerBees) { + this.onlookerBees = onlookerBees; + } + + /** + * Get the {@code Topology} containing the worker bees. + * @return the {@code Topology} containing the worker bees. + */ + public Topology<HoneyBee> getWorkerBees() { + return workerBees; + } + + /** + * Set the {@code Topology} containing the worker bees. + * @param workerBees the new {@code Topology} containing the worker bees. + */ + public void setWorkerBees(Topology<HoneyBee> workerBees) { + this.workerBees = workerBees; + } } diff --git a/src/main/java/net/sourceforge/cilib/boa/bee/AbstractBee.java b/src/main/java/net/sourceforge/cilib/boa/bee/AbstractBee.java index 1830192..ddef6ac 100644 --- a/src/main/java/net/sourceforge/cilib/boa/bee/AbstractBee.java +++ b/src/main/java/net/sourceforge/cilib/boa/bee/AbstractBee.java @@ -21,7 +21,6 @@ */ package net.sourceforge.cilib.boa.bee; -import net.sourceforge.cilib.type.types.Types; import net.sourceforge.cilib.boa.positionupdatestrategies.BeePositionUpdateStrategy; import net.sourceforge.cilib.boa.positionupdatestrategies.VisualPositionUpdateStategy; import net.sourceforge.cilib.entity.AbstractEntity; @@ -40,8 +39,8 @@ import net.sourceforge.cilib.type.types.container.Vector; * */ public abstract class AbstractBee extends AbstractEntity implements HoneyBee { - private static final long serialVersionUID = 7005546673802814268L; + private static final long serialVersionUID = 7005546673802814268L; protected BeePositionUpdateStrategy positionUpdateStrategy; protected SelectionStrategy targetSelectionStrategy; protected int dimension; @@ -80,6 +79,14 @@ public abstract class AbstractBee extends AbstractEntity implements HoneyBee { } /** + * Sets the position update strategy of the bee. + * @param positionUpdateStrategy the new position update strategy. + */ + public void setPositionUpdateStrategy(BeePositionUpdateStrategy positionUpdateStrategy) { + this.positionUpdateStrategy = positionUpdateStrategy; + } + + /** * {@inheritDoc} */ @Override @@ -118,6 +125,14 @@ public abstract class AbstractBee extends AbstractEntity implements HoneyBee { } /** + * Sets the dimension of the solution used by the bee. + * @param dimension the new dimension of the solution. + */ + public void setDimension(int dimension) { + this.dimension = dimension; + } + + /** * {@inheritDoc} */ public Vector getPosition() { @@ -151,4 +166,19 @@ public abstract class AbstractBee extends AbstractEntity implements HoneyBee { throw new UnsupportedOperationException("Reinitialise not implemented for AbstractBee"); } + /** + * Gets the target selection strategy, for selecting bees to follow in position updates. + * @return the target selection strategy. + */ + public SelectionStrategy getTargetSelectionStrategy() { + return targetSelectionStrategy; + } + + /** + * Sets the target selection strategy, for selecting bees to follow in position updates. + * @param targetSelectionStrategy the new target selection strategy. + */ + public void setTargetSelectionStrategy(SelectionStrategy targetSelectionStrategy) { + this.targetSelectionStrategy = targetSelectionStrategy; + } } diff --git a/src/main/java/net/sourceforge/cilib/boa/bee/ExplorerBee.java b/src/main/java/net/sourceforge/cilib/boa/bee/ExplorerBee.java index 3497aad..68f6ac4 100644 --- a/src/main/java/net/sourceforge/cilib/boa/bee/ExplorerBee.java +++ b/src/main/java/net/sourceforge/cilib/boa/bee/ExplorerBee.java @@ -21,11 +21,10 @@ */ package net.sourceforge.cilib.boa.bee; -import net.sourceforge.cilib.algorithm.Algorithm; import net.sourceforge.cilib.controlparameter.ConstantControlParameter; import net.sourceforge.cilib.controlparameter.ControlParameter; import net.sourceforge.cilib.math.random.generator.MersenneTwister; -import net.sourceforge.cilib.math.random.generator.Seeder; +import net.sourceforge.cilib.math.random.generator.Random; import net.sourceforge.cilib.type.types.container.Vector; import net.sourceforge.cilib.util.Cloneable; @@ -38,9 +37,9 @@ import net.sourceforge.cilib.util.Cloneable; * */ public class ExplorerBee implements Cloneable { - private static final long serialVersionUID = 1068799535328234923L; - private MersenneTwister random; //generates a random position + private static final long serialVersionUID = 1068799535328234923L; + private Random random; //generates a random position private int previousUpdatedIteration; //used to check whether the algorithm has entered a new iteration private int numberOfUpdates; //how many have occured in current iteration private ControlParameter explorerBeeUpdateLimit; @@ -70,24 +69,24 @@ public class ExplorerBee implements Cloneable { /** * {@inheritDoc} */ + @Override public ExplorerBee getClone() { return new ExplorerBee(this); } /** * Verifies it is allowed for a worker bee to convert to an explorer bee. - * @precondition an algorithm is on the algorithm stack. + * @param currentIteration the current iteration of the algorithm on the stack. * @return whether the search is allowed. */ - public boolean searchAllowed() { - int currentIteration = Algorithm.get().getIterations(); + public boolean searchAllowed(int currentIteration) { if (previousUpdatedIteration == currentIteration) { //TODO: Add variable number of updates allowed - if (Double.compare(numberOfUpdates, explorerBeeUpdateLimit.getParameter()) < 0) + if (Double.compare(numberOfUpdates, explorerBeeUpdateLimit.getParameter()) < 0) { return true; + } return false; - } - else { + } else { numberOfUpdates = 0; } return true; @@ -95,27 +94,82 @@ public class ExplorerBee implements Cloneable { /** * Returns a new random position. - * @precondition an algorithm is on the algorithm stack. * @precondition the search is allowed. + * @param currentIteration the current iteration of the algorithm on the stack. * @param position random position with same dimension and bounds as given position. * @return The new position. */ - public Vector getNewPosition(Vector position) { - previousUpdatedIteration = Algorithm.get().getIterations(); + public Vector getNewPosition(int currentIteration, Vector position) { + previousUpdatedIteration = currentIteration; numberOfUpdates++; Vector newPosition = position.getClone(); - newPosition.randomize(new MersenneTwister()); + newPosition.randomize(random); return newPosition; } + /** + * Gets the explorer bee update limit. + * @return the explorer bee update limit. + */ public ControlParameter getExplorerBeeUpdateLimit() { return explorerBeeUpdateLimit; } + /** + * Sets the explorer bee update limit. + * @param explorerBeeUpdateLimit the new explorer bee update limit. + */ public void setExplorerBeeUpdateLimit(ControlParameter explorerBeeUpdateLimit) { this.explorerBeeUpdateLimit = explorerBeeUpdateLimit; } + /** + * Gets the number of updates done for the current iteration. + * @return the number of iterations done for the current iteration. + */ + public int getNumberOfUpdates() { + return numberOfUpdates; + } + + /** + * Sets the number of updates done for the current iteration. + * @param numberOfUpdates the new number of iterations done for the current iteration. + */ + public void setNumberOfUpdates(int numberOfUpdates) { + this.numberOfUpdates = numberOfUpdates; + } + + /** + * Gets the last iteration an update was done. + * @return the last iteration an update was done. + */ + public int getPreviousUpdatedIteration() { + return previousUpdatedIteration; + } + + /** + * Sets the last iteration an update was done. + * @param previousUpdatedIteration the last iteration an update was done. + */ + public void setPreviousUpdatedIteration(int previousUpdatedIteration) { + this.previousUpdatedIteration = previousUpdatedIteration; + } + + /** + * Gets the random number generator to use for generating new positions. + * @return + */ + public Random getRandom() { + return random; + } + + /** + * Sets the random number generator to use for generating new positions. + * @param random the new random number generator to use for generating new positions. + */ + public void setRandom(Random random) { + this.random = random; + } } diff --git a/src/main/java/net/sourceforge/cilib/boa/bee/HoneyBee.java b/src/main/java/net/sourceforge/cilib/boa/bee/HoneyBee.java index 1d35188..413c3bf 100644 --- a/src/main/java/net/sourceforge/cilib/boa/bee/HoneyBee.java +++ b/src/main/java/net/sourceforge/cilib/boa/bee/HoneyBee.java @@ -35,11 +35,13 @@ public interface HoneyBee extends Entity { /** * {@inheritDoc} */ + @Override public HoneyBee getClone(); /** * {@inheritDoc} */ + @Override public Fitness getFitness(); /** diff --git a/src/main/java/net/sourceforge/cilib/boa/bee/OnlookerBee.java b/src/main/java/net/sourceforge/cilib/boa/bee/OnlookerBee.java index a9ff676..15e3d02 100644 --- a/src/main/java/net/sourceforge/cilib/boa/bee/OnlookerBee.java +++ b/src/main/java/net/sourceforge/cilib/boa/bee/OnlookerBee.java @@ -39,6 +39,10 @@ public class OnlookerBee extends AbstractBee implements HoneyBee { public OnlookerBee() { } + /** + * Copy constructor. + * @param bee the original bee to copy. + */ public OnlookerBee(AbstractBee bee) { super(bee); } @@ -65,10 +69,10 @@ public class OnlookerBee extends AbstractBee implements HoneyBee { @Override public void updatePosition() { ABC algorithm = (ABC) Algorithm.get(); - HoneyBee target = targetSelectionStrategy.select(algorithm.getWorkerTopology()); + HoneyBee target = targetSelectionStrategy.select(algorithm.getWorkerBees()); while (target == this) { - target = targetSelectionStrategy.select(algorithm.getWorkerTopology()); + target = targetSelectionStrategy.select(algorithm.getWorkerBees()); } this.positionUpdateStrategy.updatePosition(this, target); diff --git a/src/main/java/net/sourceforge/cilib/boa/bee/WorkerBee.java b/src/main/java/net/sourceforge/cilib/boa/bee/WorkerBee.java index bc34ae3..33339d8 100644 --- a/src/main/java/net/sourceforge/cilib/boa/bee/WorkerBee.java +++ b/src/main/java/net/sourceforge/cilib/boa/bee/WorkerBee.java @@ -31,8 +31,8 @@ import net.sourceforge.cilib.controlparameter.ControlParameter; * @author Andrich */ public class WorkerBee extends AbstractBee implements HoneyBee { - private static final long serialVersionUID = 3657591650621784765L; + private static final long serialVersionUID = 3657591650621784765L; private ControlParameter forageLimit; private int failureCount; @@ -68,20 +68,20 @@ public class WorkerBee extends AbstractBee implements HoneyBee { @Override public void updatePosition() { ABC algorithm = (ABC) Algorithm.get(); - HoneyBee target = targetSelectionStrategy.select(algorithm.getWorkerTopology()); + HoneyBee target = targetSelectionStrategy.select(algorithm.getWorkerBees()); while (target == this) { - target = targetSelectionStrategy.select(algorithm.getWorkerTopology()); + target = targetSelectionStrategy.select(algorithm.getWorkerBees()); } boolean success = this.positionUpdateStrategy.updatePosition(this, target); if (!success) { failureCount++; - if (failureCount >= forageLimit.getParameter()) { + if (failureCount >= forageLimit.getParameter()) { failureCount = 0; ExplorerBee explorerBee = algorithm.getExplorerBee(); - if (explorerBee.searchAllowed()) { - this.setPosition(explorerBee.getNewPosition(this.getPosition())); + if (explorerBee.searchAllowed(algorithm.getIterations())) { + this.setPosition(explorerBee.getNewPosition(algorithm.getIterations(), this.getPosition())); } } } @@ -102,4 +102,20 @@ public class WorkerBee extends AbstractBee implements HoneyBee { public void setForageLimit(ControlParameter forageLimit) { this.forageLimit = forageLimit; } + + /** + * Gets the failure count. + * @return the number of times the bee has failed to find a better position. + */ + public int getFailureCount() { + return failureCount; + } + + /** + * Sets the failure count. + * @param failureCount the new number of times the bee has failed to find a better position. + */ + public void setFailureCount(int failureCount) { + this.failureCount = failureCount; + } } diff --git a/src/main/java/net/sourceforge/cilib/boa/positionupdatestrategies/BeePositionUpdateStrategy.java b/src/main/java/net/sourceforge/cilib/boa/positionupdatestrategies/BeePositionUpdateStrategy.java index 465bc5c..082cf8c 100644 --- a/src/main/java/net/sourceforge/cilib/boa/positionupdatestrategies/BeePositionUpdateStrategy.java +++ b/src/main/java/net/sourceforge/cilib/boa/positionupdatestrategies/BeePositionUpdateStrategy.java @@ -35,8 +35,15 @@ public interface BeePositionUpdateStrategy extends Cloneable { /** * {@inheritDoc} */ + @Override public BeePositionUpdateStrategy getClone(); + /** + * Updates the position of the given bee. + * @param bee the bee the position update is for. + * @param other another bee that the position update might use to update the position. + * @return whether the position update was successful. + */ public boolean updatePosition(HoneyBee bee, HoneyBee other); } diff --git a/src/main/java/net/sourceforge/cilib/boa/positionupdatestrategies/VisualPositionUpdateStategy.java b/src/main/java/net/sourceforge/cilib/boa/positionupdatestrategies/VisualPositionUpdateStategy.java index c1bd803..6461a03 100644 --- a/src/main/java/net/sourceforge/cilib/boa/positionupdatestrategies/VisualPositionUpdateStategy.java +++ b/src/main/java/net/sourceforge/cilib/boa/positionupdatestrategies/VisualPositionUpdateStategy.java @@ -21,8 +21,8 @@ */ package net.sourceforge.cilib.boa.positionupdatestrategies; -import net.sourceforge.cilib.algorithm.Algorithm; import net.sourceforge.cilib.boa.bee.HoneyBee; +import net.sourceforge.cilib.entity.EntityType; import net.sourceforge.cilib.math.random.generator.MersenneTwister; import net.sourceforge.cilib.problem.Fitness; import net.sourceforge.cilib.type.types.Real; @@ -63,10 +63,12 @@ public class VisualPositionUpdateStategy implements BeePositionUpdateStrategy { newPosition.set(j, newValue); //Determine if new position is better than old and update - Fitness oldFitness = Algorithm.get().getOptimisationProblem().getFitness(oldPosition, false); - Fitness newFitness = Algorithm.get().getOptimisationProblem().getFitness(newPosition, false); + Fitness oldFitness = bee.getFitness().getClone(); + bee.calculateFitness(); + Fitness newFitness = bee.getFitness(); if (newFitness.compareTo(oldFitness) < 0) { bee.setPosition(oldPosition); + bee.getProperties().put(EntityType.FITNESS, oldFitness); return false; } diff --git a/src/test/java/net/sourceforge/cilib/boa/ABCTest.java b/src/test/java/net/sourceforge/cilib/boa/ABCTest.java index 31c8014..c3fc238 100644 --- a/src/test/java/net/sourceforge/cilib/boa/ABCTest.java +++ b/src/test/java/net/sourceforge/cilib/boa/ABCTest.java @@ -51,19 +51,19 @@ public class ABCTest { abc.initialise(); assertEquals(abc.getTopology().size(), 100); - assertEquals(abc.getWorkerTopology().size(), 70); - assertEquals(abc.getOnlookerTopology().size(), 30); + assertEquals(abc.getWorkerBees().size(), 70); + assertEquals(abc.getOnlookerBees().size(), 30); HashMap<Type, Type> map = new HashMap<Type, Type>(); for (HoneyBee bee : abc.getTopology()) { map.put(bee.getCandidateSolution(), bee.getCandidateSolution()); } - for (HoneyBee bee : abc.getWorkerTopology()) + for (HoneyBee bee : abc.getWorkerBees()) { map.put(bee.getCandidateSolution(), bee.getCandidateSolution()); } - for (HoneyBee bee : abc.getOnlookerTopology()) + for (HoneyBee bee : abc.getOnlookerBees()) { map.put(bee.getCandidateSolution(), bee.getCandidateSolution()); } diff --git a/src/test/java/net/sourceforge/cilib/boa/bees/ExplorerBeeTest.java b/src/test/java/net/sourceforge/cilib/boa/bees/ExplorerBeeTest.java index 87750cf..b3d916f 100644 --- a/src/test/java/net/sourceforge/cilib/boa/bees/ExplorerBeeTest.java +++ b/src/test/java/net/sourceforge/cilib/boa/bees/ExplorerBeeTest.java @@ -21,13 +21,11 @@ */ package net.sourceforge.cilib.boa.bees; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import java.util.ArrayList; import net.sourceforge.cilib.algorithm.initialisation.ClonedPopulationInitialisationStrategy; import net.sourceforge.cilib.boa.ABC; +import net.sourceforge.cilib.boa.bee.ExplorerBee; import net.sourceforge.cilib.boa.bee.WorkerBee; import net.sourceforge.cilib.controlparameter.ConstantControlParameter; import net.sourceforge.cilib.functions.ContinuousFunction; @@ -35,9 +33,9 @@ import net.sourceforge.cilib.functions.continuous.unconstrained.Rastrigin; import net.sourceforge.cilib.problem.FunctionMinimisationProblem; import net.sourceforge.cilib.stoppingcondition.MaximumIterations; import net.sourceforge.cilib.stoppingcondition.StoppingCondition; -import net.sourceforge.cilib.type.types.Real; import net.sourceforge.cilib.type.types.container.Vector; +import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -66,34 +64,27 @@ public class ExplorerBeeTest { } @Test - public void testGetNewPosition() { + public void testSearchAllowed() { //get up a position with bounds - Vector currentPosition; - - //update position with explorer bee since forage threshold is -1 - ArrayList<Vector> oldPositions = new ArrayList<Vector>(); - for (int k = 0; k < abc.getWorkerTopology().size(); k++) { - oldPositions.add((Vector)abc.getWorkerTopology().get(k).getPosition().getClone()); - } - abc.performIteration(); + Vector oldPosition = abc.getWorkerBees().get(0).getPosition().getClone();; + //update position with explorer bee + ExplorerBee explorerBee = abc.getExplorerBee(); + Assert.assertTrue(explorerBee.searchAllowed(1)); + explorerBee.getNewPosition(1,oldPosition); + //only one update is allowed for the same iteration, this must therefore be false... + Assert.assertTrue(!explorerBee.searchAllowed(1)); + //and this true. + Assert.assertTrue(explorerBee.searchAllowed(2)); + } - boolean explorerUpdateOccured = false; - //assertions - for (int k = 0; k < abc.getWorkerTopology().size(); k++) { - boolean allDimensionsChanged = true; - currentPosition = (Vector)abc.getWorkerTopology().get(k).getCandidateSolution(); - assertEquals(5,currentPosition.size()); - for (int i = 0; i < currentPosition.size(); i++) { - assertTrue(((Real)currentPosition.get(i)).getReal() != Double.NaN); - assertTrue(!Double.isInfinite(((Real)currentPosition.get(i)).getReal())); - allDimensionsChanged = allDimensionsChanged & (Double.compare(((Real)currentPosition.get(i)).getReal(), - ((Real)oldPositions.get(k).get(i)).getReal())!=0); - assertTrue(((Real)currentPosition.get(i)).getReal() <= 5.0); - assertTrue(((Real)currentPosition.get(i)).getReal() >= -5.0); - } - explorerUpdateOccured = explorerUpdateOccured | allDimensionsChanged; - } - assertTrue(explorerUpdateOccured); + @Test + public void testGetNewPosition() { + //get up a position with bounds + Vector oldPosition = abc.getWorkerBees().get(0).getPosition().getClone();; + //update position with explorer bee + ExplorerBee explorerBee = abc.getExplorerBee(); + Vector newPosition = explorerBee.getNewPosition(1,oldPosition); + Assert.assertTrue(!oldPosition.equals(newPosition)); } } diff --git a/src/test/java/net/sourceforge/cilib/boa/positionupdatestrategies/VisualPositionUpdateStategyTest.java b/src/test/java/net/sourceforge/cilib/boa/positionupdatestrategies/VisualPositionUpdateStategyTest.java index a5468f2..5b0b535 100644 --- a/src/test/java/net/sourceforge/cilib/boa/positionupdatestrategies/VisualPositionUpdateStategyTest.java +++ b/src/test/java/net/sourceforge/cilib/boa/positionupdatestrategies/VisualPositionUpdateStategyTest.java @@ -21,7 +21,6 @@ */ package net.sourceforge.cilib.boa.positionupdatestrategies; - import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import net.sourceforge.cilib.algorithm.initialisation.ClonedPopulationInitialisationStrategy; @@ -31,6 +30,7 @@ import net.sourceforge.cilib.boa.bee.WorkerBee; import net.sourceforge.cilib.controlparameter.ConstantControlParameter; import net.sourceforge.cilib.functions.ContinuousFunction; import net.sourceforge.cilib.functions.continuous.unconstrained.Rastrigin; +import net.sourceforge.cilib.problem.Fitness; import net.sourceforge.cilib.problem.FunctionMinimisationProblem; import net.sourceforge.cilib.stoppingcondition.MaximumIterations; import net.sourceforge.cilib.stoppingcondition.StoppingCondition; @@ -41,6 +41,7 @@ import org.junit.Before; import org.junit.Test; public class VisualPositionUpdateStategyTest { + private ABC abc; @Before @@ -59,7 +60,7 @@ public class VisualPositionUpdateStategyTest { initStrategy.setEntityType(bee); abc.setInitialisationStrategy(initStrategy); abc.setWorkerBeePercentage(new ConstantControlParameter(0.5)); - abc.setForageLimit(new ConstantControlParameter(-1)); + abc.setForageLimit(new ConstantControlParameter(Integer.MAX_VALUE)); abc.addStoppingCondition(condition); abc.setOptimisationProblem(problem); abc.initialise(); @@ -67,16 +68,20 @@ public class VisualPositionUpdateStategyTest { @Test public void testUpdatePosition() { - HoneyBee bee = abc.getWorkerTopology().get(0); + HoneyBee bee = abc.getWorkerBees().get(0); abc.performIteration(); - Vector currentPosition = (Vector)bee.getPosition(); - assertEquals(10,currentPosition.size()); + Fitness oldFitness = bee.getFitness().getClone(); + abc.performIteration(); + Vector currentPosition = (Vector) bee.getPosition(); + System.out.println(currentPosition); + assertEquals(10, currentPosition.size()); for (int i = 0; i < currentPosition.size(); i++) { - assertTrue(((Real)currentPosition.get(i)).getReal() != Double.NaN); - assertTrue(!Double.isInfinite(((Real)currentPosition.get(i)).getReal())); - assertTrue(((Real)currentPosition.get(i)).getReal() <= 5.0); - assertTrue(((Real)currentPosition.get(i)).getReal() >= -5.0); + assertTrue(((Real) currentPosition.get(i)).getReal() != Double.NaN); + assertTrue(!Double.isInfinite(((Real) currentPosition.get(i)).getReal())); } + Fitness newFitness = bee.getFitness(); + System.out.println(oldFitness.getValue()); + System.out.println(newFitness.getValue()); + assertTrue(newFitness.compareTo(oldFitness) >= 0); } - } diff --git a/xml/abc.xml b/xml/abc.xml index f06c5c0..f62675f 100644 --- a/xml/abc.xml +++ b/xml/abc.xml @@ -5,84 +5,60 @@ <!ATTLIST measurements id ID #IMPLIED> ]> <simulator> - <algorithms> - <algorithm id="abc" class="boa.ABC"> - <initialisationStrategy class="algorithm.initialisation.ClonedPopulationInitialisationStrategy" entityNumber="40"> - <entityType class="boa.bee.WorkerBee"/> - </initialisationStrategy> - <addStoppingCondition class="stoppingcondition.MaximumIterations" maximumIterations="1000"> - </addStoppingCondition> - <dancingSelectionStrategy class="entity.operators.selection.RouletteWheelSelectionStrategy"/> - <workerBeePercentage class="controlparameter.ConstantControlParameter" parameter="0.5"/> - <forageLimit class="controlparameter.ConstantControlParameter" parameter="500"/> - <explorerBeeUpdateLimit class="controlparameter.ConstantControlParameter" parameter="1"/> - </algorithm> - <algorithm id="pso" class="pso.PSO"> - <addStoppingCondition class="stoppingcondition.MaximumIterations" maximumIterations="1000"> - </addStoppingCondition> - </algorithm> - </algorithms> - <problems> - <problem id="spherical" class="problem.FunctionMinimisationProblem"> - <function class="functions.continuous.unconstrained.Spherical"/> - </problem> - <problem id="rosenbrock" class="problem.FunctionMinimisationProblem"> - <function class="functions.continuous.unconstrained.Rosenbrock"/> - </problem> - <problem id="rastrigin" class="problem.FunctionMinimisationProblem"> - <function class="functions.continuous.unconstrained.Rastrigin"/> - </problem> - <problem id="griewank" class="problem.FunctionMinimisationProblem"> - <function class="functions.continuous.unconstrained.Griewank"/> - </problem> - <problem id="quadric" class="problem.FunctionMinimisationProblem"> - <function class="functions.continuous.Quadric"/> - </problem> - <problem id="michalewicz" class="problem.FunctionMinimisationProblem"> - <function class="functions.continuous.unconstrained.Michalewicz"/> - </problem> - <problem id="hyperellipsoid" class="problem.FunctionMinimisationProblem"> - <function class="functions.continuous.HyperEllipsoid"/> - </problem> - <problem id="easom" class="problem.FunctionMinimisationProblem"> - <function class="functions.continuous.unconstrained.Easom"/> - </problem> - <problem id="colville" class="problem.FunctionMinimisationProblem"> - <function class="functions.continuous.Colville"/> - </problem> - <problem id="bohachevsky1" class="problem.FunctionMinimisationProblem"> - <function class="functions.continuous.unconstrained.Bohachevsky1"/> - </problem> - <problem id="ackley" class="problem.FunctionMinimisationProblem"> - <function class="functions.continuous.unconstrained.Ackley"/> - </problem> - <problem id="schwefel" class="problem.FunctionMinimisationProblem"> - <function class="functions.continuous.Schwefel"/> - </problem> - </problems> - <measurements id="fitness" class="simulator.MeasurementSuite" resolution="10" samples="30"> - <addMeasurement class="measurement.single.Fitness"/> - </measurements> - <simulations> - <simulation> - <algorithm idref="abc"/> - <problem idref="spherical"/> - <measurements idref="fitness" file="data/test/abc_rastrigin.txt"/> - </simulation> - <simulation> - <algorithm idref="abc"/> - <problem idref="griewank"/> - <measurements idref="fitness" file="data/test/abc_griewank.txt"/> - </simulation> - <!--<simulation> - <algorithm idref="abc"/> - <problem idref="rastrigin"/> - <measurements idref="fitness" file="data/test/abc_rastrigin.txt"/> - </simulation> - <simulation> - <algorithm idref="abc"/> - <problem idref="rosenbrock"/> - <measurements idref="fitness" file="data/test/abc_rosenbrock.txt"/> - </simulation>--> - </simulations> + <algorithms> + <algorithm id="abc" class="boa.ABC"> + <initialisationStrategy class="algorithm.initialisation.ClonedPopulationInitialisationStrategy" entityNumber="40"> + <entityType class="boa.bee.WorkerBee"/> + </initialisationStrategy> + <addStoppingCondition class="stoppingcondition.MaximumIterations" maximumIterations="1000"> + </addStoppingCondition> + <dancingSelectionStrategy class="entity.operators.selection.RouletteWheelSelectionStrategy"/> + <workerBeePercentage class="controlparameter.ConstantControlParameter" parameter="0.5"/> + <forageLimit class="controlparameter.ConstantControlParameter" parameter="500"/> + <explorerBeeUpdateLimit class="controlparameter.ConstantControlParameter" parameter="1"/> + </algorithm> + <algorithm id="pso" class="pso.PSO"> + <addStoppingCondition class="stoppingcondition.MaximumIterations" maximumIterations="1000"> + </addStoppingCondition> + </algorithm> + </algorithms> + <problems> + <problem id="spherical" class="problem.FunctionMinimisationProblem"> + <function class="functions.continuous.unconstrained.Spherical"/> + </problem> + <problem id="rosenbrock" class="problem.FunctionMinimisationProblem"> + <function class="functions.continuous.unconstrained.Rosenbrock"/> + </problem> + <problem id="rastrigin" class="problem.FunctionMinimisationProblem"> + <function class="functions.continuous.unconstrained.Rastrigin"/> + </problem> + <problem id="griewank" class="problem.FunctionMinimisationProblem"> + <function class="functions.continuous.unconstrained.Griewank"/> + </problem> + </problems> + <measurements id="fitness" class="simulator.MeasurementSuite" resolution="10" samples="10"> + <addMeasurement class="measurement.single.Fitness"/> + </measurements> + <simulations> + <simulation> + <algorithm idref="abc"/> + <problem idref="spherical"/> + <measurements idref="fitness" file="data/abc_spherical.txt"/> + </simulation> + <simulation> + <algorithm idref="abc"/> + <problem idref="griewank"/> + <measurements idref="fitness" file="data/abc_griewank.txt"/> + </simulation> + <simulation> + <algorithm idref="abc"/> + <problem idref="rastrigin"/> + <measurements idref="fitness" file="data/abc_rastrigin.txt"/> + </simulation> + <simulation> + <algorithm idref="abc"/> + <problem idref="rosenbrock"/> + <measurements idref="fitness" file="data/abc_rosenbrock.txt"/> + </simulation> + </simulations> </simulator> -- 1.6.0.4 |