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

Add support for semi-continuous and semi-integer vars #3

Merged
merged 1 commit into from
Apr 17, 2023
Merged

Add support for semi-continuous and semi-integer vars #3

merged 1 commit into from
Apr 17, 2023

Conversation

jeromepl
Copy link
Contributor

Adds support for semi-continuous and semi-integer variables for the HiGHS solver (the only solver at this point which support these types of variables natively)

Useful for problems where a value can be either 0 or must be greater than or equal to some minimum value.

Closes #2

test/mip_test.rb Outdated
Comment on lines 115 to 131
def test_semi_contious_mip
skip unless Opt.solvers[Opt.default_solvers[:mip]].supports_semi_continuous_variables?

x1 = Opt::SemiContinuousVariable.new(1.., "x1")
x2 = Opt::SemiContinuousVariable.new(2.., "x2")

prob = Opt::Problem.new
prob.add(3 * x1 + 2 * x2 >= 60)
prob.minimize(8 * x1 + 6 * x2)
res = prob.solve
assert_equal :optimal, res[:status]
assert_in_delta 160, res[:objective]
assert_equal 20, x1.value
assert_kind_of Float, x1.value
assert_in_delta 0, x2.value
assert_kind_of Float, x2.value
end
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested by comparing with scipy:

from scipy import optimize

optimize.milp([8, 6],
              constraints=optimize.LinearConstraint([3, 2], lb=60),
              bounds=optimize.Bounds(lb=[1, 2]),
              integrality=[2, 2])

test/mip_test.rb Outdated
Comment on lines 133 to 149
def test_semi_integer_mip
skip unless Opt.solvers[Opt.default_solvers[:mip]].supports_semi_continuous_variables?

x1 = Opt::SemiIntegerVariable.new(1.., "x1")
x2 = Opt::SemiIntegerVariable.new(2.., "x2")

prob = Opt::Problem.new
prob.add(3 * x1 + 2 * x2 >= 70)
prob.minimize(8 * x1 + 6 * x2)
res = prob.solve
assert_equal :optimal, res[:status]
assert_in_delta 188, res[:objective]
assert_equal 22, x1.value
assert_kind_of Integer, x1.value
assert_in_delta 2, x2.value
assert_kind_of Integer, x2.value
end
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested by comparing with scipy:

from scipy import optimize

optimize.milp([8, 6],
              constraints=optimize.LinearConstraint([3, 2], lb=70),
              bounds=optimize.Bounds(lb=[1, 2]),
              integrality=[3, 3])

@ankane
Copy link
Owner

ankane commented Apr 17, 2023

Hey @jeromepl, thanks for the PR. Looks really great! Two small notes:

  1. I think we can drop the Variable suffix from classes (SemiContinuous and SemiInteger) to save typing
  2. It'd be good if one of the variables was 0 for the semi-integer test (so it doesn't pass if the variables are swapped with Integer variables)

@jeromepl
Copy link
Contributor Author

@ankane Agreed and done!

@ankane ankane merged commit 5a7fad9 into ankane:master Apr 17, 2023
@ankane
Copy link
Owner

ankane commented Apr 17, 2023

Awesome, thanks! Will push a new release later tonight.

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

Successfully merging this pull request may close these issues.

Add support for semi-continuous variables
2 participants