From f78c989d8c133d035738922fc95484ea69b4d6d6 Mon Sep 17 00:00:00 2001
From: Johan Gronqvist <johan.gronqvist@control.lth.se>
Date: Mon, 12 Aug 2024 13:55:34 +0200
Subject: [PATCH] Update acceptance criteria in convergence rate gradient SDP
 problems

---
 src/Gradients.jl | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/src/Gradients.jl b/src/Gradients.jl
index 747342a..9affe8c 100644
--- a/src/Gradients.jl
+++ b/src/Gradients.jl
@@ -2156,7 +2156,7 @@ end
 
 
 
-function solve_deviation_problem(base_problem, deviation_problem, ζ²_val; P_val = nothing, always_accept_optimal = false, accept_slow_progress = false, abstol, timer)
+function solve_deviation_problem(base_problem, deviation_problem, ζ²_val; P_val = nothing, always_accept_optimal = false, always_accept_slow_progress = false, accept_slow_progress_if_improved = false, abstol, timer)
     tr = LinearAlgebra.tr
 
     (; sys_deviation, P_deviation, δP, δζ², Ms_deviation ) = deviation_problem
@@ -2203,9 +2203,9 @@ function solve_deviation_problem(base_problem, deviation_problem, ζ²_val; P_va
     prim_status = JuMP.primal_status(sys_deviation.model)
     value_improved = value(Float64, δζ², timer=timer) >= -abstol
     
-    if always_accept_optimal && term_status == JuMP.OPTIMAL
+    if (always_accept_optimal && term_status == JuMP.OPTIMAL) || (always_accept_slow_progress && term_status == JuMP.SLOW_PROGRESS)
         true
-    elseif term_status == JuMP.OPTIMAL || prim_status == JuMP.FEASIBLE_POINT || (accept_slow_progress && term_status == JuMP.SLOW_PROGRESS)
+    elseif term_status == JuMP.OPTIMAL || prim_status == JuMP.FEASIBLE_POINT || (accept_slow_progress_if_improved && term_status == JuMP.SLOW_PROGRESS)
         value_improved
     else
         false
@@ -2286,12 +2286,12 @@ function convergence_rate_gradient(sys :: DiscreteTimeSystem, x :: Signal, vars;
         P = value(base_problem.P_base, timer=timer)
         if base_success != true
             deviation_problem = prep_deviation_problem(sys, base_problem, x; margin=margin, timer=timer)
-            deviation_success = solve_deviation_problem(base_problem, deviation_problem, ζ²; accept_slow_progress = true, always_accept_optimal = true, abstol=abstol, timer=timer)
+            deviation_success = solve_deviation_problem(base_problem, deviation_problem, ζ²; accept_slow_progress_if_improved = true, always_accept_optimal = true, abstol=abstol, timer=timer)
 
             (; δP, sys_deviation, δζ² ) = deviation_problem
             P = P + value(δP, timer=timer)
             if !deviation_success
-                @warn "Initial re-solving convergence-rate problem failed with termination status $(JuMP.termination_status(deviation_problem.sys_deviation.model)). Returning zero gradient. Note: This is a bug and should never happen." maxlog=100
+                @warn "Re-solving convergence-rate problem failed with termination status $(JuMP.termination_status(deviation_problem.sys_deviation.model)). Returning zero gradient. Note: This is due to non-reproducibility in JuMP or solver." maxlog=100
                 return (; conv_rate = √(ζ²), grads = Dict( k => zero(shape(k)) for k in vars))
             end
         end
@@ -2308,7 +2308,7 @@ function convergence_rate_gradient(sys :: DiscreteTimeSystem, x :: Signal, vars;
 
         # First, without δP
         deviation_problem = prep_deviation_problem(sys, base_problem, x; allow_δP = false, margin=margin, timer=timer)
-        deviation_success = solve_deviation_problem(base_problem, deviation_problem, ζ²; P_val = P, always_accept_optimal = true, accept_slow_progress = true, abstol=abstol, timer=timer)
+        deviation_success = solve_deviation_problem(base_problem, deviation_problem, ζ²; P_val = P, always_accept_optimal = true, always_accept_slow_progress = true, abstol=abstol, timer=timer)
         
         if !deviation_success
             @warn "Solving deviation problem without δP failed with termination status $(JuMP.termination_status(deviation_problem.sys_deviation.model)). Returning zero gradient." maxlog=100
-- 
GitLab