Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PLANNER-476 CalculateCountTermination
- Loading branch information
Showing
5 changed files
with
210 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
...src/main/java/org/optaplanner/core/impl/solver/termination/CalculateCountTermination.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Copyright 2015 JBoss Inc | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.optaplanner.core.impl.solver.termination; | ||
|
||
import org.optaplanner.core.impl.phase.scope.AbstractPhaseScope; | ||
import org.optaplanner.core.impl.score.director.InnerScoreDirector; | ||
import org.optaplanner.core.impl.solver.scope.DefaultSolverScope; | ||
|
||
public class CalculateCountTermination extends AbstractTermination { | ||
|
||
private final long calculateCountLimit; | ||
|
||
public CalculateCountTermination(long calculateCountLimit) { | ||
this.calculateCountLimit = calculateCountLimit; | ||
if (calculateCountLimit < 0L) { | ||
throw new IllegalArgumentException("The calculateCountLimit (" + calculateCountLimit | ||
+ ") cannot be negative."); | ||
} | ||
} | ||
|
||
// ************************************************************************ | ||
// Terminated methods | ||
// ************************************************************************ | ||
|
||
public boolean isSolverTerminated(DefaultSolverScope solverScope) { | ||
return isTerminated(solverScope.getScoreDirector()); | ||
} | ||
|
||
public boolean isPhaseTerminated(AbstractPhaseScope phaseScope) { | ||
return isTerminated(phaseScope.getScoreDirector()); | ||
} | ||
|
||
protected boolean isTerminated(InnerScoreDirector scoreDirector) { | ||
long calculateCount = scoreDirector.getCalculateCount(); | ||
return calculateCount >= calculateCountLimit; | ||
} | ||
|
||
// ************************************************************************ | ||
// Time gradient methods | ||
// ************************************************************************ | ||
|
||
public double calculateSolverTimeGradient(DefaultSolverScope solverScope) { | ||
return calculateTimeGradient(solverScope.getScoreDirector()); | ||
} | ||
|
||
public double calculatePhaseTimeGradient(AbstractPhaseScope phaseScope) { | ||
return calculateTimeGradient(phaseScope.getScoreDirector()); | ||
} | ||
|
||
protected double calculateTimeGradient(InnerScoreDirector scoreDirector) { | ||
long calculateCount = scoreDirector.getCalculateCount(); | ||
double timeGradient = ((double) calculateCount) / ((double) calculateCountLimit); | ||
return Math.min(timeGradient, 1.0); | ||
} | ||
|
||
} |
83 changes: 83 additions & 0 deletions
83
...test/java/org/optaplanner/core/impl/solver/termination/CalculateCountTerminationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Copyright 2015 JBoss Inc | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.optaplanner.core.impl.solver.termination; | ||
|
||
import org.junit.Test; | ||
import org.optaplanner.core.impl.phase.scope.AbstractPhaseScope; | ||
import org.optaplanner.core.impl.score.director.InnerScoreDirector; | ||
import org.optaplanner.core.impl.solver.scope.DefaultSolverScope; | ||
|
||
import static org.junit.Assert.*; | ||
import static org.mockito.Mockito.*; | ||
|
||
public class CalculateCountTerminationTest { | ||
|
||
@Test | ||
public void solveTermination() { | ||
Termination termination = new CalculateCountTermination(1000L); | ||
DefaultSolverScope solverScope = mock(DefaultSolverScope.class); | ||
InnerScoreDirector scoreDirector = mock(InnerScoreDirector.class); | ||
when(solverScope.getScoreDirector()).thenReturn(scoreDirector); | ||
|
||
when(scoreDirector.getCalculateCount()).thenReturn(0L); | ||
assertEquals(false, termination.isSolverTerminated(solverScope)); | ||
assertEquals(0.0, termination.calculateSolverTimeGradient(solverScope), 0.0); | ||
when(scoreDirector.getCalculateCount()).thenReturn(100L); | ||
assertEquals(false, termination.isSolverTerminated(solverScope)); | ||
assertEquals(0.1, termination.calculateSolverTimeGradient(solverScope), 0.0); | ||
when(scoreDirector.getCalculateCount()).thenReturn(500L); | ||
assertEquals(false, termination.isSolverTerminated(solverScope)); | ||
assertEquals(0.5, termination.calculateSolverTimeGradient(solverScope), 0.0); | ||
when(scoreDirector.getCalculateCount()).thenReturn(700L); | ||
assertEquals(false, termination.isSolverTerminated(solverScope)); | ||
assertEquals(0.7, termination.calculateSolverTimeGradient(solverScope), 0.0); | ||
when(scoreDirector.getCalculateCount()).thenReturn(1000L); | ||
assertEquals(true, termination.isSolverTerminated(solverScope)); | ||
assertEquals(1.0, termination.calculateSolverTimeGradient(solverScope), 0.0); | ||
when(scoreDirector.getCalculateCount()).thenReturn(1200L); | ||
assertEquals(true, termination.isSolverTerminated(solverScope)); | ||
assertEquals(1.0, termination.calculateSolverTimeGradient(solverScope), 0.0); | ||
} | ||
|
||
@Test | ||
public void phaseTermination() { | ||
Termination termination = new CalculateCountTermination(1000L); | ||
AbstractPhaseScope phaseScope = mock(AbstractPhaseScope.class); | ||
InnerScoreDirector scoreDirector = mock(InnerScoreDirector.class); | ||
when(phaseScope.getScoreDirector()).thenReturn(scoreDirector); | ||
|
||
when(scoreDirector.getCalculateCount()).thenReturn(0L); | ||
assertEquals(false, termination.isPhaseTerminated(phaseScope)); | ||
assertEquals(0.0, termination.calculatePhaseTimeGradient(phaseScope), 0.0); | ||
when(scoreDirector.getCalculateCount()).thenReturn(100L); | ||
assertEquals(false, termination.isPhaseTerminated(phaseScope)); | ||
assertEquals(0.1, termination.calculatePhaseTimeGradient(phaseScope), 0.0); | ||
when(scoreDirector.getCalculateCount()).thenReturn(500L); | ||
assertEquals(false, termination.isPhaseTerminated(phaseScope)); | ||
assertEquals(0.5, termination.calculatePhaseTimeGradient(phaseScope), 0.0); | ||
when(scoreDirector.getCalculateCount()).thenReturn(700L); | ||
assertEquals(false, termination.isPhaseTerminated(phaseScope)); | ||
assertEquals(0.7, termination.calculatePhaseTimeGradient(phaseScope), 0.0); | ||
when(scoreDirector.getCalculateCount()).thenReturn(1000L); | ||
assertEquals(true, termination.isPhaseTerminated(phaseScope)); | ||
assertEquals(1.0, termination.calculatePhaseTimeGradient(phaseScope), 0.0); | ||
when(scoreDirector.getCalculateCount()).thenReturn(1200L); | ||
assertEquals(true, termination.isPhaseTerminated(phaseScope)); | ||
assertEquals(1.0, termination.calculatePhaseTimeGradient(phaseScope), 0.0); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters