Skip to content

Commit

Permalink
Only add Float64 constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
odow committed Feb 25, 2021
1 parent e4a27ae commit 3f3afad
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
33 changes: 22 additions & 11 deletions src/constraints.jl
Original file line number Diff line number Diff line change
Expand Up @@ -156,37 +156,48 @@ 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},
)
aff2 = PAE{Float64}(
GAEv{Float64}(
aff.v.constant,
Pair{VariableRef,Float64}[k => Float64(v) for (k, v) in aff.v.terms],
),
GAEp{Float64}(
aff.p.constant,
Pair{ParameterRef,Float64}[k => Float64(v) for (k, v) in aff.p.terms],
),
)
return build_constraint(errf, aff2, 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(data, 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 @@ -14,7 +14,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 @@ -566,6 +566,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

0 comments on commit 3f3afad

Please sign in to comment.