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
//! Array operation constraints
//!
//! This module contains constraints for array operations:
//! - Integer arrays: array_int_minimum, array_int_maximum, array_int_element
//! - Float arrays: array_float_minimum, array_float_maximum, array_float_element
use crate::model::Model;
use crate::variables::VarId;
use crate::core::error::SolverResult;
impl Model {
// ============================================================================
// Integer Array Constraints
// ============================================================================
/// Find the minimum value in an integer array.
///
/// This implements the MiniZinc/FlatZinc `array_int_minimum` constraint.
/// Constrains `result` to equal the minimum value in `array`.
///
/// # Examples
/// ```
/// use selen::prelude::*;
/// let mut m = Model::default();
/// let x = m.int(1, 10);
/// let y = m.int(5, 15);
/// let z = m.int(3, 8);
///
/// let min_result = m.array_int_minimum(&[x, y, z]).expect("non-empty array");
/// ```
///
/// # Errors
/// Returns `SolverError::InvalidInput` if the array is empty.
///
/// # Note
/// This is a convenience wrapper around the generic `min()` method,
/// provided for MiniZinc/FlatZinc compatibility. The underlying implementation
/// works for both integer and float variables.
pub fn array_int_minimum(&mut self, array: &[VarId]) -> SolverResult<VarId> {
// Delegate to the generic min() implementation which works for both int and float
self.min(array)
}
/// Find the maximum value in an integer array.
///
/// This implements the MiniZinc/FlatZinc `array_int_maximum` constraint.
/// Constrains `result` to equal the maximum value in `array`.
///
/// # Examples
/// ```
/// use selen::prelude::*;
/// let mut m = Model::default();
/// let x = m.int(1, 10);
/// let y = m.int(5, 15);
/// let z = m.int(3, 8);
///
/// let max_result = m.array_int_maximum(&[x, y, z]).expect("non-empty array");
/// ```
///
/// # Errors
/// Returns `SolverError::InvalidInput` if the array is empty.
///
/// # Note
/// This is a convenience wrapper around the generic `max()` method,
/// provided for MiniZinc/FlatZinc compatibility. The underlying implementation
/// works for both integer and float variables.
pub fn array_int_maximum(&mut self, array: &[VarId]) -> SolverResult<VarId> {
// Delegate to the generic max() implementation which works for both int and float
self.max(array)
}
/// Access an element from an integer array using a variable index.
///
/// This implements the MiniZinc/FlatZinc `array_int_element` and
/// `array_var_int_element` constraints. Constrains `result = array[index]`
/// where `index` is a variable.
///
/// # Arguments
/// * `index` - Integer variable representing the array index (0-based)
/// * `array` - Array of integer variables to index into
/// * `result` - Integer variable that will equal `array[index]`
///
/// # Examples
/// ```
/// use selen::prelude::*;
/// let mut m = Model::default();
///
/// // Array of scores
/// let scores = vec![
/// m.int(10, 10),
/// m.int(20, 20),
/// m.int(30, 30),
/// ];
///
/// let index = m.int(0, 2);
/// let selected_score = m.int(0, 50);
///
/// // selected_score = scores[index]
/// m.array_int_element(index, &scores, selected_score);
/// ```
///
/// # Note
/// This is a convenience wrapper around the generic element constraint,
/// provided for MiniZinc/FlatZinc compatibility. The underlying `props.element()`
/// implementation works for both integer and float variables.
pub fn array_int_element(&mut self, index: VarId, array: &[VarId], result: VarId) {
// Delegate to the generic element constraint which works for both int and float
self.props.element(array.to_vec(), index, result);
}
// ============================================================================
// Float Array Constraints
// ============================================================================
/// Find the minimum value in a float array.
///
/// This implements the MiniZinc/FlatZinc `array_float_minimum` constraint.
pub fn array_float_minimum(&mut self, array: &[VarId]) -> SolverResult<VarId> {
// Delegate to the generic min() implementation which works for floats
self.min(array)
}
/// Find the maximum value in a float array.
///
/// This implements the FlatZinc `array_float_maximum` constraint.
/// Constrains `result` to equal the maximum value in `array`.
///
/// # Examples
/// ```
/// use selen::prelude::*;
/// let mut m = Model::default();
/// let x = m.float(1.5, 10.5);
/// let y = m.float(2.3, 8.7);
/// let z = m.float(3.1, 6.2);
///
/// let max_result = m.array_float_maximum(&[x, y, z]).expect("non-empty array");
/// ```
///
/// # Errors
/// Returns `SolverError::InvalidInput` if the array is empty.
///
/// # Note
/// This is a convenience wrapper around the generic `max()` method,
/// provided for FlatZinc compatibility. The underlying implementation
/// works for both integer and float variables.
pub fn array_float_maximum(&mut self, array: &[VarId]) -> SolverResult<VarId> {
// Delegate to the generic max() implementation which works for floats
self.max(array)
}
/// Access an element from a float array using a variable index.
///
/// This implements the FlatZinc `array_float_element` constraint.
/// Constrains `result = array[index]` where `index` is a variable.
///
/// # Arguments
/// * `index` - Integer variable representing the array index (0-based)
/// * `array` - Array of float variables to index into
/// * `result` - Float variable that will equal `array[index]`
///
/// # Examples
/// ```
/// use selen::prelude::*;
/// let mut m = Model::default();
///
/// // Array of prices
/// let prices = vec![
/// m.float(10.5, 10.5),
/// m.float(12.3, 12.3),
/// m.float(15.0, 15.0),
/// ];
///
/// let index = m.int(0, 2);
/// let selected_price = m.float(0.0, 20.0);
///
/// // selected_price = prices[index]
/// m.array_float_element(index, &prices, selected_price);
/// ```
///
/// # Note
/// This is a convenience wrapper around the generic element constraint,
/// provided for FlatZinc compatibility. The underlying `props.element()`
/// implementation works for both integer and float variables.
pub fn array_float_element(&mut self, index: VarId, array: &[VarId], result: VarId) {
// Delegate to the generic element constraint which works for floats
self.props.element(array.to_vec(), index, result);
}
}