Skip to content

Commit

Permalink
Final changes for pull request plus some new changes to make possible…
Browse files Browse the repository at this point in the history
… to use LocalSerchType- please check.
  • Loading branch information
Boris Kocian authored and ge0ffrey committed Jul 11, 2019
1 parent 81924ab commit 1300589
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,9 @@ protected Acceptor buildAcceptor(HeuristicConfigPolicy configPolicy) {
case LATE_ACCEPTANCE:
acceptorConfig_.setAcceptorTypeList(Collections.singletonList(AcceptorType.LATE_ACCEPTANCE));
break;
case GREAT_DELUGE:
acceptorConfig_.setAcceptorTypeList(Collections.singletonList(AcceptorType.GREAT_DELUGE));
break;
default:
throw new IllegalStateException("The localSearchType (" + localSearchType_
+ ") is not implemented.");
Expand Down Expand Up @@ -258,6 +261,9 @@ protected LocalSearchForager buildForager(HeuristicConfigPolicy configPolicy) {
// Fast stepping algorithm
foragerConfig_.setAcceptedCountLimit(1);
break;
case GREAT_DELUGE:
// Fast stepping algorithm
foragerConfig_.setAcceptedCountLimit(4);
case VARIABLE_NEIGHBORHOOD_DESCENT:
foragerConfig_.setPickEarlyType(LocalSearchPickEarlyType.FIRST_LAST_STEP_SCORE_IMPROVING);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ public enum LocalSearchType {
TABU_SEARCH,
SIMULATED_ANNEALING,
LATE_ACCEPTANCE,
VARIABLE_NEIGHBORHOOD_DESCENT;
VARIABLE_NEIGHBORHOOD_DESCENT,
GREAT_DELUGE;

/**
* @return {@link #values()} without duplicates (abstract types that end up behaving as one of the other types).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamImplicit;
import org.optaplanner.core.api.score.Score;
import org.optaplanner.core.config.AbstractConfig;
import org.optaplanner.core.config.heuristic.policy.HeuristicConfigPolicy;
import org.optaplanner.core.config.localsearch.decider.acceptor.stepcountinghillclimbing.StepCountingHillClimbingType;
Expand Down Expand Up @@ -76,9 +75,9 @@ public class AcceptorConfig extends AbstractConfig<AcceptorConfig> {
protected Integer stepCountingHillClimbingSize = null;
protected StepCountingHillClimbingType stepCountingHillClimbingType = null;

protected String greatDelugeStartingWaterLevel = null;
protected Double greatDelugeRainSpeedRatio = null;
protected String greatDelugeRainSpeedScore = null;
protected String greatDelugeInitialWaterLevel = null;
protected Double greatDelugeWaterLevelIncrementRatio = null;
protected String greatDelugeWaterLevelIncrementScore = null;

@Deprecated
public List<Class<? extends Acceptor>> getAcceptorClassList() {
Expand Down Expand Up @@ -242,28 +241,28 @@ public void setStepCountingHillClimbingType(StepCountingHillClimbingType stepCou
this.stepCountingHillClimbingType = stepCountingHillClimbingType;
}

public String getGreatDelugeStartingWaterLevel() {
return greatDelugeStartingWaterLevel;
public String getGreatDelugeInitialWaterLevel() {
return greatDelugeInitialWaterLevel;
}

public void setGreatDelugeStartingWaterLevel(String greatDelugeStartingWaterLevel) {
this.greatDelugeStartingWaterLevel = greatDelugeStartingWaterLevel;
public void setGreatDelugeInitialWaterLevel(String greatDelugeInitialWaterLevel) {
this.greatDelugeInitialWaterLevel = greatDelugeInitialWaterLevel;
}

public String getGreatDelugeRainSpeedScore() {
return greatDelugeRainSpeedScore;
public String getGreatDelugeWaterLevelIncrementScore() {
return greatDelugeWaterLevelIncrementScore;
}

public void setGreatDelugeRainSpeedScore(String greatDelugeRainSpeedScore) {
this.greatDelugeRainSpeedScore = greatDelugeRainSpeedScore;
public void setGreatDelugeWaterLevelIncrementScore(String greatDelugeWaterLevelIncrementScore) {
this.greatDelugeWaterLevelIncrementScore = greatDelugeWaterLevelIncrementScore;
}

public Double getGreatDelugeRainSpeedRatio() {
return greatDelugeRainSpeedRatio;
public Double getGreatDelugeWaterLevelIncrementRatio() {
return greatDelugeWaterLevelIncrementRatio;
}

public void setGreatDelugeRainSpeedRatio(Double greatDelugeRainSpeedRatio) {
this.greatDelugeRainSpeedRatio = greatDelugeRainSpeedRatio;
public void setGreatDelugeWaterLevelIncrementRatio(Double greatDelugeWaterLevelIncrementRatio) {
this.greatDelugeWaterLevelIncrementRatio = greatDelugeWaterLevelIncrementRatio;
}

// ************************************************************************
Expand Down Expand Up @@ -526,26 +525,29 @@ public Acceptor buildAcceptor(HeuristicConfigPolicy configPolicy) {
acceptorList.add(acceptor);
}
if ((acceptorTypeList!= null && acceptorTypeList.contains(AcceptorType.GREAT_DELUGE))
|| greatDelugeRainSpeedRatio != null || greatDelugeRainSpeedScore != null) {
|| greatDelugeWaterLevelIncrementRatio != null
|| greatDelugeWaterLevelIncrementScore != null
|| greatDelugeInitialWaterLevel != null) {
GreatDelugeAcceptor acceptor = new GreatDelugeAcceptor();

if (greatDelugeRainSpeedScore != null && greatDelugeRainSpeedRatio != null) {
throw new IllegalArgumentException("The greatDelugeRainSpeedScore (" + greatDelugeRainSpeedScore
+ ") and greatDelugeRainSpeedRatio (" + greatDelugeRainSpeedRatio
+ ") cannot be both non null.");
if (greatDelugeWaterLevelIncrementScore != null && greatDelugeWaterLevelIncrementRatio != null) {
throw new IllegalArgumentException("The greatDelugeWaterLevelIncrementScore ("
+ greatDelugeWaterLevelIncrementScore + ") and greatDelugeWaterLevelIncrementRatio ("
+ greatDelugeWaterLevelIncrementRatio + ") cannot be both non null.");
}

if (greatDelugeRainSpeedRatio != null) {
acceptor.setRainSpeedRatio(greatDelugeRainSpeedRatio);
if (greatDelugeWaterLevelIncrementRatio != null) {
acceptor.setWaterLevelIncrementRatio(greatDelugeWaterLevelIncrementRatio);
}

if (greatDelugeRainSpeedScore != null) {
acceptor.setRainSpeedScore(configPolicy.getScoreDefinition().parseScore(greatDelugeRainSpeedScore));
if (greatDelugeWaterLevelIncrementScore != null) {
acceptor.setWaterLevelIncrementScore(configPolicy.getScoreDefinition()
.parseScore(greatDelugeWaterLevelIncrementScore));
}

if (greatDelugeStartingWaterLevel != null) {
acceptor.setInitialWaterLevels(configPolicy.getScoreDefinition()
.parseScore(greatDelugeStartingWaterLevel));
if (greatDelugeInitialWaterLevel != null) {
acceptor.setInitialWaterLevel(configPolicy.getScoreDefinition()
.parseScore(greatDelugeInitialWaterLevel));
}
acceptorList.add(acceptor);
}
Expand Down Expand Up @@ -609,12 +611,12 @@ public void inherit(AcceptorConfig inheritedConfig) {
inheritedConfig.getStepCountingHillClimbingSize());
stepCountingHillClimbingType = ConfigUtils.inheritOverwritableProperty(stepCountingHillClimbingType,
inheritedConfig.getStepCountingHillClimbingType());
greatDelugeStartingWaterLevel = ConfigUtils.inheritOverwritableProperty(greatDelugeStartingWaterLevel,
inheritedConfig.getGreatDelugeStartingWaterLevel());
greatDelugeRainSpeedRatio = ConfigUtils.inheritOverwritableProperty(greatDelugeRainSpeedRatio,
inheritedConfig.getGreatDelugeRainSpeedRatio());
greatDelugeRainSpeedScore = ConfigUtils.inheritOverwritableProperty(greatDelugeRainSpeedScore,
inheritedConfig.getGreatDelugeRainSpeedScore());
greatDelugeInitialWaterLevel = ConfigUtils.inheritOverwritableProperty(greatDelugeInitialWaterLevel,
inheritedConfig.getGreatDelugeInitialWaterLevel());
greatDelugeWaterLevelIncrementRatio = ConfigUtils.inheritOverwritableProperty(greatDelugeWaterLevelIncrementRatio,
inheritedConfig.getGreatDelugeWaterLevelIncrementRatio());
greatDelugeWaterLevelIncrementScore = ConfigUtils.inheritOverwritableProperty(greatDelugeWaterLevelIncrementScore,
inheritedConfig.getGreatDelugeWaterLevelIncrementScore());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,63 +9,65 @@

public class GreatDelugeAcceptor extends AbstractAcceptor {

private Score initialWaterLevels = null;
private Score initialWaterLevel = null;
private Score currentWaterLevel = null;

private Double rainSpeedRatio = DEFAULT_RAIN_SPEED_RATIO;
private Score rainSpeedScore = null;
private Score waterLevelIncrementScore = null;
private Double waterLevelIncrementRatio = DEFAULT_WATER_LEVEL_INCREMENT_RATIO;

// Good value to come out from. Source: https://github.com/UniTime/cpsolver from Tomas Muller
private static final double DEFAULT_RAIN_SPEED_RATIO = 0.99_999_995;
private static final double DEFAULT_WATER_LEVEL_INCREMENT_RATIO = 0.00_000_005;


public Score getRainSpeedScore() {
return this.rainSpeedScore;
public Score getWaterLevelIncrementScore() {
return this.waterLevelIncrementScore;
}

public void setRainSpeedScore(Score rainSpeedScore) {
this.rainSpeedScore = rainSpeedScore;
this.rainSpeedRatio = null;
public void setWaterLevelIncrementScore(Score waterLevelIncrementScore) {
this.waterLevelIncrementScore = waterLevelIncrementScore;
this.waterLevelIncrementRatio = null;
}

public Score getInitialWaterLevels() {
return this.initialWaterLevels;
public Score getInitialWaterLevel() {
return this.initialWaterLevel;
}

public void setInitialWaterLevels(Score initialLevel) {
this.initialWaterLevels = initialLevel;
public void setInitialWaterLevel(Score initialLevel) {
this.initialWaterLevel = initialLevel;
}

public Double getRainSpeedRatio() {
return this.rainSpeedRatio;
public Double getWaterLevelIncrementRatio() {
return this.waterLevelIncrementRatio;
}

public void setRainSpeedRatio(Double rainSpeedRatio) {
this.rainSpeedRatio = rainSpeedRatio;
public void setWaterLevelIncrementRatio(Double waterLevelIncrementRatio) {
this.waterLevelIncrementRatio = waterLevelIncrementRatio;
}

@Override
public void phaseStarted(LocalSearchPhaseScope phaseScope) {
super.phaseStarted(phaseScope);

if (initialWaterLevels != null) {
for (double initialLevelLevel : ScoreUtils.extractLevelDoubles(initialWaterLevels)) {
if (initialWaterLevel != null) {
for (double initialLevelLevel : ScoreUtils.extractLevelDoubles(initialWaterLevel)) {
if (initialLevelLevel < 0.0) {
throw new IllegalArgumentException("The initial level (" + initialWaterLevels
throw new IllegalArgumentException("The initial level (" + initialWaterLevel
+ ") cannot have negative level (" + initialLevelLevel + ").");
}
}
currentWaterLevel = initialWaterLevels;
currentWaterLevel = initialWaterLevel;

} else {
currentWaterLevel = phaseScope.getBestScore().negate();
}
}

@Override
public void phaseEnded(LocalSearchPhaseScope phaseScope) {
super.phaseEnded(phaseScope);
initialWaterLevels = null;
rainSpeedRatio = DEFAULT_RAIN_SPEED_RATIO;
rainSpeedScore = null;
initialWaterLevel = null;
waterLevelIncrementRatio = DEFAULT_WATER_LEVEL_INCREMENT_RATIO;
waterLevelIncrementScore = null;
}

@Override
Expand All @@ -76,12 +78,16 @@ public boolean isAccepted(LocalSearchMoveScope moveScope) {
return moveScore.compareTo(currentWaterLevel.negate()) >= 0;
}

public void stepStarted(LocalSearchStepScope stepScope) {
@Override
public void stepEnded(LocalSearchStepScope stepScope) {
super.stepEnded(stepScope);
if (rainSpeedScore != null) {
currentWaterLevel = currentWaterLevel.subtract(rainSpeedScore);

if (waterLevelIncrementScore != null) {
currentWaterLevel = currentWaterLevel.subtract(waterLevelIncrementScore);
} else {
currentWaterLevel = currentWaterLevel.multiply(rainSpeedRatio);
Score increment = currentWaterLevel.multiply(waterLevelIncrementRatio);
currentWaterLevel = currentWaterLevel.subtract(increment);
}
logger.debug(currentWaterLevel.toShortString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public class GreatDelugeAcceptorTest extends AbstractAcceptorTest {
public void isAcceptedPositiveLevelSingleScoreRainSpeed() {

GreatDelugeAcceptor acceptor = new GreatDelugeAcceptor();
acceptor.setInitialWaterLevels(SimpleScore.of(1100));
acceptor.setRainSpeedScore(SimpleScore.of(100));
acceptor.setInitialWaterLevel(SimpleScore.of(1000));
acceptor.setWaterLevelIncrementScore(SimpleScore.of(100));

DefaultSolverScope<TestdataSolution> solverScope = new DefaultSolverScope<>();
solverScope.setBestScore(SimpleScore.of(-1000));
Expand Down Expand Up @@ -96,8 +96,8 @@ public void isAcceptedPositiveLevelSingleScoreRainSpeed() {
public void isAcceptedPositiveLevelMultipleScoreRainSpeed() {

GreatDelugeAcceptor acceptor = new GreatDelugeAcceptor();
acceptor.setInitialWaterLevels(HardMediumSoftScore.of(0, 200, 500));
acceptor.setRainSpeedScore(HardMediumSoftScore.of(0,100,100));
acceptor.setInitialWaterLevel(HardMediumSoftScore.of(0, 100, 400));
acceptor.setWaterLevelIncrementScore(HardMediumSoftScore.of(0, 100, 100));

DefaultSolverScope<TestdataSolution> solverScope = new DefaultSolverScope<>();
solverScope.setBestScore(HardMediumSoftScore.of(0, -200, -1000));
Expand Down Expand Up @@ -142,8 +142,8 @@ public void isAcceptedPositiveLevelMultipleScoreRainSpeed() {
public void isAcceptedPositiveLevelMultipleScoreRainSpeedRatio() {

GreatDelugeAcceptor acceptor = new GreatDelugeAcceptor();
acceptor.setInitialWaterLevels(HardMediumSoftScore.of(0, 200, 500));
acceptor.setRainSpeedRatio(0.9);
acceptor.setInitialWaterLevel(HardMediumSoftScore.of(0, 180, 450));
acceptor.setWaterLevelIncrementRatio(0.9);

DefaultSolverScope<TestdataSolution> solverScope = new DefaultSolverScope<>();
solverScope.setBestScore(HardMediumSoftScore.of(0, -200, -1000));
Expand Down Expand Up @@ -188,23 +188,23 @@ public void isAcceptedPositiveLevelMultipleScoreRainSpeedRatio() {
public void negativeWaterLevelSingleScore() {

GreatDelugeAcceptor acceptor = new GreatDelugeAcceptor();
acceptor.setInitialWaterLevels(SimpleScore.of(-100));
acceptor.setInitialWaterLevel(SimpleScore.of(-100));
try {
acceptor.phaseStarted(null);
} catch (IllegalArgumentException e) {
assertEquals("The initial level (" + acceptor.getInitialWaterLevels()
assertEquals("The initial level (" + acceptor.getInitialWaterLevel()
+ ") cannot have negative level (" + "-100.0" + ").", e.getMessage());
}
}
@Test
public void negativeWaterLevelMultipleScore() {

GreatDelugeAcceptor acceptor = new GreatDelugeAcceptor();
acceptor.setInitialWaterLevels(HardMediumSoftScore.parseScore("1hard/-1medium/2soft"));
acceptor.setInitialWaterLevel(HardMediumSoftScore.parseScore("1hard/-1medium/2soft"));
try {
acceptor.phaseStarted(null);
} catch (IllegalArgumentException e) {
assertEquals("The initial level (" + acceptor.getInitialWaterLevels()
assertEquals("The initial level (" + acceptor.getInitialWaterLevel()
+ ") cannot have negative level (" + "-1.0" + ").", e.getMessage());
}
}
Expand Down

0 comments on commit 1300589

Please sign in to comment.