From 0883ba406c154b9108b0ffeb2a0dc161ca5c3eef Mon Sep 17 00:00:00 2001
From: Michiel Cottaar <michiel.cottaar@ndcn.ox.ac.uk>
Date: Wed, 21 Feb 2024 19:24:11 +0000
Subject: [PATCH] Apply random kicks to aid convergence

---
 src/build_sequences.jl | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)

diff --git a/src/build_sequences.jl b/src/build_sequences.jl
index 03437a1..cbd0cc7 100644
--- a/src/build_sequences.jl
+++ b/src/build_sequences.jl
@@ -1,5 +1,5 @@
 module BuildSequences
-import JuMP: Model, optimizer_with_attributes, optimize!, AbstractJuMPScalar, value, solution_summary, termination_status, LOCALLY_SOLVED, OPTIMAL, num_variables
+import JuMP: Model, optimizer_with_attributes, optimize!, AbstractJuMPScalar, value, solution_summary, termination_status, LOCALLY_SOLVED, OPTIMAL, num_variables, all_variables, set_start_value, ALMOST_LOCALLY_SOLVED
 import Ipopt
 import Juniper
 import ..Scanners: Scanner, gradient_strength
@@ -55,7 +55,20 @@ function build_sequence(f::Function, scanner::Union{Nothing, Scanner}, model::Mo
         sequence = f()
         if optimise
             if !iszero(num_variables(model))
-                optimize!(model)
+                for attempt in 1:100
+                    optimize!(model)
+                    if termination_status(model) in (LOCALLY_SOLVED, OPTIMAL)
+                        println("Success after $(attempt-1) restarts.")
+                        break
+                    else
+                        old_values = value.(all_variables(model))
+                        size_kick = 0.2 / attempt
+                        new_values = old_values .* (2 .* size_kick .* rand(length(old_values)) .+ 1. .- size_kick)
+                        for (var, v) in zip(all_variables(model), new_values)
+                            set_start_value(var, v)
+                        end
+                    end
+                end
                 if !(termination_status(model) in (LOCALLY_SOLVED, OPTIMAL))
                     @warn "Optimisation did not report successful convergence. Please check the output sequence."
                     println(solution_summary(model))
-- 
GitLab