Skip to content

Commit

Permalink
wip:vector functions
Browse files Browse the repository at this point in the history
  • Loading branch information
jalving committed Aug 7, 2024
1 parent c7d24b1 commit 7d8cefc
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 13 deletions.
24 changes: 13 additions & 11 deletions src/backends/moi_backend.jl
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,19 @@ function MOI.set(
return nothing
end

# TODO: vector affine function
function MOI.set(
backend::GraphMOIBackend,
attr::MOI.ObjectiveFunction{F},
jump_funcs::Vector{<:JuMP.AbstractJuMPScalar},
) where {F<:MOI.AbstractVectorFunction}
vector_moi_func = JuMP.moi_function(jump_funcs)
# TODO: create proper vector affine function
moi_funcs_graph = _create_graph_moi_func(backend, vector_moi_func, jump_funcs)
MOI.set(backend, attr, moi_funcs_graph)
return nothing
end

# element attributes

# By default, just try to get the attribute from the graph MOI backend
Expand Down Expand Up @@ -696,15 +709,6 @@ function _copy_subgraph_backends!(backend::GraphMOIBackend)
sub_backend = graph_backend(subgraph)
for ((element, attr), registered_name) in sub_backend.operator_map
_copy_operator(sub_backend, backend, element, attr, registered_name)
# operator = MOI.get(
# graph_backend(subgraph),
# MOI.UserDefinedFunction(registered_name, attr.arity)
# )
# # set operator on new graph backend
# attr_register = MOI.UserDefinedFunction(registered_name, attr.arity)
# MOI.set(backend, attr_register, element, operator)
# backend.element_attributes[(element, attr)] = tuple(args...)
# backend.operator_map[(element, attr)] = registered_name
end
end
end
Expand All @@ -717,10 +721,8 @@ function _copy_operator(
registered_name::Symbol,
)
operator = MOI.get(src_backend, MOI.UserDefinedFunction(registered_name, attr.arity))
# set operator on new graph backend
attr_register = MOI.UserDefinedFunction(registered_name, attr.arity)
MOI.set(dest_backend.moi_backend, attr_register, operator)
# dest_backend.element_attributes[(element, attr)] = operator
dest_backend.operator_map[(element, attr)] = registered_name
return nothing
end
Expand Down
30 changes: 30 additions & 0 deletions src/jump_interop.jl
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,36 @@ function MOI.ScalarAffineFunction(a::GenericAffExpr{C,<:NodeVariableRef}) where
return MOI.ScalarAffineFunction(terms, a.constant)
end

function _fill_vaf!(
terms::Vector{<:MOI.VectorAffineTerm}, offset::Int, oi::Int, aff::AbstractJuMPScalar
)
i = 1
for (coef, var) in JuMP.linear_terms(aff)
terms[offset + i] = MOI.VectorAffineTerm(
Int64(oi), MOI.ScalarAffineTerm(coef, index(var))
)
i += 1
end
return offset + length(JuMP.linear_terms(aff))
end

function MOI.VectorAffineFunction(affs::Vector{GenericAffExpr{C,NodeVariableRef}}) where {C}
len = 0
for aff in affs
len += length(JuMP.linear_terms(aff))
end
terms = Vector{MOI.VectorAffineTerm{C}}(undef, len)
constant = Vector{C}(undef, length(affs))
offset = 0
for (i, aff) in enumerate(affs)
constant[i] = aff.constant
offset = _fill_vaf!(terms, offset, i, aff)
end
return MOI.VectorAffineFunction(terms, constant)
end

# TODO: function moi_function_type

function JuMP.jump_function_type(
obj::OptiObject, ::Type{MOI.ScalarAffineFunction{C}}
) where {C}
Expand Down
29 changes: 27 additions & 2 deletions src/optigraph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1143,8 +1143,8 @@ end
Retrieve the current objective value on optigraph `graph`.
"""
function JuMP.objective_value(graph::OptiGraph)
return MOI.get(graph_backend(graph), MOI.ObjectiveValue())
function JuMP.objective_value(graph::OptiGraph; result::Int=1)
return MOI.get(graph_backend(graph), MOI.ObjectiveValue(result))
end

"""
Expand Down Expand Up @@ -1203,6 +1203,10 @@ function JuMP.set_objective_sense(graph::OptiGraph, sense::MOI.OptimizationSense
return nothing
end

function JuMP.set_objective_function(graph::AbstractOptiGraph, func)
return error("The objective function `$(func)` is not supported by Plasmo.")
end

"""
JuMP.set_objective_function(graph::OptiGraph, expr::JuMP.AbstractJuMPScalar)
Expand All @@ -1213,6 +1217,13 @@ function JuMP.set_objective_function(graph::OptiGraph, expr::JuMP.AbstractJuMPSc
return nothing
end

function JuMP.set_objective_function(
graph::OptiGraph, funcs::AbstractVector{<:AbstractJuMPScalar}
)
_moi_set_objective_function(graph, funcs)
return nothing
end

function _moi_set_objective_function(graph::OptiGraph, expr::JuMP.AbstractJuMPScalar)
# add variables to backend if using subgraphs
_add_backend_variables(graph_backend(graph), expr)
Expand All @@ -1222,6 +1233,20 @@ function _moi_set_objective_function(graph::OptiGraph, expr::JuMP.AbstractJuMPSc
return nothing
end

# TODO: get vector objective working
function _moi_set_objective_function(
graph::OptiGraph, funcs::AbstractVector{<:AbstractJuMPScalar}
)
# add variables to backend if using subgraphs
for func in funcs
_add_backend_variables(graph_backend(graph), func)
end
# get the moi function made from local node variable indices
func_type = JuMP.moi_function_type(typeof(funcs)) # --> VectorAffineFunction
MOI.set(graph_backend(graph), MOI.ObjectiveFunction{func_type}(), funcs)
return nothing
end

### objective coefficient - linear

"""
Expand Down

0 comments on commit 7d8cefc

Please sign in to comment.