Search in sources :

Example 6 with CounterfactualEntity

use of org.kie.kogito.explainability.local.counterfactual.entities.CounterfactualEntity in project kogito-apps by kiegroup.

the class CounterfactualEntityFactoryTest method testBinaryFactory.

@Test
void testBinaryFactory() {
    final ByteBuffer value = ByteBuffer.allocate(256);
    Feature feature = FeatureFactory.newBinaryFeature("binary-feature", value);
    CounterfactualEntity counterfactualEntity = CounterfactualEntityFactory.from(feature);
    assertTrue(counterfactualEntity instanceof FixedBinaryEntity);
    assertEquals(Type.BINARY, counterfactualEntity.asFeature().getType());
    final List<ByteBuffer> categories = Stream.of("bar".getBytes(), "baz".getBytes(), "fun".getBytes()).map(ByteBuffer::wrap).collect(Collectors.toList());
    FeatureDomain domain = BinaryFeatureDomain.create(categories);
    feature = FeatureFactory.newBinaryFeature("binary-feature", value, domain);
    counterfactualEntity = CounterfactualEntityFactory.from(feature);
    assertTrue(counterfactualEntity instanceof BinaryEntity);
    assertEquals(domain.getCategories(), ((BinaryEntity) counterfactualEntity).getValueRange());
    domain = BinaryFeatureDomain.create(new HashSet<>(categories));
    feature = FeatureFactory.newBinaryFeature("binary-feature", value, domain);
    counterfactualEntity = CounterfactualEntityFactory.from(feature);
    assertEquals(domain.getCategories(), ((BinaryEntity) counterfactualEntity).getValueRange());
    domain = BinaryFeatureDomain.create(ByteBuffer.wrap("bar".getBytes()), ByteBuffer.wrap("baz".getBytes()), ByteBuffer.wrap("fun".getBytes()));
    feature = FeatureFactory.newBinaryFeature("binary-feature", value, domain);
    counterfactualEntity = CounterfactualEntityFactory.from(feature);
    assertEquals(domain.getCategories(), ((BinaryEntity) counterfactualEntity).getValueRange());
    assertEquals(value, counterfactualEntity.asFeature().getValue().getUnderlyingObject());
}
Also used : CounterfactualEntity(org.kie.kogito.explainability.local.counterfactual.entities.CounterfactualEntity) BinaryEntity(org.kie.kogito.explainability.local.counterfactual.entities.BinaryEntity) FixedBinaryEntity(org.kie.kogito.explainability.local.counterfactual.entities.fixed.FixedBinaryEntity) FixedBinaryEntity(org.kie.kogito.explainability.local.counterfactual.entities.fixed.FixedBinaryEntity) ObjectFeatureDomain(org.kie.kogito.explainability.model.domain.ObjectFeatureDomain) EmptyFeatureDomain(org.kie.kogito.explainability.model.domain.EmptyFeatureDomain) CategoricalFeatureDomain(org.kie.kogito.explainability.model.domain.CategoricalFeatureDomain) CurrencyFeatureDomain(org.kie.kogito.explainability.model.domain.CurrencyFeatureDomain) URIFeatureDomain(org.kie.kogito.explainability.model.domain.URIFeatureDomain) DurationFeatureDomain(org.kie.kogito.explainability.model.domain.DurationFeatureDomain) TimeFeatureDomain(org.kie.kogito.explainability.model.domain.TimeFeatureDomain) NumericalFeatureDomain(org.kie.kogito.explainability.model.domain.NumericalFeatureDomain) BinaryFeatureDomain(org.kie.kogito.explainability.model.domain.BinaryFeatureDomain) FeatureDomain(org.kie.kogito.explainability.model.domain.FeatureDomain) ByteBuffer(java.nio.ByteBuffer) Feature(org.kie.kogito.explainability.model.Feature) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 7 with CounterfactualEntity

use of org.kie.kogito.explainability.local.counterfactual.entities.CounterfactualEntity in project kogito-apps by kiegroup.

