aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar Kristóf Marussy <kristof@marussy.com>2024-06-20 21:07:52 +0200
committerLibravatar Kristóf Marussy <kristof@marussy.com>2024-06-20 21:07:52 +0200
commit1140488673066f8e42362f26318b644c82cbd261 (patch)
treef7b111ce357003f3ccd1435150a70d81cdf33532
parentrefactor(logic): fix Sonar error (diff)
downloadrefinery-1140488673066f8e42362f26318b644c82cbd261.tar.gz
refinery-1140488673066f8e42362f26318b644c82cbd261.tar.zst
refinery-1140488673066f8e42362f26318b644c82cbd261.zip
refactor(dse): expose state coder parameters
-rw-r--r--subprojects/generator/src/main/java/tools/refinery/generator/ModelGeneratorFactory.java20
-rw-r--r--subprojects/store-reasoning/src/main/java/tools/refinery/store/reasoning/interpretation/PartialNeighbourhoodCalculator.java16
-rw-r--r--subprojects/store/src/main/java/tools/refinery/store/statecoding/internal/StateCoderBuilderImpl.java2
-rw-r--r--subprojects/store/src/main/java/tools/refinery/store/statecoding/neighbourhood/AbstractNeighbourhoodCalculator.java6
-rw-r--r--subprojects/store/src/main/java/tools/refinery/store/statecoding/neighbourhood/NeighbourhoodCalculator.java18
5 files changed, 46 insertions, 16 deletions
diff --git a/subprojects/generator/src/main/java/tools/refinery/generator/ModelGeneratorFactory.java b/subprojects/generator/src/main/java/tools/refinery/generator/ModelGeneratorFactory.java
index ec273cf4..877db0bb 100644
--- a/subprojects/generator/src/main/java/tools/refinery/generator/ModelGeneratorFactory.java
+++ b/subprojects/generator/src/main/java/tools/refinery/generator/ModelGeneratorFactory.java
@@ -25,6 +25,8 @@ import tools.refinery.store.util.CancellationToken;
25import java.util.Collection; 25import java.util.Collection;
26import java.util.Set; 26import java.util.Set;
27 27
28// This class is used as a fluent builder, so it's not necessary to use the return value of all of its methods.
29@SuppressWarnings("UnusedReturnValue")
28public final class ModelGeneratorFactory { 30public final class ModelGeneratorFactory {
29 @Inject 31 @Inject
30 private Provider<ModelInitializer> initializerProvider; 32 private Provider<ModelInitializer> initializerProvider;
@@ -38,6 +40,8 @@ public final class ModelGeneratorFactory {
38 40
39 private boolean partialInterpretationBasedNeighbourhoods; 41 private boolean partialInterpretationBasedNeighbourhoods;
40 42
43 private int stateCoderDepth = NeighbourhoodCalculator.DEFAULT_DEPTH;
44
41 public ModelGeneratorFactory cancellationToken(CancellationToken cancellationToken) { 45 public ModelGeneratorFactory cancellationToken(CancellationToken cancellationToken) {
42 this.cancellationToken = cancellationToken; 46 this.cancellationToken = cancellationToken;
43 return this; 47 return this;
@@ -48,8 +52,15 @@ public final class ModelGeneratorFactory {
48 return this; 52 return this;
49 } 53 }
50 54
51 public void partialInterpretationBasedNeighbourhoods(boolean partialInterpretationBasedNeighbourhoods) { 55 public ModelGeneratorFactory partialInterpretationBasedNeighbourhoods(
56 boolean partialInterpretationBasedNeighbourhoods) {
52 this.partialInterpretationBasedNeighbourhoods = partialInterpretationBasedNeighbourhoods; 57 this.partialInterpretationBasedNeighbourhoods = partialInterpretationBasedNeighbourhoods;
58 return this;
59 }
60
61 public ModelGeneratorFactory stateCoderDepth(int stateCoderDepth) {
62 this.stateCoderDepth = stateCoderDepth;
63 return this;
53 } 64 }
54 65
55 public ModelGenerator createGenerator(Problem problem) { 66 public ModelGenerator createGenerator(Problem problem) {
@@ -68,7 +79,7 @@ public final class ModelGeneratorFactory {
68 initializer.configureStoreBuilder(storeBuilder); 79 initializer.configureStoreBuilder(storeBuilder);
69 var store = storeBuilder.build(); 80 var store = storeBuilder.build();
70 return new ModelGenerator(initializer.getProblemTrace(), store, initializer.getModelSeed(), 81 return new ModelGenerator(initializer.getProblemTrace(), store, initializer.getModelSeed(),
71 solutionSerializerProvider); 82 solutionSerializerProvider);
72 } 83 }
73 84
74 private Collection<Concreteness> getRequiredInterpretations() { 85 private Collection<Concreteness> getRequiredInterpretations() {
@@ -78,7 +89,8 @@ public final class ModelGeneratorFactory {
78 } 89 }
79 90
80 private StateCodeCalculatorFactory getStateCoderCalculatorFactory() { 91 private StateCodeCalculatorFactory getStateCoderCalculatorFactory() {
81 return partialInterpretationBasedNeighbourhoods ? PartialNeighbourhoodCalculator.FACTORY : 92 return partialInterpretationBasedNeighbourhoods ?
82 NeighbourhoodCalculator::new; 93 PartialNeighbourhoodCalculator.factory(Concreteness.PARTIAL, stateCoderDepth) :
94 NeighbourhoodCalculator.factory(stateCoderDepth);
83 } 95 }
84} 96}
diff --git a/subprojects/store-reasoning/src/main/java/tools/refinery/store/reasoning/interpretation/PartialNeighbourhoodCalculator.java b/subprojects/store-reasoning/src/main/java/tools/refinery/store/reasoning/interpretation/PartialNeighbourhoodCalculator.java
index 859cf7c1..67e20dbc 100644
--- a/subprojects/store-reasoning/src/main/java/tools/refinery/store/reasoning/interpretation/PartialNeighbourhoodCalculator.java
+++ b/subprojects/store-reasoning/src/main/java/tools/refinery/store/reasoning/interpretation/PartialNeighbourhoodCalculator.java
@@ -21,13 +21,12 @@ import java.util.List;
21 21
22public class PartialNeighbourhoodCalculator extends AbstractNeighbourhoodCalculator<PartialInterpretation<?, ?>> { 22public class PartialNeighbourhoodCalculator extends AbstractNeighbourhoodCalculator<PartialInterpretation<?, ?>> {
23 private final ModelQueryAdapter queryAdapter; 23 private final ModelQueryAdapter queryAdapter;
24 private final Concreteness concreteness;
24 25
25 public static final StateCodeCalculatorFactory FACTORY = 26 protected PartialNeighbourhoodCalculator(Model model, IntSet individuals, Concreteness concreteness, int depth) {
26 (model, ignoredInterpretations, individuals) -> new PartialNeighbourhoodCalculator(model, individuals); 27 super(model, individuals, depth);
27
28 protected PartialNeighbourhoodCalculator(Model model, IntSet individuals) {
29 super(model, individuals);
30 queryAdapter = model.getAdapter(ModelQueryAdapter.class); 28 queryAdapter = model.getAdapter(ModelQueryAdapter.class);
29 this.concreteness = concreteness;
31 } 30 }
32 31
33 @Override 32 @Override
@@ -42,7 +41,7 @@ public class PartialNeighbourhoodCalculator extends AbstractNeighbourhoodCalcula
42 var partialSymbols = adapter.getStoreAdapter().getPartialSymbols(); 41 var partialSymbols = adapter.getStoreAdapter().getPartialSymbols();
43 return partialSymbols.stream() 42 return partialSymbols.stream()
44 .<PartialInterpretation<?, ?>>map(partialSymbol -> 43 .<PartialInterpretation<?, ?>>map(partialSymbol ->
45 adapter.getPartialInterpretation(Concreteness.PARTIAL, (PartialSymbol<?, ?>) partialSymbol)) 44 adapter.getPartialInterpretation(concreteness, (PartialSymbol<?, ?>) partialSymbol))
46 .toList(); 45 .toList();
47 } 46 }
48 47
@@ -60,4 +59,9 @@ public class PartialNeighbourhoodCalculator extends AbstractNeighbourhoodCalcula
60 protected Cursor<Tuple, ?> getCursor(PartialInterpretation<?, ?> interpretation) { 59 protected Cursor<Tuple, ?> getCursor(PartialInterpretation<?, ?> interpretation) {
61 return interpretation.getAll(); 60 return interpretation.getAll();
62 } 61 }
62
63 public static StateCodeCalculatorFactory factory(Concreteness concreteness, int depth) {
64 return (model, interpretations, individuals) -> new PartialNeighbourhoodCalculator(model, individuals,
65 concreteness, depth);
66 }
63} 67}
diff --git a/subprojects/store/src/main/java/tools/refinery/store/statecoding/internal/StateCoderBuilderImpl.java b/subprojects/store/src/main/java/tools/refinery/store/statecoding/internal/StateCoderBuilderImpl.java
index eed591e7..36d7d4c7 100644
--- a/subprojects/store/src/main/java/tools/refinery/store/statecoding/internal/StateCoderBuilderImpl.java
+++ b/subprojects/store/src/main/java/tools/refinery/store/statecoding/internal/StateCoderBuilderImpl.java
@@ -27,7 +27,7 @@ public class StateCoderBuilderImpl extends AbstractModelAdapterBuilder<StateCode
27 implements StateCoderBuilder { 27 implements StateCoderBuilder {
28 private final Set<AnySymbol> excluded = new HashSet<>(); 28 private final Set<AnySymbol> excluded = new HashSet<>();
29 private final MutableIntSet individuals = IntSets.mutable.empty(); 29 private final MutableIntSet individuals = IntSets.mutable.empty();
30 private StateCodeCalculatorFactory calculator = NeighbourhoodCalculator::new; 30 private StateCodeCalculatorFactory calculator = NeighbourhoodCalculator.factory();
31 private StateEquivalenceChecker checker = new StateEquivalenceCheckerImpl(); 31 private StateEquivalenceChecker checker = new StateEquivalenceCheckerImpl();
32 32
33 @Override 33 @Override
diff --git a/subprojects/store/src/main/java/tools/refinery/store/statecoding/neighbourhood/AbstractNeighbourhoodCalculator.java b/subprojects/store/src/main/java/tools/refinery/store/statecoding/neighbourhood/AbstractNeighbourhoodCalculator.java
index 5bfc4725..a4060e74 100644
--- a/subprojects/store/src/main/java/tools/refinery/store/statecoding/neighbourhood/AbstractNeighbourhoodCalculator.java
+++ b/subprojects/store/src/main/java/tools/refinery/store/statecoding/neighbourhood/AbstractNeighbourhoodCalculator.java
@@ -20,6 +20,7 @@ import java.util.*;
20public abstract class AbstractNeighbourhoodCalculator<T> implements StateCodeCalculator { 20public abstract class AbstractNeighbourhoodCalculator<T> implements StateCodeCalculator {
21 private final Model model; 21 private final Model model;
22 private final IntSet individuals; 22 private final IntSet individuals;
23 private final int depth;
23 private List<T> nullImpactValues; 24 private List<T> nullImpactValues;
24 private LinkedHashMap<T, long[]> impactValues; 25 private LinkedHashMap<T, long[]> impactValues;
25 private MutableIntLongMap individualHashValues; 26 private MutableIntLongMap individualHashValues;
@@ -28,9 +29,10 @@ public abstract class AbstractNeighbourhoodCalculator<T> implements StateCodeCal
28 29
29 protected static final long PRIME = 31; 30 protected static final long PRIME = 31;
30 31
31 protected AbstractNeighbourhoodCalculator(Model model, IntSet individuals) { 32 protected AbstractNeighbourhoodCalculator(Model model, IntSet individuals, int depth) {
32 this.model = model; 33 this.model = model;
33 this.individuals = individuals; 34 this.individuals = individuals;
35 this.depth = depth;
34 } 36 }
35 37
36 protected Model getModel() { 38 protected Model getModel() {
@@ -64,7 +66,7 @@ public abstract class AbstractNeighbourhoodCalculator<T> implements StateCodeCal
64 nextObjectCode = tempObjectCode; 66 nextObjectCode = tempObjectCode;
65 nextObjectCode.clear(); 67 nextObjectCode.clear();
66 rounds++; 68 rounds++;
67 } while (rounds <= 7 && rounds <= previousObjectCode.getEffectiveSize()); 69 } while (rounds <= depth && rounds <= previousObjectCode.getEffectiveSize());
68 70
69 long result = calculateLastSum(previousObjectCode); 71 long result = calculateLastSum(previousObjectCode);
70 return new StateCoderResult((int) result, previousObjectCode); 72 return new StateCoderResult((int) result, previousObjectCode);
diff --git a/subprojects/store/src/main/java/tools/refinery/store/statecoding/neighbourhood/NeighbourhoodCalculator.java b/subprojects/store/src/main/java/tools/refinery/store/statecoding/neighbourhood/NeighbourhoodCalculator.java
index f6071c5b..5859ccc2 100644
--- a/subprojects/store/src/main/java/tools/refinery/store/statecoding/neighbourhood/NeighbourhoodCalculator.java
+++ b/subprojects/store/src/main/java/tools/refinery/store/statecoding/neighbourhood/NeighbourhoodCalculator.java
@@ -9,16 +9,19 @@ import org.eclipse.collections.api.set.primitive.IntSet;
9import tools.refinery.store.map.Cursor; 9import tools.refinery.store.map.Cursor;
10import tools.refinery.store.model.Interpretation; 10import tools.refinery.store.model.Interpretation;
11import tools.refinery.store.model.Model; 11import tools.refinery.store.model.Model;
12import tools.refinery.store.statecoding.StateCodeCalculatorFactory;
12import tools.refinery.store.tuple.Tuple; 13import tools.refinery.store.tuple.Tuple;
13 14
14import java.util.List; 15import java.util.List;
15 16
16public class NeighbourhoodCalculator extends AbstractNeighbourhoodCalculator<Interpretation<?>> { 17public class NeighbourhoodCalculator extends AbstractNeighbourhoodCalculator<Interpretation<?>> {
18 public static final int DEFAULT_DEPTH = 7;
19
17 private final List<Interpretation<?>> interpretations; 20 private final List<Interpretation<?>> interpretations;
18 21
19 public NeighbourhoodCalculator(Model model, List<? extends Interpretation<?>> interpretations, 22 protected NeighbourhoodCalculator(Model model, List<? extends Interpretation<?>> interpretations,
20 IntSet individuals) { 23 IntSet individuals, int depth) {
21 super(model, individuals); 24 super(model, individuals, depth);
22 this.interpretations = List.copyOf(interpretations); 25 this.interpretations = List.copyOf(interpretations);
23 } 26 }
24 27
@@ -41,4 +44,13 @@ public class NeighbourhoodCalculator extends AbstractNeighbourhoodCalculator<Int
41 protected Cursor<Tuple, ?> getCursor(Interpretation<?> interpretation) { 44 protected Cursor<Tuple, ?> getCursor(Interpretation<?> interpretation) {
42 return interpretation.getAll(); 45 return interpretation.getAll();
43 } 46 }
47
48 public static StateCodeCalculatorFactory factory(int depth) {
49 return (model, interpretations, individuals) -> new NeighbourhoodCalculator(model, interpretations,
50 individuals, depth);
51 }
52
53 public static StateCodeCalculatorFactory factory() {
54 return factory(DEFAULT_DEPTH);
55 }
44} 56}