aboutsummaryrefslogtreecommitdiffstats
path: root/Domains/ca.mcgill.rtgmrt.example.modes3/src/modes3/run/CloseTrainsObjectiveHint.xtend
blob: 519a228a393bf62bdda12a299ae3f89d636e4b28 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package modes3.run

import hu.bme.mit.inf.dslreasoner.ecore2logic.Ecore2Logic
import hu.bme.mit.inf.dslreasoner.ecore2logic.Ecore2Logic_Trace
import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.Type
import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.cardinality.ExtendedLinearExpressionBuilderFactory
import hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.optimization.CostElementMatch
import hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.optimization.CostElementMatchers
import hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.optimization.CostObjectiveHint
import java.util.Collection
import java.util.Map
import modes3.Modes3Package
import modes3.queries.CloseTrains_step_2
import modes3.queries.CloseTrains_step_3
import modes3.queries.CloseTrains_step_4
import modes3.queries.CloseTrains_step_5
import modes3.queries.CloseTrains_step_6
import modes3.queries.CloseTrains_step_7

class CloseTrainsObjectiveHint extends CostObjectiveHint {
	val Type segmentType
	val Type trainType

	new(extension Ecore2Logic ecore2Logic, Ecore2Logic_Trace ecore2LogicTrace) {
		extension val Modes3Package = Modes3Package.eINSTANCE
		segmentType = ecore2LogicTrace.TypeofEClass(segment)
		trainType = ecore2LogicTrace.TypeofEClass(train)
	}

	override isExact() {
		true
	}

