idol
A C++ Framework for Optimization
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>
15 class CallbackAsBranchAndBoundCallback;
16}
17
18template<class NodeInfoT>
20 std::unique_ptr<CallbackFactory> m_callback_factory;
21
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 double best_obj() const override {
64 return m_parent.best_obj();
65 }
66
67 double best_bound() const override {
68 return m_parent.best_bound();
69 }
70
71 void terminate() override {
72 m_parent.terminate();
73 }
74
75 };
76
77 Interface m_interface;
78 std::unique_ptr<Callback> m_callback;
79 protected:
80 explicit Strategy(Callback* t_cb) : m_callback(t_cb), m_interface(*this) {}
81
82 void operator()(CallbackEvent t_event) override {
83 m_interface.execute(*m_callback, t_event);
84 }
85 };
86
87 explicit CallbackAsBranchAndBoundCallback(const CallbackFactory& t_factory)
88 : m_callback_factory(t_factory.clone()) {}
89
90 BranchAndBoundCallback<NodeInfoT> *operator()() override {
91 auto* cb = m_callback_factory->operator()();
92 return new Strategy(cb);
93 }
94
95 BranchAndBoundCallbackFactory<NodeInfoT> *clone() const override {
96 return new CallbackAsBranchAndBoundCallback<NodeInfoT>(*this);
97 }
98};
99
100#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)