choco-solver 0.1.0

Safe interface for the Choco Solver library DLL generated by GraalVM Native Image.
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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
use std::borrow::Borrow;
use std::ffi::CStr;

use crate::CHOCO_BACKEND;
use crate::CHOCO_LIB;
use crate::Sealed;
use crate::SolverError;
use crate::model::Model;
use crate::utils::{Handle, HandleT, ModelObject, make_constraint_array};
use crate::variables::{BoolVar, IntVar};

/// Comparison operators for arithmetic constraints.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EqualityOperator {
    Eq,
    Neq,
    Lt,
    Leq,
    Gt,
    Geq,
}

impl EqualityOperator {
    #[must_use]
    pub fn to_cstr(&self) -> &CStr {
        match self {
            EqualityOperator::Eq => c"=",
            EqualityOperator::Neq => c"!=",
            EqualityOperator::Lt => c"<",
            EqualityOperator::Leq => c"<=",
            EqualityOperator::Gt => c">",
            EqualityOperator::Geq => c">=",
        }
    }
}

/// Arithmetic operators used in constraints (addition, subtraction, multiplication, division).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ArithmeticOperator {
    Sum,
    Sub,
    Mul,
    Div,
}

impl ArithmeticOperator {
    #[must_use]
    pub fn to_cstr(&self) -> &CStr {
        match self {
            ArithmeticOperator::Sum => c"+",
            ArithmeticOperator::Sub => c"-",
            ArithmeticOperator::Mul => c"*",
            ArithmeticOperator::Div => c"/",
        }
    }
}

/// The operational status of a constraint.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ConstraintStatus {
    /// Constraint is not yet posted or reified.
    FREE,
    /// Constraint has been posted to the solver.
    POSTED,
    /// Constraint has been converted to a boolean variable.
    REIFIED,
}

impl TryFrom<i32> for ConstraintStatus {
    type Error = ();

    fn try_from(value: i32) -> Result<Self, Self::Error> {
        match value {
            0 => Ok(ConstraintStatus::FREE),
            1 => Ok(ConstraintStatus::POSTED),
            2 => Ok(ConstraintStatus::REIFIED),
            _ => Err(()),
        }
    }
}

/// Represents the satisfaction state of a constraint.
///
/// This enum indicates whether a constraint is satisfied, not satisfied,
/// or if it is not yet possible to determine its satisfaction state.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ESat {
    /// The constraint is satisfied.
    True,
    /// The constraint is not satisfied.
    False,
    /// The satisfaction state cannot be determined yet.
    Undefined,
}

/// Trait for creating equality constraints between variables or constants.
///
/// Provides methods to create equality (`eq`) and inequality (`ne`) constraints.
pub trait ConstraintEquality<'model, Rhs> {
    /// Creates an equality constraint (=).
    fn eq(self, other: Rhs) -> Constraint<'model>;
    /// Creates an inequality constraint (!=).
    fn ne(self, other: Rhs) -> Constraint<'model>;
}

/// Trait for creating modulo/remainder constraints.
pub trait ArithmConstraint<'model, MOD, RES> {
    /// Creates a modulo constraint: `self` % `modulo` = `res`.
    fn modulo(&'model self, modulo: MOD, res: RES) -> Constraint<'model>;
}

/// A constraint attached to a [`Model`].
///
/// Constraints can be posted, reified, or combined to express relations
/// between variables in the model.
pub struct Constraint<'model> {
    handle: Handle,
    model: &'model Model,
}

impl HandleT for Constraint<'_> {
    fn get_raw_handle(&self) -> *mut std::os::raw::c_void {
        self.handle.get_raw_handle()
    }
}
impl<'model> ModelObject<'model> for Constraint<'model> {
    fn get_model(&self) -> &'model Model {
        self.model
    }
}

impl<'model> Constraint<'model> {
    /// # Safety
    /// - The caller must ensure that the handle is valid constraint pointer and the backend is initialized.
    pub(crate) unsafe fn new(handle: *mut std::os::raw::c_void, model: &'model Model) -> Self {
        Constraint {
            handle: Handle::new(handle),
            model,
        }
    }

    pub fn post(&self) -> Result<(), SolverError> {
        if self.status() != ConstraintStatus::FREE {
            Err(SolverError::NotFreeConstraint)
        } else {
            // Safety:
            // Safe because Constraint is created from Model and therefore the backend is initialized and model handle is valid.
            CHOCO_BACKEND.with(|backend| unsafe {
                CHOCO_LIB.Java_org_chocosolver_capi_ConstraintApi_post(
                    backend.thread,
                    self.get_raw_handle(),
                )
            });
            Ok(())
        }
    }

