Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

283 use BoolVal in stead of True and False #284

Merged
merged 6 commits into from
May 9, 2023

Conversation

Wout4
Copy link
Collaborator

@Wout4 Wout4 commented Apr 27, 2023

Had to do a little bit more work than just replacing the return true and false so made it into a pr

@Wout4 Wout4 requested a review from tias April 27, 2023 11:21
@Wout4 Wout4 linked an issue Apr 27, 2023 that may be closed by this pull request
@Wout4
Copy link
Collaborator Author

Wout4 commented Apr 27, 2023

time for test suite:
3154 passed, 1 skipped, 4 warnings in 460.55s (0:07:40)

vs master
3154 passed, 1 skipped, 2 warnings in 512.05s (0:08:32)
On my machine which I expect to not be very consistent

@tias
Copy link
Collaborator

tias commented Apr 28, 2023

In retrospect I am not sure how reliable the testsuite is for time-benchmarking. I guess we would need a repo of exectuable python scripts that return a model, not even the pickles we have in bigtest, if we also want to benchmark model creation time... that is definitely for later ; )

Also you would have to compare the difference of just 1 commit to know the effect of that one change.

Anyway, on master all pass tests in ~3.5 minutes here, but this branch

FAILED tests/test_examples.py::test_examples[./examples/knapsack.py] - TypeError: int() argument must be a string, a bytes-like object or a number, not 'Operator'
FAILED tests/test_examples.py::test_examples[./examples/rcpsp.py] - AssertionError: IntVar upperbound must be integer <class 'cpmpy.expressions.core.Operator'> sum([0, 3, 2, 5, 4, 2, 3, 4, 2, 4, 6, 0])
FAILED tests/test_examples.py::test_examples[./examples/bus_schedule.py] - AssertionError: IntVar upperbound must be integer <class 'cpmpy.expressions.core.Operator'> sum([8, 10, 7, 12, 4, 4])
FAILED tests/test_examples.py::test_examples[./examples/jobshop.py] - AssertionError: IntVar upperbound must be integer <class 'cpmpy.expressions.core.Operator'> sum([3, 2, 2, 2, 1, 4, 0, 4, 3])
======================================= 4 failed, 3250 passed, 3 warnings in 143.88s (0:02:23) ========================================

e.g.

tests/test_examples.py:35: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
<frozen importlib._bootstrap_external>:790: in exec_module
    ???
<frozen importlib._bootstrap>:228: in _call_with_frames_removed
    ???
examples/rcpsp.py:22: in <module>
    start_time = intvar(0, max_duration, shape=nb_jobs)
cpmpy/expressions/variables.py:172: in intvar
    data = np.array([_IntVarImpl(lb,ub, name=_genname(name, idxs)) for idxs in np.ndindex(shape)]) # repeat new instances
cpmpy/expressions/variables.py:172: in <listcomp>
    data = np.array([_IntVarImpl(lb,ub, name=_genname(name, idxs)) for idxs in np.ndindex(shape)]) # repeat new instances
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <[AttributeError("'_IntVarImpl' object has no attribute 'name'") raised in repr()] _IntVarImpl object at 0x7f1ac53c60d0>, lb = 0
ub = sum([0, 3, 2, 5, 4, 2, 3, 4, 2, 4, 6, 0]), name = None

I guess if it passes the tests we should not be too worried by another function call at creation time, at this point.

@Wout4
Copy link
Collaborator Author

Wout4 commented May 4, 2023

ah the examples don't run on my machine and they seem to run but not fail on the automated tests

Copy link
Collaborator

@tias tias left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see comments for change on built-in overwrites.

will merge

return False # no need to create constraint
elif elem is True or elem is np.True_:
if is_false_cst(elem):
return BoolVal(False) # no need to create constraint
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we can/should in general change the return type of a Python builtin...

'any' can be used a lot in an arbitrary code-base that only includes cpmpy for a small part

maybe if we check that at least one element is a CPMpy expr, but for now I will just undo this


def max(iterable):
"""
max() overwrites python built-in,
checks if all constants and computes np.max() in that case
"""
if not any(isinstance(elem, Expression) for elem in iterable):
if is_false_cst(any(isinstance(elem, Expression) for elem in iterable)):
Copy link
Collaborator

@tias tias May 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this already shows that returning a custom type for 'any' is not a good idea...

Note that I now implemented __bool__() for BoolVal, so the original code would work now (with not), but still, better without for now

@@ -97,6 +98,6 @@ def sum(iterable):
otherwise, makes a sum Operator directly on `iterable`
"""
iterable = list(iterable) # Fix generator polling
if not any(isinstance(elem, Expression) for elem in iterable):
if is_false_cst(any(isinstance(elem, Expression) for elem in iterable)):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is what is breaking the tests on my machine
(does this mean that the examples are not run on the github CI? nor on your computer?)

@tias tias merged commit 353b1dc into master May 9, 2023
@tias tias deleted the 283-return-our-new-cpmpy-expression-of-false branch May 9, 2023 10:29
tias added a commit that referenced this pull request May 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

return our new CPMpy expression of False
2 participants