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/presolve/AbstractPresolver.h"
15#include "idol/mixed-integer/optimizers/callbacks/CallbackFactory.h"
16#include "idol/mixed-integer/optimizers/branch-and-bound/nodes/DefaultNodeInfo.h"
17#include "idol/mixed-integer/optimizers/branch-and-bound/logs/Factory.h"
18#include "idol/mixed-integer/optimizers/branch-and-bound/logs/Info.h"
29template<
class NodeT =
idol::DefaultNodeInfo>
31 std::unique_ptr<OptimizerFactory> m_relaxation_optimizer_factory;
32 std::unique_ptr<BranchingRuleFactory<NodeT>> m_branching_rule_factory;
33 std::unique_ptr<NodeSelectionRuleFactory<NodeT>> m_node_selection_rule_factory;
34 std::unique_ptr<Logs::BranchAndBound::Factory<NodeT>> m_logger_factory;
35 std::unique_ptr<NodeT> m_root_node_info;
37 std::list<std::unique_ptr<Presolvers::AbstractPresolver>> m_presolvers;
38 std::list<std::unique_ptr<BranchAndBoundCallbackFactory<NodeT>>> m_callbacks;
40 std::optional<unsigned int> m_subtree_depth;
41 std::optional<unsigned int> m_log_frequency;
43 [[nodiscard]]
Optimizer *create(
const Model &t_model)
const override;
51 template<
class ReturnT,
class T>
using only_if_has_Strategy =
typename std::pair<typename T::template Strategy<NodeT>, ReturnT>::second_type;
116 template<
class BranchingRuleFactoryT>
147 template<
class NodeSelectionRuleFactoryT>
210 BranchAndBound<NodeT>& add_presolver(const Presolvers::AbstractPresolver& t_presolver);
212 BranchAndBound<NodeT>& with_root_node_info(const NodeT& t_root_node_info);
217 m_relaxation_optimizer_factory.reset(t_node_optimizer.clone());
224 if (m_logger_factory) {
225 throw Exception(
"Logs have already been configured.");
228 m_logger_factory.reset(t_log_factory.clone());
234idol::BranchAndBound<NodeT> &idol::BranchAndBound<NodeT>::operator+=(
const idol::OptimizerFactory &t_node_optimizer) {
239idol::BranchAndBound<NodeT> &
244template <
class NodeT>
246 m_presolvers.emplace_back(t_presolver.clone());
250template <
class NodeT>
253 if (m_root_node_info) {
254 throw Exception(
"A root node info has already been given");
257 m_root_node_info.reset(t_root_node_info.clone());
265 m_callbacks.emplace_back(t_callback.clone());
273 if (m_subtree_depth.has_value()) {
274 throw Exception(
"A subtree depth has already been given");
277 m_subtree_depth = t_depth;
283template<
class NodeSelectionRuleFactoryT>
286 return with_node_selection_rule(
typename NodeSelectionRuleFactoryT::template Strategy<NodeT>(t_node_selection_rule));
293 if (m_node_selection_rule_factory) {
294 throw Exception(
"A node selection rule has already been set.");
297 m_node_selection_rule_factory.reset(t_node_selection.clone());
303template<
class BranchingRuleFactoryT>
306 return with_branching_rule(
typename BranchingRuleFactoryT::template Strategy<NodeT>(t_branching_rule));
312 if (m_branching_rule_factory) {
313 throw Exception(
"A branching rule has already been set.");
316 m_branching_rule_factory.reset(t_branching_rule.clone());
324 if (m_relaxation_optimizer_factory) {
325 throw Exception(
"A node solver has already been set.");
328 m_relaxation_optimizer_factory.reset(t_node_optimizer.clone());
337 m_relaxation_optimizer_factory(t_rhs.m_relaxation_optimizer_factory ? t_rhs.m_relaxation_optimizer_factory->clone() : nullptr),
338 m_branching_rule_factory(t_rhs.m_branching_rule_factory ? t_rhs.m_branching_rule_factory->clone() : nullptr),
339 m_node_selection_rule_factory(t_rhs.m_node_selection_rule_factory ? t_rhs.m_node_selection_rule_factory->clone() : nullptr),
340 m_subtree_depth(t_rhs.m_subtree_depth),
341 m_logger_factory(t_rhs.m_logger_factory ? t_rhs.m_logger_factory->clone() : nullptr),
342 m_root_node_info(t_rhs.m_root_node_info ? t_rhs.m_root_node_info->clone() : nullptr) {
344 for (
auto& presolve : t_rhs.m_presolvers) {
345 m_presolvers.emplace_back(presolve->clone());
348 for (
auto& cb : t_rhs.m_callbacks) {
349 m_callbacks.emplace_back(cb->clone());
357 if (!m_relaxation_optimizer_factory) {
358 throw Exception(
"No node solver has been given, please call BranchAndBound::with_node_optimizer to configure.");
361 if (!m_branching_rule_factory) {
362 throw Exception(
"No branching rule has been given, please call BranchAndBound::with_branching_rule to configure.");
365 if (!m_node_selection_rule_factory) {
366 throw Exception(
"No node selection rule has been given, please call BranchAndBound::with_node_selection_rule to configure.");
369 std::unique_ptr<Logs::BranchAndBound::Factory<NodeT>> default_logger_factory;
370 if (!m_logger_factory) {
371 default_logger_factory = std::make_unique<Logs::BranchAndBound::Info<NodeT>>();
375 *m_relaxation_optimizer_factory,
376 *m_branching_rule_factory,
377 *m_node_selection_rule_factory,
379 m_logger_factory ? *m_logger_factory : *default_logger_factory);
381 if (m_root_node_info) {
382 result->set_root_node_info(*m_root_node_info);
385 if (m_subtree_depth) {
386 result->set_subtree_depth(m_subtree_depth.value());
389 for (
auto& presolver : m_presolvers) {
390 result->add_presolver(*presolver);
393 for (
auto& cb : m_callbacks) {
394 result->add_callback(cb->operator()());
401idol::OptimizerFactory *idol::BranchAndBound<NodeT>::clone()
const {
406 template<
class NodeInfoT>
409 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)
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)