Skip to content

Commit

Permalink
Fix some issues with fix! (#294)
Browse files Browse the repository at this point in the history
* Fix multiply divide's treatment of ConstVexity() variables

* Add test

* Move test, add test for 228

* remove extra newline
  • Loading branch information
ericphanson authored and ararslan committed May 18, 2019
1 parent 6bccf6a commit 7858cec
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/atoms/affine/multiply_divide.jl
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,13 @@ function conic_form!(x::MultiplyAtom, unique_conic_forms::UniqueConicForms=Uniqu
objective = const_multiplier * objective

# left matrix multiplication
elseif x.children[1].head == :constant
elseif vexity(x.children[1]) == ConstVexity()
objective = conic_form!(x.children[2], unique_conic_forms)
objective = kron(sparse(1.0I, x.size[2], x.size[2]), x.children[1].value) * objective
objective = kron(sparse(1.0I, x.size[2], x.size[2]), evaluate(x.children[1])) * objective
# right matrix multiplication
else
objective = conic_form!(x.children[1], unique_conic_forms)
objective = kron(transpose(x.children[2].value), sparse(1.0I, x.size[1], x.size[1])) * objective
objective = kron(transpose(evaluate(x.children[2])), sparse(1.0I, x.size[1], x.size[1])) * objective
end
cache_conic_form!(unique_conic_forms, x, objective)
end
Expand Down Expand Up @@ -158,7 +158,7 @@ function conic_form!(x::DotMultiplyAtom, unique_conic_forms::UniqueConicForms=Un
# promote the size of the coefficient matrix, so eg
# 3 .* x
# works regardless of the size of x
coeff = x.children[1].value .* ones(size(x.children[2]))
coeff = evaluate(x.children[1]) .* ones(size(x.children[2]))
# promote the size of the variable
# we've previously ensured neither x nor y is 1x1
# and that the sizes are compatible,
Expand Down
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ push!(solvers, SCSSolver(verbose=0, eps=1e-6))

@testset "Convex" begin
include("test_utilities.jl")
include("test_const.jl")
include("test_affine.jl")
include("test_lp.jl")
include("test_socp.jl")
Expand Down
35 changes: 35 additions & 0 deletions test/test_const.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
@testset "Constant variables: $solver" for solver in solvers

@testset "Issue #166" begin
# Issue #166
α = Variable(5)
fix!(α, ones(5,1))

# has const vexity, but not at the head
c = (rand(5,5) * α) * ones(1,5)

β = Variable(5)
β.value = ones(5)

problem = minimize(sum(c * β), [β >= 0])
solve!(problem, solver)
@test problem.optval evaluate(sum(c * β)) atol=TOL
@test problem.optval 0.0 atol=TOL
@test β.value zeros(5) atol=TOL
end

@testset "Issue #228" begin
x = Variable(2)
y = Variable(2)
fix!(x, [1 1]')
prob = minimize(y'*(x+[2 2]'), [y>=0])
solve!(prob, solver)
@test prob.optval 0.0 atol = TOL

prob = minimize(x'*y, [y>=0])
solve!(prob, solver)
@test prob.optval 0.0 atol = TOL
end


end

0 comments on commit 7858cec

Please sign in to comment.