aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/store-reasoning-scope/src/main/java/tools/refinery/store/reasoning/scope/BoundScopePropagator.java
blob: 3ae4d84bdfb8f63664910c6e2c342ebb5c7240e2 (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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
 * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/>
 *
 * SPDX-License-Identifier: EPL-2.0
 */
package tools.refinery.store.reasoning.scope;

import com.google.ortools.linearsolver.MPConstraint;
import com.google.ortools.linearsolver.MPObjective;
import com.google.ortools.linearsolver.MPSolver;
import com.google.ortools.linearsolver.MPVariable;
import org.eclipse.collections.api.factory.primitive.IntObjectMaps;
import org.eclipse.collections.api.factory.primitive.IntSets;
import org.eclipse.collections.api.map.primitive.MutableIntObjectMap;
import org.eclipse.collections.api.set.primitive.MutableIntSet;
import tools.refinery.store.dse.propagation.BoundPropagator;
import tools.refinery.store.dse.propagation.PropagationResult;
import tools.refinery.store.model.Interpretation;
import tools.refinery.store.model.Model;
import tools.refinery.store.query.ModelQueryAdapter;
import tools.refinery.store.representation.cardinality.*;
import tools.refinery.store.tuple.Tuple;

class BoundScopePropagator implements BoundPropagator {
	private final Model model;
	private final ModelQueryAdapter queryEngine;
	private final Interpretation<CardinalityInterval> countInterpretation;
	private final MPSolver solver;
	private final MPObjective objective;
	private final MutableIntObjectMap<MPVariable> variables = IntObjectMaps.mutable.empty();
	private final MutableIntSet activeVariables = IntSets.mutable.empty();
	private final TypeScopePropagator[] propagators;
	private boolean changed = true;

	public BoundScopePropagator(Model model, ScopePropagator storeAdapter) {
		this.model = model;
		queryEngine = model.getAdapter(ModelQueryAdapter.class);
		countInterpretation = model.getInterpretation(storeAdapter.getCountSymbol());
		solver = MPSolver.createSolver("GLOP");
		objective = solver.objective();
		initializeVariables();
		countInterpretation.addListener(this::countChanged, true);
		var propagatorFactories = storeAdapter.getTypeScopePropagatorFactories();
		propagators = new TypeScopePropagator[propagatorFactories.size()];
		for (int i = 0; i < propagators.length; i++) {
			model.checkCancelled();
			propagators[i] = propagatorFactories.get(i).createPropagator(this);
		}
	}

	ModelQueryAdapter getQueryEngine() {
		return queryEngine;
	}

	private void initializeVariables() {
		var cursor = countInterpretation.getAll();
		while (cursor.move()) {
			var interval = cursor.getValue();
			if (!interval.equals(CardinalityIntervals.ONE)) {
				int nodeId = cursor.getKey().get(0);
				createVariable(nodeId, interval);
				activeVariables.add(nodeId);
			}
		}
	}

	private MPVariable createVariable(int nodeId, CardinalityInterval interval) {
		double lowerBound = interval.lowerBound();
		double upperBound = getUpperBound(interval);
		var variable = solver.makeNumVar(lowerBound, upperBound, "x" + nodeId);
		variables.put(nodeId, variable);
		return variable;
	}

	private void countChanged(Tuple key, CardinalityInterval fromValue, CardinalityInterval toValue,
							  boolean ignoredRestoring) {
		int nodeId = key.get(0);
		if ((toValue == null || toValue.equals(CardinalityIntervals.ONE))) {
			if (fromValue != null && !fromValue.equals(CardinalityIntervals.ONE)) {
				removeActiveVariable(toValue, nodeId);
			}
			return;
		}
		if (fromValue == null || fromValue.equals(CardinalityIntervals.ONE)) {
			activeVariables.add(nodeId);
		}
		var variable = variables.get(nodeId);
		if (variable == null) {
			createVariable(nodeId, toValue);
			markAsChanged();
			return;
		}
		double lowerBound = toValue.lowerBound();
		double upperBound = getUpperBound(toValue);
		if (variable.lb() != lowerBound) {
			variable.setLb(lowerBound);
			markAsChanged();
		}
		if (variable.ub() != upperBound) {
			variable.setUb(upperBound);
			markAsChanged();
		}
	}

	private void removeActiveVariable(CardinalityInterval toValue, int nodeId) {
		var variable = variables.get(nodeId);
		if (variable == null || !activeVariables.remove(nodeId)) {
			throw new AssertionError("Variable not active: " + nodeId);
		}
		if (toValue == null) {
			variable.setBounds(0, 0);
		} else {
			// Until queries are flushed and the constraints can be properly updated,
			// the variable corresponding to the (previous) multi-object has to stand in for a single object.
			variable.setBounds(1, 1);
		}
		markAsChanged();
	}

	MPConstraint makeConstraint() {
		return solver.makeConstraint();
	}

	MPVariable getVariable(int nodeId) {
		var variable = variables.get(nodeId);
		if (variable != null) {
			return variable;
		}
		var interval = countInterpretation.get(Tuple.of(nodeId));
		if (interval == null || interval.equals(CardinalityIntervals.ONE)) {
			interval = CardinalityIntervals.NONE;
		} else {
			activeVariables.add(nodeId);
			markAsChanged();
		}
		return createVariable(nodeId, interval);
	}

	void markAsChanged() {
		changed = true;
	}

	@Override
	public PropagationResult propagateOne() {
		queryEngine.flushChanges();
		if (!changed) {
			return PropagationResult.UNCHANGED;
		}
		changed = false;
		for (var propagator : propagators) {
			model.checkCancelled();
			propagator.updateBounds();
		}
		var result = PropagationResult.UNCHANGED;
		if (activeVariables.isEmpty()) {
			return checkEmptiness();
		}
		var iterator = activeVariables.intIterator();
		while (iterator.hasNext()) {
			int nodeId = iterator.next();
			var variable = variables.get(nodeId);
			if (variable == null) {
				throw new AssertionError("Missing active variable: " + nodeId);
			}
			result = result.andThen(propagateNode(nodeId, variable));
			if (result.isRejected()) {
				return result;
			}
		}
		return result;
	}

	private PropagationResult checkEmptiness() {
		model.checkCancelled();
		var emptinessCheckingResult = solver.solve();
		return switch (emptinessCheckingResult) {
			case OPTIMAL, UNBOUNDED -> PropagationResult.UNCHANGED;
			case INFEASIBLE -> PropagationResult.REJECTED;
			default -> throw new IllegalStateException("Failed to check for consistency: " + emptinessCheckingResult);
		};
	}

	private PropagationResult propagateNode(int nodeId, MPVariable variable) {
		objective.setCoefficient(variable, 1);
		try {
			model.checkCancelled();
			objective.setMinimization();
			var minimizationResult = solver.solve();
			int lowerBound;
			switch (minimizationResult) {
			case OPTIMAL -> lowerBound = RoundingUtil.roundUp(objective.value());
			case UNBOUNDED -> lowerBound = 0;
			case INFEASIBLE -> {
				return PropagationResult.REJECTED;
			}
			default -> throw new IllegalStateException("Failed to solve for minimum of %s: %s"
					.formatted(variable, minimizationResult));
			}

			model.checkCancelled();
			objective.setMaximization();
			var maximizationResult = solver.solve();
			UpperCardinality upperBound;
			switch (maximizationResult) {
			case OPTIMAL -> upperBound = UpperCardinalities.atMost(RoundingUtil.roundDown(objective.value()));
			// Problem was feasible when minimizing, the only possible source of {@code UNBOUNDED_OR_INFEASIBLE} is
			// an unbounded maximization problem. See https://github.com/google/or-tools/issues/3319
			case UNBOUNDED, INFEASIBLE -> upperBound = UpperCardinalities.UNBOUNDED;
			default -> throw new IllegalStateException("Failed to solve for maximum of %s: %s"
					.formatted(variable, minimizationResult));
			}

			var newInterval = CardinalityIntervals.between(lowerBound, upperBound);
			var oldInterval = countInterpretation.put(Tuple.of(nodeId), newInterval);
			if (newInterval.lowerBound() < oldInterval.lowerBound() ||
					newInterval.upperBound().compareTo(oldInterval.upperBound()) > 0) {
				throw new IllegalArgumentException("Failed to refine multiplicity %s of node %d to %s"
						.formatted(oldInterval, nodeId, newInterval));
			}
			return newInterval.equals(oldInterval) ? PropagationResult.UNCHANGED : PropagationResult.PROPAGATED;
		} finally {
			objective.setCoefficient(variable, 0);
		}
	}

	private static double getUpperBound(CardinalityInterval interval) {
		var upperBound = interval.upperBound();
		if (upperBound instanceof FiniteUpperCardinality finiteUpperCardinality) {
			return finiteUpperCardinality.finiteUpperBound();
		} else if (upperBound instanceof UnboundedUpperCardinality) {
			return Double.POSITIVE_INFINITY;
		} else {
			throw new IllegalArgumentException("Unknown upper bound: " + upperBound);
		}
	}
}