@Override public int compare(Score s1, Score s2) { HardSoftScore score1 = (HardSoftScore) s1; HardSoftScore score2 = (HardSoftScore) s2; long score1Side = (long) score1.getHardScore() * (long) hardWeight + (long) score1.getSoftScore(); long score2Side = (long) score2.getHardScore() * (long) hardWeight + (long) score2.getSoftScore(); return score1Side < score2Side ? -1 : (score1Side == score2Side ? 0 : 1); }
@Override public boolean equals(Object o) { // A direct implementation (instead of EqualsBuilder) to avoid dependencies if (this == o) { return true; } else if (o instanceof HardSoftScore) { HardSoftScore other = (HardSoftScore) o; return initScore == other.getInitScore() && hardScore == other.getHardScore() && softScore == other.getSoftScore(); } else { return false; } }
@Override public HardSoftScore buildOptimisticBound(InitializingScoreTrend initializingScoreTrend, HardSoftScore score) { InitializingScoreTrendLevel[] trendLevels = initializingScoreTrend.getTrendLevels(); return HardSoftScore.ofUninitialized(0, trendLevels[0] == InitializingScoreTrendLevel.ONLY_DOWN ? score.getHardScore() : Integer.MAX_VALUE, trendLevels[1] == InitializingScoreTrendLevel.ONLY_DOWN ? score.getSoftScore() : Integer.MAX_VALUE); }
@Override public HardSoftScore buildPessimisticBound(InitializingScoreTrend initializingScoreTrend, HardSoftScore score) { InitializingScoreTrendLevel[] trendLevels = initializingScoreTrend.getTrendLevels(); return HardSoftScore.ofUninitialized(0, trendLevels[0] == InitializingScoreTrendLevel.ONLY_UP ? score.getHardScore() : Integer.MIN_VALUE, trendLevels[1] == InitializingScoreTrendLevel.ONLY_UP ? score.getSoftScore() : Integer.MIN_VALUE); }
@Override public void configureConstraintWeight(Rule rule, HardSoftScore constraintWeight) { super.configureConstraintWeight(rule, constraintWeight); BiConsumer<RuleContext, Integer> matchExecutor; if (constraintWeight.equals(HardSoftScore.ZERO)) { matchExecutor = (RuleContext kcontext, Integer matchWeight) -> {}; } else if (constraintWeight.getSoftScore() == 0) { matchExecutor = (RuleContext kcontext, Integer matchWeight) -> addHardConstraintMatch(kcontext, constraintWeight.getHardScore() * matchWeight); } else if (constraintWeight.getHardScore() == 0) { matchExecutor = (RuleContext kcontext, Integer matchWeight) -> addSoftConstraintMatch(kcontext, constraintWeight.getSoftScore() * matchWeight); } else { matchExecutor = (RuleContext kcontext, Integer matchWeight) -> addMultiConstraintMatch(kcontext, constraintWeight.getHardScore() * matchWeight, constraintWeight.getSoftScore() * matchWeight); } matchExecutorByNumberMap.put(rule, matchExecutor); matchExecutorByScoreMap.put(rule, (RuleContext kcontext, HardSoftScore weightMultiplier) -> addMultiConstraintMatch(kcontext, constraintWeight.getHardScore() * weightMultiplier.getHardScore(), constraintWeight.getSoftScore() * weightMultiplier.getSoftScore())); }
@Override public HardSoftScore add(HardSoftScore augment) { return new HardSoftScore( initScore + augment.getInitScore(), hardScore + augment.getHardScore(), softScore + augment.getSoftScore()); }
@Override public HardSoftScore subtract(HardSoftScore subtrahend) { return new HardSoftScore( initScore - subtrahend.getInitScore(), hardScore - subtrahend.getHardScore(), softScore - subtrahend.getSoftScore()); }
@Override public int compareTo(HardSoftScore other) { // A direct implementation (instead of CompareToBuilder) to avoid dependencies if (initScore != other.getInitScore()) { return initScore < other.getInitScore() ? -1 : 1; } else if (hardScore != other.getHardScore()) { return hardScore < other.getHardScore() ? -1 : 1; } else { return Integer.compare(softScore, other.getSoftScore()); } }
softConstraintsPenaltyElement.setText(Integer.toString(nurseRoster.getScore().getSoftScore())); solutionElement.addContent(softConstraintsPenaltyElement);
@Test public void serializeAndDeserialize() { PlannerTestUtils.serializeAndDeserializeWithAll( HardSoftScore.of(-12, 3400), output -> { assertEquals(0, output.getInitScore()); assertEquals(-12, output.getHardScore()); assertEquals(3400, output.getSoftScore()); } ); PlannerTestUtils.serializeAndDeserializeWithAll( HardSoftScore.ofUninitialized(-7, -12, 3400), output -> { assertEquals(-7, output.getInitScore()); assertEquals(-12, output.getHardScore()); assertEquals(3400, output.getSoftScore()); } ); }
@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 buildOptimisticBoundOnlyUp() { HardSoftScoreDefinition scoreDefinition = new HardSoftScoreDefinition(); HardSoftScore optimisticBound = scoreDefinition.buildOptimisticBound( InitializingScoreTrend.buildUniformTrend(InitializingScoreTrendLevel.ONLY_UP, 2), HardSoftScore.of(-1, -2)); assertEquals(0, optimisticBound.getInitScore()); assertEquals(Integer.MAX_VALUE, optimisticBound.getHardScore()); assertEquals(Integer.MAX_VALUE, optimisticBound.getSoftScore()); }
@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 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()); }
softConstraintsPenaltyElement.setText(Integer.toString(nurseRoster.getScore().getSoftScore())); solutionElement.addContent(softConstraintsPenaltyElement);
@Test public void testMarshallHardSoftScore() { HardSoftScore score = HardSoftScore.valueOf(10, 20); HardSoftScore result = marshallUnmarshallScore(score); assertNotNull(result); assertEquals(10, result.getHardScore()); assertEquals(20, result.getSoftScore()); }