ZonoOpt v2.1.0
Loading...
Searching...
No Matches
BranchAndBound.hpp
Go to the documentation of this file.
1#ifndef ZONOOPT_BRANCH_AND_BOUND_
2#define ZONOOPT_BRANCH_AND_BOUND_
3
15#include <thread>
16#include <atomic>
17#include <mutex>
18#include <condition_variable>
19#include <sstream>
20#include <iomanip>
21#include <variant>
22#include <memory_resource>
23#include <cmath>
24
25#include "BnbDataStructures.hpp"
27#include "ADMM.hpp"
28
29namespace ZonoOpt::detail
30{
31 class BranchAndBound
32 {
33 public:
34 explicit BranchAndBound(const MI_data& data);
35
36 // solve
37 OptSolution solve();
38
39 // branch and bound where all possible solutions are returned
40 std::pair<std::vector<OptSolution>, OptSolution> multi_solve(int max_sols = std::numeric_limits<int>::max());
41
42 // warmstart - only used for ADMM-FP
43 void warmstart(const Eigen::Vector<zono_float, -1>& xi_ws, const Eigen::Vector<zono_float, -1>& u_ws)
44 {
45 this->xi_ws = xi_ws;
46 this->u_ws = u_ws;
47 }
48
49 private:
50 struct NodeDeleter
51 {
52 std::pmr::synchronized_pool_resource* pool_ptr;
53
54 explicit NodeDeleter(std::pmr::synchronized_pool_resource* pool_ptr) : pool_ptr(pool_ptr)
55 {
56 }
57
58 void operator()(Node* node) const
59 {
60 if (node)
61 {
62 node->~Node();
63 pool_ptr->deallocate(node, sizeof(Node), alignof(Node));
64 }
65 }
66 };
67
68 struct NodeCompare
69 {
70 bool operator()(const std::unique_ptr<Node, NodeDeleter>& n1,
71 const std::unique_ptr<Node, NodeDeleter>& n2) const
72 {
73 return n1->solution.J > n2->solution.J;
74 }
75 };
76
77 const MI_data data;
78 std::pmr::synchronized_pool_resource pool;
79 NodeCompare comp;
80
81 PriorityQueuePrunable<std::unique_ptr<Node, NodeDeleter>, NodeCompare> node_queue; // priority queue for nodes
82 mutable std::mutex pq_mtx;
83 std::condition_variable pq_cv_bnb, pq_cv_admm_fp;
84 // condition variables for branch-and-bound and ADMM-FP threads
85
86 bool multi_sol = false;
87 std::shared_ptr<ADMM_data> bnb_data, admm_fp_data; // data for branch-and-bound threads / ADMM-FP threads
88
89 std::atomic<bool> converged = false;
90 std::atomic<bool> done = false;
91 std::atomic<bool> feasible = false; // feasible solution found
92 std::atomic<bool> admm_fp_incumbent = false; // incumbent is from ADMM FP
93 std::atomic<long int> qp_iter = 0; // number of QP iterations
94 std::atomic<int> iter = 0; // number of iterations
95 std::atomic<int> iter_admm_fp = 0; // number of feasibility pump iterations
96 std::atomic<zono_float> J_max = std::numeric_limits<zono_float>::infinity(); // upper bound
97 ThreadSafeAccess<Eigen::Vector<zono_float, -1>> z, x, u; // solution vector
98 std::atomic<zono_float> primal_residual = std::numeric_limits<zono_float>::infinity();
99 std::atomic<zono_float> dual_residual = std::numeric_limits<zono_float>::infinity();
100 ThreadSafeIncrementable<double> total_startup_time{0.0};
101 ThreadSafeIncrementable<double> total_run_time{0.0};
102 ThreadSafeMultiset J_threads; // threads for J values
103 ThreadSafeVector<OptSolution> solutions; // solutions found
104
105 // warmstart variables
106 Eigen::Vector<zono_float, -1> xi_ws, u_ws;
107
108 // allocate nodes
109 std::unique_ptr<Node, NodeDeleter> make_node(const std::shared_ptr<ADMM_data>& admm_data);
110
111 std::unique_ptr<Node, NodeDeleter> clone_node(const std::unique_ptr<Node, NodeDeleter>& other);
112
113 // solver core
114 std::variant<OptSolution, std::pair<std::vector<OptSolution>, OptSolution>> solver_core(
115 int max_sols = std::numeric_limits<int>::max());
116
117 // solve node and branch
118 void solve_and_branch(const std::unique_ptr<Node, NodeDeleter>& node);
119
120 void admm_fp_solve(const std::unique_ptr<ADMM_FP_solver>& node);
121
122 // check if integer feasible, xb is vector of relaxed binary variables
123 bool is_integer_feasible(const Eigen::Ref<const Eigen::Vector<zono_float, -1>> xb) const;
124
125 // most fractional branching
126 void branch_most_frac(const std::unique_ptr<Node, NodeDeleter>& node);
127
128 // loop for multithreading
129 void worker_loop();
130
131 void admm_fp_loop(std::unique_ptr<ADMM_FP_solver>&& node);
132
133 // push node to queue
134 void push_node(std::unique_ptr<Node, NodeDeleter>&& node);
135
136 // prune
137 void prune(zono_float J_prune);
138
139 // check if 2 solutions correspond to the same binaries
140 bool check_bin_equal(const OptSolution& sol1, const OptSolution& sol2) const;
141 };
142} // namespace ZonoOpt::detail
143
144
145#endif
Convex and mixed-integer ADMM implementations used within ZonoOpt.
Data structures for mixed-integer optimization in ZonoOpt library.
Optimization settings and solution data structures for ZonoOpt library.
#define zono_float
Defines the floating-point type used in ZonoOpt.
Definition ZonoOpt.hpp:45