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

Add a settable optimize_hook with access to the current scenario. #219

Merged
merged 3 commits into from
May 29, 2019

Conversation

odow
Copy link
Owner

@odow odow commented May 20, 2019

No description provided.

@codecov
Copy link

codecov bot commented May 20, 2019

Codecov Report

Merging #219 into master will decrease coverage by 0.02%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #219      +/-   ##
==========================================
- Coverage    93.3%   93.27%   -0.03%     
==========================================
  Files          11       12       +1     
  Lines        1075     1101      +26     
==========================================
+ Hits         1003     1027      +24     
- Misses         72       74       +2
Impacted Files Coverage Δ
src/algorithm.jl 98.24% <100%> (-0.2%) ⬇️
src/user_interface.jl 97.26% <100%> (+0.04%) ⬆️
src/SDDP.jl 100% <0%> (ø) ⬆️
src/plugins/headers.jl 100% <0%> (ø) ⬆️
src/visualization/dashboard.jl 0% <0%> (ø)
src/visualization/spaghetti_plot.jl 82.6% <0%> (+19.27%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 33e7024...f9550cf. Read the comment docs.

@odow odow merged commit 88af622 into master May 29, 2019
@odow odow deleted the od/scenarios branch May 29, 2019 17:24
@peraaslid
Copy link

Can I use this to add new restriction before/after each iteration? I'd like to add DC powerflow active losses as "cuts" iterative. Could you give an example of how to use the pre-/post-optimize hook?

@odow
Copy link
Owner Author

odow commented Jun 5, 2019

The current example is here:

SDDP.jl/test/algorithm.jl

Lines 150 to 176 in 9b5ebc9

@testset "optimize_hook" begin
model = SDDP.LinearPolicyGraph(
stages = 2,
optimizer = with_optimizer(GLPK.Optimizer),
lower_bound = 0.0
) do sp, t
@variable(sp, x >= 0, SDDP.State, initial_value = 0)
@stageobjective(sp, x.out)
end
pre_optimize_called = 0
post_optimize_called = 0
node = model[1]
SDDP.pre_optimize_hook(node) do model, node, state, noise, scenario_path, require_duals
pre_optimize_called = 1
return pre_optimize_called
end
SDDP.post_optimize_hook(node) do ret
post_optimize_called = ret + 2
return
end
SDDP.solve_subproblem(
model, node, Dict(:x => 0.0), nothing, Tuple{Int, Any}[(1, nothing)];
require_duals = false
)
@test pre_optimize_called == 1
@test post_optimize_called == 3
end

For each node in the tree, you can add a hook. To loop through nodes, you can go

for (index, node) in model.nodes
    # add hook
end

pre_optimize hook is a function that takes the same arguments as solve_subproblem. If it returns a value, and post_optimize_hook is set, then the returned value will be passed as a the sole argument to post_optimize_hook.

function my_pre_optimize_hook(model, node, state, noise, scenario_path, require_duals)
    sp = node.subproblem
    active_loss_constraint = @constraint(sp, sp[:x] <= 1) 
    return active_loss_constraint
end

function my_post_optimize_hook(ret)
    # `ret` is the `active_loss_constraint` reference returned from `my_pre_optimize_hook`
    println("Active loss constraint = $(ret)")
    return
end

for (index, node) in model.nodes
    SDDP.pre_optimize_hook(my_pre_optimize_hook, node)
    SDDP.post_optimize_hook(my_post_optimize_hook, node)
end

Note that post_optimize_hook doesn't get called immediately after JuMP.optimize!. I need that for my own work, but I could be compelled to change it if you need.

SDDP.jl/src/algorithm.jl

Lines 293 to 295 in 9b5ebc9

if node.post_optimize_hook !== nothing
node.post_optimize_hook(pre_optimize_ret)
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants