huub 100.0.0

CP+SAT solver framework built to be reliable, performant, and extensible
Documentation
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
//! Initialization context used when model constraints subscribe to events.

use crate::{
	IntSet, IntVal,
	actions::{
		BoolInitActions, BoolInspectionActions, InitActions, IntInitActions, IntInspectionActions,
		IntPropCond, ReasoningContext, ReasoningEngine,
	},
	model::{
		AdvRef, Advisor, ConRef, Decision, Model,
		resolved::Resolved,
		view::{View, boolean::BoolView, integer::IntView},
	},
	solver::{IntLitMeaning, activation_list::ActivationAction, queue::PriorityLevel},
};

/// Wrapper around [`Model`] that knows the constraint being
/// initialized.
#[derive(Debug)]
pub struct ModelInitContext<'a> {
	/// Index of the constraint being initialized.
	con: ConRef,
	/// Reference to the Model in which the constraint exists.
	model: &'a mut Model,
	/// The priority level at which the constraint will be enqueued.
	pub(crate) priority: PriorityLevel,
	/// Whether the subscriptions of the propagator would suggest the propagator
	/// should be enqueued.
	semantic_enqueue: bool,
	/// Whether the propagator explicitly requested to be enqueued or not
	/// enqueued.
	decision_enqueue: Option<bool>,
}

impl BoolInitActions<ModelInitContext<'_>> for Decision<bool> {
	fn advise_when_fixed(&self, ctx: &mut ModelInitContext<'_>, data: u64) {
		self.resolve_alias(ctx.model).advise_when_fixed(ctx, data);
	}

	fn enqueue_when_fixed(&self, ctx: &mut ModelInitContext<'_>) {
		self.resolve_alias(ctx.model).enqueue_when_fixed(ctx);
	}
}

impl BoolInspectionActions<ModelInitContext<'_>> for Decision<bool> {
	fn val(&self, ctx: &ModelInitContext<'_>) -> Option<bool> {
		self.val(ctx.model)
	}
}

impl IntInitActions<ModelInitContext<'_>> for Decision<IntVal> {
	fn advise_when(&self, ctx: &mut ModelInitContext<'_>, cond: IntPropCond, data: u64) {
		self.resolve_alias(ctx.model).advise_when(ctx, cond, data);
	}

	fn enqueue_when(&self, ctx: &mut ModelInitContext<'_>, condition: IntPropCond) {
		self.resolve_alias(ctx.model).enqueue_when(ctx, condition);
	}
}

impl IntInspectionActions<ModelInitContext<'_>> for Decision<IntVal> {
	fn bounds(&self, ctx: &ModelInitContext<'_>) -> (IntVal, IntVal) {
		self.bounds(ctx.model)
	}

	fn domain(&self, ctx: &ModelInitContext<'_>) -> IntSet {
		self.domain(ctx.model)
	}

	fn in_domain(&self, ctx: &ModelInitContext<'_>, val: IntVal) -> bool {
		self.in_domain(ctx.model, val)
	}

	fn lit_meaning(&self, ctx: &ModelInitContext<'_>, lit: View<bool>) -> Option<IntLitMeaning> {
		self.lit_meaning(ctx.model, lit)
	}

	fn max(&self, ctx: &ModelInitContext<'_>) -> IntVal {
		self.max(ctx.model)
	}

	fn max_lit(&self, ctx: &ModelInitContext<'_>) -> View<bool> {
		self.max_lit(ctx.model)
	}

	fn min(&self, ctx: &ModelInitContext<'_>) -> IntVal {
		self.min(ctx.model)
	}

	fn min_lit(&self, ctx: &ModelInitContext<'_>) -> View<bool> {
		self.min_lit(ctx.model)
	}

	fn try_lit(&self, ctx: &ModelInitContext<'_>, meaning: IntLitMeaning) -> Option<View<bool>> {
		self.try_lit(ctx.model, meaning)
	}

	fn val(&self, ctx: &ModelInitContext<'_>) -> Option<IntVal> {
		self.val(ctx.model)
	}
}

