Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into px/indicator
Browse files Browse the repository at this point in the history
  • Loading branch information
pedromxavier committed Nov 26, 2023
2 parents fffb1cc + 7aa2379 commit 6896b56
Show file tree
Hide file tree
Showing 17 changed files with 307 additions and 98 deletions.
20 changes: 10 additions & 10 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
name = "ToQUBO"
uuid = "9a412ddf-83fa-43b6-9748-7843c851aa65"
name = "ToQUBO"
uuid = "9a412ddf-83fa-43b6-9748-7843c851aa65"
authors = ["pedromxavier <[email protected]>", "pedroripper <[email protected]>", "joaquimg <[email protected]>", "AndradeTiago <[email protected]>", "bernalde <[email protected]>"]
version = "0.1.7"
version = "0.1.8-dev"

[deps]
MathOptInterface = "b8f27783-ece8-5eb3-8dc8-9495eed66fee"
MathOptInterface = "b8f27783-ece8-5eb3-8dc8-9495eed66fee"
PseudoBooleanOptimization = "c8fa9a04-bc42-452d-8558-dc51757be744"
QUBOTools = "60eb5b62-0a39-4ddc-84c5-97d2adff9319"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
TOML = "fa267f1f-6049-4f14-aa54-33bafae1ed76"
QUBOTools = "60eb5b62-0a39-4ddc-84c5-97d2adff9319"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
TOML = "fa267f1f-6049-4f14-aa54-33bafae1ed76"

[compat]
MathOptInterface = "1"
MathOptInterface = "1"
PseudoBooleanOptimization = "0.2"
QUBOTools = "0.9"
julia = "1.9"
QUBOTools = "0.9"
julia = "1.9"
2 changes: 1 addition & 1 deletion src/attributes/compiler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ When set, this boolean flag guarantees that every coefficient in the final formu
struct Discretize <: CompilerAttribute end

function MOI.get(model::Optimizer, ::Discretize)::Bool
return get(model.compiler_settings, :discretize, false)
return get(model.compiler_settings, :discretize, true)
end

function MOI.set(model::Optimizer, ::Discretize, flag::Bool)
Expand Down
6 changes: 3 additions & 3 deletions src/compiler/build.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,23 @@ function objective_function(model::Virtual.Model{T}, ::AbstractArchitecture) whe
end

for (ci, g) in model.g
ρ = model.ρ[ci]
ρ = MOI.get(model, Attributes.ConstraintEncodingPenalty(), ci)

for (ω, c) in g
model.H[ω] += ρ * c
end
end

for (vi, h) in model.h
θ = model.θ[vi]
θ = MOI.get(model, Attributes.VariableEncodingPenalty(), vi)

for (ω, c) in h
model.H[ω] += θ * c
end
end

for (ci, s) in model.s
η = model.η[ci]
η = MOI.get(model, Attributes.SlackVariableEncodingPenalty(), ci)

for (ω, c) in s
model.H[ω] += η * c
Expand Down
3 changes: 0 additions & 3 deletions src/compiler/compiler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,6 @@ function compile!(model::Virtual.Model{T}, arch::AbstractArchitecture) where {T}
# Add Regular Constraints
constraints!(model, arch)

# Add Encoding Constraints
encoding_constraints!(model, arch)

# Compute penalties
penalties!(model, arch)