the class CounterfactualEntityFactoryTest method testTimeFactory.

@Test
void testTimeFactory() {
    final LocalTime value = LocalTime.now();
    Feature feature = FeatureFactory.newTimeFeature("time-feature", value);
    CounterfactualEntity counterfactualEntity = CounterfactualEntityFactory.from(feature);
    assertTrue(counterfactualEntity instanceof FixedTimeEntity);
    assertEquals(Type.TIME, counterfactualEntity.asFeature().getType());
    FeatureDomain domain = TimeFeatureDomain.create(value.minusHours(10), value.plusHours(10));
    feature = FeatureFactory.newTimeFeature("time-feature", value, domain);
    counterfactualEntity = CounterfactualEntityFactory.from(feature);
    assertTrue(counterfactualEntity instanceof TimeEntity);
    assertEquals(Type.TIME, counterfactualEntity.asFeature().getType());
    assertEquals(value, ((TimeEntity) counterfactualEntity).getProposedValue());
}
Also used : CounterfactualEntity(org.kie.kogito.explainability.local.counterfactual.entities.CounterfactualEntity) FixedTimeEntity(org.kie.kogito.explainability.local.counterfactual.entities.fixed.FixedTimeEntity) TimeEntity(org.kie.kogito.explainability.local.counterfactual.entities.TimeEntity) LocalTime(java.time.LocalTime) FixedTimeEntity(org.kie.kogito.explainability.local.counterfactual.entities.fixed.FixedTimeEntity) ObjectFeatureDomain(org.kie.kogito.explainability.model.domain.ObjectFeatureDomain) EmptyFeatureDomain(org.kie.kogito.explainability.model.domain.EmptyFeatureDomain) CategoricalFeatureDomain(org.kie.kogito.explainability.model.domain.CategoricalFeatureDomain) CurrencyFeatureDomain(org.kie.kogito.explainability.model.domain.CurrencyFeatureDomain) URIFeatureDomain(org.kie.kogito.explainability.model.domain.URIFeatureDomain) DurationFeatureDomain(org.kie.kogito.explainability.model.domain.DurationFeatureDomain) TimeFeatureDomain(org.kie.kogito.explainability.model.domain.TimeFeatureDomain) NumericalFeatureDomain(org.kie.kogito.explainability.model.domain.NumericalFeatureDomain) BinaryFeatureDomain(org.kie.kogito.explainability.model.domain.BinaryFeatureDomain) FeatureDomain(org.kie.kogito.explainability.model.domain.FeatureDomain) Feature(org.kie.kogito.explainability.model.Feature) Test(org.junit.jupiter.api.Test)

Example 8 with CounterfactualEntity

use of org.kie.kogito.explainability.local.counterfactual.entities.CounterfactualEntity in project kogito-apps by kiegroup.

the class CounterfactualEntityFactoryTest method testDurationFactory.