impl IntInitActions<ModelInitContext<'_>> for IntVal {
	fn advise_when(&self, _: &mut ModelInitContext<'_>, _: IntPropCond, _: u64) {
		// Value will never change, so no advisor will ever be called
	}

	fn enqueue_when(&self, ctx: &mut ModelInitContext<'_>, _: IntPropCond) {
		ctx.semantic_enqueue();
	}
}

impl<'a> ModelInitContext<'a> {
	/// Returns whether to enqueue the propagator based on its explicit requests
	/// or otherwise the semantics of its subscriptions.
	pub(crate) fn enqueue(&self) -> bool {
		if let Some(enqueue) = self.decision_enqueue {
			enqueue
		} else {
			self.semantic_enqueue
		}
	}
	/// Creates a new [`ModelPostingContext`] for the given constraint
	/// reference.
	pub(crate) fn new(model: &'a mut Model, con: ConRef) -> Self {
		ModelInitContext {
			con,
			model,
			priority: PriorityLevel::Medium,
			semantic_enqueue: false,
			decision_enqueue: None,
		}
	}

	/// Mark that subscriptions imply the propagator should be enqueued now.
	pub(crate) fn semantic_enqueue(&mut self) {
		self.semantic_enqueue = true;
	}
}

impl InitActions for ModelInitContext<'_> {
	fn advise_on_backtrack(&mut self) {
		// Model does not backtrack, so no advisor is required.
	}

	fn enqueue_now(&mut self, option: bool) {
		self.decision_enqueue = Some(option);
	}

	fn set_priority(&mut self, priority: PriorityLevel) {
		self.priority = priority;
	}
}

impl ReasoningContext for ModelInitContext<'_> {
	type Atom = <Model as ReasoningEngine>::Atom;
	type Conflict = <Model as ReasoningEngine>::Conflict;
}

impl BoolInitActions<ModelInitContext<'_>> for Resolved<Decision<bool>> {
	fn advise_when_fixed(&self, ctx: &mut ModelInitContext<'_>, data: u64) {
		ctx.model.advisors.push(Advisor {
			con: ctx.con,
			data,
			negated: false,
			bool2int: false,
			condition: None,
		});
		let adv = AdvRef::new(ctx.model.advisors.len() - 1);
		ctx.model.bool_vars[self.0.idx()]
			.constraints
			.push(ActivationAction::Advise(adv).into());
	}

	fn enqueue_when_fixed(&self, ctx: &mut ModelInitContext<'_>) {
		ctx.model.bool_vars[self.0.idx()]
			.constraints
			.push(ActivationAction::Enqueue(ctx.con).into());
	}
}

impl BoolInitActions<ModelInitContext<'_>> for Resolved<View<bool>> {
	fn advise_when_fixed(&self, ctx: &mut ModelInitContext<'_>, data: u64) {
		let (iv, cond, event) = match self.0.0 {
			BoolView::Decision(lit) => return Resolved(lit).advise_when_fixed(ctx, data),
			BoolView::Const(_) => {
				// Value does not change, so no advisor will ever be called.
				return;
			}
			BoolView::IntEq(iv, v) => (iv, IntLitMeaning::Eq(v), IntPropCond::Domain),
			BoolView::IntGreaterEq(iv, v) => (iv, IntLitMeaning::GreaterEq(v), IntPropCond::Bounds),
			BoolView::IntLess(iv, v) => (iv, IntLitMeaning::Less(v), IntPropCond::Bounds),
			BoolView::IntNotEq(iv, v) => (iv, IntLitMeaning::NotEq(v), IntPropCond::Domain),
		};
		ctx.model.advisors.push(Advisor {
			con: ctx.con,
			data,
			negated: false,
			bool2int: false,
			condition: Some(cond),
		});
		let adv = AdvRef::new(ctx.model.advisors.len() - 1);
		ctx.model.int_vars[iv.idx()]
			.constraints
			.add(ActivationAction::Advise(adv), event);
	}

	fn enqueue_when_fixed(&self, ctx: &mut ModelInitContext<'_>) {
		match self.0.0 {
			BoolView::Decision(lit) => Resolved(lit).enqueue_when_fixed(ctx),
			BoolView::Const(_) => ctx.semantic_enqueue(),
			// TODO: These definitions might enqueue when the boolean is not fixed. Use advisors
			// instead?
			BoolView::IntEq(iv, _) | BoolView::IntNotEq(iv, _) => {
				iv.enqueue_when(ctx, IntPropCond::Domain);
			}
			BoolView::IntGreaterEq(iv, _) | BoolView::IntLess(iv, _) => {
				iv.enqueue_when(ctx, IntPropCond::Bounds);
			}
		}
	}
}

