5#ifndef IDOL_BRANCHANDBOUND_H
6#define IDOL_BRANCHANDBOUND_H
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"
28template<
class NodeT =
idol::DefaultNodeInfo>
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 std::unique_ptr<NodeT> m_root_node_info;
36 std::list<std::unique_ptr<BranchAndBoundCallbackFactory<NodeT>>> m_callbacks;
38 std::optional<unsigned int> m_subtree_depth;
39 std::optional<unsigned int> m_log_frequency;
47 template<
class ReturnT,
class T>
using only_if_has_Strategy =
typename std::pair<typename T::template Strategy<NodeT>, ReturnT>::second_type;
112 template<
class BranchingRuleFactoryT>
143 template<
class NodeSelectionRuleFactoryT>
208 BranchAndBound<NodeT>& with_root_node_info(const NodeT& t_root_node_info);
213 m_relaxation_optimizer_factory.reset(t_node_optimizer.clone());
220 if (m_logger_factory) {
221 throw Exception(
"Logs have already been configured.");
224 m_logger_factory.reset(t_log_factory.clone());
240template <
class NodeT>
243 if (m_root_node_info) {
244 throw Exception(
"A root node info has already been given");
247 m_root_node_info.reset(t_root_node_info.clone());
255 m_callbacks.emplace_back(t_callback.clone());
263 if (m_subtree_depth.has_value()) {
264 throw Exception(
"A subtree depth has already been given");
267 m_subtree_depth = t_depth;
273template<
class NodeSelectionRuleFactoryT>
276 return with_node_selection_rule(
typename NodeSelectionRuleFactoryT::template Strategy<NodeT>(t_node_selection_rule));
283 if (m_node_selection_rule_factory) {
284 throw Exception(
"A node selection rule has already been set.");
287 m_node_selection_rule_factory.reset(t_node_selection.clone());
293template<
class BranchingRuleFactoryT>
296 return with_branching_rule(
typename BranchingRuleFactoryT::template Strategy<NodeT>(t_branching_rule));
302 if (m_branching_rule_factory) {
303 throw Exception(
"A branching rule has already been set.");
306 m_branching_rule_factory.reset(t_branching_rule.clone());
314 if (m_relaxation_optimizer_factory) {
315 throw Exception(
"A node solver has already been set.");
318 m_relaxation_optimizer_factory.reset(t_node_optimizer.
clone());
327 m_relaxation_optimizer_factory(t_rhs.m_relaxation_optimizer_factory ? t_rhs.m_relaxation_optimizer_factory->
clone() : nullptr),
328 m_branching_rule_factory(t_rhs.m_branching_rule_factory ? t_rhs.m_branching_rule_factory->
clone() : nullptr),
329 m_node_selection_rule_factory(t_rhs.m_node_selection_rule_factory ? t_rhs.m_node_selection_rule_factory->
clone() : nullptr),
330 m_subtree_depth(t_rhs.m_subtree_depth),
331 m_logger_factory(t_rhs.m_logger_factory ? t_rhs.m_logger_factory->
clone() : nullptr),
332 m_root_node_info(t_rhs.m_root_node_info ? t_rhs.m_root_node_info->
clone() : nullptr) {
334 for (
auto& cb : t_rhs.m_callbacks) {
335 m_callbacks.emplace_back(cb->clone());
343 if (!m_relaxation_optimizer_factory) {
344 throw Exception(
"No node solver has been given, please call BranchAndBound::with_node_optimizer to configure.");
347 if (!m_branching_rule_factory) {
348 throw Exception(
"No branching rule has been given, please call BranchAndBound::with_branching_rule to configure.");
351 if (!m_node_selection_rule_factory) {
352 throw Exception(
"No node selection rule has been given, please call BranchAndBound::with_node_selection_rule to configure.");
355 std::unique_ptr<Logs::BranchAndBound::Factory<NodeT>> default_logger_factory;
356 if (!m_logger_factory) {
357 default_logger_factory = std::make_unique<Logs::BranchAndBound::Info<NodeT>>();
361 *m_relaxation_optimizer_factory,
362 *m_branching_rule_factory,
363 *m_node_selection_rule_factory,
365 m_logger_factory ? *m_logger_factory : *default_logger_factory);
367 this->handle_default_parameters(result);
369 if (m_root_node_info) {
370 result->set_root_node_info(*m_root_node_info);
373 if (m_subtree_depth) {
374 result->set_subtree_depth(m_subtree_depth.value());
377 for (
auto& cb : m_callbacks) {
378 result->add_callback(cb->operator()());
390 template<
class NodeInfoT>
393 result += t_node_optimizer;
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)
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