@Test
void testDurationFactory() {
    final Duration value = Duration.ofDays(1);
    Feature feature = FeatureFactory.newDurationFeature("duration-feature", value);
    CounterfactualEntity counterfactualEntity = CounterfactualEntityFactory.from(feature);
    assertTrue(counterfactualEntity instanceof FixedDurationEntity);
    assertEquals(Type.DURATION, counterfactualEntity.asFeature().getType());
    FeatureDomain domain = DurationFeatureDomain.create(0, 60, ChronoUnit.SECONDS);
    feature = FeatureFactory.newDurationFeature("duration-feature", value, domain);
    counterfactualEntity = CounterfactualEntityFactory.from(feature);
    assertTrue(counterfactualEntity instanceof DurationEntity);
    assertEquals(Type.DURATION, counterfactualEntity.asFeature().getType());
    assertFalse(counterfactualEntity.isConstrained());
    CounterfactualEntity entity = DurationEntity.from(feature, Duration.ZERO, Duration.ofDays(2));
    assertEquals(0, entity.distance());
    assertTrue(((DurationEntity) entity).getValueRange().contains(1e5));
    assertFalse(((DurationEntity) entity).getValueRange().contains(2e5));
    assertFalse(entity.isConstrained());
    entity = DurationEntity.from(feature, Duration.ZERO, Duration.ofDays(2), false);
    assertEquals(0, entity.distance());
    assertFalse(entity.isConstrained());
    FeatureDistribution distribution = new NumericFeatureDistribution(feature, new Random().doubles(10).toArray());
    entity = DurationEntity.from(feature, Duration.ZERO, Duration.ofDays(2), distribution);
    assertEquals(0, entity.distance());
    assertFalse(entity.isConstrained());
}
Also used : CounterfactualEntity(org.kie.kogito.explainability.local.counterfactual.entities.CounterfactualEntity) FeatureDistribution(org.kie.kogito.explainability.model.FeatureDistribution) NumericFeatureDistribution(org.kie.kogito.explainability.model.NumericFeatureDistribution) FixedDurationEntity(org.kie.kogito.explainability.local.counterfactual.entities.fixed.FixedDurationEntity) Random(java.util.Random) ObjectFeatureDomain(org.kie.kogito.explainability.model.domain.ObjectFeatureDomain) EmptyFeatureDomain(org.kie.kogito.explainability.model.domain.EmptyFeatureDomain) CategoricalFeatureDomain(org.kie.kogito.explainability.model.domain.CategoricalFeatureDomain) CurrencyFeatureDomain(org.kie.kogito.explainability.model.domain.CurrencyFeatureDomain) URIFeatureDomain(org.kie.kogito.explainability.model.domain.URIFeatureDomain) DurationFeatureDomain(org.kie.kogito.explainability.model.domain.DurationFeatureDomain) TimeFeatureDomain(org.kie.kogito.explainability.model.domain.TimeFeatureDomain) NumericalFeatureDomain(org.kie.kogito.explainability.model.domain.NumericalFeatureDomain) BinaryFeatureDomain(org.kie.kogito.explainability.model.domain.BinaryFeatureDomain) FeatureDomain(org.kie.kogito.explainability.model.domain.FeatureDomain) Duration(java.time.Duration) Feature(org.kie.kogito.explainability.model.Feature) FixedDurationEntity(org.kie.kogito.explainability.local.counterfactual.entities.fixed.FixedDurationEntity) DurationEntity(org.kie.kogito.explainability.local.counterfactual.entities.DurationEntity) NumericFeatureDistribution(org.kie.kogito.explainability.model.NumericFeatureDistribution) Test(org.junit.jupiter.api.Test)

Example 9 with CounterfactualEntity

use of org.kie.kogito.explainability.local.counterfactual.entities.CounterfactualEntity in project kogito-apps by kiegroup.

the class CounterfactualEntityFactoryTest method testDoubleFactory.

@Test
void testDoubleFactory() {
    final double value = 5.5;
    final FeatureDomain domain = NumericalFeatureDomain.create(0.0, 10.0);
    final Feature feature = FeatureFactory.newNumericalFeature("double-feature", value, domain);
    final CounterfactualEntity counterfactualEntity = CounterfactualEntityFactory.from(feature);
    assertTrue(counterfactualEntity instanceof DoubleEntity);
    assertEquals(value, counterfactualEntity.asFeature().getValue().asNumber());
}
Also used : CounterfactualEntity(org.kie.kogito.explainability.local.counterfactual.entities.CounterfactualEntity) FixedDoubleEntity(org.kie.kogito.explainability.local.counterfactual.entities.fixed.FixedDoubleEntity) DoubleEntity(org.kie.kogito.explainability.local.counterfactual.entities.DoubleEntity) ObjectFeatureDomain(org.kie.kogito.explainability.model.domain.ObjectFeatureDomain) EmptyFeatureDomain(org.kie.kogito.explainability.model.domain.EmptyFeatureDomain) CategoricalFeatureDomain(org.kie.kogito.explainability.model.domain.CategoricalFeatureDomain) CurrencyFeatureDomain(org.kie.kogito.explainability.model.domain.CurrencyFeatureDomain) URIFeatureDomain(org.kie.kogito.explainability.model.domain.URIFeatureDomain) DurationFeatureDomain(org.kie.kogito.explainability.model.domain.DurationFeatureDomain) TimeFeatureDomain(org.kie.kogito.explainability.model.domain.TimeFeatureDomain) NumericalFeatureDomain(org.kie.kogito.explainability.model.domain.NumericalFeatureDomain) BinaryFeatureDomain(org.kie.kogito.explainability.model.domain.BinaryFeatureDomain) FeatureDomain(org.kie.kogito.explainability.model.domain.FeatureDomain) Feature(org.kie.kogito.explainability.model.Feature) Test(org.junit.jupiter.api.Test)

