use of org.optaplanner.core.api.score.buildin.bendablebigdecimal.BendableBigDecimalScore in project kogito-apps by kiegroup.
the class CounterfactualExplainerTest method mockExplainerInvocation.
@SuppressWarnings("unchecked")
CounterfactualResult mockExplainerInvocation(Consumer<CounterfactualResult> intermediateResultsConsumer, Long maxRunningTimeSeconds) throws ExecutionException, InterruptedException, TimeoutException {
// Mock SolverManager and SolverJob to guarantee deterministic test behaviour
SolverJob<CounterfactualSolution, UUID> solverJob = mock(SolverJob.class);
CounterfactualSolution solution = mock(CounterfactualSolution.class);
BendableBigDecimalScore score = BendableBigDecimalScore.zero(0, 0);
when(solverManager.solveAndListen(any(), any(), any(), any())).thenReturn(solverJob);
when(solverJob.getFinalBestSolution()).thenReturn(solution);
when(solution.getScore()).thenReturn(score);
when(solverManagerFactory.apply(any())).thenReturn(solverManager);
// Setup Explainer
final CounterfactualConfig counterfactualConfig = new CounterfactualConfig().withSolverManagerFactory(solverManagerFactory);
final CounterfactualExplainer counterfactualExplainer = new CounterfactualExplainer(counterfactualConfig);
// Setup mock model, what it does is not important
Prediction prediction = new CounterfactualPrediction(new PredictionInput(Collections.emptyList()), new PredictionOutput(Collections.emptyList()), null, UUID.randomUUID(), maxRunningTimeSeconds);
return counterfactualExplainer.explainAsync(prediction, (List<PredictionInput> inputs) -> CompletableFuture.completedFuture(Collections.emptyList()), intermediateResultsConsumer).get(Config.INSTANCE.getAsyncTimeout(), Config.INSTANCE.getAsyncTimeUnit());
}
use of org.optaplanner.core.api.score.buildin.bendablebigdecimal.BendableBigDecimalScore in project kogito-apps by kiegroup.
the class CounterfactualExplainerTest method testSequenceIds.
@ParameterizedTest
@ValueSource(ints = { 1, 2, 3, 5, 8 })
@SuppressWarnings("unchecked")
void testSequenceIds(int numberOfIntermediateSolutions) throws ExecutionException, InterruptedException, TimeoutException {
final List<Long> sequenceIds = new ArrayList<>();
final Consumer<CounterfactualResult> captureSequenceIds = counterfactual -> {
sequenceIds.add(counterfactual.getSequenceId());
};
ArgumentCaptor<Consumer<CounterfactualSolution>> intermediateSolutionConsumerCaptor = ArgumentCaptor.forClass(Consumer.class);
CounterfactualResult result = mockExplainerInvocation(captureSequenceIds, null);
verify(solverManager).solveAndListen(any(), any(), intermediateSolutionConsumerCaptor.capture(), any());
Consumer<CounterfactualSolution> intermediateSolutionConsumer = intermediateSolutionConsumerCaptor.getValue();
// Mock the intermediate Solution callback being invoked
IntStream.range(0, numberOfIntermediateSolutions).forEach(i -> {
CounterfactualSolution intermediate = mock(CounterfactualSolution.class);
BendableBigDecimalScore intermediateScore = BendableBigDecimalScore.zero(0, 0);
when(intermediate.getScore()).thenReturn(intermediateScore);
intermediateSolutionConsumer.accept(intermediate);
});
// The final and intermediate Solutions should all have unique Sequence Ids.
sequenceIds.add(result.getSequenceId());
assertEquals(numberOfIntermediateSolutions + 1, sequenceIds.size());
assertEquals(numberOfIntermediateSolutions + 1, (int) sequenceIds.stream().distinct().count());
}
use of org.optaplanner.core.api.score.buildin.bendablebigdecimal.BendableBigDecimalScore in project kogito-apps by kiegroup.
the class CounterFactualScoreCalculator method calculateScore.
/**
* Calculates the counterfactual score for each proposed solution.
* This method assumes that each model used as {@link org.kie.kogito.explainability.model.PredictionProvider} is
* consistent, in the sense that for repeated operations, the size of the returned collection of
* {@link PredictionOutput} is the same, if the size of {@link PredictionInput} doesn't change.
*
* @param solution Proposed solution
* @return A {@link BendableBigDecimalScore} with three "hard" levels and one "soft" level
*/
@Override
public BendableBigDecimalScore calculateScore(CounterfactualSolution solution) {
BendableBigDecimalScore currentScore = calculateInputScore(solution);
final List<Feature> flattenedFeatures = solution.getEntities().stream().map(CounterfactualEntity::asFeature).collect(Collectors.toList());
final List<Feature> input = CompositeFeatureUtils.unflattenFeatures(flattenedFeatures, solution.getOriginalFeatures());
final List<PredictionInput> inputs = List.of(new PredictionInput(input));
final CompletableFuture<List<PredictionOutput>> predictionAsync = solution.getModel().predictAsync(inputs);
try {
List<PredictionOutput> predictions = predictionAsync.get(Config.INSTANCE.getAsyncTimeout(), Config.INSTANCE.getAsyncTimeUnit());
solution.setPredictionOutputs(predictions);
final BendableBigDecimalScore outputScore = calculateOutputScore(solution);
currentScore = currentScore.add(outputScore);
} catch (ExecutionException e) {
logger.error("Prediction returned an error {}", e.getMessage());
} catch (InterruptedException e) {
logger.error("Interrupted while waiting for prediction {}", e.getMessage());
Thread.currentThread().interrupt();
} catch (TimeoutException e) {
logger.error("Timed out while waiting for prediction");
}
return currentScore;
}
use of org.optaplanner.core.api.score.buildin.bendablebigdecimal.BendableBigDecimalScore in project kogito-apps by kiegroup.
the class CounterfactualScoreCalculatorTest method testGoalSizeMatch.
/**
* If the goal and the model's output is the same, the distances should all be zero.
*/
@Test
void testGoalSizeMatch() throws ExecutionException, InterruptedException {
final CounterFactualScoreCalculator scoreCalculator = new CounterFactualScoreCalculator();
PredictionProvider model = TestUtils.getFeatureSkipModel(0);
List<Feature> features = new ArrayList<>();
List<FeatureDomain> featureDomains = new ArrayList<>();
List<Boolean> constraints = new ArrayList<>();
// f-1
features.add(FeatureFactory.newNumericalFeature("f-1", 1.0));
featureDomains.add(NumericalFeatureDomain.create(0.0, 10.0));
constraints.add(false);
// f-2
features.add(FeatureFactory.newNumericalFeature("f-2", 2.0));
featureDomains.add(NumericalFeatureDomain.create(0.0, 10.0));
constraints.add(false);
// f-3
features.add(FeatureFactory.newBooleanFeature("f-3", true));
featureDomains.add(EmptyFeatureDomain.create());
constraints.add(false);
PredictionInput input = new PredictionInput(features);
PredictionFeatureDomain domains = new PredictionFeatureDomain(featureDomains);
List<CounterfactualEntity> entities = CounterfactualEntityFactory.createEntities(input);
List<Output> goal = new ArrayList<>();
goal.add(new Output("f-2", Type.NUMBER, new Value(2.0), 0.0));
goal.add(new Output("f-3", Type.BOOLEAN, new Value(true), 0.0));
final CounterfactualSolution solution = new CounterfactualSolution(entities, features, model, goal, UUID.randomUUID(), UUID.randomUUID(), 0.0);
BendableBigDecimalScore score = scoreCalculator.calculateScore(solution);
List<PredictionOutput> predictionOutputs = model.predictAsync(List.of(input)).get();
assertTrue(score.isFeasible());
assertEquals(2, goal.size());
// A single prediction is expected
assertEquals(1, predictionOutputs.size());
// Single prediction with two features
assertEquals(2, predictionOutputs.get(0).getOutputs().size());
assertEquals(0, score.getHardScore(0).compareTo(BigDecimal.ZERO));
assertEquals(0, score.getHardScore(1).compareTo(BigDecimal.ZERO));
assertEquals(0, score.getHardScore(2).compareTo(BigDecimal.ZERO));
assertEquals(0, score.getSoftScore(0).compareTo(BigDecimal.ZERO));
assertEquals(0, score.getSoftScore(1).compareTo(BigDecimal.ZERO));
assertEquals(3, score.getHardLevelsSize());
assertEquals(2, score.getSoftLevelsSize());
}
use of org.optaplanner.core.api.score.buildin.bendablebigdecimal.BendableBigDecimalScore in project kogito-apps by kiegroup.
the class CounterfactualScoreCalculatorTest method testNullBooleanInput.
/**
* Null values for input Boolean features should be accepted as valid
*/
@Test
void testNullBooleanInput() throws ExecutionException, InterruptedException {
final CounterFactualScoreCalculator scoreCalculator = new CounterFactualScoreCalculator();
PredictionProvider model = TestUtils.getFeatureSkipModel(0);
List<Feature> features = new ArrayList<>();
List<FeatureDomain> featureDomains = new ArrayList<>();
List<Boolean> constraints = new ArrayList<>();
// f-1
features.add(FeatureFactory.newNumericalFeature("f-1", 1.0));
featureDomains.add(NumericalFeatureDomain.create(0.0, 10.0));
constraints.add(false);
// f-2
features.add(FeatureFactory.newBooleanFeature("f-2", null));
featureDomains.add(EmptyFeatureDomain.create());
constraints.add(false);
// f-3
features.add(FeatureFactory.newBooleanFeature("f-3", true));
featureDomains.add(EmptyFeatureDomain.create());
constraints.add(false);
PredictionInput input = new PredictionInput(features);
PredictionFeatureDomain domains = new PredictionFeatureDomain(featureDomains);
List<CounterfactualEntity> entities = CounterfactualEntityFactory.createEntities(input);
List<Output> goal = new ArrayList<>();
goal.add(new Output("f-2", Type.BOOLEAN, new Value(null), 0.0));
goal.add(new Output("f-3", Type.BOOLEAN, new Value(true), 0.0));
final CounterfactualSolution solution = new CounterfactualSolution(entities, features, model, goal, UUID.randomUUID(), UUID.randomUUID(), 0.0);
BendableBigDecimalScore score = scoreCalculator.calculateScore(solution);
List<PredictionOutput> predictionOutputs = model.predictAsync(List.of(input)).get();
assertTrue(score.isFeasible());
assertEquals(2, goal.size());
// A single prediction is expected
assertEquals(1, predictionOutputs.size());
// Single prediction with two features
assertEquals(2, predictionOutputs.get(0).getOutputs().size());
assertEquals(0, score.getHardScore(0).compareTo(BigDecimal.ZERO));
assertEquals(0, score.getHardScore(1).compareTo(BigDecimal.ZERO));
assertEquals(0, score.getHardScore(2).compareTo(BigDecimal.ZERO));
assertEquals(0, score.getSoftScore(0).compareTo(BigDecimal.ZERO));
assertEquals(0, score.getSoftScore(1).compareTo(BigDecimal.ZERO));
assertEquals(3, score.getHardLevelsSize());
assertEquals(2, score.getSoftLevelsSize());
}
Aggregations