    /// Reifies the constraint, i.e., returns a BoolVar whose instantiation in a solution
    /// corresponds to the satisfaction state of the constraint in this solution.
    ///
    /// # Returns
    ///
    /// A `BoolVar` that encodes the satisfaction state of the constraint.
    ///
    /// # Panics
    ///
    /// Panics if the backend fails to reify the constraint and returns a null handle.
    #[must_use]
    pub fn reify(&self) -> BoolVar<'model> {
        CHOCO_BACKEND.with(|backend|
            // Safety:
        // Safe because Constraint is created from Model and therefore the backend is initialized and model handle is valid.
            unsafe {
           let var_handle = CHOCO_LIB.Java_org_chocosolver_capi_ConstraintApi_reify(
                backend.thread,
                self.get_raw_handle(),
            );
             assert!(!var_handle.is_null(), "Failed to reify constraint");
             BoolVar::from_raw_handle(var_handle, self.model)})
    }

    /// Reifies the constraint with a given BoolVar whose instantiation in a solution
    /// corresponds to the satisfaction state of the constraint in this solution.
    ///
    /// # Arguments
    ///
    /// * `boolvar` - The BoolVar to reify with.
    pub fn reify_with(&self, boolvar: &BoolVar<'model>) {
        // Safety:
        // Safe because Constraint is created from Model and therefore the backend is initialized and model handle is valid.
        CHOCO_BACKEND.with(|backend| unsafe {
            CHOCO_LIB.Java_org_chocosolver_capi_ConstraintApi_reify_with(
                backend.thread,
                self.get_raw_handle(),
                boolvar.get_raw_handle(),
            )
        });
    }

    /// Encapsulates this constraint in an implication relationship.
    /// The truth value of this constraint implies the truth value of the BoolVar.
    ///
    /// # Arguments
    ///
    /// * `boolvar` - The BoolVar that is implied by this constraint.
    pub fn implies(&self, boolvar: &BoolVar<'model>) {
        // Safety:
        // Safe because Constraint is created from Model and therefore the backend is initialized and model handle is valid.
        CHOCO_BACKEND.with(|backend| unsafe {
            CHOCO_LIB.Java_org_chocosolver_capi_ConstraintApi_implies(
                backend.thread,
                self.get_raw_handle(),
                boolvar.get_raw_handle(),
            )
        });
    }

    /// Encapsulates this constraint in an implication relationship.
    /// Represents half-reification of the constraint. If the BoolVar is true,
    /// then the constraint must be satisfied.
    ///
    /// # Arguments
    ///
    /// * `boolvar` - The BoolVar that implies this constraint.
    pub fn implied_by(&self, boolvar: &BoolVar<'model>) {
        // Safety:
        // Safe because Constraint is created from Model and therefore the backend is initialized and model handle is valid.
        CHOCO_BACKEND.with(|backend| unsafe {
            CHOCO_LIB.Java_org_chocosolver_capi_ConstraintApi_implied_by(
                backend.thread,
                self.get_raw_handle(),
                boolvar.get_raw_handle(),
            )
        });
    }

    /// Check whether the constraint is satisfied.
    ///
    /// Returns `ESat::True` if the constraint is satisfied, `ESat::False` if it is not,
    /// or `ESat::Undefined` if it is not yet possible to determine whether the constraint
    /// is satisfied or not.
    ///
    /// # Note
    ///
    /// This method can be useful to verify whether a constraint is satisfied (or not)
    /// regardless of the variables' instantiation.
    ///
    /// # Returns
    ///
    /// The satisfaction state of the constraint.
    #[must_use]
    pub fn is_satisfied(&self) -> ESat {
        // Safety:
        // Safe because Constraint is created from Model and therefore the backend is initialized and model handle is valid.
        let state = CHOCO_BACKEND.with(|backend| unsafe {
            CHOCO_LIB.Java_org_chocosolver_capi_ConstraintApi_is_satisfied(
                backend.thread,
                self.get_raw_handle(),
            )
        });
        match state {
            0 => ESat::False,
            1 => ESat::True,
            _ => ESat::Undefined,
        }
    }

    #[must_use]
    pub fn status(&self) -> ConstraintStatus {
        // Safety:
        // Safe because Constraint is created from Model and therefore the backend is initialized and model handle is valid.
        let status_code = CHOCO_BACKEND.with(|backend| unsafe {
            CHOCO_LIB.Java_org_chocosolver_capi_ConstraintApi_getStatus(
                backend.thread,
                self.get_raw_handle(),
            )
        });
        match ConstraintStatus::try_from(status_code) {
            Ok(status) => status,
            Err(_) => panic!("Unknown constraint status code: {}", status_code),
        }
    }
    /// Posts a constraint ensuring that if self constraint is satisfied , then `then_constraint`
    /// must be satisfied as well. Otherwise, `else_constraint` must be satisfied.
    pub fn if_then_else(
        &self,
        then_constraint: &Constraint<'model>,
        else_constraint: &Constraint<'model>,
    ) {
        // Safety:
        // Safe because Constraint is created from Model and therefore the backend is initialized and model handle is valid.
        CHOCO_BACKEND.with(|backend| unsafe {
            CHOCO_LIB.Java_org_chocosolver_capi_ReificationApi_if_then_else(
                backend.thread,
                self.model.get_raw_handle(),
                self.get_raw_handle(),
                then_constraint.get_raw_handle(),
                else_constraint.get_raw_handle(),
            )
        });
    }
    /// Creates an if-then constraint: self constraint -> then_constraint.
    pub fn if_then(&self, then_constraint: &Constraint<'model>) {
        // Safety:
        // Safe because Constraint is created from Model and therefore the backend is initialized and model handle is valid.
        CHOCO_BACKEND.with(|backend| unsafe {
            CHOCO_LIB.Java_org_chocosolver_capi_ReificationApi_if_then(
                backend.thread,
                self.model.get_raw_handle(),
                self.get_raw_handle(),
                then_constraint.get_raw_handle(),
            )
        });
    }

    /// Posts an equivalence constraint stating that:
    /// `self` constraint is satisfied (or true) <=> `constraint` is satisfied.
    pub fn if_only_if(&self, constraint: &Constraint<'model>) {
        // Safety:
        // Safe because Constraint is created from Model and therefore the backend is initialized and model handle is valid.
        CHOCO_BACKEND.with(|backend| unsafe {
            CHOCO_LIB.Java_org_chocosolver_capi_ReificationApi_if_only_if(
                backend.thread,
                self.model.get_raw_handle(),
                self.get_raw_handle(),
                constraint.get_raw_handle(),
            )
        });
    }
}

/// # Safety
/// - This trait assumes that the implementor use this only on IntVar or i32 types.
unsafe trait IntoRawIntVarHandleT: Copy {
    fn into_raw_ptr(self, model: &Model) -> *mut std::os::raw::c_void;
}

/// # SAFETY:
/// - Implemented on &IntVar and i32 only, as required by the trait safety comment.
unsafe impl<'model, T: Borrow<IntVar<'model>>> IntoRawIntVarHandleT for &T {
    fn into_raw_ptr(self, _model: &Model) -> *mut std::os::raw::c_void {
        self.borrow().get_raw_handle()
    }
}
/// # SAFETY:
/// - Implemented on &IntVar and i32 only, as required by the trait safety comment.
/// - the integer variable is not actually destroyed after use, but this is safe as the backend will manage memory and the temporary IntVar will be garbage collected when the model is disposed.
unsafe impl IntoRawIntVarHandleT for i32 {
    fn into_raw_ptr(self, model: &Model) -> *mut std::os::raw::c_void {
        // Safety:
        // Safe because IntVar is created from Model and therefore the backend is initialized, model handle is valid.
        CHOCO_BACKEND.with(|backend| unsafe {
            CHOCO_LIB.Java_org_chocosolver_capi_IntVarApi_intVar_i(
                backend.thread,
                model.get_raw_handle(),
                self,
            )
        })
    }
}

pub(crate) trait ConstraintArithmT<'model> {
    fn create(&self) -> Constraint<'model>;
}

impl<'model, X: Borrow<IntVar<'model>>> ConstraintArithmT<'model> for (X, EqualityOperator, i32) {
    fn create(&self) -> Constraint<'model> {
        let int_var = self.0.borrow();
        CHOCO_BACKEND.with(|backend|
            // Safety:
            // Safe because Constraint is created from Model and therefore the backend is initialized and model handle is valid.
            unsafe {
            let constraint_handle = CHOCO_LIB
                .Java_org_chocosolver_capi_ConstraintApi_arithm_iv_cst(
                    backend.thread,
                    int_var.get_model().get_raw_handle(),
                    int_var.get_raw_handle(),
                    self.1.to_cstr().as_ptr().cast_mut(),
                    self.2,
                );
assert!(
            !constraint_handle.is_null(),
            "Invalid parameters combination for arithm constraint. Please refer to the doc"
        );
        Constraint::new(constraint_handle, int_var.get_model())})
    }
}

