coin-or / coin-or/pulp

Repeated variable __dummy when used with copy()

Open
#175 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
2.5k
Forks
432
PR merge metrics
No merged PRs in 30d

Description

My sequence workflow is quite simple. I build a `LpProblem` with a set of common constraints, and then copy the `LpProblem` to test many different additional conditions and check if those new problems can be solved.

However the generation of `__dummy` variables doesn't seem to play well with the calls to `copy()`. Here is an example.

```python
import pulp

p0 = pulp.LpProblem()
p0 += pulp.LpAffineExpression(0) == 1
p = p0.copy()
p.writeLP("/dev/null")
p = p0.copy()
p.writeLP("/dev/null")
```

This raises the following exception.
```
Traceback (most recent call last):
File "./test.py", line 10, in
p.writeLP("/dev/null")
File "/usr/lib/python3/dist-packages/pulp/pulp.py", line 1531, in writeLP
+ str(repeated_names))
pulp.constants.PulpError: Repeated variable names in Lp format
[('__dummy', 2)]
```
What I understand happens is that during the first call to `writeLP` a dummy variable is generated for both the constraint and the objective. The constraint is modified to add the dummy variable (because it doesn't have any variable), and this modification is visible outside of the scope of the model.
During the second call to `writeLP` a new `__dummy` variable is needed for the objective since no dummy variable has been generated in `p0`. And thus we end up with two distinct variables with the name `__dummy`.

Here is an example of the undesired side effect of `writeLP`.
```python
import pulp

p = pulp.LpProblem()
constraint = pulp.LpAffineExpression(0) == 1
p += constraint
print(constraint)
p.writeLP("/dev/null")
print(constraint)
```
Which outputs:
```
0 = 1
__dummy = 1
```

I think the sane thing to do would be to not modify the trivial constraints in `writeLP` but rather generate a new one.

Best regards.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.