Skip to content
Snippets Groups Projects
Unverified Commit 1ae20f0d authored by Michiel Cottaar's avatar Michiel Cottaar
Browse files

Refactor out optimisation call with single cost function

parent b5eb24d0
No related branches found
No related tags found
1 merge request!6Robust optimisation
This commit is part of merge request !6. Comments created here will be created in the context of that merge request.
......@@ -24,6 +24,10 @@ function iterate_cost()
end
end
function total_cost_func()
return sum([f for (_, f) in GLOBAL_MODEL[][2]]; init=0.)
end
"""
Wrapper to build a sequence.
......@@ -71,37 +75,13 @@ function build_sequence(f::Function, scanner::Union{Nothing, Scanner}, model::Tu
if optimise
jump_model = GLOBAL_MODEL[][1]
if !iszero(num_variables(jump_model))
prev_cost_func = nothing
for cost_func in iterate_cost()
@objective jump_model Min cost_func
min_objective = Inf
for attempt in 1:n_attempts
if attempt != 1
old_values = value.(all_variables(jump_model))
size_kick = 0.5 / attempt
new_values = old_values .* (2 .* size_kick .* rand(length(old_values)) .+ 1. .- size_kick)
for (var, v) in zip(all_variables(jump_model), new_values)
set_start_value(var, v)
end
end
optimize!(jump_model)
while termination_status(jump_model) == INVALID_MODEL
@variable(jump_model)
optimize!(jump_model)
end
if termination_status(jump_model) in (LOCALLY_SOLVED, OPTIMAL)
if objective_value(jump_model) < min_objective
min_objective = objective_value(jump_model)
elseif isapprox(min_objective, objective_value(jump_model), rtol=1e-6)
break
end
end
end
if !(termination_status(jump_model) in (LOCALLY_SOLVED, OPTIMAL))
println(solution_summary(jump_model))
error("Optimisation failed to converge.")
if !isnothing(prev_cost_func)
@constraint jump_model prev_cost_func == objective_value(jump_model)
end
@constraint jump_model cost_func == objective_value(jump_model)
jump_model.is_model_dirty = false
optimise_with_cost_func(jump_model, cost_func, n_attempts)
prev_cost_func = cost_func
end
end
return fixed(sequence)
......@@ -114,6 +94,37 @@ function build_sequence(f::Function, scanner::Union{Nothing, Scanner}, model::Tu
end
end
function optimise_with_cost_func(jump_model::Model, cost_func, n_attempts)
@objective jump_model Min cost_func
min_objective = Inf
for attempt in 1:n_attempts
if attempt != 1
old_values = value.(all_variables(jump_model))
size_kick = 0.5 / attempt
new_values = old_values .* (2 .* size_kick .* rand(length(old_values)) .+ 1. .- size_kick)
for (var, v) in zip(all_variables(jump_model), new_values)
set_start_value(var, v)
end
end
optimize!(jump_model)
while termination_status(jump_model) == INVALID_MODEL
@variable(jump_model)
optimize!(jump_model)
end
if termination_status(jump_model) in (LOCALLY_SOLVED, OPTIMAL)
if objective_value(jump_model) < min_objective
min_objective = objective_value(jump_model)
elseif isapprox(min_objective, objective_value(jump_model), rtol=1e-6)
break
end
end
end
if !(termination_status(jump_model) in (LOCALLY_SOLVED, OPTIMAL))
println(solution_summary(jump_model))
error("Optimisation failed to converge.")
end
end
function build_sequence(f::Function, scanner::Union{Nothing, Scanner}, optimiser_constructor; optimise=true, n_attempts=100, kwargs...)
if optimise || GLOBAL_MODEL[] == IGNORE_MODEL
model = (
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment