@@ -119,7 +119,7 @@ def min(iterable):
119
119
"""
120
120
if not any (isinstance (elem , Expression ) for elem in iterable ):
121
121
return np .min (iterable )
122
- return GlobalConstraint ( "min" , list ( iterable ), is_bool = False )
122
+ return Minimum ( iterable )
123
123
124
124
def max (iterable ):
125
125
"""
@@ -128,7 +128,7 @@ def max(iterable):
128
128
"""
129
129
if not any (isinstance (elem , Expression ) for elem in iterable ):
130
130
return np .max (iterable )
131
- return GlobalConstraint ( "max" , list ( iterable ), is_bool = False )
131
+ return Maximum ( iterable )
132
132
133
133
134
134
@@ -178,6 +178,30 @@ def decompose(self):
178
178
return constraints
179
179
180
180
181
+ class Minimum (GlobalConstraint ):
182
+ """
183
+ Computes the minimum value of the arguments
184
+
185
+ It is a 'functional' global constraint which implicitly returns a numeric variable
186
+ """
187
+ def __init__ (self , arg_list ):
188
+ super ().__init__ ("min" , arg_list , is_bool = False )
189
+
190
+ def value (self ):
191
+ return min ([_argval (a ) for a in self .args ])
192
+
193
+ class Maximum (GlobalConstraint ):
194
+ """
195
+ Computes the maximum value of the arguments
196
+
197
+ It is a 'functional' global constraint which implicitly returns a numeric variable
198
+ """
199
+ def __init__ (self , arg_list ):
200
+ super ().__init__ ("max" , arg_list , is_bool = False )
201
+
202
+ def value (self ):
203
+ return max ([_argval (a ) for a in self .args ])
204
+
181
205
class Element (GlobalConstraint ):
182
206
"""
183
207
The 'Element' global constraint enforces that the result equals Arr[Idx]
@@ -197,21 +221,24 @@ def __init__(self, arg_list):
197
221
super ().__init__ ("element" , arg_list , is_bool = False )
198
222
199
223
def value (self ):
200
- # XXX, make argval shared util function?
201
- def argval (a ):
202
- return a .value () if isinstance (a , Expression ) else a
203
- idxval = argval (self .args [1 ])
224
+ idxval = _argval (self .args [1 ])
204
225
if not idxval is None :
205
- return argval (self .args [0 ][idxval ])
226
+ return _argval (self .args [0 ][idxval ])
206
227
return None # default
207
228
208
229
def __repr__ (self ):
209
230
return "{}[{}]" .format (self .args [0 ], self .args [1 ])
210
231
211
232
233
+
212
234
def _all_pairs (args ):
213
235
""" internal helper function
214
236
"""
215
237
pairs = list (combinations (args , 2 ))
216
238
return pairs
217
239
240
+ # XXX, make argval shared util function?
241
+ def _argval (a ):
242
+ """ returns .value() of Expression, otherwise the variable itself
243
+ """
244
+ return a .value () if isinstance (a , Expression ) else a
0 commit comments