Expand Down
61 changes: 45 additions & 16 deletions src/compiler/constraints.jl
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ function constraint(
# Scalar Affine Equality Constraint: g(x) = a'x - b = 0
g = _parse(model, f, s, arch)

PBO.discretize!(g)
if Attributes.discretize(model)
PBO.discretize!(g)
end

# Bounds & Slack Variable
l, u = PBO.bounds(g)
Expand Down Expand Up @@ -141,7 +143,9 @@ function constraint(
# Scalar Affine Inequality Constraint: g(x) = a'x - b ≤ 0
g = _parse(model, f, s, arch)

PBO.discretize!(g)
if Attributes.discretize(model)
PBO.discretize!(g)
end

# Bounds & Slack Variable
l, u = PBO.bounds(g)
Expand All @@ -157,9 +161,12 @@ function constraint(
end

# Slack Variable
e = Attributes.slack_variable_encoding_method(model, ci)
S = (zero(T), abs(l))
z = Encoding.encode!(model, ci, e, S)
z = if Attributes.discretize(model)
variable_ℤ!(model, ci, S)
else
variable_ℝ!(model, ci, S)
end

for (ω, c) in Virtual.expansion(z)
g[ω] += c
Expand Down Expand Up @@ -217,7 +224,9 @@ function constraint(
# Scalar Affine Inequality Constraint: g(x) = a'x - b ≥ 0
g = _parse(model, f, s, arch)

PBO.discretize!(g)
if Attributes.discretize(model)
PBO.discretize!(g)
end

# Bounds & Slack Variable
l, u = PBO.bounds(g)
Expand All @@ -233,9 +242,12 @@ function constraint(
end

# Slack Variable
e = Attributes.slack_variable_encoding_method(model, ci)
S = (zero(T), abs(u))
z = Encoding.encode!(model, ci, e, S)
z = if Attributes.discretize(model)
variable_ℤ!(model, ci, S)
else
variable_ℝ!(model, ci, S)
end

for (ω, c) in Virtual.expansion(z)
g[ω] -= c
Expand Down Expand Up @@ -279,7 +291,9 @@ function constraint(
# Scalar Quadratic Equality Constraint: g(x) = x' Q x + a' x - b = 0
g = _parse(model, f, s, arch)

PBO.discretize!(g)
if Attributes.discretize(model)
PBO.discretize!(g)
end

# Bounds & Slack Variable
l, u = PBO.bounds(g)
Expand Down Expand Up @@ -339,7 +353,9 @@ function constraint(
# Scalar Quadratic Inequality Constraint: g(x) = x' Q x + a' x - b ≤ 0
g = _parse(model, f, s, arch)

PBO.discretize!(g)
if Attributes.discretize(model)
PBO.discretize!(g)
end

# Bounds & Slack Variable
l, u = PBO.bounds(g)
Expand All @@ -358,9 +374,12 @@ function constraint(
end

# Slack Variable
e = Attributes.slack_variable_encoding_method(model, ci)
S = (zero(T), abs(l))
z = Encoding.encode!(model, ci, e, S)
z = if Attributes.discretize(model)
variable_ℤ!(model, ci, S)
else
variable_ℝ!(model, ci, S)
end

for (ω, c) in Virtual.expansion(z)
g[ω] += c
Expand Down Expand Up @@ -407,7 +426,9 @@ function constraint(
# Scalar Quadratic Inequality Constraint: g(x) = x' Q x + a' x - b ≥ 0
g = _parse(model, f, s, arch)

PBO.discretize!(g)
if Attributes.discretize(model)
PBO.discretize!(g)
end

# Bounds & Slack Variable
l, u = PBO.bounds(g)
Expand All @@ -423,9 +444,12 @@ function constraint(
end

# Slack Variable
e = Attributes.slack_variable_encoding_method(model, ci)
S = (zero(T), abs(u))
z = Encoding.encode!(model, ci, e, S)
z = if Attributes.discretize(model)
variable_ℤ!(model, ci, S)
else
variable_ℝ!(model, ci, S)
end

for (ω, c) in Virtual.expansion(z)
g[ω] -= c
Expand Down Expand Up @@ -488,12 +512,14 @@ function constraint(
end

g[w] = one(T)

# Tell the compiler that quadratization is necessary
MOI.set(model, Attributes.Quadratize(), true)
end
end

# Slack variable
e = Encoding.Mirror{T}()
z = Encoding.encode!(model, ci, e)
z = variable_𝔹!(model, ci)

for (ω, c) in Virtual.expansion(z)
g[ω] += c
Expand All @@ -503,6 +529,7 @@ function constraint(

return g^2 + h
end
<<<<<<< HEAD

function constraint(
model::Virtual.Model{T},
Expand Down Expand Up @@ -592,3 +619,5 @@ function encoding_constraints!(model::Virtual.Model{T}, ::AbstractArchitecture)

return nothing
end
=======
>>>>>>> origin/master
16 changes: 16 additions & 0 deletions src/compiler/error.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,20 @@ end

function compilation_error(msg::Union{Nothing,String} = nothing)
throw(CompilationError(msg))
<<<<<<< HEAD
=======

return nothing
end

function compilation_error!(model::Virtual.Model, msg::Union{Nothing,String} = nothing; status::AbstractString = "")
# Update model status
MOI.set(model, Attributes.CompilationStatus(), MOI.OTHER_ERROR)
MOI.set(model, MOI.RawStatusString(), status)

# Throw error
compilation_error(msg)

return nothing
>>>>>>> origin/master
end
6 changes: 3 additions & 3 deletions src/compiler/penalties.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function penalties!(model::Virtual.Model{T}, ::AbstractArchitecture) where {T}
ρ = σ */ ϵ + β)
end

model.ρ[ci] = ρ
MOI.set(model, Attributes.ConstraintEncodingPenalty(), ci, ρ)
end

for (vi, h) in model.h
Expand All @@ -24,7 +24,7 @@ function penalties!(model::Virtual.Model{T}, ::AbstractArchitecture) where {T}
θ = σ */ ϵ + β)
end

model.θ[vi] = θ
MOI.set(model, Attributes.VariableEncodingPenalty(), vi, θ)
end

for (ci, s) in model.s
Expand All @@ -35,7 +35,7 @@ function penalties!(model::Virtual.Model{T}, ::AbstractArchitecture) where {T}
η = σ */ ϵ + β)
end

model.η[ci] = η
MOI.set(model, Attributes.SlackVariableEncodingPenalty(), ci, η)
end

return nothing
Expand Down
Loading

0 comments on commit 6896b56

Please sign in to comment.