Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix computing duals #1091

Draft
wants to merge 3 commits into
base: v0.7.2.x-release
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 7 additions & 16 deletions src/run_spineopt_basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,7 @@
if ret == 0
try
_save_marginal_values!(m)
_save_bound_marginal_values!(m, v -> _reduced_cost_cplex(v, cplex_model, CPLEX))
_save_bound_marginal_values!(m)

Check warning on line 723 in src/run_spineopt_basic.jl

View check run for this annotation

Codecov / codecov/patch

src/run_spineopt_basic.jl#L723

Added line #L723 was not covered by tests
catch err
@error err
CPLEX.CPXchgprobtype(cplex_model.env, cplex_model.lp, prob_type)
Expand All @@ -731,25 +731,16 @@
ret == 0
end

function _reduced_cost_cplex(v::VariableRef, cplex_model, CPLEX)
m = owner_model(v)
sign = objective_sense(m) == MIN_SENSE ? 1.0 : -1.0
col = Cint(CPLEX.column(cplex_model, index(v)) - 1)
p = Ref{Cdouble}()
CPLEX.CPXgetdj(cplex_model.env, cplex_model.lp, p, col, col)
rc = p[]
sign * rc
end

function _calculate_duals_fallback(m; log_level=3, for_benders=false)
@timelog log_level 1 "Copying model..." (m_dual_lp, ref_map) = copy_model(m)
set_optimizer(m_dual_lp, m.ext[:spineopt].lp_solver)
@log log_level 1 "Set LP solver $(solver_name(m_dual_lp)) for the copy."
if for_benders
@timelog log_level 1 "Relaxing discrete variables..." _relax_discrete_vars!(m, ref_map)
fix_variables = if for_benders
(:bound_units_invested_available, :bound_connections_invested_available, :bound_storages_invested_available)
else
@timelog log_level 1 "Fixing discrete variables..." _relax_discrete_vars!(m, ref_map; and_fix=true)
()
end
@timelog log_level 1 "Relaxing discrete variables..." _relax_discrete_vars!(m, ref_map; fix_variables)
dual_fallback(con) = DualPromise(ref_map[con])
reduced_cost_fallback(var) = ReducedCostPromise(ref_map[var])
_save_marginal_values!(m, dual_fallback)
Expand All @@ -767,7 +758,7 @@
end
end

function _relax_discrete_vars!(m::Model, ref_map::ReferenceMap; and_fix=false)
function _relax_discrete_vars!(m::Model, ref_map::ReferenceMap; fix_variables=())
for (name, var_by_ind) in m.ext[:spineopt].variables
def = m.ext[:spineopt].variables_definition[name]
def[:bin] === def[:int] === nothing && continue
Expand All @@ -781,7 +772,7 @@
else
continue
end
if and_fix
if name in fix_variables
val = _variable_value(var)
fix(ref_var, val; force=true)
end
Expand Down
Loading