impl<'model, X: Borrow<IntVar<'model>>, Y: Borrow<IntVar<'model>>> ConstraintArithmT<'model>
    for (X, EqualityOperator, Y)
{
    fn create(&self) -> Constraint<'model> {
        let x_var = self.0.borrow();
        let y_var = self.2.borrow();
        CHOCO_BACKEND.with(|backend|
// Safety:
        // Safe because Constraint is created from Model and therefore the backend is initialized and model handle is valid.
            unsafe {
            let constraint_handle = CHOCO_LIB
                .Java_org_chocosolver_capi_ConstraintApi_arithm_iv_iv(
                    backend.thread,
                    x_var.get_model().get_raw_handle(),
                    x_var.get_raw_handle(),
                    self.1.to_cstr().as_ptr().cast_mut(),
                    y_var.get_raw_handle(),
                );
assert!(
            !constraint_handle.is_null(),
            "Invalid parameters combination for arithm constraint. Please refer to the doc"
        );
        Constraint::new(constraint_handle, x_var.get_model())})
    }
}

impl<'model, X: Borrow<IntVar<'model>>, Y: IntoRawIntVarHandleT> ConstraintArithmT<'model>
    for (X, EqualityOperator, Y, ArithmeticOperator, &IntVar<'model>)
{
    fn create(&self) -> Constraint<'model> {
        let x_var = self.0.borrow();
        let yy = self.2.into_raw_ptr(x_var.get_model());
        assert!(
            !yy.is_null(),
            "Failed to convert parameter to raw pointer for arithm constraint"
        );

        CHOCO_BACKEND.with(|backend|
// Safety:
        // Safe because Constraint is created from Model and therefore the backend is initialized and model handle is valid.
            unsafe {
            let constraint_handle = CHOCO_LIB
                .Java_org_chocosolver_capi_ConstraintApi_arithm_iv_iv_iv(
                    backend.thread,
                    x_var.get_model().get_raw_handle(),
                    x_var.get_raw_handle(),
                    self.1.to_cstr().as_ptr().cast_mut(),
                    yy,
                    self.3.to_cstr().as_ptr().cast_mut(),
                    self.4.get_raw_handle(),
                );
                assert!(
            !constraint_handle.is_null(),
            "Invalid parameters combination for arithm constraint. Please refer to the doc"
        );
        Constraint::new(constraint_handle, x_var.get_model())})
    }
}
impl<'model, X: Borrow<IntVar<'model>>, Y: IntoRawIntVarHandleT> ConstraintArithmT<'model>
    for (X, EqualityOperator, Y, ArithmeticOperator, i32)
{
    fn create(&self) -> Constraint<'model> {
        let x_var = self.0.borrow();
        let yy = self.2.into_raw_ptr(x_var.get_model());

        CHOCO_BACKEND.with(|backend|
            // Safety:
            // Safe because Constraint is created from Model and therefore the backend is initialized and model handle is valid.
            unsafe {
            let constraint_handle = CHOCO_LIB
                .Java_org_chocosolver_capi_ConstraintApi_arithm_iv_iv_cst(
                    backend.thread,
                    x_var.get_model().get_raw_handle(),
                    x_var.get_raw_handle(),
                    self.1.to_cstr().as_ptr().cast_mut(),
                    yy,
                    self.3.to_cstr().as_ptr().cast_mut(),
                    self.4,
                );
                assert!(
            !constraint_handle.is_null(),
            "Invalid parameters combination for arithm constraint. Please refer to the doc");
        Constraint::new(constraint_handle, x_var.get_model())
        })
    }
}

