ZonoOpt 2.5.0
Loading...
Searching...
No Matches
BranchAndBound.hpp
Go to the documentation of this file.
1#pragma once
2
14#include <atomic>
15#include <mutex>
16#include <condition_variable>
17#include <variant>
18#include <memory_resource>
19#include <cmath>
20#include <random>
21#include <exception>
22
23#include "BnbDataStructures.hpp"
24#include "Defines.hpp"
26#include "ADMM.hpp"
27
28namespace ZonoOpt::detail
29{
30 class BranchAndBound
31 {
32 public:
33 explicit BranchAndBound(const MI_data& data);
34
35 // solve
36 OptSolution solve();
37
38 // branch and bound where all possible solutions are returned
39 std::pair<std::vector<OptSolution>, OptSolution> multi_solve(int max_sols = std::numeric_limits<int>::max());
40
41 // warmstart - only used for ADMM-FP
42 void warmstart(const Eigen::Vector<zono_float, -1>& xi_ws, const Eigen::Vector<zono_float, -1>& u_ws)
43 {
44 this->xi_ws = xi_ws;
45 this->u_ws = u_ws;
46 }
47
48 private:
49 struct NodeDeleter
50 {
51 std::pmr::synchronized_pool_resource* pool_ptr;
52
53 explicit NodeDeleter(std::pmr::synchronized_pool_resource* pool_ptr) : pool_ptr(pool_ptr)
54 {
55 }
56
57 void operator()(Node* node) const
58 {
59 if (node)
60 {
61 node->~Node();
62 pool_ptr->deallocate(node, sizeof(Node), alignof(Node));
63 }
64 }
65 };
66
67 struct NodeCompare
68 {
69 bool operator()(const std::unique_ptr<Node, NodeDeleter>& n1,
70 const std::unique_ptr<Node, NodeDeleter>& n2) const
71 {
72 return n1->solution.J > n2->solution.J; // min-heap on J
73 }
74 };
75
76 struct JThreadCompare
77 {
78 bool operator()(const std::pair<int, zono_float>& v1,
79 const std::pair<int, zono_float>& v2) const
80 {
81 if (v1.second != v2.second) return v1.second < v2.second;
82 return v1.first < v2.first;
83 }
84 };
85
86 template <typename T, typename Comp=std::less<T>>
87 struct ThreadGuard
88 {
89 ThreadSafeSet<T, Comp>& thread_tags;
90 T tag;
91 bool specified = false;
92
93 explicit ThreadGuard(ThreadSafeSet<T, Comp>& thread_tags): thread_tags(thread_tags)
94 {
95 }
96
97 void specify_tag(const T& tag)
98 {
99 if (!specified)
100 {
101 this->tag = tag;
102 this->specified = true;
103 this->thread_tags.add(tag);
104 }
105 }
106
107 ~ThreadGuard()
108 {
109 if (specified)
110 this->thread_tags.remove(tag);
111 }
112 };
113
114 const MI_data data;
115 std::pmr::synchronized_pool_resource pool;
116 NodeCompare comp;
117
118 PriorityQueuePrunable<std::unique_ptr<Node, NodeDeleter>, NodeCompare> node_queue; // priority queue for nodes
119 PriorityQueuePrunable<std::unique_ptr<Node, NodeDeleter>, NodeCompare> dive_queue; // dive queue for search mode 1
120 mutable std::mutex pq_mtx;
121 mutable std::mutex incumbent_mtx; // guards atomic check-and-update of incumbent (J_max, z, x, u, etc.)
122 std::condition_variable pq_cv_bnb, pq_cv_admm_fp;
123 // condition variables for branch-and-bound and ADMM-FP threads
124
125 bool multi_sol = false;
126 std::shared_ptr<ADMM_data> bnb_data, admm_fp_data; // data for branch-and-bound threads / ADMM-FP threads
127
128 std::atomic<bool> converged = false;
129 std::atomic<bool> done = false;
130 std::atomic<bool> feasible = false; // feasible solution found
131 std::atomic<bool> admm_fp_incumbent = false; // incumbent is from ADMM FP
132 std::atomic<long int> qp_iter = 0; // number of QP iterations
133 std::atomic<int> iter = 0; // number of iterations
134 std::atomic<int> iter_admm_fp = 0; // number of feasibility pump iterations
135 std::atomic<zono_float> J_max = std::numeric_limits<zono_float>::infinity(); // upper bound
136 ThreadSafeAccess<Eigen::Vector<zono_float, -1>> z, x, u; // solution vector
137 std::atomic<zono_float> primal_residual = std::numeric_limits<zono_float>::infinity();
138 std::atomic<zono_float> dual_residual = std::numeric_limits<zono_float>::infinity();
139 ThreadSafeIncrementable<double> total_startup_time{0.0};
140 ThreadSafeIncrementable<double> total_run_time{0.0};
141 ThreadSafeSet<std::pair<int, zono_float>, JThreadCompare> J_threads; // threads for J values
142 ThreadSafeVector<OptSolution> solutions; // solutions found
143 std::uniform_int_distribution<int> uniform_dist{0, std::numeric_limits<int>::max()};
144 std::mt19937 rng{0};
145
146 // first exception thrown by a worker thread or the monitoring loop, rethrown from solve()
147 std::exception_ptr thread_exception;
148 std::mutex exception_mtx;
149
150 // warmstart variables
151 Eigen::Vector<zono_float, -1> xi_ws, u_ws;
152
153 // allocate nodes
154 std::unique_ptr<Node, NodeDeleter> make_node(const std::shared_ptr<ADMM_data>& admm_data);
155
156 std::unique_ptr<Node, NodeDeleter> clone_node(const std::unique_ptr<Node, NodeDeleter>& other);
157
158 // solver core
159 std::variant<OptSolution, std::pair<std::vector<OptSolution>, OptSolution>> solver_core(
160 int max_sols = std::numeric_limits<int>::max());
161
162 // solve node and branch
163 void solve_and_branch(const std::unique_ptr<Node, NodeDeleter>& node);
164
165 void admm_fp_solve(const std::unique_ptr<ADMM_FP_solver>& node);
166
167 // check if integer feasible, xb is vector of relaxed binary variables
168 bool is_integer_feasible(const Eigen::Ref<const Eigen::Vector<zono_float, -1>> xb) const;
169
170 // most fractional branching
171 void branch_most_frac(const std::unique_ptr<Node, NodeDeleter>& node);
172
173 // loop for multithreading
174 void worker_loop();
175
176 void admm_fp_loop(std::unique_ptr<ADMM_FP_solver>&& node);
177
178 // set done and wake all waiting threads; must not be called while holding pq_mtx
179 void stop_threads();
180
181 // store the first exception from any thread and stop the search
182 void record_exception(std::exception_ptr e);
183
184 // push node to queue
185 void push_node(std::unique_ptr<Node, NodeDeleter>&& node);
186
187 // push node to dive queue (search mode 1)
188 void push_dive_node(std::unique_ptr<Node, NodeDeleter>&& node);
189
190 // prune
191 void prune(zono_float J_prune);
192
193 // check if 2 solutions correspond to the same binaries
194 bool check_bin_equal(const OptSolution& sol1, const OptSolution& sol2) const;
195
196 // check whether integer variables are fully specified
197 bool is_box_integer(const Box& box) const;
198
199 // get lower bound
200 zono_float get_lower_bound();
201 };
202} // namespace ZonoOpt::detail
Convex and mixed-integer ADMM implementations used within ZonoOpt.
Data structures for mixed-integer optimization in ZonoOpt library.
Defines for ZonoOpt library.
Optimization settings and solution data structures for ZonoOpt library.
#define zono_float
Defines the floating-point type used in ZonoOpt.
Definition Defines.hpp:22