impl BoolInspectionActions<ModelInitContext<'_>> for Resolved<Decision<bool>> {
	fn val(&self, ctx: &ModelInitContext<'_>) -> Option<bool> {
		self.0.val(ctx.model)
	}
}

impl BoolInspectionActions<ModelInitContext<'_>> for Resolved<View<bool>> {
	fn val(&self, ctx: &ModelInitContext<'_>) -> Option<bool> {
		self.val(ctx.model)
	}
}

impl IntInitActions<ModelInitContext<'_>> for Resolved<Decision<IntVal>> {
	fn advise_when(&self, ctx: &mut ModelInitContext<'_>, cond: IntPropCond, data: u64) {
		ctx.model.advisors.push(Advisor {
			con: ctx.con,
			data,
			negated: false,
			bool2int: false,
			condition: None,
		});
		let adv = AdvRef::new(ctx.model.advisors.len() - 1);
		ctx.model.int_vars[self.idx()]
			.constraints
			.add(ActivationAction::Advise(adv), cond);
	}

	fn enqueue_when(&self, ctx: &mut ModelInitContext<'_>, condition: IntPropCond) {
		if condition != IntPropCond::Fixed {
			ctx.semantic_enqueue();
		}
		ctx.model.int_vars[self.idx()]
			.constraints
			.add(ActivationAction::Enqueue(ctx.con), condition);
	}
}