impl<'model, X: Borrow<IntVar<'model>>, Y: IntoRawIntVarHandleT> ConstraintArithmT<'model>
    for (X, ArithmeticOperator, Y, EqualityOperator, &IntVar<'model>)
{
    fn create(&self) -> Constraint<'model> {
        let x_var = self.0.borrow();
        let yy = self.2.into_raw_ptr(x_var.get_model());
        assert!(
            !yy.is_null(),
            "Failed to convert parameter to raw pointer for arithm constraint"
        );

        CHOCO_BACKEND.with(|backend|
            // Safety:
        // Safe because Constraint is created from Model and therefore the backend is initialized and model handle is valid.
             unsafe {
            let constraint_handle =CHOCO_LIB
                .Java_org_chocosolver_capi_ConstraintApi_arithm_iv_iv_iv(
                    backend.thread,
                    x_var.get_model().get_raw_handle(),
                    x_var.get_raw_handle(),
                    self.1.to_cstr().as_ptr().cast_mut(),
                    yy,
                    self.3.to_cstr().as_ptr().cast_mut(),
                    self.4.get_raw_handle(),
                );
                assert!(
            !constraint_handle.is_null(),
            "Invalid parameters combination for arithm constraint. Please refer to the doc");
        Constraint::new(constraint_handle, x_var.get_model())
        })
    }
}
impl<'model, X: Borrow<IntVar<'model>>, Y: IntoRawIntVarHandleT> ConstraintArithmT<'model>
    for (X, ArithmeticOperator, Y, EqualityOperator, i32)
{
    fn create(&self) -> Constraint<'model> {
        let x_var = self.0.borrow();
        let yy = self.2.into_raw_ptr(x_var.get_model());

        CHOCO_BACKEND.with(|backend|
            // Safety:
            // Safe because Constraint is created from Model and therefore the backend is initialized and model handle is valid.
            unsafe {
             let constraint_handle = CHOCO_LIB
                .Java_org_chocosolver_capi_ConstraintApi_arithm_iv_iv_cst(
                    backend.thread,
                    x_var.get_model().get_raw_handle(),
                    x_var.get_raw_handle(),
                    self.1.to_cstr().as_ptr().cast_mut(),
                    yy,
                    self.3.to_cstr().as_ptr().cast_mut(),
                    self.4,
                );
        assert!(
            !constraint_handle.is_null(),
            "Invalid parameters combination for arithm constraint. Please refer to the doc");
        Constraint::new(constraint_handle, x_var.get_model())
        })
    }
}
#[allow(private_bounds)]
/// Logical operations on slices of Constraints/BoolVars
pub trait ConstraintArrayLogicOps<'model>: Sealed {
    /// AND of Constraints
    /// # Arguments
    /// * `constraints` - slice of Constraints/BoolVars
    /// # Returns
    /// Constraint representing the AND of the Constraints/BoolVars
    /// # Panic:
    /// if slice is empty
    fn and(self) -> Constraint<'model>;
    /// OR of Constraints
    /// # Arguments
    /// * `constraints` - slice of Constraints/BoolVars
    /// # Returns
    /// Constraint representing the OR of the Constraints/BoolVars
    /// # Panic:
    /// if slice is empty
    fn or(self) -> Constraint<'model>;
}

impl<'model, Q: Borrow<Constraint<'model>> + Sealed> ConstraintArrayLogicOps<'model> for &[Q] {
    fn and(self) -> Constraint<'model> {
        let array_handle = make_constraint_array(self);
        let model = self
            .first()
            .expect("Slice shall contains at least one element")
            .borrow()
            .get_model();
        CHOCO_BACKEND.with(|backend|
            // Safety:
            // Safe because Constraint is created from Model and therefore the backend is initialized and model handle is valid.
            unsafe {
            let constraint_handle =CHOCO_LIB
                .Java_org_chocosolver_capi_ConstraintApi_and_cs_cs(
                    backend.thread,
                    model.get_raw_handle(),
                    array_handle,
                );
        assert!(
            !constraint_handle.is_null(),
            "Failed to create AND constraint"
        );
        Constraint::new(constraint_handle, model)
    })
    }
    fn or(self) -> Constraint<'model> {
        let array_handle = make_constraint_array(self);
        let model = self
            .first()
            .expect("Slice shall contains at least one element")
            .borrow()
            .get_model();
        CHOCO_BACKEND.with(|backend|
            // Safety:
            // Safe because Constraint is created from Model and therefore the backend is initialized and model handle is valid.
            unsafe {
            let constraint_handle = CHOCO_LIB
                .Java_org_chocosolver_capi_ConstraintApi_or_cs_cs(
                    backend.thread,
                    model.get_raw_handle(),
                    array_handle,
                );
        assert!(!constraint_handle.is_null(),
            "Failed to create OR constraint"
        );
        Constraint::new(constraint_handle, model)
        })
    }
}

#[cfg(test)]
mod tests;