Skip to content

Commit

Permalink
correct moi order with dicts
Browse files Browse the repository at this point in the history
  • Loading branch information
votroto committed Mar 12, 2024
1 parent c0886b3 commit 687fdbc
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
14 changes: 6 additions & 8 deletions src/QCQP/MOI_wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -228,16 +228,14 @@ function _subs!(
end
end
if !isempty(old_var)
old_var_map = Dict(zip(old_var, new_var))
all_old_vars = MP.variables(p.polynomial)

updated_vars = [get(old_var_map, v, v) for v in all_old_vars]
variable_map = collect(zip(p.variables, updated_vars))
sort!(variable_map, by = x -> x[2], rev = true)
moi_variables = [x[1] for x in variable_map]
to_old_map = Dict(zip(new_var, old_var))
to_moi_map = Dict(zip(MP.variables(p.polynomial), p.variables))

Check warning on line 232 in src/QCQP/MOI_wrapper.jl

View check run for this annotation

Codecov / codecov/patch

src/QCQP/MOI_wrapper.jl#L231-L232

Added lines #L231 - L232 were not covered by tests

poly = MP.subs(p.polynomial, old_var => new_var)
p = PolyJuMP.ScalarPolynomialFunction(poly, moi_variables)
all_new_vars = MP.variables(poly)
moi_vars = [to_moi_map[get(to_old_map, v, v)] for v in all_new_vars]

Check warning on line 236 in src/QCQP/MOI_wrapper.jl

View check run for this annotation

Codecov / codecov/patch

src/QCQP/MOI_wrapper.jl#L235-L236

Added lines #L235 - L236 were not covered by tests

p = PolyJuMP.ScalarPolynomialFunction(poly, moi_vars)

Check warning on line 238 in src/QCQP/MOI_wrapper.jl

View check run for this annotation

Codecov / codecov/patch

src/QCQP/MOI_wrapper.jl#L238

Added line #L238 was not covered by tests
end
return p, index_to_var
end
Expand Down
7 changes: 3 additions & 4 deletions src/nl_to_polynomial.jl
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ function _to_polynomial!(
return MP.div(a, b)

Check warning on line 98 in src/nl_to_polynomial.jl

View check run for this annotation

Codecov / codecov/patch

src/nl_to_polynomial.jl#L97-L98

Added lines #L97 - L98 were not covered by tests
else
return a / b

Check warning on line 100 in src/nl_to_polynomial.jl

View check run for this annotation

Codecov / codecov/patch

src/nl_to_polynomial.jl#L100

Added line #L100 was not covered by tests
end
end
elseif _is_variable(expr)
return _to_polynomial!(d, T, operands[1])
else
Expand All @@ -113,9 +113,8 @@ function _to_polynomial(expr, ::Type{T}) where {T}
end

function _scalar_polynomial(d::Dict{K,V}, ::Type{T}, poly) where {T,K,V}
variable_map = collect(d)
sort!(variable_map, by = x -> x[2], rev = true)
variables = [x[1] for x in variable_map]
inv = Dict(v => k for (k, v) in d)
variables = [inv[v] for v in MP.variables(poly)]
P = MP.polynomial_type(V, T)
return ScalarPolynomialFunction{T,P}(poly, variables)
end
Expand Down

0 comments on commit 687fdbc

Please sign in to comment.