impl IntInitActions<ModelInitContext<'_>> for Resolved<View<IntVal>> {
	fn advise_when(&self, ctx: &mut ModelInitContext<'_>, cond: IntPropCond, data: u64) {
		match self.0.0 {
			IntView::Linear(lin) => {
				let negated = lin.scale.is_negative();
				ctx.model.advisors.push(Advisor {
					con: ctx.con,
					data,
					negated,
					bool2int: false,
					condition: None,
				});
				let adv = AdvRef::new(ctx.model.advisors.len() - 1);
				ctx.model.int_vars[lin.var.idx()]
					.constraints
					.add(ActivationAction::Advise(adv), cond);
			}
			IntView::Const(_) => ctx.semantic_enqueue(),
			IntView::Bool(lin) => {
				let var = lin.var.resolve_alias(ctx.model);
				let (iv, cond, event) = match var.into_inner().0 {
					BoolView::Decision(lit) => {
						ctx.model.advisors.push(Advisor {
							con: ctx.con,
							data,
							negated: false,
							bool2int: true,
							condition: None,
						});
						let adv = AdvRef::new(ctx.model.advisors.len() - 1);
						ctx.model.bool_vars[lit.idx()]
							.constraints
							.push(ActivationAction::Advise(adv).into());
						return;
					}
					BoolView::Const(_) => {
						// Value does not change, so no advisor will ever be called
						return;
					}
					BoolView::IntEq(iv, v) => (iv, IntLitMeaning::Eq(v), IntPropCond::Domain),
					BoolView::IntGreaterEq(iv, v) => {
						(iv, IntLitMeaning::GreaterEq(v), IntPropCond::Bounds)
					}
					BoolView::IntLess(iv, v) => (iv, IntLitMeaning::Less(v), IntPropCond::Bounds),
					BoolView::IntNotEq(iv, v) => (iv, IntLitMeaning::NotEq(v), IntPropCond::Domain),
				};
				ctx.model.advisors.push(Advisor {
					con: ctx.con,
					data,
					negated: false,
					bool2int: true,
					condition: Some(cond),
				});
				let adv = AdvRef::new(ctx.model.advisors.len() - 1);
				ctx.model.int_vars[iv.idx()]
					.constraints
					.add(ActivationAction::Advise(adv), event);
			}
		}
	}

	fn enqueue_when(&self, ctx: &mut ModelInitContext<'_>, condition: IntPropCond) {
		match self.0.0 {
			IntView::Linear(lin) => {
				let condition = match condition {
					IntPropCond::LowerBound if lin.scale.is_negative() => IntPropCond::UpperBound,
					IntPropCond::UpperBound if lin.scale.is_negative() => IntPropCond::LowerBound,
					_ => condition,
				};
				Resolved(lin.var).enqueue_when(ctx, condition);
			}
			IntView::Const(_) => ctx.semantic_enqueue(),
			IntView::Bool(lin) => {
				if condition != IntPropCond::Fixed {
					ctx.semantic_enqueue();
				}
				lin.var.enqueue_when_fixed(ctx);
			}
		}
	}
}

impl IntInspectionActions<ModelInitContext<'_>> for Resolved<Decision<IntVal>> {
	fn bounds(&self, ctx: &ModelInitContext<'_>) -> (IntVal, IntVal) {
		self.bounds(ctx.model)
	}

	fn domain(&self, ctx: &ModelInitContext<'_>) -> IntSet {
		self.domain(ctx.model)
	}

	fn in_domain(&self, ctx: &ModelInitContext<'_>, val: IntVal) -> bool {
		self.in_domain(ctx.model, val)
	}

	fn lit_meaning(&self, ctx: &ModelInitContext<'_>, lit: View<bool>) -> Option<IntLitMeaning> {
		self.lit_meaning(ctx.model, lit)
	}

	fn max(&self, ctx: &ModelInitContext<'_>) -> IntVal {
		self.max(ctx.model)
	}

	fn max_lit(&self, ctx: &ModelInitContext<'_>) -> View<bool> {
		self.max_lit(ctx.model)
	}

	fn min(&self, ctx: &ModelInitContext<'_>) -> IntVal {
		self.min(ctx.model)
	}

	fn min_lit(&self, ctx: &ModelInitContext<'_>) -> View<bool> {
		self.min_lit(ctx.model)
	}

	fn try_lit(&self, ctx: &ModelInitContext<'_>, meaning: IntLitMeaning) -> Option<View<bool>> {
		self.try_lit(ctx.model, meaning)
	}

	fn val(&self, ctx: &ModelInitContext<'_>) -> Option<IntVal> {
		self.val(ctx.model)
	}
}

