use of org.kie.kogito.explainability.local.counterfactual.CounterfactualResult in project kogito-apps by kiegroup.
the class PrequalificationDmnCounterfactualExplainerTest method testValidCounterfactual.
@Test
void testValidCounterfactual() throws ExecutionException, InterruptedException, TimeoutException {
PredictionProvider model = getModel();
final List<Output> goal = List.of(new Output("Qualified?", Type.BOOLEAN, new Value(true), 0.0d));
final TerminationConfig terminationConfig = new TerminationConfig().withScoreCalculationCountLimit(steps);
final SolverConfig solverConfig = SolverConfigBuilder.builder().withTerminationConfig(terminationConfig).build();
solverConfig.setRandomSeed(randomSeed);
solverConfig.setEnvironmentMode(EnvironmentMode.REPRODUCIBLE);
CounterfactualConfig config = new CounterfactualConfig().withGoalThreshold(0.1);
config.withSolverConfig(solverConfig);
final CounterfactualExplainer explainer = new CounterfactualExplainer(config);
PredictionInput input = getTestInputVariable();
PredictionOutput output = new PredictionOutput(goal);
// test model
List<PredictionOutput> predictionOutputs = model.predictAsync(List.of(getTestInputFixed())).get(Config.INSTANCE.getAsyncTimeout(), Config.INSTANCE.getAsyncTimeUnit());
final Output predictionOutput = predictionOutputs.get(0).getOutputs().get(0);
assertEquals("Qualified?", predictionOutput.getName());
assertFalse((Boolean) predictionOutput.getValue().getUnderlyingObject());
Prediction prediction = new CounterfactualPrediction(input, output, null, UUID.randomUUID(), null);
CounterfactualResult counterfactualResult = explainer.explainAsync(prediction, model).get();
List<Feature> cfFeatures = counterfactualResult.getEntities().stream().map(CounterfactualEntity::asFeature).collect(Collectors.toList());
List<Feature> unflattened = CompositeFeatureUtils.unflattenFeatures(cfFeatures, input.getFeatures());
List<PredictionOutput> outputs = model.predictAsync(List.of(new PredictionInput(unflattened))).get();
assertTrue(counterfactualResult.isValid());
final Output decideOutput = outputs.get(0).getOutputs().get(0);
assertEquals("Qualified?", decideOutput.getName());
assertTrue((Boolean) decideOutput.getValue().getUnderlyingObject());
}
Aggregations