Skip to content

Commit 4183d91

Browse files
authored
Fix ConstraintPrimal for Zeros constraints (#130)
1 parent 607e5f2 commit 4183d91

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/MOI_wrapper/MOI_wrapper.jl

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ mutable struct _Solution
5757
dual_eq::Vector{pfloat}
5858
dual_ineq::Vector{pfloat}
5959
slack::Vector{pfloat}
60+
slack_eq::Vector{pfloat}
6061
objective_value::pfloat
6162
dual_objective_value::pfloat
6263
solve_time::pfloat
@@ -71,6 +72,7 @@ function _Solution()
7172
pfloat[],
7273
pfloat[],
7374
pfloat[],
75+
pfloat[],
7476
NaN,
7577
NaN,
7678
NaN,
@@ -221,6 +223,9 @@ function _optimize!(dest::Optimizer, src::OptimizerCache)
221223
PermutedExponentialCone,
222224
}(),
223225
)
226+
# This is before ECOS_setup because ECOS modifies the constant vectors
227+
# in-place!
228+
slack_eq = copy(Ab.constants)
224229
inner = ECOS_setup(
225230
A.n,
226231
G.m,
@@ -248,12 +253,19 @@ function _optimize!(dest::Optimizer, src::OptimizerCache)
248253
ret_val = ECOS_solve(inner)
249254
ecos_prob = unsafe_load(inner)::pwork
250255
stat = unsafe_load(ecos_prob.info)::stats
256+
x = copy(unsafe_wrap(Array, ecos_prob.x, ecos_prob.n))
257+
for col in 1:A.n
258+
for i in A.colptr[col]:(A.colptr[col+1]-1)
259+
slack_eq[A.rowval[i+1]+1] += A.nzval[i+1] * x[col]
260+
end
261+
end
251262
dest.sol = _Solution(
252263
ret_val,
253-
copy(unsafe_wrap(Array, ecos_prob.x, ecos_prob.n)),
264+
x,
254265
copy(unsafe_wrap(Array, ecos_prob.y, ecos_prob.p)),
255266
copy(unsafe_wrap(Array, ecos_prob.z, ecos_prob.m)),
256267
copy(unsafe_wrap(Array, ecos_prob.s, ecos_prob.m)),
268+
slack_eq,
257269
max_sense ? -stat.pcost : stat.pcost,
258270
max_sense ? -stat.dcost : stat.dcost,
259271
stat.tsetup + stat.tsolve,
@@ -403,7 +415,7 @@ function MOI.get(
403415
ci::MOI.ConstraintIndex{MOI.VectorAffineFunction{pfloat},MOI.Zeros},
404416
)
405417
MOI.check_result_index_bounds(optimizer, attr)
406-
return zeros(length(_rows(optimizer, ci)))
418+
return optimizer.sol.slack_eq[_rows(optimizer, ci)]
407419
end
408420

409421
function MOI.get(

0 commit comments

Comments
 (0)