JuliaQUBO / QUBO.jl

A Julia Ecosystem for Quadratic Unconstrained Binary Optimization
https://juliaqubo.github.io/QUBO.jl/
Other
32 stars 2 forks source link

Adding Constraints and Penalties at the same time. #7

Open dvnoble opened 9 months ago

dvnoble commented 9 months ago

Hello!

I'd like to know how can I add a constraint and set its penalty at the same time. Which is the best way of doing that? Can I do it in batches (adding many constraints and their respective penalties in a single command)?

I haven't found an answer in the documentation, sorry if I missed something.

Thanks

pedromxavier commented 1 month ago

The way to define the penalty factor for a constraint is through the ToQUBO.Attributes.ConstraintEncodingPenaltyHint() atttribute, as in

julia> using JuMP, QUBO

julia> model = Model(ToQUBO.Optimizer)
A JuMP Model
├ solver: Virtual QUBO Model
├ objective_sense: FEASIBILITY_SENSE
├ num_variables: 0
├ num_constraints: 0
└ Names registered in the model: none

julia> @variable(model, 0 <= x[1:3] <= 10)
3-element Vector{VariableRef}:
 x[1]
 x[2]
 x[3]

julia> @constraint(model, c, x[1] + x[2] + x[3] <= 10)
c : x[1] + x[2] + x[3] ≤ 10

julia> set_attribute(c, ToQUBO.Attributes.ConstraintEncodingPenaltyHint(), 20.0)

julia> optimize!(model)