Skip to content

Commit

Permalink
New implementation of constraint programming.
Browse files Browse the repository at this point in the history
  • Loading branch information
dourouc05 committed Feb 10, 2020
1 parent ed22a47 commit 29a47c2
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/JuMP.jl
Original file line number Diff line number Diff line change
Expand Up @@ -861,6 +861,8 @@ include("indicator.jl")
include("complement.jl")
# SDConstraint
include("sd.jl")
# Constraint programming
include("cp.jl")

"""
Base.getindex(m::JuMP.AbstractModel, name::Symbol)
Expand Down
26 changes: 26 additions & 0 deletions src/cp.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Copyright 2017, Iain Dunning, Joey Huchette, Miles Lubin, and contributors
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

# This file extends JuMP to allow constraint programming. It allows constraints
# of the form:
#
# @constraint(model, alldifferent(x, y))

function _build_alldifferent_constraint(
errorf::Function,
F::AbstractArray{<:AbstractJuMPScalar}
)
return VectorConstraint(F, MOI.Complements(length(F)))
end

function parse_one_operator_constraint(
errorf::Function,
::Bool,
::Val{:alldifferent},
F
)
f, parse_code = _MA.rewrite(F)
return parse_code, :(_build_alldifferent_constraint($errorf, $f))
end
14 changes: 13 additions & 1 deletion src/macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,11 @@ function parse_constraint(_error::Function, sense::Symbol, lhs, rhs)
vectorized, parse_one_operator_constraint(_error, vectorized, Val(sense), lhs, rhs)...
end

function parse_constraint(_error::Function, sense::Symbol, F)
(sense, vectorized) = _check_vectorized(sense)
vectorized, parse_one_operator_constraint(_error, vectorized, Val(sense), F)...
end

function parse_ternary_constraint(_error::Function, vectorized::Bool, lb, ::Union{Val{:(<=)}, Val{:(≤)}}, aff, rsign::Union{Val{:(<=)}, Val{:(≤)}}, ub)
newaff, parseaff = _MA.rewrite(aff)
newlb, parselb = _MA.rewrite(lb)
Expand Down Expand Up @@ -310,6 +315,9 @@ end

# TODO: update 3-argument @constraint macro to pass through names like @variable

is_one_argument_constraint(_) = false
is_one_argument_constraint(::Val{:alldifferent}) = true

"""
_constraint_macro(args, macro_name::Symbol, parsefun::Function)
Expand Down Expand Up @@ -368,7 +376,11 @@ function _constraint_macro(args, macro_name::Symbol, parsefun::Function)
# in a function returning `ConstraintRef`s and give it to `Containers.container`.
idxvars, indices = Containers._build_ref_sets(_error, c)

vectorized, parsecode, buildcall = parsefun(_error, x.args...)
if x.args[1] != :call || !is_one_argument_constraint(Val(x.args[1]))
vectorized, parsecode, buildcall = parsefun(_error, x.args...)
else
vectorized, parsecode, buildcall = parsefun(_error, x.args[1], x.args[2:end])
end
_add_kw_args(buildcall, kw_args)
if vectorized
# TODO: Pass through names here.
Expand Down

0 comments on commit 29a47c2

Please sign in to comment.