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/TrainLocationsObjectiveHint.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/TrainLocationsObjectiveHint.xtend')
-rw-r--r-- | Domains/ca.mcgill.rtgmrt.example.modes3/src/modes3/run/TrainLocationsObjectiveHint.xtend | 85 |
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 @@ | |||
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.TrainLocations_step_2 | ||
14 | import modes3.queries.TrainLocations_step_3 | ||
15 | |||
16 | class 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 | ||