Skip to content

Commit

Permalink
Merge pull request #16 from joaquimg/jg/moi10
Browse files Browse the repository at this point in the history
MOI 0.10
  • Loading branch information
joaquimg authored Jan 10, 2022
2 parents 462e241 + bd3b769 commit 57fb6ab
Show file tree
Hide file tree
Showing 6 changed files with 424 additions and 228 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
- version: '1'
os: ubuntu-latest
arch: x64
- version: '1.0'
- version: '1.6'
os: ubuntu-latest
arch: x64
steps:
Expand Down
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
name = "QuadraticToBinary"
uuid = "014a38d5-7acb-4e20-b6c0-4fe5c2344fd1"
authors = ["Joaquim Garcia <[email protected]>"]
version = "0.2.4"
version = "0.3.0"

[deps]
MathOptInterface = "b8f27783-ece8-5eb3-8dc8-9495eed66fee"
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"

[compat]
MathOptInterface = "0.9.12"
MathOptInterface = "0.10.6"
DataStructures = "0.17.10, 0.18.0"
julia = "1"

Expand Down
60 changes: 26 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ MIP solvers such as [Cbc](https://github.com/JuliaOpt/Cbc.jl), [CPLEX](https://g

## Example

If one wants to solve the optimization problem with the package:
If one wants to solve the optimization problem with this package:

```julia
# Max 2x + y
Expand All @@ -32,9 +32,9 @@ If one wants to solve the optimization problem with the package:

One should model as a quadratic program and simply wrap the solver with a
`QuadraticToBinary.Optimizer`, with one extra requirement: all variables appearing
in quadratic terms must be bounded above an below.
in quadratic terms must be bounded above and below.

Therefore the new model can be:
Therefore, the new model can be:


```julia
Expand All @@ -52,17 +52,9 @@ using MathOptInterface
using QuadraticToBinary
using Cbc

const MOI = MathOptInterface
const MOIU = MOI.Utilities

const optimizer = MOI.Bridges.full_bridge_optimizer(
MOIU.CachingOptimizer(
MOIU.UniversalFallback(MOIU.Model{Float64}()),
Cbc.Optimizer()), Float64)

model = Model(
()->QuadraticToBinary.Optimizer{Float64}(
optimizer))
MOI.instantiate(Cbc.Optimizer, with_bridge_type = Float64)))

@variable(model, 1 <= x <= 10)
@variable(model, 1 <= y <= 10)
Expand All @@ -79,10 +71,10 @@ primal_status(model)

objective_value(model) # ≈ 9.0

value(x) # ≈ 4.0
value(y) # ≈ 1.0
@assert value(x) 4.0
@assert value(y) 1.0

value(c) # ≈ 4.0
@assert value(c) 4.0
```

### MathOptInterface with Cbc solver
Expand All @@ -91,49 +83,47 @@ value(c) # ≈ 4.0
using MathOptInterface
using QuadraticToBinary
const MOI = MathOptInterface
const MOIU = MOI.Utilities
using Cbc

const optimizer = MOI.Bridges.full_bridge_optimizer(
MOIU.CachingOptimizer(
MOIU.UniversalFallback(MOIU.Model{Float64}()),
Cbc.Optimizer()), Float64)
optimizer = MOI.instantiate(Cbc.Optimizer, with_bridge_type = Float64)

model = QuadraticToBinary.Optimizer{Float64}(optimizer)

x = MOI.add_variable(model)
y = MOI.add_variable(model)

MOI.add_constraint(model, MOI.SingleVariable(x), MOI.GreaterThan(1.0))
MOI.add_constraint(model, MOI.SingleVariable(y), MOI.GreaterThan(1.0))
MOI.add_constraint(model, x, MOI.GreaterThan(1.0))
MOI.add_constraint(model, y, MOI.GreaterThan(1.0))

MOI.add_constraint(model, MOI.SingleVariable(x), MOI.LessThan(10.0))
MOI.add_constraint(model, MOI.SingleVariable(y), MOI.LessThan(10.0))
MOI.add_constraint(model, x, MOI.LessThan(10.0))
MOI.add_constraint(model, y, MOI.LessThan(10.0))

cf = MOI.ScalarQuadraticFunction(
[MOI.ScalarAffineTerm(0.0, x)], [MOI.ScalarQuadraticTerm(1.0, x, y)], 0.0)
c = MOI.add_constraint(model, cf, MOI.LessThan(4.0))
c = MOI.add_constraint(model, 1.0 * x * y, MOI.LessThan(4.0))

MOI.set(model, MOI.ObjectiveFunction{MOI.ScalarAffineFunction{Float64}}(),
MOI.ScalarAffineFunction(MOI.ScalarAffineTerm.([2.0, 1.0], [x, y]), 0.0))
2.0 * x + y)
MOI.set(model, MOI.ObjectiveSense(), MOI.MAX_SENSE)

MOI.optimize!(model)

MOI.get(model, MOI.TerminationStatus()) # config.optimal_status
@assert MOI.get(model, MOI.TerminationStatus()) == MOI.OPTIMAL

MOI.get(model, MOI.PrimalStatus()) # MOI.FEASIBLE_POINT
@assert MOI.get(model, MOI.PrimalStatus()) == MOI.FEASIBLE_POINT

MOI.get(model, MOI.ObjectiveValue()) # ≈ 9.0
@assert MOI.get(model, MOI.ObjectiveValue()) 9.0

MOI.get(model, MOI.VariablePrimal(), x) # ≈ 4.0
MOI.get(model, MOI.VariablePrimal(), y) # ≈ 1.0
@assert MOI.get(model, MOI.VariablePrimal(), x) 4.0
@assert MOI.get(model, MOI.VariablePrimal(), y) 1.0

MOI.get(model, MOI.ConstraintPrimal(), c) # ≈ 4.0
@assert MOI.get(model, MOI.ConstraintPrimal(), c) 4.0
```

Note that duals are not available because the problem was approximated as a MIP.

## QuadraticToBinary.Optimizer Attributes

### Precision

It is possible to change the precision of the approximations to the number `val`,
for all variables:

Expand All @@ -150,6 +140,8 @@ MOI.set(model, QuadraticToBinary.VariablePrecision(), vi, val)
The precision for each varible will be `val * (UB - LB)`. Where `UB` and `LB` are,
respectively, the upper and lower bound of the variable.

### Bounds

For the sake of simplicity, the following two attributes are made available:
`QuadraticToBinary.FallbackUpperBound` and `QuadraticToBinary.FallbackLowerBound`.
As usual, these can be get and set with the `MOI.get` and `MOI.set` methods.
Expand Down
Loading

2 comments on commit 57fb6ab

@joaquimg
Copy link
Owner Author

Choose a reason for hiding this comment

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

@JuliaRegistrator register()

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/52027

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.3.0 -m "<description of version>" 57fb6ab0c512db69afcc887c293a1436dd84af99
git push origin v0.3.0

Please sign in to comment.