aboutsummaryrefslogtreecommitdiffstats
path: root/Domains/ca.mcgill.rtgmrt.example.modes3/src/modes3/run/TrainLocationsObjectiveHint.xtend
diff options
context:
space:
mode:
Diffstat (limited to 'Domains/ca.mcgill.rtgmrt.example.modes3/src/modes3/run/TrainLocationsObjectiveHint.xtend')
-rw-r--r--Domains/ca.mcgill.rtgmrt.example.modes3/src/modes3/run/TrainLocationsObjectiveHint.xtend85
1 files changed, 85 insertions, 0 deletions
diff --git a/Domains/ca.mcgill.rtgmrt.example.modes3/src/modes3/run/TrainLocationsObjectiveHint.xtend b/Domains/ca.mcgill.rtgmrt.example.modes3/src/modes3/run/TrainLocationsObjectiveHint.xtend
new file mode 100644
index 00000000..cc2d7925
--- /dev/null
+++ b/Domains/ca.mcgill.rtgmrt.example.modes3/src/modes3/run/TrainLocationsObjectiveHint.xtend
@@ -0,0 +1,85 @@
1package modes3.run
2
3import hu.bme.mit.inf.dslreasoner.ecore2logic.Ecore2Logic
4import hu.bme.mit.inf.dslreasoner.ecore2logic.Ecore2Logic_Trace
5import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.Type
6import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.cardinality.ExtendedLinearExpressionBuilderFactory
7import hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.optimization.CostElementMatch
8import hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.optimization.CostElementMatchers
9import hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.optimization.CostObjectiveHint
10import java.util.Collection
11import java.util.Map
12import modes3.Modes3Package
13import modes3.queries.TrainLocations_step_2
14import modes3.queries.TrainLocations_step_3
15
16class TrainLocationsObjectiveHint extends CostObjectiveHint {
17 val Type segmentType
18 val Type trainType
19
20 new(extension Ecore2Logic ecore2Logic, Ecore2Logic_Trace ecore2LogicTrace) {
21 extension val Modes3Package = Modes3Package.eINSTANCE
22 segmentType = ecore2LogicTrace.TypeofEClass(segment)
23 trainType = ecore2LogicTrace.TypeofEClass(train)
24 }
25
26 override isExact() {
27 true
28 }
29
30 override createPolyhedronExtensionOperator(Map<String, CostElementMatchers> costElementMatchers) {
31 val step2 = costElementMatchers.get(TrainLocations_step_2.instance.fullyQualifiedName)
32 val step3 = costElementMatchers.get(TrainLocations_step_3.instance.fullyQualifiedName);
33
34 [
35 val objectiveBuilder = createBuilder
36
37 for (m : step2.matches) {
38 val dimension = getDimension(m.match)
39 objectiveBuilder.add(step2.weight, dimension)
40 dimension.tightenLowerBound(0)
41 if (m.multi) {
42 createBuilder.add(1, dimension).add(-1, trainType).build.assertEqualsTo(0)
43 } else {
44 dimension.tightenUpperBound(1)
45 if (m.must) {
46 dimension.tightenLowerBound(1)
47 }
48 }
49 }
50
51 val step3Matches = step3.matches
52 for (m : step3Matches) {
53 val dimension = getDimension(m.match)
54 objectiveBuilder.add(step3.weight, dimension)
55 dimension.tightenLowerBound(0)
56 if (!m.multi) {
57 dimension.tightenUpperBound(1)
58 if (m.must) {
59 dimension.tightenLowerBound(1)
60 }
61 }
62 }
63 boundLimit(step3Matches, 2, trainType, 1)
64 boundLimit(step3Matches, 3, segmentType, 1)
65
66 objectiveBuilder.buildWithBounds
67 ]
68 }
69
70 private static def boundLimit(extension ExtendedLinearExpressionBuilderFactory factory,
71 Collection<CostElementMatch> matches, int index, Type type, int count) {
72 for (pair : matches.groupBy[match.get(index)].entrySet) {
73 val multiplicityBuilder = createBuilder
74 for (m : pair.value) {
75 multiplicityBuilder.add(1, m.match)
76 }
77 if (CostElementMatchers.isMulti(pair.key)) {
78 multiplicityBuilder.add(-count, type)
79 multiplicityBuilder.build.tightenUpperBound(0)
80 } else {
81 multiplicityBuilder.build.tightenUpperBound(count)
82 }
83 }
84 }
85} \ No newline at end of file