ZonoOpt 2.5.0
Loading...
Searching...
No Matches
SolverDataStructures.hpp
Go to the documentation of this file.
1#pragma once
2
14#include <limits>
15#include <memory>
16#include <sstream>
17#include <string>
18
19#include "Eigen/Dense"
20
21#include "Defines.hpp"
22
23namespace ZonoOpt
24{
38 {
39 virtual ~SolverSettings() = default;
40
44 virtual std::unique_ptr<SolverSettings> clone() const = 0;
45
56 virtual void verify_available() const {}
57
63 virtual std::string solver_name() const = 0;
64 };
65
71 {
72 // general settings
73
75 bool verbose = false;
76
79
81 double t_max = std::numeric_limits<double>::max();
82
83 // ADMM settings
84
86 int k_max_admm = 5000;
87
89 zono_float rho = static_cast<zono_float>(10.0);
90
92 zono_float eps_dual = static_cast<zono_float>(1e-2);
93
95 zono_float eps_prim = static_cast<zono_float>(1e-3);
96
98 int k_inf_check = 10;
99
101 bool inf_norm_conv = true;
102
105
108
109 // mixed integer settings
110
112 int search_mode = 0;
113
115 bool polish = true;
116
119
122
124 zono_float eps_r = static_cast<zono_float>(1e-2);
125
127 zono_float eps_a = static_cast<zono_float>(1e-1);
128
130 int k_max_bnb = static_cast<int>(1e5);
131
134
137
140
142 int max_nodes = static_cast<int>(1e5);
143
146
149
152
154 int k_max_admm_fp_ph1 = 10000;
155
157 int k_max_admm_fp_ph2 = 90000;
158
161
163 zono_float eps_perturb = static_cast<zono_float>(1e-3);
164
166 int k_restart = 5000;
167
169 bool enable_rng_seed = false;
170
172 unsigned int rng_seed = 0;
173
174 // polymorphic copy
175 std::unique_ptr<SolverSettings> clone() const override
176 {
177 return std::make_unique<OptSettings>(*this);
178 }
179
180 // validity check
186 bool settings_valid() const
187 {
188 const bool general_valid = t_max > 0 && verbosity_interval > 0;
189 const bool admm_valid = (rho > 0 && k_max_admm > 0 &&
190 eps_dual >= 0 && eps_prim >= 0 && k_inf_check > 0 && contractor_iter > 0);
191 const bool mi_valid = (eps_r >= 0 && eps_a >= 0 && eps_dual_search > 0 && eps_prim_search > 0 &&
192 k_max_bnb > 0 && n_threads_bnb >= 0 && n_threads_admm_fp >= 0 && max_nodes > 0 &&
196 (search_mode == 0 || search_mode == 1) &&
197 eps_perturb > 0);
198
199 return (general_valid && admm_valid && mi_valid);
200 }
201
207 std::string print() const
208 {
209 std::stringstream ss;
210 ss << "OptSettings structure: " << std::endl;
211 ss << " verbose: " << (verbose ? "true" : "false") << std::endl;
212 ss << " verbosity_interval: " << verbosity_interval << std::endl;
213 ss << " t_max: " << t_max << std::endl;
214 ss << " k_max_admm: " << k_max_admm << std::endl;
215 ss << " rho: " << rho << std::endl;
216 ss << " eps_dual: " << eps_dual << std::endl;
217 ss << " eps_prim: " << eps_prim << std::endl;
218 ss << " k_inf_check: " << k_inf_check << std::endl;
219 ss << " inf_norm_conv: " << (inf_norm_conv ? "true" : "false") << std::endl;
220 ss << " use_interval_contractor: " << (use_interval_contractor ? "true" : "false") << std::endl;
221 ss << " contractor_iter: " << contractor_iter << std::endl;
222 ss << " search_mode: " << search_mode << std::endl;
223 ss << " polish: " << polish << std::endl;
224 ss << " eps_dual_search: " << eps_dual_search << std::endl;
225 ss << " eps_prim_search: " << eps_prim_search << std::endl;
226 ss << " eps_r: " << eps_r << std::endl;
227 ss << " eps_a: " << eps_a << std::endl;
228 ss << " k_max_bnb: " << k_max_bnb << std::endl;
229 ss << " n_threads_bnb: " << n_threads_bnb << std::endl;
230 ss << " n_threads_admm_fp: " << n_threads_admm_fp << std::endl;
231 ss << " single_threaded_admm_fp: " << (single_threaded_admm_fp ? "true" : "false") << std::endl;
232 ss << " max_nodes: " << max_nodes << std::endl;
233 ss << " contractor_tree_search_depth: " << contractor_tree_search_depth << std::endl;
234 ss << " enable_perturb_admm_fp: " << (enable_perturb_admm_fp ? "true" : "false") << std::endl;
235 ss << " k_max_admm_fp_ph1: " << k_max_admm_fp_ph1 << std::endl;
236 ss << " k_max_admm_fp_ph2: " << k_max_admm_fp_ph2 << std::endl;
237 ss << " cycle_detection_buffer_size: " << cycle_detection_buffer_size << std::endl;
238 ss << " eps_perturb: " << eps_perturb << std::endl;
239 ss << " k_restart: " << k_restart << std::endl;
240 ss << " enable_rng_seed: " << (enable_rng_seed ? "true" : "false") << std::endl;
241 ss << " rng_seed: " << rng_seed << std::endl;
242 ss << " enable_restart_admm_fp: " << (enable_restart_admm_fp ? "true" : "false") << std::endl;
243 return ss.str();
244 }
245
246 std::string solver_name() const override
247 {
248 return "ZonoOpt";
249 }
250
255 OptSettings() = default;
256 };
257
271 {
272 virtual ~ExternalSolverResults() = default;
273
275 virtual std::shared_ptr<ExternalSolverResults> clone() const = 0;
276
278 virtual std::string print() const { return "ExternalSolverResults"; }
279 };
280
292 {
293 // general — always populated
294
296 Eigen::Vector<zono_float, -1> z;
297
299 zono_float J = -std::numeric_limits<zono_float>::infinity();
300
302 double run_time = 0.0;
303
305 bool converged = false;
306
308 bool infeasible = false;
309
310 // admm-specific — meaningful only when the internal solver was used.
311
313 double startup_time = 0.0;
314
316 int iter = 0;
317
319 Eigen::Vector<zono_float, -1> x;
320
322 Eigen::Vector<zono_float, -1> u;
323
325 zono_float primal_residual = std::numeric_limits<zono_float>::infinity();
326
328 zono_float dual_residual = std::numeric_limits<zono_float>::infinity();
329
330 // external-solver-specific — populated only when an external solver was used.
331
335 std::shared_ptr<ExternalSolverResults> external_results;
336
342 std::string print() const
343 {
344 std::stringstream ss;
345 ss << "OptSolution structure:" << std::endl;
346 ss << " z: vector of length " << z.size() << std::endl;
347 ss << " J: " << J << std::endl;
348 ss << " run_time: " << run_time << std::endl;
349 ss << " startup_time: " << startup_time << std::endl;
350 ss << " iter: " << iter << std::endl;
351 ss << " converged: " << (converged ? "true" : "false") << std::endl;
352 ss << " infeasible: " << (infeasible ? "true" : "false") << std::endl;
353 ss << " x: vector of length " << x.size() << std::endl;
354 ss << " u: vector of length " << u.size() << std::endl;
355 ss << " primal_residual: " << primal_residual << std::endl;
356 ss << " dual_residual: " << dual_residual << std::endl;
358 ss << " external_results: " << external_results->print() << std::endl;
359 return ss.str();
360 }
361 };
362
363 // ---- Program-wide default solver settings -------------------------------
364 //
365 // ZonoOpt's optimization methods take a SolverSettings argument whose default
366 // is whatever set_default_solver_settings() last installed. This lets a user
367 // switch the entire library to (e.g.) Gurobi by calling
368 // set_default_solver_settings(GurobiSettings{});
369 // once at program startup, instead of passing GurobiSettings to every call.
370 //
371 // Initial default is OptSettings() (internal ADMM/branch-and-bound).
372 //
373 // Not thread-safe: set_default_solver_settings should be called from a single
374 // thread at startup before parallel optimization begins. Existing const
375 // SolverSettings& references obtained from get_default_solver_settings() are
376 // invalidated by a subsequent set_default_solver_settings() call.
377
378 namespace detail
379 {
380 inline std::unique_ptr<SolverSettings>& default_settings_storage()
381 {
382 static std::unique_ptr<SolverSettings> p = std::make_unique<OptSettings>();
383 return p;
384 }
385 } // namespace detail
386
393 {
394 return *detail::default_settings_storage();
395 }
396
407 inline void set_default_solver_settings(const SolverSettings& settings)
408 {
409 settings.verify_available();
410 detail::default_settings_storage() = settings.clone();
411 }
412
419 {
421 Eigen::Vector<zono_float, -1> z;
422
424 Eigen::Vector<zono_float, -1> u;
425 };
426
431 {
433 bool remove_redundancy = false;
434
436 int n_leaves = std::numeric_limits<int>::max();
437
440 };
441
442} // end namespace ZonoOpt
Defines for ZonoOpt library.
#define zono_float
Defines the floating-point type used in ZonoOpt.
Definition Defines.hpp:22
Definition ZonoOpt.hpp:51
const SolverSettings & get_default_solver_settings()
Returns a reference to the current program-wide default solver settings.
Definition SolverDataStructures.hpp:392
void set_default_solver_settings(const SolverSettings &settings)
Replaces the program-wide default solver settings with a polymorphic copy of settings.
Definition SolverDataStructures.hpp:407
Abstract base for external-solver-specific solution metadata.
Definition SolverDataStructures.hpp:271
virtual std::shared_ptr< ExternalSolverResults > clone() const =0
Polymorphic copy; required so callers can deep-copy an OptSolution.
virtual std::string print() const
Human-readable summary of the solver-specific fields.
Definition SolverDataStructures.hpp:278
virtual ~ExternalSolverResults()=default
Parameters controlling leaf enumeration in get_leaves, complement, and set_diff.
Definition SolverDataStructures.hpp:431
int contractor_iter
number of interval contractor iterations to run if using remove_redundancy
Definition SolverDataStructures.hpp:439
int n_leaves
maximum number of leaves to find
Definition SolverDataStructures.hpp:436
bool remove_redundancy
call remove_redundancy on each identified leaf
Definition SolverDataStructures.hpp:433
Settings for the internal ZonoOpt ADMM / branch-and-bound solver.
Definition SolverDataStructures.hpp:71
bool polish
flag to perform solution polishing
Definition SolverDataStructures.hpp:115
int k_inf_check
check infeasibility every k_inf_check iterations
Definition SolverDataStructures.hpp:98
int k_restart
perform restart operation if primal residual does not improve over this many iterations in ADMM-FP
Definition SolverDataStructures.hpp:166
bool use_interval_contractor
flag to use interval contractor for constraint tightening / implication
Definition SolverDataStructures.hpp:104
double t_max
max time for optimization
Definition SolverDataStructures.hpp:81
int k_max_bnb
max number of branch-and-bound iterations
Definition SolverDataStructures.hpp:130
unsigned int rng_seed
rng seed for ADMM-FP
Definition SolverDataStructures.hpp:172
bool enable_rng_seed
enable rng seed for ADMM-FP
Definition SolverDataStructures.hpp:169
std::unique_ptr< SolverSettings > clone() const override
Polymorphic copy. Must be overridden by every concrete subclass.
Definition SolverDataStructures.hpp:175
int cycle_detection_buffer_size
in ADMM-FP, this is the max size of the buffer that checks for cycles
Definition SolverDataStructures.hpp:160
zono_float eps_r
relative convergence tolerance
Definition SolverDataStructures.hpp:124
bool enable_restart_admm_fp
enable restarts (significant perturbations) in ADMM-FP
Definition SolverDataStructures.hpp:151
zono_float eps_a
absolute convergence tolerance
Definition SolverDataStructures.hpp:127
bool verbose
display optimization progress
Definition SolverDataStructures.hpp:75
std::string print() const
displays settings as string
Definition SolverDataStructures.hpp:207
int n_threads_bnb
max threads for branch and bound
Definition SolverDataStructures.hpp:133
zono_float eps_prim_search
primal residual convergence tolerance during branch and bound and search
Definition SolverDataStructures.hpp:121
int contractor_tree_search_depth
when applying interval contractor in branch and bound, this is how deep to search the constraint tree...
Definition SolverDataStructures.hpp:145
zono_float eps_prim
primal convergence tolerance
Definition SolverDataStructures.hpp:95
int k_max_admm_fp_ph2
max ADMM iterations for ADMM-FP phase 2 (no objective)
Definition SolverDataStructures.hpp:157
OptSettings()=default
OptSettings constructor.
bool inf_norm_conv
use infinity norm for convergence check (if false, scaled 2-norm is used)
Definition SolverDataStructures.hpp:101
int verbosity_interval
print every verbose_interval iterations
Definition SolverDataStructures.hpp:78
zono_float rho
admm penalty parameter, higher prioritizes feasibility during iterations, lower prioritizes optimalit...
Definition SolverDataStructures.hpp:89
int k_max_admm_fp_ph1
max ADMM iterations for ADMM-FP phase 1 (objective included)
Definition SolverDataStructures.hpp:154
int k_max_admm
max convex admm iterations
Definition SolverDataStructures.hpp:86
zono_float eps_dual
dual convergence tolerance
Definition SolverDataStructures.hpp:92
std::string solver_name() const override
Return name of the solver backend selected by this settings type, e.g., "ZonoOpt",...
Definition SolverDataStructures.hpp:246
bool enable_perturb_admm_fp
enable perturbations in ADMM-FP
Definition SolverDataStructures.hpp:148
int max_nodes
terminate if more than this many nodes are in branch and bound queue
Definition SolverDataStructures.hpp:142
zono_float eps_perturb
relative tolerance for cycle detection, triggers perturbation
Definition SolverDataStructures.hpp:163
int search_mode
0: best first, 1: best dive
Definition SolverDataStructures.hpp:112
zono_float eps_dual_search
dual residual convergence tolerance during branch and bound and search
Definition SolverDataStructures.hpp:118
bool settings_valid() const
Checks whether settings struct is valid.
Definition SolverDataStructures.hpp:186
bool single_threaded_admm_fp
enables single-threaded ADMM-FP solution, overrides n_threads_bnb, n_threads_admm_fp
Definition SolverDataStructures.hpp:139
int contractor_iter
number of interval contractor iterations
Definition SolverDataStructures.hpp:107
int n_threads_admm_fp
max threads for ADMM-FP
Definition SolverDataStructures.hpp:136
Solution data structure for optimization routines in ZonoOpt library.
Definition SolverDataStructures.hpp:292
zono_float dual_residual
dual residual, corresponds to optimality
Definition SolverDataStructures.hpp:328
bool infeasible
true if optimization problem is provably infeasible
Definition SolverDataStructures.hpp:308
Eigen::Vector< zono_float, -1 > x
ADMM primal variable, approximately equal to z when converged.
Definition SolverDataStructures.hpp:319
Eigen::Vector< zono_float, -1 > z
solution vector
Definition SolverDataStructures.hpp:296
double run_time
time to compute solution (seconds)
Definition SolverDataStructures.hpp:302
std::string print() const
displays solution as string
Definition SolverDataStructures.hpp:342
zono_float J
objective
Definition SolverDataStructures.hpp:299
zono_float primal_residual
primal residual, corresponds to feasibility
Definition SolverDataStructures.hpp:325
std::shared_ptr< ExternalSolverResults > external_results
Definition SolverDataStructures.hpp:335
int iter
number of iterations
Definition SolverDataStructures.hpp:316
double startup_time
time to factorize matrices and run interval contractors
Definition SolverDataStructures.hpp:313
bool converged
true if optimization has converged (proved optimal for external solvers; satisfied tolerances for int...
Definition SolverDataStructures.hpp:305
Eigen::Vector< zono_float, -1 > u
ADMM dual variable.
Definition SolverDataStructures.hpp:322
Abstract base for all solver settings.
Definition SolverDataStructures.hpp:38
virtual void verify_available() const
Verify the solver backend selected by this settings type is usable.
Definition SolverDataStructures.hpp:56
virtual std::string solver_name() const =0
Return name of the solver backend selected by this settings type, e.g., "ZonoOpt",...
virtual std::unique_ptr< SolverSettings > clone() const =0
Polymorphic copy. Must be overridden by every concrete subclass.
virtual ~SolverSettings()=default
Warm start parameters for optimization routines in ZonoOpt library.
Definition SolverDataStructures.hpp:419
Eigen::Vector< zono_float, -1 > z
primal variable
Definition SolverDataStructures.hpp:421
Eigen::Vector< zono_float, -1 > u
dual variable
Definition SolverDataStructures.hpp:424