Skip to content

Commit

Permalink
fix issue with quadratic functions
Browse files Browse the repository at this point in the history
  • Loading branch information
jalving committed Feb 4, 2024
1 parent ccf1c89 commit ca6c00f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 25 deletions.
1 change: 0 additions & 1 deletion src/jump_interop.jl
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,6 @@ function JuMP.GenericQuadExpr{C,NodeVariableRef}(
graph::OptiGraph,
f::MOI.ScalarQuadraticFunction,
) where {C}

# affine terms
quad = JuMP.GenericQuadExpr{C,NodeVariableRef}(
JuMP.GenericAffExpr{C,NodeVariableRef}(
Expand Down
33 changes: 11 additions & 22 deletions src/moi_backend.jl
Original file line number Diff line number Diff line change
Expand Up @@ -265,11 +265,17 @@ function _create_graph_moi_func(
coeff = term[1]
var1 = term[2]
var2 = term[3]

# TODO: debug why i have to do this; the coeff is 1, but it prints as 2
if var1 == var2
coeff *= 2
end

var_idx_1 = backend.element_to_graph_map[var1]
var_idx_2 = backend.element_to_graph_map[var2]

moi_func_graph.quadratic_terms[i] = MOI.ScalarQuadraticTerm{Float64}(
coeff,
coeff,
var_idx_1,
var_idx_2
)
Expand All @@ -280,7 +286,10 @@ function _create_graph_moi_func(
coeff = term[1]
var = term[2]
backend_var_idx = backend.element_to_graph_map[var]
moi_func_graph.affine_terms[i] = MOI.ScalarAffineTerm{Float64}(coeff, backend_var_idx)
moi_func_graph.affine_terms[i] = MOI.ScalarAffineTerm{Float64}(
coeff,
backend_var_idx
)
end
return moi_func_graph
end
Expand All @@ -307,26 +316,6 @@ function _create_graph_moi_func(
return moi_func_graph
end

# function _update_nonlinear_func!(
# backend::GraphMOIBackend,
# moi_func::MOI.ScalarNonlinearFunction,
# jump_func::JuMP.GenericNonlinearExpr
# )
# for i = 1:length(jump_func.args)
# jump_arg = jump_func.args[i]
# moi_arg = moi_func.args[i]
# if jump_arg isa Number
# continue
# elseif typeof(jump_arg) == NodeVariableRef
# moi_func.args[i] = backend.element_to_graph_map[jump_arg]
# else
# new_func = _create_graph_moi_func(backend, moi_arg, jump_arg)
# moi_func.args[i] = new_func
# end
# end
# return
# end

# add variables to a backend for linking across subgraphs
function _add_backend_variables(
backend::GraphMOIBackend,
Expand Down
2 changes: 0 additions & 2 deletions src/optigraph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -338,13 +338,11 @@ function _moi_set_objective_function(
expr::JuMP.GenericQuadExpr{C,NodeVariableRef}
) where C <: Real
moi_func = JuMP.moi_function(expr)

# add variables to backend if using subgraphs
_add_backend_variables(graph_backend(graph), expr)

# update the moi function variable indices
graph_moi_func = _create_graph_moi_func(graph_backend(graph), moi_func, expr)

MOI.set(
graph_backend(graph),
MOI.ObjectiveFunction{MOI.ScalarQuadraticFunction{C}}(),
Expand Down

0 comments on commit ca6c00f

Please sign in to comment.