Solution nextSolution = null;
@Override
public Solution getNextInput(Problem problem)
{
HCBag bag = (HCBag) Persistence.load(problem.name(), HCBag.class);
if (bag == null)
state = HillClimbing.START;
else
unpack(bag);
if (bag != null && bag.getRemainingEvaluations() == 0)
{
log.info("NO BUDGET: bailing out with the best so far solution");
nextSolution = bag.getBestSoFarSolution();
return nextSolution;
}
switch (state)
{
case START :
log.info("START");
name = problem.name();
remainingEvaluations = problem.getRemainingTrials();
currentSolution = problem.initialSolution();
bestSoFarSolution = currentSolution;
nextSolution = currentSolution;
neighbours = new ArrayList<Solution>();
neighbourPos = 0;
state = HillClimbing.GENERATE_NEIGHBOURS;
break;
case MOVE :
{
log.info("CLIMB");
double bestImprovement = 0;
boolean moved = false;
FitnessComparator comparator = new SimpleFitnessComparator();
for (Solution neighbour : neighbours)
{
double improvement = comparator.compare(problem, neighbour, currentSolution);
if (improvement > bestImprovement)
{
currentSolution = neighbour;
nextSolution = neighbour;
bestImprovement = improvement;
moved = true;
if (comparator.compare(problem, neighbour, bag.getBestSoFarSolution()) > 0)
{
log.info("UPDATING BEST SO FAR. FITNESS:" + neighbour.getFitness());
bestSoFarSolution = neighbour;
}
}
}
if (!moved && bag.getRemainingEvaluations() > 0)
{
log.info("RANDOM RESTART");
currentSolution = problem.generateRandomRestart();
nextSolution = currentSolution;
state = HillClimbing.GENERATE_NEIGHBOURS;