-
-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathutils.jl
132 lines (122 loc) · 5.38 KB
/
utils.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
function get_maxiters(data)
Iterators.IteratorSize(typeof(DEFAULT_DATA)) isa Iterators.IsInfinite ||
Iterators.IteratorSize(typeof(DEFAULT_DATA)) isa Iterators.SizeUnknown ?
typemax(Int) : length(data)
end
maybe_with_logger(f, logger) = logger === nothing ? f() : Logging.with_logger(f, logger)
function default_logger(logger)
Logging.min_enabled_level(logger) ≤ ProgressLogging.ProgressLevel && return nothing
if Sys.iswindows() || (isdefined(Main, :IJulia) && Main.IJulia.inited)
progresslogger = ConsoleProgressMonitor.ProgressLogger()
else
progresslogger = TerminalLoggers.TerminalLogger()
end
logger1 = LoggingExtras.EarlyFilteredLogger(progresslogger) do log
log.level == ProgressLogging.ProgressLevel
end
logger2 = LoggingExtras.EarlyFilteredLogger(logger) do log
log.level != ProgressLogging.ProgressLevel
end
LoggingExtras.TeeLogger(logger1, logger2)
end
macro withprogress(progress, exprs...)
quote
if $progress
$maybe_with_logger($default_logger($Logging.current_logger())) do
$ProgressLogging.@withprogress $(exprs...)
end
else
$(exprs[end])
end
end |> esc
end
decompose_trace(trace) = trace
function _check_and_convert_maxiters(maxiters)
if !(isnothing(maxiters)) && maxiters <= 0.0
error("The number of maxiters has to be a non-negative and non-zero number.")
elseif !(isnothing(maxiters))
return convert(Int, round(maxiters))
end
end
function _check_and_convert_maxtime(maxtime)
if !(isnothing(maxtime)) && maxtime <= 0.0
error("The maximum time has to be a non-negative and non-zero number.")
elseif !(isnothing(maxtime))
return convert(Float32, maxtime)
end
end
# RetCode handling for BBO and others.
using SciMLBase: ReturnCode
# Define a dictionary to map regular expressions to ReturnCode values
const STOP_REASON_MAP = Dict(
r"Delta fitness .* below tolerance .*" => ReturnCode.Success,
r"Fitness .* within tolerance .* of optimum" => ReturnCode.Success,
r"CONVERGENCE: NORM_OF_PROJECTED_GRADIENT_<=_PGTOL" => ReturnCode.Success,
r"^CONVERGENCE: REL_REDUCTION_OF_F_<=_FACTR\*EPSMCH\s*$" => ReturnCode.Success,
r"Terminated" => ReturnCode.Terminated,
r"MaxIters|MAXITERS_EXCEED|Max number of steps .* reached" => ReturnCode.MaxIters,
r"MaxTime|TIME_LIMIT" => ReturnCode.MaxTime,
r"Max time" => ReturnCode.MaxTime,
r"DtLessThanMin" => ReturnCode.DtLessThanMin,
r"Unstable" => ReturnCode.Unstable,
r"InitialFailure" => ReturnCode.InitialFailure,
r"ConvergenceFailure|ITERATION_LIMIT" => ReturnCode.ConvergenceFailure,
r"Infeasible|INFEASIBLE|DUAL_INFEASIBLE|LOCALLY_INFEASIBLE|INFEASIBLE_OR_UNBOUNDED" => ReturnCode.Infeasible,
r"STOP: TOTAL NO. of ITERATIONS REACHED LIMIT" => ReturnCode.MaxIters,
r"STOP: TOTAL NO. of f AND g EVALUATIONS EXCEEDS LIMIT" => ReturnCode.MaxIters,
r"STOP: ABNORMAL_TERMINATION_IN_LNSRCH" => ReturnCode.Unstable,
r"STOP: ERROR INPUT DATA" => ReturnCode.InitialFailure,
r"STOP: FTOL.TOO.SMALL" => ReturnCode.ConvergenceFailure,
r"STOP: GTOL.TOO.SMALL" => ReturnCode.ConvergenceFailure,
r"STOP: XTOL.TOO.SMALL" => ReturnCode.ConvergenceFailure,
r"STOP: TERMINATION" => ReturnCode.Terminated,
r"Optimization completed" => ReturnCode.Success,
r"Convergence achieved" => ReturnCode.Success,
r"ROUNDOFF_LIMITED" => ReturnCode.Success
)
# Function to deduce ReturnCode from a stop_reason string using the dictionary
function deduce_retcode(stop_reason::String)
for (pattern, retcode) in STOP_REASON_MAP
if occursin(pattern, stop_reason)
return retcode
end
end
@warn "Unrecognized stop reason: $stop_reason. Defaulting to ReturnCode.Default."
return ReturnCode.Default
end
# Function to deduce ReturnCode from a Symbol
function deduce_retcode(retcode::Symbol)
if retcode == :Default || retcode == :DEFAULT
return ReturnCode.Default
elseif retcode == :Success || retcode == :EXACT_SOLUTION_LEFT ||
retcode == :FLOATING_POINT_LIMIT || retcode == :true || retcode == :OPTIMAL ||
retcode == :LOCALLY_SOLVED || retcode == :ROUNDOFF_LIMITED ||
retcode == :SUCCESS ||
retcode == :STOPVAL_REACHED || retcode == :FTOL_REACHED ||
retcode == :XTOL_REACHED
return ReturnCode.Success
elseif retcode == :Terminated
return ReturnCode.Terminated
elseif retcode == :MaxIters || retcode == :MAXITERS_EXCEED ||
retcode == :MAXEVAL_REACHED
return ReturnCode.MaxIters
elseif retcode == :MaxTime || retcode == :TIME_LIMIT || retcode == :MAXTIME_REACHED
return ReturnCode.MaxTime
elseif retcode == :DtLessThanMin
return ReturnCode.DtLessThanMin
elseif retcode == :Unstable
return ReturnCode.Unstable
elseif retcode == :InitialFailure
return ReturnCode.InitialFailure
elseif retcode == :ConvergenceFailure || retcode == :ITERATION_LIMIT
return ReturnCode.ConvergenceFailure
elseif retcode == :Failure || retcode == :false
return ReturnCode.Failure
elseif retcode == :Infeasible || retcode == :INFEASIBLE ||
retcode == :DUAL_INFEASIBLE || retcode == :LOCALLY_INFEASIBLE ||
retcode == :INFEASIBLE_OR_UNBOUNDED
return ReturnCode.Infeasible
else
return ReturnCode.Failure
end
end