ustunb / risk-slim

simple customizable risk scores in python
BSD 3-Clause "New" or "Revised" License
132 stars 34 forks source link

Operational constraints not working #30

Open juanhu96 opened 11 months ago

juanhu96 commented 11 months ago

Hi there,

I'm currently applying riskSLIM to a problem of mine and am trying to implement operational constraints in the form of 'At least one of A or B must be selected'. However, I've noticed that riskSLIM seems to only accommodate constraints of the type 'At most one of A or B can be selected'. To address this, I attempted to add the 'at least one' constraint with the following three versions of code:

`cons.add( lin_expr = [SparsePair(ind = get_alpha_ind(constraint), val = [1.0]*len(constraint))], senses = "E", rhs = [1.0] )

cons.add( lin_expr = [SparsePair(ind = get_alpha_ind(constraint), val = [1.0]*len(constraint))], senses = "G", rhs = [1.0] )

cons.add( lin_expr = [SparsePair(ind = get_alpha_ind(constraint), val = [-1.0]*len(constraint))], senses = "L", rhs = [-1.0] )`

but none worked. Although these constraints are successfully added to the CPLEX model (I printed out the entire model to confirm), the resulting scoring tables consistently violate these specific constraints. I've spent a quite a few time examining the code, yet I'm unable to determine why this isn't working. Is this a limitation of the model itself, or is there something I'm overlooking?

Any helps would greatly appreciated. Thanks!

BopGau17 commented 10 months ago

Hi there! I'm currently new to this topic and also trying to implement the same constraint that you want here. Does it help if you try a linear constraint like the second version again?

cons.add( lin_expr = [SparsePair(ind = get_alpha_ind(["A", "B"]), val = [1.0, 1.0]], senses = ["G"] rhs = [1.0] )

To my knowledge this should work, since this basically forces alpha_A + alpha_B >= 1.0 and this constraint is only satisfied if one of the alphas is 1.0 or both of them are 1.0

I would appreciate your answer!