-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cover cp.abs() using the builtin (#513)
* cover cp.abs() using the builtin * abs implementation * actually abs for only 1 element * tests for builtins * support vectorized * recursive for iterable * fix error in test for all_diff_except_n * add testcase * add testcase * fix missing 'not' remove redundant 'abs' code * still overwrite __abs__ in expressions * Update python_builtins.py bit more explicit... --------- Co-authored-by: wout4 <wout.vanroose@gmail.com> Co-authored-by: Tias Guns <tias@ulyssis.org>
- Loading branch information
1 parent
f343c19
commit 085d919
Showing
3 changed files
with
72 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import unittest | ||
|
||
import cpmpy as cp | ||
from cpmpy.exceptions import CPMpyException | ||
|
||
iv = cp.intvar(-8, 8, shape=5) | ||
|
||
|
||
class TestBuiltin(unittest.TestCase): | ||
|
||
def test_max(self): | ||
constraints = [cp.max(iv) + 9 <= 8] | ||
model = cp.Model(constraints) | ||
self.assertTrue(model.solve()) | ||
self.assertTrue(cp.max(iv.value()) <= -1) | ||
|
||
model = cp.Model(cp.max(iv).decompose_comparison('!=', 4)) | ||
self.assertTrue(model.solve()) | ||
self.assertNotEqual(str(cp.max(iv.value())), '4') | ||
|
||
def test_min(self): | ||
constraints = [cp.min(iv) + 9 == 8] | ||
model = cp.Model(constraints) | ||
self.assertTrue(model.solve()) | ||
self.assertEqual(str(cp.min(iv.value())), '-1') | ||
|
||
model = cp.Model(cp.min(iv).decompose_comparison('==', 4)) | ||
self.assertTrue(model.solve()) | ||
self.assertEqual(str(cp.min(iv.value())), '4') | ||
|
||
def test_abs(self): | ||
constraints = [cp.abs(iv[0]) + 9 <= 8] | ||
model = cp.Model(constraints) | ||
self.assertFalse(model.solve()) | ||
|
||
#with list | ||
constraints = [cp.abs(iv+2) <= 8, iv < 0] | ||
model = cp.Model(constraints) | ||
self.assertTrue(model.solve()) | ||
|
||
constraints = [cp.abs([iv[0], iv[2], iv[1], -8]) <= 8, iv < 0] | ||
model = cp.Model(constraints) | ||
self.assertTrue(model.solve()) | ||
|
||
model = cp.Model(cp.abs(iv[0]).decompose_comparison('!=', 4)) | ||
self.assertTrue(model.solve()) | ||
self.assertNotEqual(str(cp.abs(iv[0].value())), '4') |