aboutsummaryrefslogtreecommitdiffstats
path: root/Domains/ca.mcgill.rtgmrt.example.modes3/src/modes3/run/EndOfSidingObjectiveHint.xtend
diff options
context:
space:
mode:
Diffstat (limited to 'Domains/ca.mcgill.rtgmrt.example.modes3/src/modes3/run/EndOfSidingObjectiveHint.xtend')
-rw-r--r--Domains/ca.mcgill.rtgmrt.example.modes3/src/modes3/run/EndOfSidingObjectiveHint.xtend139
1 files changed, 139 insertions, 0 deletions
diff --git a/Domains/ca.mcgill.rtgmrt.example.modes3/src/modes3/run/EndOfSidingObjectiveHint.xtend b/Domains/ca.mcgill.rtgmrt.example.modes3/src/modes3/run/EndOfSidingObjectiveHint.xtend
new file mode 100644
index 00000000..f7e23a57
--- /dev/null
+++ b/Domains/ca.mcgill.rtgmrt.example.modes3/src/modes3/run/EndOfSidingObjectiveHint.xtend
@@ -0,0 +1,139 @@
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.EndOfSiding_step_2
14import modes3.queries.EndOfSiding_step_3
15import modes3.queries.EndOfSiding_step_4
16import modes3.queries.EndOfSiding_step_5
17
18class EndOfSidingObjectiveHint extends CostObjectiveHint {
19 val Type segmentType
20 val Type trainType
21
22 new(extension Ecore2Logic ecore2Logic, Ecore2Logic_Trace ecore2LogicTrace) {
23 extension val Modes3Package = Modes3Package.eINSTANCE
24 segmentType = ecore2LogicTrace.TypeofEClass(segment)
25 trainType = ecore2LogicTrace.TypeofEClass(train)
26 }
27
28 override isExact() {
29 true
30// false
31 }
32
33 override createPolyhedronExtensionOperator(Map<String, CostElementMatchers> costElementMatchers) {
34 val step2 = costElementMatchers.get(EndOfSiding_step_2.instance.fullyQualifiedName)
35 val step3 = costElementMatchers.get(EndOfSiding_step_3.instance.fullyQualifiedName)
36 val step4 = costElementMatchers.get(EndOfSiding_step_4.instance.fullyQualifiedName)
37 val step5 = costElementMatchers.get(EndOfSiding_step_5.instance.fullyQualifiedName);
38
39 [
40 val objectiveBuilder = createBuilder
41
42 for (m : step2.matches) {
43 val dimension = getDimension(m.match)
44 objectiveBuilder.add(step2.weight, dimension)
45 dimension.tightenLowerBound(0)
46 if (m.multi) {
47 createBuilder.add(1, dimension).add(-1, trainType).build.assertEqualsTo(0)
48 } else {
49 dimension.tightenUpperBound(1)
50 if (m.must) {
51 dimension.tightenLowerBound(1)
52 }
53 }
54 }
55
56 val step3Matches = step3.matches
57 for (m : step3Matches) {
58 val dimension = getDimension(m.match)
59 objectiveBuilder.add(step3.weight, dimension)
60 dimension.tightenLowerBound(0)
61 if (!m.multi) {
62 dimension.tightenUpperBound(1)
63 if (m.must) {
64 dimension.tightenLowerBound(1)
65 }
66 }
67 }
68 for (pair : step3Matches.groupBy[step2.projectMayMatch(match, 2)].entrySet) {
69 val multiplicityBuilder = createBuilder
70 for (m : pair.value) {
71 multiplicityBuilder.add(1, m.match)
72 }
73 multiplicityBuilder.add(-1, pair.key)
74 multiplicityBuilder.build.assertEqualsTo(0)
75 }
76 boundLimit(step3Matches, 2, trainType, 1)
77 boundLimit(step3Matches, 3, segmentType, 1)
78
79 val step4Matches = step4.matches
80 for (m : step4Matches) {
81 val dimension = getDimension(m.match)
82 objectiveBuilder.add(step4.weight, dimension)
83 dimension.tightenLowerBound(0)
84 if (!m.multi) {
85 dimension.tightenUpperBound(1)
86 if (m.must) {
87 dimension.tightenLowerBound(1)
88 }
89 }
90 }
91 for (pair : step4Matches.groupBy[step3.projectMayMatch(match, 2, 3)].entrySet) {
92 val multiplicityBuilder = createBuilder
93 for (m : pair.value) {
94 multiplicityBuilder.add(1, m.match)
95 }
96 multiplicityBuilder.add(-2, pair.key)
97 multiplicityBuilder.build.tightenUpperBound(0)
98 }
99 boundLimit(step4Matches, 2, trainType, 2)
100 boundLimit(step4Matches, 3, segmentType, 2)
101 boundLimit(step4Matches, 4, segmentType, 2)
102
103 val step5Matches = step5.matches
104 for (m : step5Matches) {
105 val dimension = getDimension(m.match)
106 objectiveBuilder.add(step5.weight, dimension)
107 dimension.tightenLowerBound(0)
108 if (!m.multi) {
109 dimension.tightenUpperBound(1)
110 if (m.must) {
111 dimension.tightenLowerBound(1)
112 }
113 }
114 createBuilder.add(1, m.match).add(-1, step4.projectMayMatch(m.match, 2, 3, 4)).build.tightenUpperBound(0)
115 }
116 boundLimit(step5Matches, 2, trainType, 1)
117 boundLimit(step5Matches, 3, segmentType, 2)
118 boundLimit(step5Matches, 4, segmentType, 1)
119
120 objectiveBuilder.buildWithBounds
121 ]
122 }
123
124 private static def boundLimit(extension ExtendedLinearExpressionBuilderFactory factory,
125 Collection<CostElementMatch> matches, int index, Type type, int count) {
126 for (pair : matches.groupBy[match.get(index)].entrySet) {
127 val multiplicityBuilder = createBuilder
128 for (m : pair.value) {
129 multiplicityBuilder.add(1, m.match)
130 }
131 if (CostElementMatchers.isMulti(pair.key)) {
132 multiplicityBuilder.add(-count, type)
133 multiplicityBuilder.build.tightenUpperBound(0)
134 } else {
135 multiplicityBuilder.build.tightenUpperBound(count)
136 }
137 }
138 }
139} \ No newline at end of file