ZonoOpt 2.5.0
Loading...
Searching...
No Matches
BnbDataStructures.hpp
Go to the documentation of this file.
1#pragma once
2
14#include <queue>
15#include <mutex>
16#include <utility>
17#include <memory>
18#include <set>
19#include <algorithm>
20#include <vector>
21#include <functional>
22
23#include "ADMM.hpp"
24#include "Defines.hpp"
25#include "Interval.hpp"
27
28namespace ZonoOpt::detail
29{
30 struct MI_data
31 {
32 std::shared_ptr<ADMM_data> admm_data;
33 std::pair<int, int> idx_b; // indices of binary variables
34 bool zero_one_form = false; // true: binary->{0,1}. false: binary->{-1,1}
35 };
36
37 template <typename T>
38 class ThreadSafeAccess
39 {
40 public:
41 ThreadSafeAccess() = default;
42
43 // get method
44 T get() const
45 {
46 std::lock_guard<std::mutex> lock(mtx);
47 return data;
48 }
49
50 // set method
51 void set(const T& value)
52 {
53 std::lock_guard<std::mutex> lock(mtx);
54 data = value;
55 }
56
57 private:
58 mutable std::mutex mtx;
59 T data;
60 };
61
62 template <typename T>
63 class ThreadSafeIncrementable
64 {
65 public:
66 explicit ThreadSafeIncrementable(T value) : data(value) {}
67
68 // get method
69 T get() const
70 {
71 std::lock_guard<std::mutex> lock(mtx);
72 return data;
73 }
74
75 // set method
76 void set(T value)
77 {
78 std::lock_guard<std::mutex> lock(mtx);
79 data = value;
80 }
81
82 // increment
83 void operator+=(T value)
84 {
85 std::lock_guard<std::mutex> lock(mtx);
86 data += value;
87 }
88
89 private:
90 mutable std::mutex mtx;
91 T data;
92 };
93
94 template <typename T, typename Compare=std::less<T>>
95 class ThreadSafeSet
96 {
97 public:
98 // constructor
99 ThreadSafeSet() = default;
100
101 // add val
102 void add(const T val)
103 {
104 std::lock_guard<std::mutex> lock(mtx);
105 data.insert(val);
106 }
107
108 // remove val
109 void remove(const T val)
110 {
111 std::lock_guard<std::mutex> lock(mtx);
112 if (const auto it = data.find(val); it != data.end())
113 data.erase(it);
114 }
115
116 // clear
117 void clear()
118 {
119 std::lock_guard<std::mutex> lock(mtx);
120 data.clear();
121 }
122
123 // get min, bool indicates if valid (i.e. not empty)
124 std::pair<T, bool> get_min() const
125 {
126 std::lock_guard<std::mutex> lock(mtx);
127 if (data.empty())
128 {
129 T val;
130 return std::make_pair(val, false);
131 }
132 else
133 {
134 return std::make_pair(*data.begin(), true);
135 }
136 }
137
138 // get size
139 size_t size() const
140 {
141 std::lock_guard<std::mutex> lock(mtx);
142 return data.size();
143 }
144
145 private:
146 mutable std::mutex mtx;
147 std::set<T, Compare> data;
148 };
149
150 template <typename T>
151 class ThreadSafeVector
152 {
153 public:
154 // constructor
155 ThreadSafeVector() = default;
156
157 // add element
158 void push_back(const T& value)
159 {
160 std::lock_guard<std::mutex> lock(mtx);
161 data.push_back(value);
162 }
163
164 // clear vector
165 void clear()
166 {
167 std::lock_guard<std::mutex> lock(mtx);
168 data.clear();
169 }
170
171 // get size
172 size_t size() const
173 {
174 std::lock_guard<std::mutex> lock(mtx);
175 return data.size();
176 }
177
178 // get vector
179 std::vector<T> get() const
180 {
181 std::lock_guard<std::mutex> lock(mtx);
182 return data;
183 }
184
185 // check containment
186 bool contains(const T& value, std::function<bool(const OptSolution&, const OptSolution&)>& compare) const
187 {
188 std::lock_guard<std::mutex> lock(mtx);
189 for (auto it = data.begin(); it != data.end(); ++it)
190 {
191 if (compare(*it, value)) return true;
192 }
193 return false;
194 }
195
196 // atomically insert value only if no equivalent element already exists; returns true if inserted
197 bool push_back_if_not_contains(const T& value, std::function<bool(const OptSolution&, const OptSolution&)>& compare)
198 {
199 std::lock_guard<std::mutex> lock(mtx);
200 for (const auto& existing : data)
201 {
202 if (compare(existing, value)) return false;
203 }
204 data.push_back(value);
205 return true;
206 }
207
208 private:
209 mutable std::mutex mtx;
210 std::vector<T> data;
211 };
212
213 class Node final : public ADMM_solver
214 {
215 public:
216 // constructors
217 explicit Node(const std::shared_ptr<ADMM_data>& data) : ADMM_solver(data)
218 {
219 // init box constraints
220 this->x_box = *data->x_box;
221 }
222
223 // copy constructor
224 Node(const Node& other) : ADMM_solver(other)
225 {
226 this->x_box = other.x_box;
227 this->solution = other.solution;
228 this->width = this->x_box.width();
229 }
230
231 // public fields
232 OptSolution solution = OptSolution();
233 zono_float width = std::numeric_limits<zono_float>::infinity(); // width of the box
234
235 // run contractor
236 bool run_contractor()
237 {
238 const bool contractor_feasible = this->startup(this->x_box, this->solution);
239 this->width = this->x_box.width();
240 return contractor_feasible;
241 }
242
243 // fix bound
244 bool fix_bound(int ind, const zono_float val)
245 {
246 // update bounds
247 this->x_box.set_element(ind, Interval(val, val));
248
249 // run contractor
250 const bool contractor_feasible = this->startup(this->x_box, this->solution, {ind});
251 this->width = this->x_box.width();
252 return contractor_feasible;
253 }
254
255 // solve
256 void solve(std::atomic<bool>* stop = nullptr)
257 {
258 this->solve_core(this->x_box, this->solution, stop);
259 }
260
261 // update convergence tolerances
262 void update_convergence_tolerances(const zono_float eps_prim, const zono_float eps_dual)
263 {
264 this->eps_prim = eps_prim;
265 this->eps_dual = eps_dual;
266 }
267
268 // get box
269 const Box& get_box() const
270 {
271 return x_box;
272 }
273
274 private:
275 // members
276 Box x_box;
277 };
278
279 template <typename T, typename Compare=std::less<T>>
280 class PriorityQueuePrunable final : public std::priority_queue<T, std::vector<T>, Compare>
281 {
282 public:
283 // constructor
284 explicit PriorityQueuePrunable(const Compare& comp = Compare()) : std::priority_queue<
285 T, std::vector<T>, Compare>(comp)
286 {
287 }
288
289 // prune queue
290 void prune(const T& t)
291 {
292 auto it_prune = std::remove_if(this->c.begin(), this->c.end(), [&](const T& item)
293 {
294 return this->comp(item, t);
295 });
296 if (it_prune != this->c.end())
297 {
298 this->c.erase(it_prune, this->c.end());
299 std::make_heap(this->c.begin(), this->c.end(), this->comp);
300 }
301 }
302
303 // bottom
304 const T& bottom() const
305 {
306 return this->c.back();
307 }
308
309 // pop top
310 T pop_top()
311 {
312 T top = std::move(this->c.front());
313 this->pop();
314 return top;
315 }
316
317 // clear
318 void clear()
319 {
320 while (!this->empty())
321 this->pop();
322 }
323
324 };
325} // namespace ZonoOpt::detail
Convex and mixed-integer ADMM implementations used within ZonoOpt.
Defines for ZonoOpt library.
Interval class.
Optimization settings and solution data structures for ZonoOpt library.
#define zono_float
Defines the floating-point type used in ZonoOpt.
Definition Defines.hpp:22