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

Only add Float64 constraints #80

Merged
merged 2 commits into from
Feb 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 25 additions & 11 deletions src/constraints.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,19 @@ DGAE{C,V,P}() where {C,V,P} = zero(DGAE{C,V,P})

Base.convert(::Type{PAE{C}}, aff::GAEv{C}) where {C} = PAE{C}(aff, GAEp{C}(zero(C)))

function Base.convert(::Type{PAE{T}}, aff::PAE) where {T}
return PAE{T}(
GAEv{T}(
T(aff.v.constant),
Pair{VariableRef,T}[k => T(v) for (k, v) in aff.v.terms],
),
GAEp{T}(
T(aff.p.constant),
Pair{ParameterRef,T}[k => T(v) for (k, v) in aff.p.terms],
),
)
end

function JuMP.map_coefficients_inplace!(f::Function, a::PAE)
map_coefficients_inplace!(f, a.v)
# The iterator remains valid if existing elements are updated.
Expand Down Expand Up @@ -156,37 +169,38 @@ end
# Build constraint
# ------------------------------------------------------------------------------

function JuMP.build_constraint(_error::Function, aff::PAE, set::S) where S <: Union{MOI.LessThan,MOI.GreaterThan,MOI.EqualTo}
offset = aff.v.constant
function JuMP.build_constraint(
::Function,
aff::PAE{Float64},
set::Union{MOI.LessThan,MOI.GreaterThan,MOI.EqualTo},
)
shifted_set = MOIU.shift_constant(set, -aff.v.constant)
aff.v.constant = 0.0
shifted_set = MOIU.shift_constant(set, -offset)
return JuMP.ScalarConstraint(aff, shifted_set)
end

function JuMP.build_constraint(_error::Function, aff::PAE, lb, ub)
JuMP.build_constraint(_error, aff, MOI.Interval(lb, ub))
function JuMP.build_constraint(
errf::Function,
aff::PAE,
set::Union{MOI.LessThan,MOI.GreaterThan,MOI.EqualTo},
)
return build_constraint(errf, convert(PAE{Float64}, aff), set)
end

function JuMP.add_constraint(m::JuMP.Model, c::PAEC{S}, name::String="") where S

# build LinearConstraint
c_lin = JuMP.ScalarConstraint(c.func.v, c.set)

# JuMP´s standard add_constrint
cref = JuMP.add_constraint(m, c_lin, name)

data = _getparamdata(m)::_ParameterData

# needed for lazy get dual
if lazy_duals(data)
for (p, coef) in c.func.p.terms
push!(data.constraints_map[index(p)], ParametrizedConstraintRef(cref, coef))
end
end

# save the parameter part of a parametric affine expression
_get_param_dict(data, S)[cref] = c.func.p

return cref
end

Expand Down
2 changes: 1 addition & 1 deletion src/macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function JuMP.build_variable(_error::Function, info::JuMP.VariableInfo, ::Param)
if !info.has_fix
return ParameterValue(0.0)
else
return ParameterValue(info.fixed_value)
return ParameterValue(convert(Float64, info.fixed_value))
end
end

Expand Down
9 changes: 9 additions & 0 deletions test/tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,15 @@ function test_mutable_operate(args...)
@test isequal(ex, a)
end

function test_float(args...)
model = ModelWithParams()
@variable(model, p == 1, Param())
@variable(model, x)
@expression(model, ex, 2 * p + 1)
@constraint(model, ex <= 0)
# @constraint(model, ex >= x)
end

function runtests(optimizer)
for name in names(@__MODULE__; all = true)
if startswith("$(name)", "test_")
Expand Down