idol
A C++ Framework for Optimization
Loading...
Searching...
No Matches
BranchAndBound.h
1//
2// Created by henri on 21/03/23.
3//
4
5#ifndef IDOL_BRANCHANDBOUND_H
6#define IDOL_BRANCHANDBOUND_H
7
8#include <memory>
9#include "idol/general/optimizers/OptimizerFactory.h"
10#include "Optimizers_BranchAndBound.h"
11#include "idol/mixed-integer/optimizers/branch-and-bound/callbacks/BranchAndBoundCallbackFactory.h"
12#include "idol/mixed-integer/optimizers/branch-and-bound/callbacks/BranchAndBoundCallback.h"
13#include "idol/mixed-integer/optimizers/branch-and-bound/callbacks/CallbackAsBranchAndBoundCallback.h"
14#include "idol/mixed-integer/optimizers/callbacks/CallbackFactory.h"
15#include "idol/mixed-integer/optimizers/branch-and-bound/nodes/DefaultNodeInfo.h"
16#include "idol/mixed-integer/optimizers/branch-and-bound/logs/Factory.h"
17#include "idol/mixed-integer/optimizers/branch-and-bound/logs/Info.h"
18
19namespace idol {
20 template<class NodeT>
21 class BranchAndBound;
22}
23
28template<class NodeT = idol::DefaultNodeInfo>
29class idol::BranchAndBound : public OptimizerFactoryWithDefaultParameters<BranchAndBound<NodeT>> {
30 std::unique_ptr<OptimizerFactory> m_relaxation_optimizer_factory;
31 std::unique_ptr<BranchingRuleFactory<NodeT>> m_branching_rule_factory;
32 std::unique_ptr<NodeSelectionRuleFactory<NodeT>> m_node_selection_rule_factory;
33 std::unique_ptr<Logs::BranchAndBound::Factory<NodeT>> m_logger_factory;
34
35 std::list<std::unique_ptr<BranchAndBoundCallbackFactory<NodeT>>> m_callbacks;
36
37 std::optional<unsigned int> m_subtree_depth;
38 std::optional<unsigned int> m_log_frequency;
39public:
46 template<class ReturnT, class T> using only_if_has_Strategy = typename std::pair<typename T::template Strategy<NodeT>, ReturnT>::second_type;
47
56 BranchAndBound() = default;
57
62 BranchAndBound(const BranchAndBound& t_rhs);
63
77
78 void set_node_optimizer(const OptimizerFactory& t_node_optimizer);
79
80 BranchAndBound<NodeT>& operator+=(const OptimizerFactory& t_node_optimizer);
81
94
111 template<class BranchingRuleFactoryT>
112 only_if_has_Strategy<BranchAndBound<NodeT>&, BranchingRuleFactoryT> with_branching_rule(const BranchingRuleFactoryT& t_branching_rule);
113
125
142 template<class NodeSelectionRuleFactoryT>
143 only_if_has_Strategy<BranchAndBound<NodeT>&, NodeSelectionRuleFactoryT> with_node_selection_rule(const NodeSelectionRuleFactoryT& t_node_selection_rule);
144
145 BranchAndBound(BranchAndBound&&) noexcept = default;
146 BranchAndBound& operator=(const BranchAndBound&) = delete;
147 BranchAndBound& operator=(BranchAndBound&&) noexcept = delete;
148
149 Optimizer *operator()(const Model &t_model) const override;
150
151 [[nodiscard]] OptimizerFactory *clone() const override;
152
173 BranchAndBound<NodeT>& with_subtree_depth(unsigned int t_depth);
174
175 BranchAndBound<NodeT>& with_logger(const Logs::BranchAndBound::Factory<NodeT>& t_log_factory);
176
189 BranchAndBound<NodeT>& add_callback(const BranchAndBoundCallbackFactory<NodeT> & t_callback);
190
205 BranchAndBound<NodeT>& add_callback(const CallbackFactory& t_callback);
206
207};
208
209template<class NodeT>
210void idol::BranchAndBound<NodeT>::set_node_optimizer(const idol::OptimizerFactory &t_node_optimizer) {
211 m_relaxation_optimizer_factory.reset(t_node_optimizer.clone());
212}
213
214template<class NodeT>
217
218 if (m_logger_factory) {
219 throw Exception("Logs have already been configured.");
220 }
221
222 m_logger_factory.reset(t_log_factory.clone());
223
224 return *this;
225}
226
227template<class NodeT>
229 return with_node_optimizer(t_node_optimizer);
230}
231
232template<class NodeT>
237
238template<class NodeT>
240
241 m_callbacks.emplace_back(t_callback.clone());
242
243 return *this;
244}
245
246template<class NodeT>
248
249 if (m_subtree_depth.has_value()) {
250 throw Exception("A subtree depth has already been given");
251 }
252
253 m_subtree_depth = t_depth;
254
255 return *this;
256}
257
258template<class NodeT>
259template<class NodeSelectionRuleFactoryT>
260typename idol::BranchAndBound<NodeT>::template only_if_has_Strategy<idol::BranchAndBound<NodeT>&, NodeSelectionRuleFactoryT>
261idol::BranchAndBound<NodeT>::with_node_selection_rule(const NodeSelectionRuleFactoryT &t_node_selection_rule) {
262 return with_node_selection_rule(typename NodeSelectionRuleFactoryT::template Strategy<NodeT>(t_node_selection_rule));
263}
264
265template<class NodeT>
268
269 if (m_node_selection_rule_factory) {
270 throw Exception("A node selection rule has already been set.");
271 }
272
273 m_node_selection_rule_factory.reset(t_node_selection.clone());
274
275 return *this;
276}
277
278template<class NodeT>
279template<class BranchingRuleFactoryT>
280typename idol::BranchAndBound<NodeT>::template only_if_has_Strategy<idol::BranchAndBound<NodeT>&, BranchingRuleFactoryT>
281idol::BranchAndBound<NodeT>::with_branching_rule(const BranchingRuleFactoryT &t_branching_rule) {
282 return with_branching_rule(typename BranchingRuleFactoryT::template Strategy<NodeT>(t_branching_rule));
283}
284
285template<class NodeT>
287
288 if (m_branching_rule_factory) {
289 throw Exception("A branching rule has already been set.");
290 }
291
292 m_branching_rule_factory.reset(t_branching_rule.clone());
293
294 return *this;
295}
296
297template<class NodeT>
299
300 if (m_relaxation_optimizer_factory) {
301 throw Exception("A node solver has already been set.");
302 }
303
304 m_relaxation_optimizer_factory.reset(t_node_optimizer.clone());
305
306 return *this;
307}
308
309template<class NodeT>
311
313 m_relaxation_optimizer_factory(t_rhs.m_relaxation_optimizer_factory ? t_rhs.m_relaxation_optimizer_factory->clone() : nullptr),
314 m_branching_rule_factory(t_rhs.m_branching_rule_factory ? t_rhs.m_branching_rule_factory->clone() : nullptr),
315 m_node_selection_rule_factory(t_rhs.m_node_selection_rule_factory ? t_rhs.m_node_selection_rule_factory->clone() : nullptr),
316 m_subtree_depth(t_rhs.m_subtree_depth),
317 m_logger_factory(t_rhs.m_logger_factory ? t_rhs.m_logger_factory->clone() : nullptr) {
318
319 for (auto& cb : t_rhs.m_callbacks) {
320 m_callbacks.emplace_back(cb->clone());
321 }
322
323}
324
325template<class NodeT>
327
328 if (!m_relaxation_optimizer_factory) {
329 throw Exception("No node solver has been given, please call BranchAndBound::with_node_optimizer to configure.");
330 }
331
332 if (!m_branching_rule_factory) {
333 throw Exception("No branching rule has been given, please call BranchAndBound::with_branching_rule to configure.");
334 }
335
336 if (!m_node_selection_rule_factory) {
337 throw Exception("No node selection rule has been given, please call BranchAndBound::with_node_selection_rule to configure.");
338 }
339
340 std::unique_ptr<Logs::BranchAndBound::Factory<NodeT>> default_logger_factory;
341 if (!m_logger_factory) {
342 default_logger_factory = std::make_unique<Logs::BranchAndBound::Info<NodeT>>();
343 }
344
345 auto* result = new Optimizers::BranchAndBound<NodeT>(t_model,
346 *m_relaxation_optimizer_factory,
347 *m_branching_rule_factory,
348 *m_node_selection_rule_factory,
350 m_logger_factory ? *m_logger_factory : *default_logger_factory);
351
352 this->handle_default_parameters(result);
353
354 if (m_subtree_depth.has_value()) {
355 result->set_subtree_depth(m_subtree_depth.value());
356 }
357
358 for (auto& cb : m_callbacks) {
359 result->add_callback(cb->operator()());
360 }
361
362 return result;
363}
364
365template<class NodeT>
369
370namespace idol {
371 template<class NodeInfoT>
372 BranchAndBound<NodeInfoT> operator+(const BranchAndBound<NodeInfoT>& t_branch_and_bound, const OptimizerFactory& t_node_optimizer) {
373 BranchAndBound<NodeInfoT> result(t_branch_and_bound);
374 result += t_node_optimizer;
375 return result;
376 }
377}
378
379#endif //IDOL_BRANCHANDBOUND_H
only_if_has_Strategy< BranchAndBound< NodeT > &, BranchingRuleFactoryT > with_branching_rule(const BranchingRuleFactoryT &t_branching_rule)
BranchAndBound< NodeT > & with_branching_rule(const BranchingRuleFactory< NodeT > &t_branching_rule)
BranchAndBound< NodeT > & with_node_selection_rule(const NodeSelectionRuleFactory< NodeT > &t_node_selection)
BranchAndBound< NodeT > & with_node_optimizer(const OptimizerFactory &t_node_optimizer)
BranchAndBound< NodeT > & add_callback(const BranchAndBoundCallbackFactory< NodeT > &t_callback)
BranchAndBound()=default
OptimizerFactory * clone() const override
typename std::pair< typename T::template Strategy< NodeT >, ReturnT >::second_type only_if_has_Strategy
BranchAndBound< NodeT > & with_subtree_depth(unsigned int t_depth)
only_if_has_Strategy< BranchAndBound< NodeT > &, NodeSelectionRuleFactoryT > with_node_selection_rule(const NodeSelectionRuleFactoryT &t_node_selection_rule)
Optimizer * operator()(const Model &t_model) const override
virtual OptimizerFactory * clone() const =0