Skip to content

Commit

Permalink
Add getValue for norms
Browse files Browse the repository at this point in the history
  • Loading branch information
IainNZ committed Mar 3, 2016
1 parent 5f9f366 commit 072b1ca
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/norms.jl
Expand Up @@ -71,6 +71,7 @@ _build_norm(Lp, terms::Vector{GenericAffExpr}) = _build_norm(Lp, [terms...])
# Alias for AffExprs. Short-hand used in operator overloads, etc.
typealias Norm{P} GenericNorm{P,Float64,Variable}

getValue{P,C,V}(n::GenericNorm{P,C,V}) = norm(getValue(n.terms),P)

##########################################################################
# GenericNormExpr
Expand All @@ -94,10 +95,11 @@ typealias GenericSOCExpr{C,V} GenericNormExpr{2,C,V}
# Alias for ‖Ax‖₂ and AffExpr case
typealias SOCExpr GenericSOCExpr{Float64,Variable}

getValue(n::GenericNormExpr) = n.coeff * getValue(n.norm) + getValue(n.aff)

##########################################################################
# GenericSOCConstraint
# Second-order cone constraint of form
# Second-order cone constraint of form
# α||Ax-b||₂ + cᵀx + d ≤ 0
type GenericSOCConstraint{T<:GenericSOCExpr} <: JuMPConstraint
normexpr::T
Expand Down
8 changes: 7 additions & 1 deletion test/qcqpmodel.jl
Expand Up @@ -13,6 +13,9 @@
#############################################################################
using JuMP, FactCheck

# If solvers not loaded, load them (i.e running just these tests)
!isdefined(:lp_solvers) && include("solvers.jl")

facts("[qcqpmodel] Test quad objective (discrete)") do
for solver in quad_mip_solvers
context("With solver $(typeof(solver))") do
Expand Down Expand Up @@ -325,6 +328,8 @@ context("With solver $(typeof(solver))") do
@fact solve(m) --> :Optimal
@fact getObjectiveValue(m) --> roughly(2+sqrt(2), 1e-5)
@fact norm(getValue(x)-[1+sqrt(1/2),1+sqrt(1/2),1]) --> roughly(0, 1e-6)
@fact getValue(norm(x-1)) --> roughly(1, 1e-5)
@fact getValue(norm(x-1)-2) --> roughly(-1, 1e-5)
end; end; end

facts("[qcqpmodel] Test quad problem modification") do
Expand Down Expand Up @@ -367,11 +372,12 @@ context("With solver $(typeof(solver))") do

@setObjective(mod, Max, v)

@addConstraint(mod, norm(x[:]) <= 1)
@addConstraint(mod, norm(x) <= 1)
@addConstraint(mod, v^2 <= u * x[1])

@fact solve(mod) --> :Optimal
@fact getValue(x) --> roughly([1,0,0,0,0], 1e-2)
@fact getValue(u) --> roughly(5, 1e-4)
@fact getValue(v) --> roughly(sqrt(5), 1e-6)
@fact getValue(norm(x)) --> roughly(1, 1e-4)
end; end; end

0 comments on commit 072b1ca

Please sign in to comment.