use of org.kie.kogito.explainability.model.PredictionOutput 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.kie.kogito.explainability.model.PredictionOutput in project kogito-apps by kiegroup.
the class CounterfactualExplainerTest method testIntermediateUniqueIds.
@ParameterizedTest
@ValueSource(ints = { 0, 1, 2 })
void testIntermediateUniqueIds(int seed) throws ExecutionException, InterruptedException, TimeoutException {
Random random = new Random();
random.setSeed(seed);
final List<Output> goal = new ArrayList<>();
List<Feature> features = List.of(FeatureFactory.newNumericalFeature("f-num1", 10.0, NumericalFeatureDomain.create(0, 20)));
PredictionProvider model = TestUtils.getFeaturePassModel(0);
final TerminationConfig terminationConfig = new TerminationConfig().withScoreCalculationCountLimit(100_000L);
final SolverConfig solverConfig = SolverConfigBuilder.builder().withTerminationConfig(terminationConfig).build();
solverConfig.setRandomSeed((long) seed);
solverConfig.setEnvironmentMode(EnvironmentMode.REPRODUCIBLE);
final List<UUID> intermediateIds = new ArrayList<>();
final List<UUID> executionIds = new ArrayList<>();
final Consumer<CounterfactualResult> captureIntermediateIds = counterfactual -> {
intermediateIds.add(counterfactual.getSolutionId());
};
final Consumer<CounterfactualResult> captureExecutionIds = counterfactual -> {
executionIds.add(counterfactual.getExecutionId());
};
final CounterfactualConfig counterfactualConfig = new CounterfactualConfig().withSolverConfig(solverConfig);
solverConfig.withEasyScoreCalculatorClass(MockCounterFactualScoreCalculator.class);
final CounterfactualExplainer counterfactualExplainer = new CounterfactualExplainer(counterfactualConfig);
PredictionInput input = new PredictionInput(features);
PredictionOutput output = new PredictionOutput(goal);
final UUID executionId = UUID.randomUUID();
Prediction prediction = new CounterfactualPrediction(input, output, null, executionId, null);
final CounterfactualResult counterfactualResult = counterfactualExplainer.explainAsync(prediction, model, captureIntermediateIds.andThen(captureExecutionIds)).get(Config.INSTANCE.getAsyncTimeout(), Config.INSTANCE.getAsyncTimeUnit());
for (CounterfactualEntity entity : counterfactualResult.getEntities()) {
logger.debug("Entity: {}", entity);
}
// all intermediate Ids must be distinct
assertEquals((int) intermediateIds.stream().distinct().count(), intermediateIds.size());
assertEquals(1, (int) executionIds.stream().distinct().count());
assertEquals(executionIds.get(0), executionId);
}
use of org.kie.kogito.explainability.model.PredictionOutput in project kogito-apps by kiegroup.
the class CounterfactualExplainerTest method runCounterfactualSearch.
private CounterfactualResult runCounterfactualSearch(Long randomSeed, List<Output> goal, List<Feature> features, PredictionProvider model, double goalThresold) throws InterruptedException, ExecutionException, TimeoutException {
final TerminationConfig terminationConfig = new TerminationConfig().withScoreCalculationCountLimit(steps);
final SolverConfig solverConfig = SolverConfigBuilder.builder().withTerminationConfig(terminationConfig).build();
solverConfig.setRandomSeed(randomSeed);
solverConfig.setEnvironmentMode(EnvironmentMode.REPRODUCIBLE);
final CounterfactualConfig counterfactualConfig = new CounterfactualConfig();
counterfactualConfig.withSolverConfig(solverConfig).withGoalThreshold(goalThresold);
final CounterfactualExplainer explainer = new CounterfactualExplainer(counterfactualConfig);
final PredictionInput input = new PredictionInput(features);
PredictionOutput output = new PredictionOutput(goal);
Prediction prediction = new CounterfactualPrediction(input, output, null, UUID.randomUUID(), null);
return explainer.explainAsync(prediction, model).get(predictionTimeOut, predictionTimeUnit);
}
use of org.kie.kogito.explainability.model.PredictionOutput in project kogito-apps by kiegroup.
the class CounterfactualScoreCalculatorTest method BooleanDistanceNull.
@Test
void BooleanDistanceNull() {
// Null as a goal
Feature predictionFeature = FeatureFactory.newBooleanFeature("x", true);
Feature goalFeature = FeatureFactory.newBooleanFeature("y", null);
Output predictionOutput = outputFromFeature(predictionFeature);
Output goalOutput = outputFromFeature(goalFeature);
double distance = CounterFactualScoreCalculator.outputDistance(predictionOutput, goalOutput);
assertEquals(Type.BOOLEAN, goalOutput.getType());
assertEquals(1.0, distance);
// Null as a prediction
predictionFeature = FeatureFactory.newBooleanFeature("x", null);
goalFeature = FeatureFactory.newBooleanFeature("y", false);
predictionOutput = outputFromFeature(predictionFeature);
goalOutput = outputFromFeature(goalFeature);
distance = CounterFactualScoreCalculator.outputDistance(predictionOutput, goalOutput);
assertEquals(Type.BOOLEAN, predictionOutput.getType());
assertEquals(1.0, distance);
// Null as both prediction and goal
predictionFeature = FeatureFactory.newBooleanFeature("x", null);
goalFeature = FeatureFactory.newBooleanFeature("y", null);
predictionOutput = outputFromFeature(predictionFeature);
goalOutput = outputFromFeature(goalFeature);
distance = CounterFactualScoreCalculator.outputDistance(predictionOutput, goalOutput);
assertEquals(Type.BOOLEAN, predictionOutput.getType());
assertEquals(0.0, distance);
}
use of org.kie.kogito.explainability.model.PredictionOutput in project kogito-apps by kiegroup.
the class CounterfactualScoreCalculatorTest method IntegerDistanceNull.
@ParameterizedTest
@ValueSource(ints = { 0, 1, 2, 3, 4 })
void IntegerDistanceNull(int seed) {
final Random random = new Random(seed);
final int value = random.nextInt(1000);
// Null as a goal
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> {
Feature predictionFeature = FeatureFactory.newNumericalFeature("x", value);
Feature goalFeature = FeatureFactory.newNumericalFeature("x", null);
Output predictionOutput = outputFromFeature(predictionFeature);
Output goalOutput = outputFromFeature(goalFeature);
CounterFactualScoreCalculator.outputDistance(predictionOutput, goalOutput);
});
assertEquals("Unsupported NaN or NULL for numeric feature 'x'", exception.getMessage());
// Null as a prediction
exception = assertThrows(IllegalArgumentException.class, () -> {
Feature predictionFeature = FeatureFactory.newNumericalFeature("x", null);
Feature goalFeature = FeatureFactory.newNumericalFeature("x", value);
Output predictionOutput = outputFromFeature(predictionFeature);
Output goalOutput = outputFromFeature(goalFeature);
CounterFactualScoreCalculator.outputDistance(predictionOutput, goalOutput);
});
assertEquals("Unsupported NaN or NULL for numeric feature 'x'", exception.getMessage());
// Null as both prediction and goal
exception = assertThrows(IllegalArgumentException.class, () -> {
Feature predictionFeature = FeatureFactory.newNumericalFeature("x", null);
Feature goalFeature = FeatureFactory.newNumericalFeature("x", null);
Output predictionOutput = outputFromFeature(predictionFeature);
Output goalOutput = outputFromFeature(goalFeature);
CounterFactualScoreCalculator.outputDistance(predictionOutput, goalOutput);
});
assertEquals("Unsupported NaN or NULL for numeric feature 'x'", exception.getMessage());
}
Aggregations