/** * @param kcontext never null, the magic variable in DRL * @param softWeight higher is better, negative for a penalty, positive for a reward */ public void addSoftConstraintMatch(RuleContext kcontext, int softWeight) { softScore += softWeight; registerConstraintMatch(kcontext, () -> softScore -= softWeight, () -> HardSoftScore.of(0, softWeight)); }
/** * @param kcontext never null, the magic variable in DRL * @param hardWeight higher is better, negative for a penalty, positive for a reward */ public void addHardConstraintMatch(RuleContext kcontext, int hardWeight) { hardScore += hardWeight; registerConstraintMatch(kcontext, () -> hardScore -= hardWeight, () -> HardSoftScore.of(hardWeight, 0)); }
@Test public void multiply() { assertEquals(HardSoftScore.of(6, -6), HardSoftScore.of(5, -5).multiply(1.2)); assertEquals(HardSoftScore.of(1, -2), HardSoftScore.of(1, -1).multiply(1.2)); assertEquals(HardSoftScore.of(4, -5), HardSoftScore.of(4, -4).multiply(1.2)); assertEquals(HardSoftScore.ofUninitialized(-14, 8, -10), HardSoftScore.ofUninitialized(-7, 4, -5).multiply(2.0)); }
protected <Solution_> LocalSearchMoveScope<Solution_> buildMoveScope( LocalSearchStepScope<Solution_> stepScope, int hardScore, int softScore) { Move<Solution_> move = mock(Move.class); LocalSearchMoveScope<Solution_> moveScope = new LocalSearchMoveScope<>(stepScope, 0, move); moveScope.setScore(HardSoftScore.of(hardScore, softScore)); moveScope.setAccepted(true); return moveScope; }
@Test(expected = IllegalStateException.class) public void addWithWrongDimension() { HardSoftScore hardSoftScore1 = HardSoftScore.of(-100, -1); HardSoftScore hardSoftScore2 = HardSoftScore.of(-200, -10); ScoreDifferencePercentage scoreDifferencePercentage = ScoreDifferencePercentage.calculateScoreDifferencePercentage(hardSoftScore1, hardSoftScore2); SimpleScore score1 = SimpleScore.of(-100); SimpleScore score2 = SimpleScore.of(-200); ScoreDifferencePercentage scoreDifferencePercentage2 = ScoreDifferencePercentage.calculateScoreDifferencePercentage(score1, score2); scoreDifferencePercentage.add(scoreDifferencePercentage2); }
@Test public void withInitScore() { assertEquals(HardSoftScore.ofUninitialized(-7, -147, -258), HardSoftScore.of(-147, -258).withInitScore(-7)); }
@Test public void buildOptimisticBoundOnlyDown() { HardSoftScoreDefinition scoreDefinition = new HardSoftScoreDefinition(); HardSoftScore optimisticBound = scoreDefinition.buildOptimisticBound( InitializingScoreTrend.buildUniformTrend(InitializingScoreTrendLevel.ONLY_DOWN, 2), HardSoftScore.of(-1, -2)); assertEquals(0, optimisticBound.getInitScore()); assertEquals(-1, optimisticBound.getHardScore()); assertEquals(-2, optimisticBound.getSoftScore()); }
@Test public void toShortString() { assertEquals("0", HardSoftScore.of(0, 0).toShortString()); assertEquals("-258soft", HardSoftScore.of(0, -258).toShortString()); assertEquals("-147hard", HardSoftScore.of(-147, 0).toShortString()); assertEquals("-147hard/-258soft", HardSoftScore.of(-147, -258).toShortString()); assertEquals("-7init", HardSoftScore.ofUninitialized(-7, 0, 0).toShortString()); assertEquals("-7init/-258soft", HardSoftScore.ofUninitialized(-7, 0, -258).toShortString()); assertEquals("-7init/-147hard/-258soft", HardSoftScore.ofUninitialized(-7, -147, -258).toShortString()); }
@Test public void buildPessimisticBoundOnlyUp() { HardSoftScoreDefinition scoreDefinition = new HardSoftScoreDefinition(); HardSoftScore pessimisticBound = scoreDefinition.buildPessimisticBound( InitializingScoreTrend.buildUniformTrend(InitializingScoreTrendLevel.ONLY_UP, 2), HardSoftScore.of(-1, -2)); assertEquals(0, pessimisticBound.getInitScore()); assertEquals(-1, pessimisticBound.getHardScore()); assertEquals(-2, pessimisticBound.getSoftScore()); }
@Test public void parseScore() { assertEquals(HardSoftScore.of(-147, -258), HardSoftScore.parseScore("-147hard/-258soft")); assertEquals(HardSoftScore.ofUninitialized(-7, -147, -258), HardSoftScore.parseScore("-7init/-147hard/-258soft")); assertEquals(HardSoftScore.of(-147, Integer.MIN_VALUE), HardSoftScore.parseScore("-147hard/*soft")); }
@Test public void buildPessimisticBoundOnlyDown() { HardSoftScoreDefinition scoreDefinition = new HardSoftScoreDefinition(); HardSoftScore pessimisticBound = scoreDefinition.buildPessimisticBound( InitializingScoreTrend.buildUniformTrend(InitializingScoreTrendLevel.ONLY_DOWN, 2), HardSoftScore.of(-1, -2)); assertEquals(0, pessimisticBound.getInitScore()); assertEquals(Integer.MIN_VALUE, pessimisticBound.getHardScore()); assertEquals(Integer.MIN_VALUE, pessimisticBound.getSoftScore()); }
@Test public void serializeAndDeserialize() { assertSerializeAndDeserialize(null, new TestHardSoftScoreWrapper(null)); HardSoftScore score = HardSoftScore.of(1200, 34); assertSerializeAndDeserialize(score, new TestHardSoftScoreWrapper(score)); score = HardSoftScore.ofUninitialized(-7, 1200, 34); assertSerializeAndDeserialize(score, new TestHardSoftScoreWrapper(score)); }
@Test public void serializeAndDeserialize() { assertSerializeAndDeserialize(null, new TestHardSoftScoreWrapper(null)); HardSoftScore score = HardSoftScore.of(1200, 34); assertSerializeAndDeserialize(score, new TestHardSoftScoreWrapper(score)); score = HardSoftScore.ofUninitialized(-7, 1200, 34); assertSerializeAndDeserialize(score, new TestHardSoftScoreWrapper(score)); }