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

Invalid parentheses expansion when minus sign is used #15

Closed
tjozwik opened this issue Apr 20, 2024 · 2 comments
Closed

Invalid parentheses expansion when minus sign is used #15

tjozwik opened this issue Apr 20, 2024 · 2 comments

Comments

@tjozwik
Copy link
Contributor

tjozwik commented Apr 20, 2024

Let's have a simple optimization problem with integer variables - X2_i and X1_i.

# frozen_string_literal: true

require 'rulp'

ENV['SOLVER'] = 'cbc'
Rulp::log_level = Logger::DEBUG

problem = Rulp::Max(- X1_i - 2 * X2_i)
problem[
  X1_i + X2_i == 10,
  X1_i >= 0,
  X2_i >= 0
]

problem.solve

It may be rewritten as minimization, of course. But it's not the case of the issue.

Optimal solution is achieved for X1_i = 10 and X2_i = 0.

When adding additional parentheses into my objective, I'm getting invalid results:

# Before
problem = Rulp::Max(- X1_i - 2 * X2_i)

# After
problem = Rulp::Max(- (X1_i + 2 * X2_i))

It ends up with X1_i = 0 and X2_i = 10 as a solution.

I found out that the new objective is improperly converted to LP format. Rulp generates the following LP model:

Maximize
 obj:  - X1 + 2 X2
Subject to
 c0: X1 + X2 = 10

Bounds
 0 <= X1
 0 <= X2
General
 X1 X2
End

When I use minus sign before the parenthesed expression, it reverses the first component's sign only. Shouldn't it reverse the signs of all the parenthesed components?

@wouterken
Copy link
Owner

Hey @tjozwik
Thank you, you're right, looks like that bug has been there unreported for almost a decade!
I've pushed what I believe is the appropriate fix. Would you mind testing version 0.0.46 and letting me know if the results are as expected?

@tjozwik
Copy link
Contributor Author

tjozwik commented May 13, 2024

@wouterken it seems to work as expected in v0.0.46. Thanks :)

@tjozwik tjozwik closed this as completed May 13, 2024
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