impl IntInspectionActions<ModelInitContext<'_>> for Resolved<View<IntVal>> {
	fn bounds(&self, ctx: &ModelInitContext<'_>) -> (IntVal, IntVal) {
		self.bounds(ctx.model)
	}

	fn domain(&self, ctx: &ModelInitContext<'_>) -> IntSet {
		self.domain(ctx.model)
	}

	fn in_domain(&self, ctx: &ModelInitContext<'_>, val: IntVal) -> bool {
		self.in_domain(ctx.model, val)
	}

	fn lit_meaning(&self, ctx: &ModelInitContext<'_>, lit: View<bool>) -> Option<IntLitMeaning> {
		self.lit_meaning(ctx.model, lit)
	}

	fn max(&self, ctx: &ModelInitContext<'_>) -> IntVal {
		self.max(ctx.model)
	}

	fn max_lit(&self, ctx: &ModelInitContext<'_>) -> View<bool> {
		self.max_lit(ctx.model)
	}

	fn min(&self, ctx: &ModelInitContext<'_>) -> IntVal {
		self.min(ctx.model)
	}

	fn min_lit(&self, ctx: &ModelInitContext<'_>) -> View<bool> {
		self.min_lit(ctx.model)
	}

	fn try_lit(&self, ctx: &ModelInitContext<'_>, meaning: IntLitMeaning) -> Option<View<bool>> {
		self.try_lit(ctx.model, meaning)
	}

	fn val(&self, ctx: &ModelInitContext<'_>) -> Option<IntVal> {
		self.val(ctx.model)
	}
}

impl BoolInitActions<ModelInitContext<'_>> for View<bool> {
	fn advise_when_fixed(&self, ctx: &mut ModelInitContext<'_>, data: u64) {
		self.resolve_alias(ctx.model).advise_when_fixed(ctx, data);
	}
	fn enqueue_when_fixed(&self, ctx: &mut ModelInitContext<'_>) {
		self.resolve_alias(ctx.model).enqueue_when_fixed(ctx);
	}
}

impl BoolInspectionActions<ModelInitContext<'_>> for View<bool> {
	fn val(&self, ctx: &ModelInitContext<'_>) -> Option<bool> {
		self.val(ctx.model)
	}
}

impl IntInitActions<ModelInitContext<'_>> for View<IntVal> {
	fn advise_when(&self, ctx: &mut ModelInitContext<'_>, cond: IntPropCond, data: u64) {
		self.resolve_alias(ctx.model).advise_when(ctx, cond, data);
	}

	fn enqueue_when(&self, ctx: &mut ModelInitContext<'_>, condition: IntPropCond) {
		self.resolve_alias(ctx.model).enqueue_when(ctx, condition);
	}
}

impl IntInspectionActions<ModelInitContext<'_>> for View<IntVal> {
	fn bounds(&self, ctx: &ModelInitContext<'_>) -> (IntVal, IntVal) {
		self.bounds(ctx.model)
	}

	fn domain(&self, ctx: &ModelInitContext<'_>) -> IntSet {
		self.domain(ctx.model)
	}

	fn in_domain(&self, ctx: &ModelInitContext<'_>, val: IntVal) -> bool {
		self.in_domain(ctx.model, val)
	}

	fn lit_meaning(&self, ctx: &ModelInitContext<'_>, lit: View<bool>) -> Option<IntLitMeaning> {
		self.lit_meaning(ctx.model, lit)
	}

	fn max(&self, ctx: &ModelInitContext<'_>) -> IntVal {
		self.max(ctx.model)
	}

	fn max_lit(&self, ctx: &ModelInitContext<'_>) -> View<bool> {
		self.max_lit(ctx.model)
	}

	fn min(&self, ctx: &ModelInitContext<'_>) -> IntVal {
		self.min(ctx.model)
	}

	fn min_lit(&self, ctx: &ModelInitContext<'_>) -> View<bool> {
		self.min_lit(ctx.model)
	}

	fn try_lit(&self, ctx: &ModelInitContext<'_>, meaning: IntLitMeaning) -> Option<View<bool>> {
		self.try_lit(ctx.model, meaning)
	}

	fn val(&self, ctx: &ModelInitContext<'_>) -> Option<IntVal> {
		self.val(ctx.model)
	}
}

impl BoolInitActions<ModelInitContext<'_>> for bool {
	fn advise_when_fixed(&self, _: &mut ModelInitContext<'_>, _: u64) {
		// Value does not change, so no advisor will ever be called
	}
	fn enqueue_when_fixed(&self, ctx: &mut ModelInitContext<'_>) {
		ctx.semantic_enqueue();
	}
}