diff options
author | Kristóf Marussy <marussy@mit.bme.hu> | 2020-11-02 02:02:40 +0100 |
---|---|---|
committer | Kristóf Marussy <marussy@mit.bme.hu> | 2020-11-02 02:02:40 +0100 |
commit | f06427cd7375551582461f91b3458339a8227f9b (patch) | |
tree | 97bc6ec85f4c384e5080a6611b492caf460b6ce9 /Domains/ca.mcgill.rtgmrt.example.modes3/src/modes3/run/CloseTrainsObjectiveHint.xtend | |
parent | Must unit propagation (diff) | |
download | VIATRA-Generator-f06427cd7375551582461f91b3458339a8227f9b.tar.gz VIATRA-Generator-f06427cd7375551582461f91b3458339a8227f9b.tar.zst VIATRA-Generator-f06427cd7375551582461f91b3458339a8227f9b.zip |
Optimizing generator with linear objective functions
Diffstat (limited to 'Domains/ca.mcgill.rtgmrt.example.modes3/src/modes3/run/CloseTrainsObjectiveHint.xtend')
-rw-r--r-- | Domains/ca.mcgill.rtgmrt.example.modes3/src/modes3/run/CloseTrainsObjectiveHint.xtend | 201 |
1 files changed, 201 insertions, 0 deletions
diff --git a/Domains/ca.mcgill.rtgmrt.example.modes3/src/modes3/run/CloseTrainsObjectiveHint.xtend b/Domains/ca.mcgill.rtgmrt.example.modes3/src/modes3/run/CloseTrainsObjectiveHint.xtend new file mode 100644 index 00000000..519a228a --- /dev/null +++ b/Domains/ca.mcgill.rtgmrt.example.modes3/src/modes3/run/CloseTrainsObjectiveHint.xtend | |||
@@ -0,0 +1,201 @@ | |||
1 | package modes3.run | ||
2 | |||
3 | import hu.bme.mit.inf.dslreasoner.ecore2logic.Ecore2Logic | ||
4 | import hu.bme.mit.inf.dslreasoner.ecore2logic.Ecore2Logic_Trace | ||
5 | import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.Type | ||
6 | import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.cardinality.ExtendedLinearExpressionBuilderFactory | ||
7 | import hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.optimization.CostElementMatch | ||
8 | import hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.optimization.CostElementMatchers | ||
9 | import hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.optimization.CostObjectiveHint | ||
10 | import java.util.Collection | ||
11 | import java.util.Map | ||
12 | import modes3.Modes3Package | ||
13 | import modes3.queries.CloseTrains_step_2 | ||
14 | import modes3.queries.CloseTrains_step_3 | ||
15 | import modes3.queries.CloseTrains_step_4 | ||
16 | import modes3.queries.CloseTrains_step_5 | ||
17 | import modes3.queries.CloseTrains_step_6 | ||
18 | import modes3.queries.CloseTrains_step_7 | ||
19 | |||
20 | class CloseTrainsObjectiveHint extends CostObjectiveHint { | ||
21 | val Type segmentType | ||
22 | val Type trainType | ||
23 | |||
24 | new(extension Ecore2Logic ecore2Logic, Ecore2Logic_Trace ecore2LogicTrace) { | ||
25 | extension val Modes3Package = Modes3Package.eINSTANCE | ||
26 | segmentType = ecore2LogicTrace.TypeofEClass(segment) | ||
27 | trainType = ecore2LogicTrace.TypeofEClass(train) | ||
28 | } | ||
29 | |||
30 | override isExact() { | ||
31 | true | ||
32 | } | ||
33 | |||
34 | override createPolyhedronExtensionOperator(Map<String, CostElementMatchers> costElementMatchers) { | ||
35 | val step2 = costElementMatchers.get(CloseTrains_step_2.instance.fullyQualifiedName) | ||
36 | val step3 = costElementMatchers.get(CloseTrains_step_3.instance.fullyQualifiedName) | ||
37 | val step4 = costElementMatchers.get(CloseTrains_step_4.instance.fullyQualifiedName) | ||
38 | val step5 = costElementMatchers.get(CloseTrains_step_5.instance.fullyQualifiedName) | ||
39 | val step6 = costElementMatchers.get(CloseTrains_step_6.instance.fullyQualifiedName) | ||
40 | val step7 = costElementMatchers.get(CloseTrains_step_7.instance.fullyQualifiedName); | ||
41 | |||
42 | [ | ||
43 | val objectiveBuilder = createBuilder | ||
44 | |||
45 | for (m : step2.matches) { | ||
46 | val dimension = getDimension(m.match) | ||
47 | objectiveBuilder.add(step2.weight, dimension) | ||
48 | dimension.tightenLowerBound(0) | ||
49 | if (m.multi) { | ||
50 | createBuilder.add(1, dimension).add(-1, trainType).build.assertEqualsTo(0) | ||
51 | } else { | ||
52 | dimension.tightenUpperBound(1) | ||
53 | if (m.must) { | ||
54 | dimension.tightenLowerBound(1) | ||
55 | } | ||
56 | } | ||
57 | } | ||
58 | |||
59 | val step3Matches = step3.matches | ||
60 | for (m : step3Matches) { | ||
61 | val dimension = getDimension(m.match) | ||
62 | objectiveBuilder.add(step3.weight, dimension) | ||
63 | dimension.tightenLowerBound(0) | ||
64 | if (!m.multi) { | ||
65 | dimension.tightenUpperBound(1) | ||
66 | if (m.must) { | ||
67 | dimension.tightenLowerBound(1) | ||
68 | } | ||
69 | } | ||
70 | } | ||
71 | for (pair : step3Matches.groupBy[step2.projectMayMatch(match, 2)].entrySet) { | ||
72 | val multiplicityBuilder = createBuilder | ||
73 | for (m : pair.value) { | ||
74 | multiplicityBuilder.add(1, m.match) | ||
75 | } | ||
76 | multiplicityBuilder.add(-1, pair.key) | ||
77 | multiplicityBuilder.build.assertEqualsTo(0) | ||
78 | } | ||
79 | boundLimit(step3Matches, 2, trainType, 1) | ||
80 | boundLimit(step3Matches, 3, segmentType, 1) | ||
81 | |||
82 | val step4Matches = step4.matches | ||
83 | for (m : step4Matches) { | ||
84 | val dimension = getDimension(m.match) | ||
85 | objectiveBuilder.add(step4.weight, dimension) | ||
86 | dimension.tightenLowerBound(0) | ||
87 | if (!m.multi) { | ||
88 | dimension.tightenUpperBound(1) | ||
89 | if (m.must) { | ||
90 | dimension.tightenLowerBound(1) | ||
91 | } | ||
92 | } | ||
93 | } | ||
94 | for (pair : step4Matches.groupBy[step3.projectMayMatch(match, 2, 3)].entrySet) { | ||
95 | val multiplicityBuilder = createBuilder | ||
96 | for (m : pair.value) { | ||
97 | multiplicityBuilder.add(1, m.match) | ||
98 | } | ||
99 | multiplicityBuilder.add(-2, pair.key) | ||
100 | multiplicityBuilder.build.tightenUpperBound(0) | ||
101 | } | ||
102 | boundLimit(step4Matches, 2, trainType, 2) | ||
103 | boundLimit(step4Matches, 3, segmentType, 2) | ||
104 | boundLimit(step4Matches, 4, segmentType, 2) | ||
105 | |||
106 | val step5Matches = step5.matches | ||
107 | for (m : step5Matches) { | ||
108 | val dimension = getDimension(m.match) | ||
109 | objectiveBuilder.add(step5.weight, dimension) | ||
110 | dimension.tightenLowerBound(0) | ||
111 | if (!m.multi) { | ||
112 | dimension.tightenUpperBound(1) | ||
113 | if (m.must) { | ||
114 | dimension.tightenLowerBound(1) | ||
115 | } | ||
116 | } | ||
117 | } | ||
118 | for (pair : step5Matches.groupBy[step4.projectMayMatch(match, 2, 3, 4)].entrySet) { | ||
119 | val multiplicityBuilder = createBuilder | ||
120 | for (m : pair.value) { | ||
121 | multiplicityBuilder.add(1, m.match) | ||
122 | } | ||
123 | multiplicityBuilder.add(-2, pair.key) | ||
124 | multiplicityBuilder.build.tightenUpperBound(0) | ||
125 | } | ||
126 | boundLimit(step5Matches, 2, trainType, 4) | ||
127 | boundLimit(step5Matches, 3, segmentType, 4) | ||
128 | boundLimit(step5Matches, 4, segmentType, 4) | ||
129 | boundLimit(step5Matches, 5, segmentType, 4) | ||
130 | |||
131 | val step6Matches = step6.matches | ||
132 | for (m : step6Matches) { | ||
133 | val dimension = getDimension(m.match) | ||
134 | objectiveBuilder.add(step6.weight, dimension) | ||
135 | dimension.tightenLowerBound(0) | ||
136 | if (m.multi) { | ||
137 | if (m.match.get(3) == m.match.get(5)) { | ||
138 | createBuilder.add(2, m.match).add(-1, step5.projectMayMatch(m.match, 2, 3, 4, 5)).build. | ||
139 | assertEqualsTo(0) | ||
140 | } else { | ||
141 | createBuilder.add(1, m.match).add(-1, step5.projectMayMatch(m.match, 2, 3, 4, 5)).build. | ||
142 | assertEqualsTo(0) | ||
143 | } | ||
144 | } else { | ||
145 | dimension.tightenUpperBound(1) | ||
146 | if (m.must) { | ||
147 | dimension.tightenLowerBound(1) | ||
148 | } | ||
149 | } | ||
150 | } | ||
151 | boundLimit(step6Matches, 2, trainType, 2) | ||
152 | boundLimit(step6Matches, 3, segmentType, 2) | ||
153 | boundLimit(step6Matches, 4, segmentType, 2) | ||
154 | boundLimit(step6Matches, 5, segmentType, 2) | ||
155 | |||
156 | val step7Matches = step7.matches | ||
157 | for (m : step7Matches) { | ||
158 | val dimension = getDimension(m.match) | ||
159 | objectiveBuilder.add(step7.weight, dimension) | ||
160 | dimension.tightenLowerBound(0) | ||
161 | if (!m.multi) { | ||
162 | dimension.tightenUpperBound(1) | ||
163 | if (m.must) { | ||
164 | dimension.tightenLowerBound(1) | ||
165 | } | ||
166 | } | ||
167 | } | ||
168 | for (pair : step7Matches.groupBy[step6.projectMayMatch(match, 2, 3, 4, 5)].entrySet) { | ||
169 | val multiplicityBuilder = createBuilder | ||
170 | for (m : pair.value) { | ||
171 | multiplicityBuilder.add(1, m.match) | ||
172 | } | ||
173 | multiplicityBuilder.add(-1, pair.key) | ||
174 | multiplicityBuilder.build.tightenUpperBound(0) | ||
175 | } | ||
176 | boundLimit(step7Matches, 2, trainType, 2) | ||
177 | boundLimit(step7Matches, 3, segmentType, 2) | ||
178 | boundLimit(step7Matches, 4, segmentType, 2) | ||
179 | boundLimit(step7Matches, 5, segmentType, 2) | ||
180 | boundLimit(step7Matches, 6, trainType, 2) | ||
181 | |||
182 | objectiveBuilder.buildWithBounds | ||
183 | ] | ||
184 | } | ||
185 | |||
186 | private static def boundLimit(extension ExtendedLinearExpressionBuilderFactory factory, | ||
187 | Collection<CostElementMatch> matches, int index, Type type, int count) { | ||
188 | for (pair : matches.groupBy[match.get(index)].entrySet) { | ||
189 | val multiplicityBuilder = createBuilder | ||
190 | for (m : pair.value) { | ||
191 | multiplicityBuilder.add(1, m.match) | ||
192 | } | ||
193 | if (CostElementMatchers.isMulti(pair.key)) { | ||
194 | multiplicityBuilder.add(-count, type) | ||
195 | multiplicityBuilder.build.tightenUpperBound(0) | ||
196 | } else { | ||
197 | multiplicityBuilder.build.tightenUpperBound(count) | ||
198 | } | ||
199 | } | ||
200 | } | ||
201 | } | ||