Loading...
Searching...
No Matches
CallbackAsBranchAndBoundCallback.h
1//
2// Created by henri on 12/04/23.
3//
4
5#ifndef IDOL_CALLBACKASBRANCHANDBOUNDCALLBACK_H
6#define IDOL_CALLBACKASBRANCHANDBOUNDCALLBACK_H
7
8#include <memory>
9#include "idol/mixed-integer/optimizers/callbacks/CallbackFactory.h"
10#include "BranchAndBoundCallbackFactory.h"
11#include "BranchAndBoundCallback.h"
12
13namespace idol {
14 template<class NodeInfoT>
16}
17
18template<class NodeInfoT>
20 std::unique_ptr<CallbackFactory> m_callback_factory;
21
22 CallbackAsBranchAndBoundCallback(const CallbackAsBranchAndBoundCallback& t_src)
23 : m_callback_factory(t_src.m_callback_factory->clone()) {}
24public:
25
26 class Strategy : public BranchAndBoundCallback<NodeInfoT> {
27
28 friend class ::idol::CallbackAsBranchAndBoundCallback<NodeInfoT>;
29
30 class Interface : public CallbackI {
31 friend class Strategy;
32
33 Strategy& m_parent;
34 protected:
35 explicit Interface(Strategy& t_parent) : m_parent(t_parent) {}
36
37 [[nodiscard]] const Model &original_model() const override {
38 return m_parent.original_model();
39 }
40
41 void add_user_cut(const TempCtr &t_cut) override {
42 m_parent.add_user_cut(t_cut);
43 }
44
45 void add_lazy_cut(const TempCtr &t_cut) override {
46 m_parent.add_lazy_cut(t_cut);
47 }
48
49 void submit_heuristic_solution(PrimalPoint t_solution) override {
50 auto* info = new NodeInfoT();
51 info->set_primal_solution(std::move(t_solution));
52 m_parent.submit_heuristic_solution(info);
53 }
54
55 [[nodiscard]] PrimalPoint primal_solution() const override {
56 return m_parent.node().info().primal_solution();
57 }
58
59 [[nodiscard]] const Timer &time() const override {
60 return m_parent.time();
61 }
62
63 [[nodiscard]] double best_obj() const override {
64 return m_parent.best_obj();
65 }
66
67 [[nodiscard]] double best_bound() const override {
68 return m_parent.best_bound();
69 }
70
71 void terminate() override {
72 m_parent.terminate();
73 }
74
75 [[nodiscard]] unsigned node_count() const override {
76 return m_parent.node_count();
77 }
78 };
79
80 Interface m_interface;
81 std::unique_ptr<Callback> m_callback;
82 protected:
83 explicit Strategy(Callback* t_cb) : m_callback(t_cb), m_interface(*this) {}
84
85 void operator()(CallbackEvent t_event) override {
86 m_interface.execute(*m_callback, t_event);
87 }
88 };
89
90 explicit CallbackAsBranchAndBoundCallback(const CallbackFactory& t_factory)
91 : m_callback_factory(t_factory.clone()) {}
92
93 BranchAndBoundCallback<NodeInfoT> *operator()() override {
94 auto* cb = m_callback_factory->operator()();
95 return new Strategy(cb);
96 }
97
98 BranchAndBoundCallbackFactory<NodeInfoT> *clone() const override {
99 return new CallbackAsBranchAndBoundCallback<NodeInfoT>(*this);
100 }
101};
102
103#endif //IDOL_CALLBACKASBRANCHANDBOUNDCALLBACK_H
void submit_heuristic_solution(NodeInfoT *t_info)
void add_user_cut(const TempCtr &t_cut)
const Node< NodeInfoT > & node() const
void add_lazy_cut(const TempCtr &t_cut)