Example 10 with CounterfactualEntity

use of org.kie.kogito.explainability.local.counterfactual.entities.CounterfactualEntity in project kogito-apps by kiegroup.

the class CounterfactualExplainerTest method testFinalUniqueIds.

@ParameterizedTest
@ValueSource(ints = { 0, 1, 2 })
void testFinalUniqueIds(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 should be unique
    assertEquals((int) intermediateIds.stream().distinct().count(), intermediateIds.size());
    // There should be at least one intermediate id
    assertTrue(intermediateIds.size() > 0);
    // There should be at least one execution id
    assertTrue(executionIds.size() > 0);
    // We should have the same number of execution ids as intermediate ids (captured from intermediate results)
    assertEquals(executionIds.size(), intermediateIds.size());
    // All execution ids should be the same
    assertEquals(1, (int) executionIds.stream().distinct().count());
    // The last intermediate id must be different from the final result id
    assertNotEquals(intermediateIds.get(intermediateIds.size() - 1), counterfactualResult.getSolutionId());
    // Captured execution ids should be the same as the one provided
    assertEquals(executionIds.get(0), executionId);
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) FeatureFactory(org.kie.kogito.explainability.model.FeatureFactory) Feature(org.kie.kogito.explainability.model.Feature) LoggerFactory(org.slf4j.LoggerFactory) Assertions.assertNotEquals(org.junit.jupiter.api.Assertions.assertNotEquals) TimeoutException(java.util.concurrent.TimeoutException) Random(java.util.Random) CounterfactualEntity(org.kie.kogito.explainability.local.counterfactual.entities.CounterfactualEntity) Value(org.kie.kogito.explainability.model.Value) TerminationConfig(org.optaplanner.core.config.solver.termination.TerminationConfig) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) FeatureDistribution(org.kie.kogito.explainability.model.FeatureDistribution) EmptyFeatureDomain(org.kie.kogito.explainability.model.domain.EmptyFeatureDomain) Mockito.atLeast(org.mockito.Mockito.atLeast) PredictionOutput(org.kie.kogito.explainability.model.PredictionOutput) CategoricalFeatureDomain(org.kie.kogito.explainability.model.domain.CategoricalFeatureDomain) DataUtils(org.kie.kogito.explainability.utils.DataUtils) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) PredictionInput(org.kie.kogito.explainability.model.PredictionInput) List(java.util.List) Stream(java.util.stream.Stream) NormalDistribution(org.apache.commons.math3.distribution.NormalDistribution) Output(org.kie.kogito.explainability.model.Output) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) SolverJob(org.optaplanner.core.api.solver.SolverJob) Mockito.mock(org.mockito.Mockito.mock) IntStream(java.util.stream.IntStream) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) SolverConfig(org.optaplanner.core.config.solver.SolverConfig) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) PerturbationContext(org.kie.kogito.explainability.model.PerturbationContext) Prediction(org.kie.kogito.explainability.model.Prediction) DataDomain(org.kie.kogito.explainability.model.DataDomain) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) EnvironmentMode(org.optaplanner.core.config.solver.EnvironmentMode) CompletableFuture(java.util.concurrent.CompletableFuture) SolverManager(org.optaplanner.core.api.solver.SolverManager) Function(java.util.function.Function) ArrayList(java.util.ArrayList) MockCounterFactualScoreCalculator(org.kie.kogito.explainability.local.counterfactual.score.MockCounterFactualScoreCalculator) ArgumentCaptor(org.mockito.ArgumentCaptor) NumericFeatureDistribution(org.kie.kogito.explainability.model.NumericFeatureDistribution) CounterfactualPrediction(org.kie.kogito.explainability.model.CounterfactualPrediction) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) LinkedList(java.util.LinkedList) BendableBigDecimalScore(org.optaplanner.core.api.score.buildin.bendablebigdecimal.BendableBigDecimalScore) ValueSource(org.junit.jupiter.params.provider.ValueSource) Logger(org.slf4j.Logger) Mockito.when(org.mockito.Mockito.when) Type(org.kie.kogito.explainability.model.Type) PredictionProvider(org.kie.kogito.explainability.model.PredictionProvider) Mockito.verify(org.mockito.Mockito.verify) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) TestUtils(org.kie.kogito.explainability.TestUtils) NumericalFeatureDomain(org.kie.kogito.explainability.model.domain.NumericalFeatureDomain) Config(org.kie.kogito.explainability.Config) Collections(java.util.Collections) FeatureDomain(org.kie.kogito.explainability.model.domain.FeatureDomain) PredictionInput(org.kie.kogito.explainability.model.PredictionInput) Prediction(org.kie.kogito.explainability.model.Prediction) CounterfactualPrediction(org.kie.kogito.explainability.model.CounterfactualPrediction) ArrayList(java.util.ArrayList) PredictionProvider(org.kie.kogito.explainability.model.PredictionProvider) Feature(org.kie.kogito.explainability.model.Feature) CounterfactualPrediction(org.kie.kogito.explainability.model.CounterfactualPrediction) CounterfactualEntity(org.kie.kogito.explainability.local.counterfactual.entities.CounterfactualEntity) TerminationConfig(org.optaplanner.core.config.solver.termination.TerminationConfig) Random(java.util.Random) PredictionOutput(org.kie.kogito.explainability.model.PredictionOutput) PredictionOutput(org.kie.kogito.explainability.model.PredictionOutput) Output(org.kie.kogito.explainability.model.Output) UUID(java.util.UUID) SolverConfig(org.optaplanner.core.config.solver.SolverConfig) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

