use of org.kie.kogito.explainability.local.counterfactual.entities.CounterfactualEntity in project kogito-apps by kiegroup.
the class CounterfactualEntityFactoryTest method testCurrencyFactory.
@Test
void testCurrencyFactory() {
final Currency value = Currency.getInstance(Locale.ITALY);
Feature feature = FeatureFactory.newCurrencyFeature("currrency-feature", value);
CounterfactualEntity counterfactualEntity = CounterfactualEntityFactory.from(feature);
assertTrue(counterfactualEntity instanceof FixedCurrencyEntity);
assertEquals(Type.CURRENCY, counterfactualEntity.asFeature().getType());
final Feature fixedFeature = FeatureFactory.newCurrencyFeature("currrency-feature", value);
FeatureDomain domain = CurrencyFeatureDomain.create(Currency.getAvailableCurrencies());
feature = FeatureFactory.newCurrencyFeature("currrency-feature", value, domain);
counterfactualEntity = CounterfactualEntityFactory.from(feature);
assertTrue(counterfactualEntity instanceof CurrencyEntity);
assertEquals(domain.getCategories(), ((CurrencyEntity) counterfactualEntity).getValueRange());
assertEquals(value, counterfactualEntity.asFeature().getValue().getUnderlyingObject());
domain = CurrencyFeatureDomain.create(new ArrayList<>(Currency.getAvailableCurrencies()));
feature = FeatureFactory.newCurrencyFeature("currrency-feature", value, domain);
counterfactualEntity = CounterfactualEntityFactory.from(feature);
assertTrue(counterfactualEntity instanceof CurrencyEntity);
assertEquals(domain.getCategories(), ((CurrencyEntity) counterfactualEntity).getValueRange());
assertEquals(value, counterfactualEntity.asFeature().getValue().getUnderlyingObject());
Currency[] currencies = List.of(Locale.ITALY, Locale.UK, Locale.US).stream().map(Currency::getInstance).collect(Collectors.toList()).toArray(new Currency[0]);
domain = CurrencyFeatureDomain.create(currencies);
feature = FeatureFactory.newCurrencyFeature("currrency-feature", value, domain);
counterfactualEntity = CounterfactualEntityFactory.from(feature);
assertTrue(counterfactualEntity instanceof CurrencyEntity);
assertEquals(currencies.length, ((CurrencyEntity) counterfactualEntity).getValueRange().size());
assertEquals(value, counterfactualEntity.asFeature().getValue().getUnderlyingObject());
}
use of org.kie.kogito.explainability.local.counterfactual.entities.CounterfactualEntity in project kogito-apps by kiegroup.
the class CounterfactualEntityFactoryTest method testObjectFactory.
@Test
void testObjectFactory() {
final URI value = URI.create("./");
Feature feature = FeatureFactory.newObjectFeature("f", value);
CounterfactualEntity counterfactualEntity = CounterfactualEntityFactory.from(feature);
assertTrue(counterfactualEntity instanceof FixedObjectEntity);
assertEquals(Type.UNDEFINED, counterfactualEntity.asFeature().getType());
FeatureDomain domain = ObjectFeatureDomain.create("test", 45L);
feature = FeatureFactory.newObjectFeature("uri-feature", value, domain);
counterfactualEntity = CounterfactualEntityFactory.from(feature);
assertTrue(counterfactualEntity instanceof ObjectEntity);
assertEquals(value, counterfactualEntity.asFeature().getValue().getUnderlyingObject());
domain = ObjectFeatureDomain.create(List.of("test", 45L));
feature = FeatureFactory.newObjectFeature("uri-feature", value, domain);
counterfactualEntity = CounterfactualEntityFactory.from(feature);
assertTrue(counterfactualEntity instanceof ObjectEntity);
assertEquals(value, counterfactualEntity.asFeature().getValue().getUnderlyingObject());
domain = ObjectFeatureDomain.create(Set.of("test", 45L));
feature = FeatureFactory.newObjectFeature("uri-feature", value, domain);
counterfactualEntity = CounterfactualEntityFactory.from(feature);
assertTrue(counterfactualEntity instanceof ObjectEntity);
assertEquals(value, counterfactualEntity.asFeature().getValue().getUnderlyingObject());
}
use of org.kie.kogito.explainability.local.counterfactual.entities.CounterfactualEntity in project kogito-apps by kiegroup.
the class CounterfactualExplainerTest method testConsumers.
@ParameterizedTest
@ValueSource(ints = { 0, 1, 2 })
void testConsumers(int seed) throws ExecutionException, InterruptedException, TimeoutException {
Random random = new Random();
random.setSeed(seed);
final List<Output> goal = List.of(new Output("inside", Type.BOOLEAN, new Value(true), 0.0));
List<Feature> features = new LinkedList<>();
features.add(FeatureFactory.newNumericalFeature("f-num1", 100.0, NumericalFeatureDomain.create(0.0, 1000.0)));
features.add(FeatureFactory.newNumericalFeature("f-num2", 100.0, NumericalFeatureDomain.create(0.0, 1000.0)));
features.add(FeatureFactory.newNumericalFeature("f-num3", 100.0, NumericalFeatureDomain.create(0.0, 1000.0)));
features.add(FeatureFactory.newNumericalFeature("f-num4", 100.0, NumericalFeatureDomain.create(0.0, 1000.0)));
final TerminationConfig terminationConfig = new TerminationConfig().withScoreCalculationCountLimit(10_000L);
// for the purpose of this test, only a few steps are necessary
final SolverConfig solverConfig = SolverConfigBuilder.builder().withTerminationConfig(terminationConfig).build();
solverConfig.setRandomSeed((long) seed);
solverConfig.setEnvironmentMode(EnvironmentMode.REPRODUCIBLE);
@SuppressWarnings("unchecked") final Consumer<CounterfactualResult> assertIntermediateCounterfactualNotNull = mock(Consumer.class);
final CounterfactualConfig counterfactualConfig = new CounterfactualConfig().withSolverConfig(solverConfig).withGoalThreshold(0.01);
final CounterfactualExplainer counterfactualExplainer = new CounterfactualExplainer(counterfactualConfig);
PredictionInput input = new PredictionInput(features);
final double center = 500.0;
final double epsilon = 10.0;
final PredictionProvider model = TestUtils.getSumThresholdModel(center, epsilon);
PredictionOutput output = new PredictionOutput(goal);
Prediction prediction = new CounterfactualPrediction(input, output, null, UUID.randomUUID(), null);
final CounterfactualResult counterfactualResult = counterfactualExplainer.explainAsync(prediction, model, assertIntermediateCounterfactualNotNull).get(Config.INSTANCE.getAsyncTimeout(), Config.INSTANCE.getAsyncTimeUnit());
for (CounterfactualEntity entity : counterfactualResult.getEntities()) {
logger.debug("Entity: {}", entity);
}
logger.debug("Outputs: {}", counterfactualResult.getOutput().get(0).getOutputs());
// At least one intermediate result is generated
verify(assertIntermediateCounterfactualNotNull, atLeast(1)).accept(any());
}
use of org.kie.kogito.explainability.local.counterfactual.entities.CounterfactualEntity in project kogito-apps by kiegroup.
the class CounterfactualExplainerTest method testNonEmptyInput.
@ParameterizedTest
@ValueSource(ints = { 0, 1, 2 })
void testNonEmptyInput(int seed) throws ExecutionException, InterruptedException, TimeoutException {
Random random = new Random();
random.setSeed(seed);
final List<Output> goal = List.of(new Output("class", Type.NUMBER, new Value(10.0), 0.0d));
List<Feature> features = new LinkedList<>();
for (int i = 0; i < 4; i++) {
features.add(FeatureFactory.newNumericalFeature("f-" + i, random.nextDouble(), NumericalFeatureDomain.create(0.0, 1000.0)));
}
final TerminationConfig terminationConfig = new TerminationConfig().withScoreCalculationCountLimit(10L);
// for the purpose of this test, only a few steps are necessary
final SolverConfig solverConfig = SolverConfigBuilder.builder().withTerminationConfig(terminationConfig).build();
solverConfig.setRandomSeed((long) seed);
solverConfig.setEnvironmentMode(EnvironmentMode.REPRODUCIBLE);
final CounterfactualConfig counterfactualConfig = new CounterfactualConfig().withSolverConfig(solverConfig);
final CounterfactualExplainer counterfactualExplainer = new CounterfactualExplainer(counterfactualConfig);
PredictionProvider model = TestUtils.getSumSkipModel(0);
PredictionInput input = new PredictionInput(features);
PredictionOutput output = new PredictionOutput(goal);
Prediction prediction = new CounterfactualPrediction(input, output, null, UUID.randomUUID(), null);
final CounterfactualResult counterfactualResult = counterfactualExplainer.explainAsync(prediction, model).get(Config.INSTANCE.getAsyncTimeout(), Config.INSTANCE.getAsyncTimeUnit());
for (CounterfactualEntity entity : counterfactualResult.getEntities()) {
logger.debug("Entity: {}", entity);
}
logger.debug("Outputs: {}", counterfactualResult.getOutput().get(0).getOutputs());
assertNotNull(counterfactualResult);
assertNotNull(counterfactualResult.getEntities());
}
use of org.kie.kogito.explainability.local.counterfactual.entities.CounterfactualEntity in project kogito-apps by kiegroup.
the class CounterfactualExplainerTest method testCounterfactualCategoricalStrictFail.
/**
* Search for a counterfactual using categorical features with the Symbolic arithmetic model.
* The outcome match is strict (goal threshold of zero).
* The CF should be invalid with this number of iterations.
*
* @param seed
* @throws ExecutionException
* @throws InterruptedException
* @throws TimeoutException
*/
@ParameterizedTest
@ValueSource(ints = { 0, 1, 2 })
void testCounterfactualCategoricalStrictFail(int seed) throws ExecutionException, InterruptedException, TimeoutException {
Random random = new Random();
random.setSeed(seed);
final List<Output> goal = List.of(new Output("result", Type.NUMBER, new Value(25.0), 0.0d));
List<Feature> features = new LinkedList<>();
features.add(FeatureFactory.newNumericalFeature("x-1", 5.0, NumericalFeatureDomain.create(0.0, 100.0)));
features.add(FeatureFactory.newNumericalFeature("x-2", 40.0, NumericalFeatureDomain.create(0.0, 100.0)));
features.add(FeatureFactory.newCategoricalFeature("operand", "*", CategoricalFeatureDomain.create("+", "-", "/", "*")));
final CounterfactualResult result = runCounterfactualSearch((long) seed, goal, features, TestUtils.getSymbolicArithmeticModel(), 0.0);
final List<CounterfactualEntity> counterfactualEntities = result.getEntities();
Stream<Feature> counterfactualFeatures = counterfactualEntities.stream().map(CounterfactualEntity::asFeature);
String operand = counterfactualFeatures.filter(feature -> feature.getName().equals("operand")).findFirst().get().getValue().asString();
List<Feature> numericalFeatures = counterfactualEntities.stream().map(CounterfactualEntity::asFeature).filter(feature -> !feature.getName().equals("operand")).collect(Collectors.toList());
double opResult = 0.0;
for (Feature feature : numericalFeatures) {
switch(operand) {
case "+":
opResult += feature.getValue().asNumber();
break;
case "-":
opResult -= feature.getValue().asNumber();
break;
case "*":
opResult *= feature.getValue().asNumber();
break;
case "/":
opResult /= feature.getValue().asNumber();
break;
}
}
final double epsilon = 0.1;
assertFalse(result.isValid());
assertTrue(opResult <= 25.0 + epsilon);
assertTrue(opResult >= 25.0 - epsilon);
}
Aggregations