CyLP icon indicating copy to clipboard operation
CyLP copied to clipboard

Summing over axis

Open skosch opened this issue 8 years ago • 2 comments

First off, thank you for a wonderful, wonderful tool. CyLP has saved me so much time already :+1:

I may well be doing things wrong, but I often find myself wishing I could write constraints like

s += my_binary_matrix.sum(axis = 1) == 1

but it appears that the variables' sum() function doesn't support it. Is there another way of expressing sums across a subset of dimensions (that doesn't require writing loops)?

skosch avatar Oct 30 '16 20:10 skosch

@skosch Have you found a solution for this type of constraint? I'm facing a similar problem

GalPerelman avatar Oct 17 '21 13:10 GalPerelman

Hmm, looking back at this now, if I understand correctly, this could be accomplished just by taking a dot product of the vector of variables with a single row of the constraint matrix.

s += a*x == 1

where a is the row you want to use for generating the constraint. I imagine that taking a slice of the matrix should work, but I'd have to play around to see the best way of accomplishing what you're trying to accomplish.

tkralphs avatar Oct 17 '21 15:10 tkralphs