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

axis arg for sum method #278

Closed
MxMartin opened this issue Apr 18, 2023 · 2 comments
Closed

axis arg for sum method #278

MxMartin opened this issue Apr 18, 2023 · 2 comments

Comments

@MxMartin
Copy link

Hi!

Thanks for this fantastic package first of all!
I just started evaluating it for a project where so far I used Gurobi.
The axis argument on the sum method was very useful for me there, e.g. in case where assignment[b,t,m] was the assignment of a batch to a certain machine for a certain task. So far I modelled the constraint to ensure that each batch was assigned to at most one machine per task as assignment.sum(axis=-1)<=1 or the capacity constraints on each machine as assignment.sum(axis=(0,1))<=capacity.

Hence I'd love to see the axis argument implemented for the sum method.

Until then: What is the recommended workaround? Am I overlooking anything?

Kind regards,

@IgnaceBleukx
Copy link
Collaborator

Hi Martin,

This is certainly on our wishlist to implement the axis argument for all operators (not only sum).
We started a shortlist for operators in issue #117, if you have any suggestions, please feel free to post them there.
A workaround you can use until then is something similar to this:

from cpmpy import *

assignment = intvar(0,10,shape=(3,4,5), name="assignment")
capacity = intvar(0,100, shape=5, name="capacity")

# each batch is assigned to at most one machine per task
model += cpm_array([assignment[i,j,:].sum() for i in range(3) for j in range(4)]) <= 1
# or equivalently:
for i in range(3):
    for j in range(4):
        model += assignment[i,j,:].sum() <= 1

# capacity constraints on each machine:
model += [assignment[:,:,m].sum() for m in range(5)] <= capacity

I hope this helps.

Kind regards,
Ignace

@MxMartin
Copy link
Author

Awesome, thanks for the swift response!

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

No branches or pull requests

2 participants