CounterfactualEntity (org.kie.kogito.explainability.local.counterfactual.entities.CounterfactualEntity)45 Feature (org.kie.kogito.explainability.model.Feature)45 Test (org.junit.jupiter.api.Test)34 EmptyFeatureDomain (org.kie.kogito.explainability.model.domain.EmptyFeatureDomain)23 NumericalFeatureDomain (org.kie.kogito.explainability.model.domain.NumericalFeatureDomain)23 Output (org.kie.kogito.explainability.model.Output)22 PredictionOutput (org.kie.kogito.explainability.model.PredictionOutput)22 FeatureDomain (org.kie.kogito.explainability.model.domain.FeatureDomain)21 PredictionProvider (org.kie.kogito.explainability.model.PredictionProvider)18 Value (org.kie.kogito.explainability.model.Value)18 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)17 PredictionInput (org.kie.kogito.explainability.model.PredictionInput)17 LinkedList (java.util.LinkedList)16 CategoricalFeatureDomain (org.kie.kogito.explainability.model.domain.CategoricalFeatureDomain)16 Random (java.util.Random)14 ValueSource (org.junit.jupiter.params.provider.ValueSource)13 BinaryFeatureDomain (org.kie.kogito.explainability.model.domain.BinaryFeatureDomain)12 CurrencyFeatureDomain (org.kie.kogito.explainability.model.domain.CurrencyFeatureDomain)12 DurationFeatureDomain (org.kie.kogito.explainability.model.domain.DurationFeatureDomain)12 ObjectFeatureDomain (org.kie.kogito.explainability.model.domain.ObjectFeatureDomain)12