coin-or / pulp

A python Linear Programming API
http://coin-or.github.io/pulp/
Other
2.08k stars 383 forks source link

Vanishing variables while solving with CBC #665

Open dbokal opened 1 year ago

dbokal commented 1 year ago

Details for the issue

I have implemented an approach to find a minimum infeasible set of constraints that works roughly as follows:

1) Assume [C1, C2, C3, ..., Ctotal] are the constraints of the problem 2) Using bisection, I find the last constraint, such that [C1, ..., CLast] is feasible, but [C1, ..., CLast+1] is infeasible. 3) Using bisection, I find the first constraint, such that [CFirst, ..., CLast+1] is feasible, but [CFirst-1, ..., CLast+1] is infeasible. 4) For each of the remaining constraints Ci, I remove the constraint if [CFirst-1, Ci-1,Ci+1,...CLast+1] is infeasible.

As removing these constraints does not bother with the variables, some variables may get redundant, but I do not care about that.

What did you expect to see?

I expected the above to work just fine, but...

What did you see instead?

... what I observed is an exception in the line variables[name].varValue = values[name] of the following function in pulp.py that stores the solution:

        variables = self.variablesDict()
        for name in values:
            if name != "__dummy":
                variables[name].varValue = values[name]

Namely, there were variables that were sent to the solver and stored in the dictionary as values[name] so that they could be interpeted, but they did not appear in the variables dictionary. Seemingly they disappeared from the variables dictionary while the problem was being solved.

What I did is simply validate that the variable vas not pruned:

        variables = self.variablesDict()
        for name in values:
            if name != "__dummy":
                if name not in variables:
                    if warnOnPruning:
                         print(f"Warning: Variable has been pruned: {name}.")
                else:
                    variables[name].varValue = values[name]

Does this make a reasonable and general solution? Should I submit this to pulp? The process of forking etc. is tedious, so I would only go for it if you guys find the solution reasonable and are willing to include it into future versions. If there is desire, I can also include the above searching for minimum infeasible set.

Useful extra information

What operating system are you using?

I'm using python version:

I installed PuLP via:

Did you also

pchtsp commented 9 months ago

Can you please paste the code with some formatting? I cannot read it well. Also, I do not understand if this is an issue or a feature request.

dbokal commented 9 months ago

Thank you very much for your response!

Can you please paste the code with some formatting?

Indeed it was ugly, I did not observe the <> code icon in the edit.

Also, I do not understand if this is an issue or a feature request.

It is an issue that I had and I (believe) I fixed. It would be great if the fix is consistent with the general behavior of pulp and can be integrated into the main distribution, so that I do not need to refix it next time I update pulp.

If there is a process I need to follow, I am willing to do so, but perhaps it is easiest for an active developer to replace the function I changed in his code and submit it to the main package. Please advise.

pchtsp commented 9 months ago

Thanks for the response and the clarification. I still do not understand the use case or set of circumstances where this happens. Do you have a reproducible example that I can test? Are you editing the model internals? If a variable is not used in a constraint, it should not belong in the model.

Maybe we could do a more consistant pruning before solving so that this exception doesn't happen. But for that we need the reproducible example. Also, so we can add it as a unit test inside the library.

dbokal commented 9 months ago

Agreed. I myself have a unit test to that effect so that I will get warned with the updates of pulp. I will look into a smaller publishable unit test and send it to you.

Note that this may take a while, as I am currently working on a next project. Is there an optimal time or a final time by which I should send you the unit test and sample data?

Also, may I just email the files to you avoiding the web interface hassle?

V sre., 17. jan. 2024 10:30 je oseba Franco Peschiera < @.***> napisala:

Thanks for the response and the clarification. I still do not understand the use case or set of circumstances where this happens. Do you have a reproducible example that I can test? Are you editing the model internals? If a variable is not used in a constraint, it should not belong in the model.

Maybe we could do a more consistant pruning before solving so that this exception doesn't happen. But for that we need the reproducible example. Also, so we can add it as a unit test inside the library.

— Reply to this email directly, view it on GitHub https://github.com/coin-or/pulp/issues/665#issuecomment-1895422354, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHNJTOQ47LXSD2CMFVIIT33YO6K4DAVCNFSM6AAAAAAZYA6G7GVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQOJVGQZDEMZVGQ . You are receiving this because you authored the thread.Message ID: @.***>