diff options
Diffstat (limited to 'Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/optimization/ThreeValuedCostObjective.xtend')
-rw-r--r-- | Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/optimization/ThreeValuedCostObjective.xtend | 80 |
1 files changed, 80 insertions, 0 deletions
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..9b1a7e9f --- /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,80 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.optimization | ||
2 | |||
3 | import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.cardinality.BoundSaturationListener | ||
4 | import java.util.Map | ||
5 | import org.eclipse.viatra.dse.base.ThreadContext | ||
6 | import org.eclipse.xtend.lib.annotations.Accessors | ||
7 | |||
8 | class ThreeValuedCostObjective extends AbstractThreeValuedObjective implements BoundSaturationListener { | ||
9 | @Accessors val Map<String, CostElementMatchers> matchers | ||
10 | double lowerBoundHint = Double.NEGATIVE_INFINITY | ||
11 | double upperBoundHint = Double.POSITIVE_INFINITY | ||
12 | |||
13 | new(String name, Map<String, CostElementMatchers> matchers, ObjectiveKind kind, ObjectiveThreshold threshold, | ||
14 | int level) { | ||
15 | super(name, kind, threshold, level) | ||
16 | this.matchers = matchers | ||
17 | } | ||
18 | |||
19 | override createNew() { | ||
20 | // new ThreeValuedCostObjective(name, matchers, kind, threshold, level) | ||
21 | throw new UnsupportedOperationException("ThreeValuedCostObjective can only be used from a single thread") | ||
22 | } | ||
23 | |||
24 | override init(ThreadContext context) { | ||
25 | } | ||
26 | |||
27 | override getRawFitness(ThreadContext context) { | ||
28 | var double cost = 0 | ||
29 | for (matcher : matchers.values) { | ||
30 | cost += matcher.weight * matcher.currentNumberOfMatches | ||
31 | } | ||
32 | cost | ||
33 | } | ||
34 | |||
35 | override getLowestPossibleFitness(ThreadContext threadContext) { | ||
36 | var double cost = 0 | ||
37 | for (matcher : matchers.values) { | ||
38 | if (matcher.weight >= 0) { | ||
39 | cost += matcher.weight * matcher.minimumNumberOfMatches | ||
40 | } else { | ||
41 | cost += matcher.weight * matcher.maximumNumberOfMatches | ||
42 | } | ||
43 | } | ||
44 | val boundWithHint = Math.max(lowerBoundHint, cost) | ||
45 | if (boundWithHint > upperBoundHint) { | ||
46 | throw new IllegalStateException("Inconsistent cost bounds") | ||
47 | } | ||
48 | boundWithHint | ||
49 | } | ||
50 | |||
51 | override getHighestPossibleFitness(ThreadContext threadContext) { | ||
52 | var double cost = 0 | ||
53 | for (matcher : matchers.values) { | ||
54 | if (matcher.weight <= 0) { | ||
55 | cost += matcher.weight * matcher.minimumNumberOfMatches | ||
56 | } else { | ||
57 | cost += matcher.weight * matcher.maximumNumberOfMatches | ||
58 | } | ||
59 | } | ||
60 | val boundWithHint = Math.min(upperBoundHint, cost) | ||
61 | if (boundWithHint < lowerBoundHint) { | ||
62 | throw new IllegalStateException("Inconsistent cost bounds") | ||
63 | } | ||
64 | boundWithHint | ||
65 | } | ||
66 | |||
67 | override boundsSaturated(Integer lower, Integer upper) { | ||
68 | lowerBoundHint = if (lower === null) { | ||
69 | Double.NEGATIVE_INFINITY | ||
70 | } else { | ||
71 | lower | ||
72 | } | ||
73 | upperBoundHint = if (upper === null) { | ||
74 | Double.POSITIVE_INFINITY | ||
75 | } else { | ||
76 | upper | ||
77 | } | ||
78 | println('''Bounds saturated: «lower»..«upper»''') | ||
79 | } | ||
80 | } | ||