	override createPolyhedronExtensionOperator(Map<String, CostElementMatchers> costElementMatchers) {
		val step2 = costElementMatchers.get(CloseTrains_step_2.instance.fullyQualifiedName)
		val step3 = costElementMatchers.get(CloseTrains_step_3.instance.fullyQualifiedName)
		val step4 = costElementMatchers.get(CloseTrains_step_4.instance.fullyQualifiedName)
		val step5 = costElementMatchers.get(CloseTrains_step_5.instance.fullyQualifiedName)
		val step6 = costElementMatchers.get(CloseTrains_step_6.instance.fullyQualifiedName)
		val step7 = costElementMatchers.get(CloseTrains_step_7.instance.fullyQualifiedName);

		[
			val objectiveBuilder = createBuilder

			for (m : step2.matches) {
				val dimension = getDimension(m.match)
				objectiveBuilder.add(step2.weight, dimension)
				dimension.tightenLowerBound(0)
				if (m.multi) {
					createBuilder.add(1, dimension).add(-1, trainType).build.assertEqualsTo(0)
				} else {
					dimension.tightenUpperBound(1)
					if (m.must) {
						dimension.tightenLowerBound(1)
					}
				}
			}

			val step3Matches = step3.matches
			for (m : step3Matches) {
				val dimension = getDimension(m.match)
				objectiveBuilder.add(step3.weight, dimension)
				dimension.tightenLowerBound(0)
				if (!m.multi) {
					dimension.tightenUpperBound(1)
					if (m.must) {
						dimension.tightenLowerBound(1)
					}
				}
			}
			for (pair : step3Matches.groupBy[step2.projectMayMatch(match, 2)].entrySet) {
				val multiplicityBuilder = createBuilder
				for (m : pair.value) {
					multiplicityBuilder.add(1, m.match)
				}
				multiplicityBuilder.add(-1, pair.key)
				multiplicityBuilder.build.assertEqualsTo(0)
			}
			boundLimit(step3Matches, 2, trainType, 1)
			boundLimit(step3Matches, 3, segmentType, 1)

			val step4Matches = step4.matches
			for (m : step4Matches) {
				val dimension = getDimension(m.match)
				objectiveBuilder.add(step4.weight, dimension)
				dimension.tightenLowerBound(0)
				if (!m.multi) {
					dimension.tightenUpperBound(1)
					if (m.must) {
						dimension.tightenLowerBound(1)
					}
				}
			}
			for (pair : step4Matches.groupBy[step3.projectMayMatch(match, 2, 3)].entrySet) {
				val multiplicityBuilder = createBuilder
				for (m : pair.value) {
					multiplicityBuilder.add(1, m.match)
				}
				multiplicityBuilder.add(-2, pair.key)
				multiplicityBuilder.build.tightenUpperBound(0)
			}
			boundLimit(step4Matches, 2, trainType, 2)
			boundLimit(step4Matches, 3, segmentType, 2)
			boundLimit(step4Matches, 4, segmentType, 2)

			val step5Matches = step5.matches
			for (m : step5Matches) {
				val dimension = getDimension(m.match)
				objectiveBuilder.add(step5.weight, dimension)
				dimension.tightenLowerBound(0)
				if (!m.multi) {
					dimension.tightenUpperBound(1)
					if (m.must) {
						dimension.tightenLowerBound(1)
					}
				}
			}
			for (pair : step5Matches.groupBy[step4.projectMayMatch(match, 2, 3, 4)].entrySet) {
				val multiplicityBuilder = createBuilder
				for (m : pair.value) {
					multiplicityBuilder.add(1, m.match)
				}
				multiplicityBuilder.add(-2, pair.key)
				multiplicityBuilder.build.tightenUpperBound(0)
			}
			boundLimit(step5Matches, 2, trainType, 4)
			boundLimit(step5Matches, 3, segmentType, 4)
			boundLimit(step5Matches, 4, segmentType, 4)
			boundLimit(step5Matches, 5, segmentType, 4)

			val step6Matches = step6.matches
			for (m : step6Matches) {
				val dimension = getDimension(m.match)
				objectiveBuilder.add(step6.weight, dimension)
				dimension.tightenLowerBound(0)
				if (m.multi) {
					if (m.match.get(3) == m.match.get(5)) {
						createBuilder.add(2, m.match).add(-1, step5.projectMayMatch(m.match, 2, 3, 4, 5)).build.
							assertEqualsTo(0)
					} else {
						createBuilder.add(1, m.match).add(-1, step5.projectMayMatch(m.match, 2, 3, 4, 5)).build.
							assertEqualsTo(0)
					}
				} else {
					dimension.tightenUpperBound(1)
					if (m.must) {
						dimension.tightenLowerBound(1)
					}
				}
			}
			boundLimit(step6Matches, 2, trainType, 2)
			boundLimit(step6Matches, 3, segmentType, 2)
			boundLimit(step6Matches, 4, segmentType, 2)
			boundLimit(step6Matches, 5, segmentType, 2)

			val step7Matches = step7.matches
			for (m : step7Matches) {
				val dimension = getDimension(m.match)
				objectiveBuilder.add(step7.weight, dimension)
				dimension.tightenLowerBound(0)
				if (!m.multi) {
					dimension.tightenUpperBound(1)
					if (m.must) {
						dimension.tightenLowerBound(1)
					}
				}
			}
			for (pair : step7Matches.groupBy[step6.projectMayMatch(match, 2, 3, 4, 5)].entrySet) {
				val multiplicityBuilder = createBuilder
				for (m : pair.value) {
					multiplicityBuilder.add(1, m.match)
				}
				multiplicityBuilder.add(-1, pair.key)
				multiplicityBuilder.build.tightenUpperBound(0)
			}
			boundLimit(step7Matches, 2, trainType, 2)
			boundLimit(step7Matches, 3, segmentType, 2)
			boundLimit(step7Matches, 4, segmentType, 2)
			boundLimit(step7Matches, 5, segmentType, 2)
			boundLimit(step7Matches, 6, trainType, 2)

			objectiveBuilder.buildWithBounds
		]
	}

	private static def boundLimit(extension ExtendedLinearExpressionBuilderFactory factory,
		Collection<CostElementMatch> matches, int index, Type type, int count) {
		for (pair : matches.groupBy[match.get(index)].entrySet) {
			val multiplicityBuilder = createBuilder
			for (m : pair.value) {
				multiplicityBuilder.add(1, m.match)
			}
			if (CostElementMatchers.isMulti(pair.key)) {
				multiplicityBuilder.add(-count, type)
				multiplicityBuilder.build.tightenUpperBound(0)
			} else {
				multiplicityBuilder.build.tightenUpperBound(count)
			}
		}
	}
}