diff options
author | 2019-04-08 00:58:00 +0200 | |
---|---|---|
committer | 2019-04-08 00:58:00 +0200 | |
commit | c1f185fd8fc2c3dfc123d9271726c588963c7c01 (patch) | |
tree | 88a5bb94017e7d3f0fbce0a51a78c2549b0977bd /Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit | |
parent | Infrastructure for objective functions (diff) | |
download | VIATRA-Generator-c1f185fd8fc2c3dfc123d9271726c588963c7c01.tar.gz VIATRA-Generator-c1f185fd8fc2c3dfc123d9271726c588963c7c01.tar.zst VIATRA-Generator-c1f185fd8fc2c3dfc123d9271726c588963c7c01.zip |
Objective POC implementation
Diffstat (limited to 'Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit')
8 files changed, 284 insertions, 9 deletions
diff --git a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/ViatraReasoner.xtend b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/ViatraReasoner.xtend index 8831b0ff..6898550d 100644 --- a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/ViatraReasoner.xtend +++ b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/ViatraReasoner.xtend | |||
@@ -1,5 +1,7 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner | 1 | package hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner |
2 | 2 | ||
3 | import com.google.common.collect.ImmutableList | ||
4 | import com.google.common.collect.Lists | ||
3 | import hu.bme.mit.inf.dslreasoner.logic.model.builder.DocumentationLevel | 5 | import hu.bme.mit.inf.dslreasoner.logic.model.builder.DocumentationLevel |
4 | import hu.bme.mit.inf.dslreasoner.logic.model.builder.LogicReasoner | 6 | import hu.bme.mit.inf.dslreasoner.logic.model.builder.LogicReasoner |
5 | import hu.bme.mit.inf.dslreasoner.logic.model.builder.LogicReasonerException | 7 | import hu.bme.mit.inf.dslreasoner.logic.model.builder.LogicReasonerException |
@@ -25,6 +27,8 @@ import hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.dse.SurelyViolatedObject | |||
25 | import hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.dse.UnfinishedMultiplicityObjective | 27 | import hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.dse.UnfinishedMultiplicityObjective |
26 | import hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.dse.ViatraReasonerSolutionSaver | 28 | import hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.dse.ViatraReasonerSolutionSaver |
27 | import hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.dse.WF2ObjectiveConverter | 29 | import hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.dse.WF2ObjectiveConverter |
30 | import hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.optimization.ThreeValuedCostElement | ||
31 | import hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.optimization.ThreeValuedCostObjective | ||
28 | import hu.bme.mit.inf.dslreasoner.workspace.ReasonerWorkspace | 32 | import hu.bme.mit.inf.dslreasoner.workspace.ReasonerWorkspace |
29 | import java.util.List | 33 | import java.util.List |
30 | import java.util.Map | 34 | import java.util.Map |
@@ -84,9 +88,36 @@ class ViatraReasoner extends LogicReasoner { | |||
84 | wf2ObjectiveConverter.createCompletenessObjective(method.unfinishedWF) | 88 | wf2ObjectiveConverter.createCompletenessObjective(method.unfinishedWF) |
85 | )) | 89 | )) |
86 | 90 | ||
91 | val extermalObjectives = Lists.newArrayListWithExpectedSize(viatraConfig.costObjectives.size) | ||
92 | for (entry : viatraConfig.costObjectives.indexed) { | ||
93 | val objectiveName = '''costObjective«entry.key»''' | ||
94 | val objectiveConfig = entry.value | ||
95 | val elementsBuilder = ImmutableList.builder | ||
96 | for (elementConfig : objectiveConfig.elements) { | ||
97 | val relationName = elementConfig.patternQualifiedName | ||
98 | val modalQueries = method.modalRelationQueries.get(relationName) | ||
99 | if (modalQueries === null) { | ||
100 | throw new IllegalArgumentException("Unknown relation: " + relationName) | ||
101 | } | ||
102 | elementsBuilder.add(new ThreeValuedCostElement( | ||
103 | modalQueries.currentQuery, | ||
104 | modalQueries.mayQuery, | ||
105 | modalQueries.mustQuery, | ||
106 | elementConfig.weight | ||
107 | )) | ||
108 | } | ||
109 | val costElements = elementsBuilder.build | ||
110 | val costObjective = new ThreeValuedCostObjective(objectiveName, costElements, objectiveConfig.kind, | ||
111 | objectiveConfig.threshold, 3) | ||
112 | dse.addObjective(costObjective) | ||
113 | if (objectiveConfig.findExtremum) { | ||
114 | extermalObjectives += costObjective | ||
115 | } | ||
116 | } | ||
117 | |||
87 | val solutionStore = new SolutionStore(configuration.solutionScope.numberOfRequiredSolutions) | 118 | val solutionStore = new SolutionStore(configuration.solutionScope.numberOfRequiredSolutions) |
88 | solutionStore.registerSolutionFoundHandler(new LoggerSolutionFoundHandler(viatraConfig)) | 119 | solutionStore.registerSolutionFoundHandler(new LoggerSolutionFoundHandler(viatraConfig)) |
89 | val solutionSaver = new ViatraReasonerSolutionSaver(newArrayOfSize(0, 0)) | 120 | val solutionSaver = new ViatraReasonerSolutionSaver(newArrayList(extermalObjectives)) |
90 | val solutionCopier = solutionSaver.solutionCopier | 121 | val solutionCopier = solutionSaver.solutionCopier |
91 | solutionStore.withSolutionSaver(solutionSaver) | 122 | solutionStore.withSolutionSaver(solutionSaver) |
92 | dse.solutionStore = solutionStore | 123 | dse.solutionStore = solutionStore |
diff --git a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/ViatraReasonerConfiguration.xtend b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/ViatraReasonerConfiguration.xtend index 9ef23c59..e6aee20c 100644 --- a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/ViatraReasonerConfiguration.xtend +++ b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/ViatraReasonerConfiguration.xtend | |||
@@ -7,18 +7,22 @@ import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.TypeDeclaration | |||
7 | import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.ModelGenerationMethod | 7 | import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.ModelGenerationMethod |
8 | import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.TypeInferenceMethod | 8 | import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.TypeInferenceMethod |
9 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.visualisation.PartialInterpretationVisualiser | 9 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.visualisation.PartialInterpretationVisualiser |
10 | import hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.optimization.ObjectiveKind | ||
11 | import hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.optimization.ObjectiveThreshold | ||
10 | import java.util.LinkedList | 12 | import java.util.LinkedList |
11 | import java.util.List | 13 | import java.util.List |
12 | import java.util.Set | 14 | import java.util.Set |
13 | import org.eclipse.xtext.xbase.lib.Functions.Function1 | 15 | import org.eclipse.xtext.xbase.lib.Functions.Function1 |
14 | 16 | ||
15 | enum StateCoderStrategy { | 17 | enum StateCoderStrategy { |
16 | Neighbourhood, NeighbourhoodWithEquivalence, IDBased, DefinedByDiversity | 18 | Neighbourhood, |
19 | NeighbourhoodWithEquivalence, | ||
20 | IDBased, | ||
21 | DefinedByDiversity | ||
17 | } | 22 | } |
18 | 23 | ||
19 | class ViatraReasonerConfiguration extends LogicSolverConfiguration{ | 24 | class ViatraReasonerConfiguration extends LogicSolverConfiguration { |
20 | //public var Iterable<PQuery> existingQueries | 25 | // public var Iterable<PQuery> existingQueries |
21 | |||
22 | public var nameNewElements = false | 26 | public var nameNewElements = false |
23 | public var StateCoderStrategy stateCoderStrategy = StateCoderStrategy.Neighbourhood | 27 | public var StateCoderStrategy stateCoderStrategy = StateCoderStrategy.Neighbourhood |
24 | public var TypeInferenceMethod typeInferenceMethod = TypeInferenceMethod.PreliminaryAnalysis | 28 | public var TypeInferenceMethod typeInferenceMethod = TypeInferenceMethod.PreliminaryAnalysis |
@@ -26,7 +30,7 @@ class ViatraReasonerConfiguration extends LogicSolverConfiguration{ | |||
26 | * Once per 1/randomBacktrackChance the search selects a random state. | 30 | * Once per 1/randomBacktrackChance the search selects a random state. |
27 | */ | 31 | */ |
28 | public var int randomBacktrackChance = 20; | 32 | public var int randomBacktrackChance = 20; |
29 | 33 | ||
30 | /** | 34 | /** |
31 | * Describes the required diversity between the solutions. | 35 | * Describes the required diversity between the solutions. |
32 | * Null means that the solutions have to have different state codes only. | 36 | * Null means that the solutions have to have different state codes only. |
@@ -44,7 +48,9 @@ class ViatraReasonerConfiguration extends LogicSolverConfiguration{ | |||
44 | /** | 48 | /** |
45 | * Configuration for cutting search space. | 49 | * Configuration for cutting search space. |
46 | */ | 50 | */ |
47 | public var SearchSpaceConstraint searchSpaceConstraints = new SearchSpaceConstraint | 51 | public var SearchSpaceConstraint searchSpaceConstraints = new SearchSpaceConstraint |
52 | |||
53 | public var List<CostObjectiveConfiguration> costObjectives = newArrayList | ||
48 | } | 54 | } |
49 | 55 | ||
50 | class DiversityDescriptor { | 56 | class DiversityDescriptor { |
@@ -73,4 +79,16 @@ class SearchSpaceConstraint { | |||
73 | public static val UNLIMITED_MAXDEPTH = Integer.MAX_VALUE | 79 | public static val UNLIMITED_MAXDEPTH = Integer.MAX_VALUE |
74 | public var int maxDepth = UNLIMITED_MAXDEPTH | 80 | public var int maxDepth = UNLIMITED_MAXDEPTH |
75 | public var List<Function1<ModelGenerationMethod, ModelGenerationMethodBasedGlobalConstraint>> additionalGlobalConstraints = new LinkedList | 81 | public var List<Function1<ModelGenerationMethod, ModelGenerationMethodBasedGlobalConstraint>> additionalGlobalConstraints = new LinkedList |
76 | } \ No newline at end of file | 82 | } |
83 | |||
84 | class CostObjectiveConfiguration { | ||
85 | public var List<CostObjectiveElementConfiguration> elements = newArrayList | ||
86 | public var ObjectiveKind kind | ||
87 | public var ObjectiveThreshold threshold | ||
88 | public var boolean findExtremum | ||
89 | } | ||
90 | |||
91 | class CostObjectiveElementConfiguration { | ||
92 | public var String patternQualifiedName | ||
93 | public var int weight | ||
94 | } | ||
diff --git a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/dse/DseUtils.xtend b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/dse/DseUtils.xtend index 3a897aa3..3c2e3319 100644 --- a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/dse/DseUtils.xtend +++ b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/dse/DseUtils.xtend | |||
@@ -1,5 +1,6 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.dse | 1 | package hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.dse |
2 | 2 | ||
3 | import hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.optimization.IThreeValuedObjective | ||
3 | import org.eclipse.viatra.dse.base.ThreadContext | 4 | import org.eclipse.viatra.dse.base.ThreadContext |
4 | import org.eclipse.viatra.dse.objectives.Comparators | 5 | import org.eclipse.viatra.dse.objectives.Comparators |
5 | import org.eclipse.viatra.dse.objectives.Fitness | 6 | import org.eclipse.viatra.dse.objectives.Fitness |
diff --git a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/dse/ModelGenerationCompositeObjective.xtend b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/dse/ModelGenerationCompositeObjective.xtend index af6d1bbd..9a33753c 100644 --- a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/dse/ModelGenerationCompositeObjective.xtend +++ b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/dse/ModelGenerationCompositeObjective.xtend | |||
@@ -1,6 +1,7 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.dse | 1 | package hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.dse |
2 | 2 | ||
3 | import com.google.common.collect.ImmutableList | 3 | import com.google.common.collect.ImmutableList |
4 | import hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.optimization.IThreeValuedObjective | ||
4 | import java.util.Comparator | 5 | import java.util.Comparator |
5 | import java.util.List | 6 | import java.util.List |
6 | import org.eclipse.viatra.dse.base.ThreadContext | 7 | import org.eclipse.viatra.dse.base.ThreadContext |
diff --git a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/optimization/AbstractThreeValuedObjective.xtend b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/optimization/AbstractThreeValuedObjective.xtend new file mode 100644 index 00000000..241bef2a --- /dev/null +++ b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/optimization/AbstractThreeValuedObjective.xtend | |||
@@ -0,0 +1,102 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.optimization | ||
2 | |||
3 | import java.util.Comparator | ||
4 | import org.eclipse.viatra.dse.base.ThreadContext | ||
5 | import org.eclipse.xtend.lib.annotations.Accessors | ||
6 | import org.eclipse.xtend.lib.annotations.Data | ||
7 | |||
8 | abstract class ObjectiveThreshold { | ||
9 | public static val NO_THRESHOLD = new hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.optimization.ObjectiveThreshold { | ||
10 | override isHard() { | ||
11 | false | ||
12 | } | ||
13 | |||
14 | override satisfiesThreshold(double cost, Comparator<Double> comparator) { | ||
15 | true | ||
16 | } | ||
17 | } | ||
18 | |||
19 | private new() { | ||
20 | } | ||
21 | |||
22 | def boolean isHard() { | ||
23 | true | ||
24 | } | ||
25 | |||
26 | def boolean satisfiesThreshold(double cost, Comparator<Double> comparator) | ||
27 | |||
28 | @Data | ||
29 | static class Exclusive extends ObjectiveThreshold { | ||
30 | val double threshold | ||
31 | |||
32 | override satisfiesThreshold(double cost, Comparator<Double> comparator) { | ||
33 | comparator.compare(threshold, cost) > 0 | ||
34 | } | ||
35 | } | ||
36 | |||
37 | @Data | ||
38 | static class Inclusive extends ObjectiveThreshold { | ||
39 | val double threshold | ||
40 | |||
41 | override satisfiesThreshold(double cost, Comparator<Double> comparator) { | ||
42 | comparator.compare(threshold, cost) >= 0 | ||
43 | } | ||
44 | } | ||
45 | } | ||
46 | |||
47 | abstract class AbstractThreeValuedObjective implements IThreeValuedObjective { | ||
48 | @Accessors val String name | ||
49 | @Accessors ObjectiveKind kind | ||
50 | @Accessors ObjectiveThreshold threshold | ||
51 | @Accessors int level | ||
52 | |||
53 | protected new(String name, ObjectiveKind kind, ObjectiveThreshold threshold, int level) { | ||
54 | this.name = name | ||
55 | this.kind = kind | ||
56 | this.threshold = threshold | ||
57 | this.level = level | ||
58 | } | ||
59 | |||
60 | abstract def double getLowestPossibleFitness(ThreadContext threadContext) | ||
61 | |||
62 | abstract def double getHighestPossibleFitness(ThreadContext threadContext) | ||
63 | |||
64 | override getWorstPossibleFitness(ThreadContext threadContext) { | ||
65 | switch (kind) { | ||
66 | case LOWER_IS_BETTER: | ||
67 | getHighestPossibleFitness(threadContext) | ||
68 | case HIGHER_IS_BETTER: | ||
69 | getLowestPossibleFitness(threadContext) | ||
70 | default: | ||
71 | throw new IllegalStateException("Unknown three valued objective kind: " + kind) | ||
72 | } | ||
73 | } | ||
74 | |||
75 | override getBestPossibleFitness(ThreadContext threadContext) { | ||
76 | switch (kind) { | ||
77 | case LOWER_IS_BETTER: | ||
78 | getLowestPossibleFitness(threadContext) | ||
79 | case HIGHER_IS_BETTER: | ||
80 | getHighestPossibleFitness(threadContext) | ||
81 | default: | ||
82 | throw new IllegalStateException("Unknown three valued objective kind: " + kind) | ||
83 | } | ||
84 | } | ||
85 | |||
86 | override isHardObjective() { | ||
87 | threshold.hard | ||
88 | } | ||
89 | |||
90 | override satisifiesHardObjective(Double fitness) { | ||
91 | threshold.satisfiesThreshold(fitness, comparator) | ||
92 | } | ||
93 | |||
94 | override getComparator() { | ||
95 | kind.comparator | ||
96 | } | ||
97 | |||
98 | override setComparator(Comparator<Double> comparator) { | ||
99 | kind = ObjectiveKind.fromComparator(comparator) | ||
100 | } | ||
101 | |||
102 | } | ||
diff --git a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/dse/IThreeValuedObjective.xtend b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/optimization/IThreeValuedObjective.xtend index 8c93d4ec..4a870a3e 100644 --- a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/dse/IThreeValuedObjective.xtend +++ b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/optimization/IThreeValuedObjective.xtend | |||
@@ -1,4 +1,4 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.dse | 1 | package hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.optimization |
2 | 2 | ||
3 | import org.eclipse.viatra.dse.base.ThreadContext | 3 | import org.eclipse.viatra.dse.base.ThreadContext |
4 | import org.eclipse.viatra.dse.objectives.IObjective | 4 | import org.eclipse.viatra.dse.objectives.IObjective |
diff --git a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/optimization/ObjectiveKind.java b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/optimization/ObjectiveKind.java new file mode 100644 index 00000000..f65428fe --- /dev/null +++ b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/optimization/ObjectiveKind.java | |||
@@ -0,0 +1,36 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.optimization; | ||
2 | |||
3 | import java.util.Comparator; | ||
4 | |||
5 | import org.eclipse.viatra.dse.objectives.Comparators; | ||
6 | |||
7 | public enum ObjectiveKind { | ||
8 | LOWER_IS_BETTER { | ||
9 | |||
10 | @Override | ||
11 | public Comparator<Double> getComparator() { | ||
12 | return Comparators.LOWER_IS_BETTER; | ||
13 | } | ||
14 | |||
15 | }, | ||
16 | HIGHER_IS_BETTER { | ||
17 | |||
18 | @Override | ||
19 | public Comparator<Double> getComparator() { | ||
20 | return Comparators.HIGHER_IS_BETTER; | ||
21 | } | ||
22 | |||
23 | }; | ||
24 | |||
25 | public abstract Comparator<Double> getComparator(); | ||
26 | |||
27 | public static ObjectiveKind fromComparator(Comparator<Double> comparator) { | ||
28 | if (Comparators.LOWER_IS_BETTER.equals(comparator)) { | ||
29 | return ObjectiveKind.LOWER_IS_BETTER; | ||
30 | } else if (Comparators.HIGHER_IS_BETTER.equals(comparator)) { | ||
31 | return ObjectiveKind.HIGHER_IS_BETTER; | ||
32 | } else { | ||
33 | throw new IllegalStateException("Only LOWER_IS_BETTER and HIGHER_IS_BETTER comparators are supported."); | ||
34 | } | ||
35 | } | ||
36 | } | ||
diff --git a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/optimization/ThreeValuedCostObjective.xtend b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/optimization/ThreeValuedCostObjective.xtend new file mode 100644 index 00000000..362ef4a3 --- /dev/null +++ b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/optimization/ThreeValuedCostObjective.xtend | |||
@@ -0,0 +1,86 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.optimization | ||
2 | |||
3 | import com.google.common.collect.ImmutableList | ||
4 | import java.util.Collection | ||
5 | import org.eclipse.viatra.dse.base.ThreadContext | ||
6 | import org.eclipse.viatra.query.runtime.api.IPatternMatch | ||
7 | import org.eclipse.viatra.query.runtime.api.IQuerySpecification | ||
8 | import org.eclipse.viatra.query.runtime.api.ViatraQueryMatcher | ||
9 | import org.eclipse.xtend.lib.annotations.Data | ||
10 | |||
11 | @Data | ||
12 | class ThreeValuedCostElement { | ||
13 | val IQuerySpecification<? extends ViatraQueryMatcher<? extends IPatternMatch>> currentMatchQuery | ||
14 | val IQuerySpecification<? extends ViatraQueryMatcher<? extends IPatternMatch>> mayMatchQuery | ||
15 | val IQuerySpecification<? extends ViatraQueryMatcher<? extends IPatternMatch>> mustMatchQuery | ||
16 | val int weight | ||
17 | } | ||
18 | |||
19 | class ThreeValuedCostObjective extends AbstractThreeValuedObjective { | ||
20 | val Collection<ThreeValuedCostElement> costElements | ||
21 | Collection<CostElementMatchers> matchers | ||
22 | |||
23 | new(String name, Collection<ThreeValuedCostElement> costElements, ObjectiveKind kind, ObjectiveThreshold threshold, | ||
24 | int level) { | ||
25 | super(name, kind, threshold, level) | ||
26 | this.costElements = costElements | ||
27 | } | ||
28 | |||
29 | override createNew() { | ||
30 | new ThreeValuedCostObjective(name, costElements, kind, threshold, level) | ||
31 | } | ||
32 | |||
33 | override init(ThreadContext context) { | ||
34 | val queryEngine = context.queryEngine | ||
35 | matchers = ImmutableList.copyOf(costElements.map [ element | | ||
36 | new CostElementMatchers( | ||
37 | queryEngine.getMatcher(element.currentMatchQuery), | ||
38 | queryEngine.getMatcher(element.mayMatchQuery), | ||
39 | queryEngine.getMatcher(element.mustMatchQuery), | ||
40 | element.weight | ||
41 | ) | ||
42 | ]) | ||
43 | } | ||
44 | |||
45 | override getFitness(ThreadContext context) { | ||
46 | var int cost = 0 | ||
47 | for (matcher : matchers) { | ||
48 | cost += matcher.weight * matcher.currentMatcher.countMatches | ||
49 | } | ||
50 | cost as double | ||
51 | } | ||
52 | |||
53 | override getLowestPossibleFitness(ThreadContext threadContext) { | ||
54 | var int cost = 0 | ||
55 | for (matcher : matchers) { | ||
56 | if (matcher.weight >= 0) { | ||
57 | cost += matcher.weight * matcher.mustMatcher.countMatches | ||
58 | } else if (matcher.mayMatcher.countMatches > 0) { | ||
59 | // TODO Count may matches. | ||
60 | return Double.NEGATIVE_INFINITY | ||
61 | } | ||
62 | } | ||
63 | cost as double | ||
64 | } | ||
65 | |||
66 | override getHighestPossibleFitness(ThreadContext threadContext) { | ||
67 | var int cost = 0 | ||
68 | for (matcher : matchers) { | ||
69 | if (matcher.weight <= 0) { | ||
70 | cost += matcher.weight * matcher.mustMatcher.countMatches | ||
71 | } else if (matcher.mayMatcher.countMatches > 0) { | ||
72 | // TODO Count may matches. | ||
73 | return Double.POSITIVE_INFINITY | ||
74 | } | ||
75 | } | ||
76 | cost as double | ||
77 | } | ||
78 | |||
79 | @Data | ||
80 | private static class CostElementMatchers { | ||
81 | val ViatraQueryMatcher<? extends IPatternMatch> currentMatcher | ||
82 | val ViatraQueryMatcher<? extends IPatternMatch> mayMatcher | ||
83 | val ViatraQueryMatcher<? extends IPatternMatch> mustMatcher | ||
84 | val int weight | ||
85 | } | ||
86 | } | ||