ZonoOpt 2.4.0
Loading...
Searching...
No Matches
SCIPApi.hpp
Go to the documentation of this file.
1#ifndef ZONOOPT_SCIP_API_HPP_
2#define ZONOOPT_SCIP_API_HPP_
3
13#ifdef _WIN32
14 #include <windows.h>
15 typedef HMODULE ZonoOptScipLibHandle;
16 #define ZONOOPT_SCIP_GET_SYMBOL GetProcAddress
17#else
18 #include <dlfcn.h>
19 typedef void* ZonoOptScipLibHandle;
20 #define ZONOOPT_SCIP_GET_SYMBOL dlsym
21#endif
22
23#include <memory>
24#include <string>
25#include <vector>
26
27namespace ZonoOpt
28{
29namespace detail
30{
31
32class SCIPApi {
33public:
34
35 // SCIP_RETCODE values that we use (mirrored from scip_retcode.h so we don't need SCIP headers).
36 static constexpr int SCIP_OKAY = 1;
37
38 // SCIP_VARTYPE values
39 static constexpr int SCIP_VARTYPE_BINARY = 0;
40 static constexpr int SCIP_VARTYPE_INTEGER = 1;
41 static constexpr int SCIP_VARTYPE_IMPLINT = 2;
42 static constexpr int SCIP_VARTYPE_CONTINUOUS = 3;
43
44 // SCIP_Status enum values. Only SCIP 10+ is supported; SCIP 10 reorganized the enum
45 // so terminating outcomes come first (OPTIMAL=1, INFEASIBLE=2, UNBOUNDED=3, INFORUNBD=4).
46 // SCIP 8 and 9 used different layouts which we have not verified, so they are rejected
47 // at load time.
48 static constexpr int SCIP_STATUS_OPTIMAL = 1;
49 static constexpr int SCIP_STATUS_INFEASIBLE = 2;
50 static constexpr int SCIP_STATUS_UNBOUNDED = 3;
51 static constexpr int SCIP_STATUS_INFORUNBD = 4;
52
53 // Loaded SCIP version. Zero if no library was successfully loaded.
54 int scip_major = 0;
55 int scip_minor = 0;
56 int scip_patch = 0;
57
59 static constexpr int MIN_SCIP_MAJOR = 10;
60
61 // Opaque SCIP_* pointer typedefs (we treat them as void*).
62 using ScipPtr = void*; // SCIP*
63 using VarPtr = void*; // SCIP_VAR*
64 using ConsPtr = void*; // SCIP_CONS*
65 using SolPtr = void*; // SCIP_SOL*
66 using SparseSolPtr = void*; // SCIP_SPARSESOL*
67
68 using LibPtr = std::shared_ptr<void>;
69
70 // ---- function pointer typedefs ------------------------------------------
71 typedef int (*SCIPcreate_t)(ScipPtr*);
72 typedef int (*SCIPfree_t)(ScipPtr*);
73 typedef int (*SCIPincludeDefaultPlugins_t)(ScipPtr);
74 typedef int (*SCIPcreateProbBasic_t)(ScipPtr, const char*);
75 typedef int (*SCIPfreeProb_t)(ScipPtr);
76 typedef int (*SCIPsetMessagehdlrQuiet_t)(ScipPtr, unsigned int);
77 typedef double (*SCIPinfinity_t)(ScipPtr);
78
79 typedef int (*SCIPcreateVarBasic_t)(ScipPtr, VarPtr*, const char*, double, double, double, int);
80 typedef int (*SCIPaddVar_t)(ScipPtr, VarPtr);
81 typedef int (*SCIPreleaseVar_t)(ScipPtr, VarPtr*);
82 typedef int (*SCIPchgVarType_t)(ScipPtr, VarPtr, int, unsigned int*);
83
84 typedef int (*SCIPcreateConsBasicLinear_t)(ScipPtr, ConsPtr*, const char*, int, VarPtr*, double*, double, double);
85 typedef int (*SCIPcreateConsBasicQuadraticNonlinear_t)(ScipPtr, ConsPtr*, const char*,
86 int, VarPtr*, double*,
87 int, VarPtr*, VarPtr*, double*,
88 double, double);
89 typedef int (*SCIPaddCons_t)(ScipPtr, ConsPtr);
90 typedef int (*SCIPreleaseCons_t)(ScipPtr, ConsPtr*);
91
92 typedef int (*SCIPsolve_t)(ScipPtr);
93 typedef int (*SCIPfreeTransform_t)(ScipPtr);
94 typedef long long (*SCIPgetNTotalNodes_t)(ScipPtr);
95 typedef double (*SCIPgetGap_t)(ScipPtr);
96 typedef double (*SCIPgetDualbound_t)(ScipPtr);
97 typedef int (*SCIPgetStatus_t)(ScipPtr);
98 typedef SolPtr (*SCIPgetBestSol_t)(ScipPtr);
99 typedef int (*SCIPgetNSols_t)(ScipPtr);
100 typedef SolPtr*(*SCIPgetSols_t)(ScipPtr);
101 typedef double (*SCIPgetSolVal_t)(ScipPtr, SolPtr, VarPtr);
102 typedef double (*SCIPgetSolOrigObj_t)(ScipPtr, SolPtr);
103 typedef double (*SCIPgetSolvingTime_t)(ScipPtr);
104
105 typedef int (*SCIPsetBoolParam_t)(ScipPtr, const char*, unsigned int);
106 typedef int (*SCIPsetIntParam_t)(ScipPtr, const char*, int);
107 typedef int (*SCIPsetLongintParam_t)(ScipPtr, const char*, long long);
108 typedef int (*SCIPsetRealParam_t)(ScipPtr, const char*, double);
109 typedef int (*SCIPsetCharParam_t)(ScipPtr, const char*, char);
110 typedef int (*SCIPsetStringParam_t)(ScipPtr, const char*, const char*);
111
112 typedef int (*SCIPmajorVersion_t)();
113 typedef int (*SCIPminorVersion_t)();
114 typedef int (*SCIPtechVersion_t)();
115
116 // Count handler: enumerates feasible integer/binary assignments and (when
117 // collect=TRUE) stores them in a sparse format separate from the regular
118 // solution pool. Used by solve_miqp_scip_multisol for feasibility-only
119 // enumeration of leaves.
120 typedef int (*SCIPcount_t)(ScipPtr);
121 typedef void (*SCIPgetCountedSparseSols_t)(ScipPtr, VarPtr**, int*, SparseSolPtr**, int*);
122 typedef long long* (*SCIPsparseSolGetLbs_t)(SparseSolPtr);
123 typedef int (*SCIPsparseSolGetNVars_t)(SparseSolPtr);
124
125 // ---- function pointers --------------------------------------------------
126 SCIPcreate_t SCIPcreate = nullptr;
127 SCIPfree_t SCIPfree = nullptr;
128 SCIPincludeDefaultPlugins_t SCIPincludeDefaultPlugins = nullptr;
129 SCIPcreateProbBasic_t SCIPcreateProbBasic = nullptr;
130 SCIPfreeProb_t SCIPfreeProb = nullptr;
131 SCIPsetMessagehdlrQuiet_t SCIPsetMessagehdlrQuiet = nullptr;
132 SCIPinfinity_t SCIPinfinity = nullptr;
133
134 SCIPcreateVarBasic_t SCIPcreateVarBasic = nullptr;
135 SCIPaddVar_t SCIPaddVar = nullptr;
136 SCIPreleaseVar_t SCIPreleaseVar = nullptr;
137 SCIPchgVarType_t SCIPchgVarType = nullptr;
138
139 SCIPcreateConsBasicLinear_t SCIPcreateConsBasicLinear = nullptr;
140 SCIPcreateConsBasicQuadraticNonlinear_t SCIPcreateConsBasicQuadraticNonlinear = nullptr;
141 SCIPaddCons_t SCIPaddCons = nullptr;
142 SCIPreleaseCons_t SCIPreleaseCons = nullptr;
143
144 SCIPsolve_t SCIPsolve = nullptr;
145 SCIPfreeTransform_t SCIPfreeTransform = nullptr;
146 SCIPgetNTotalNodes_t SCIPgetNTotalNodes = nullptr;
147 SCIPgetGap_t SCIPgetGap = nullptr;
148 SCIPgetDualbound_t SCIPgetDualbound = nullptr;
149 SCIPgetStatus_t SCIPgetStatus = nullptr;
150 SCIPgetBestSol_t SCIPgetBestSol = nullptr;
151 SCIPgetNSols_t SCIPgetNSols = nullptr;
152 SCIPgetSols_t SCIPgetSols = nullptr;
153 SCIPgetSolVal_t SCIPgetSolVal = nullptr;
154 SCIPgetSolOrigObj_t SCIPgetSolOrigObj = nullptr;
155 SCIPgetSolvingTime_t SCIPgetSolvingTime = nullptr;
156
157 SCIPsetBoolParam_t SCIPsetBoolParam = nullptr;
158 SCIPsetIntParam_t SCIPsetIntParam = nullptr;
159 SCIPsetLongintParam_t SCIPsetLongintParam = nullptr;
160 SCIPsetRealParam_t SCIPsetRealParam = nullptr;
161 SCIPsetCharParam_t SCIPsetCharParam = nullptr;
162 SCIPsetStringParam_t SCIPsetStringParam = nullptr;
163
164 SCIPmajorVersion_t SCIPmajorVersion = nullptr;
165 SCIPminorVersion_t SCIPminorVersion = nullptr;
166 SCIPtechVersion_t SCIPtechVersion = nullptr;
167
168 SCIPcount_t SCIPcount = nullptr;
169 SCIPgetCountedSparseSols_t SCIPgetCountedSparseSols = nullptr;
170 SCIPsparseSolGetLbs_t SCIPsparseSolGetLbs = nullptr;
171 SCIPsparseSolGetNVars_t SCIPsparseSolGetNVars = nullptr;
172
173 // ---- API management -----------------------------------------------------
174 static SCIPApi& instance();
175
177 bool is_available() const { return _lib_ptr != nullptr; }
178
181 const std::string& unavailable_reason() const { return _unavailable_reason; }
182
189 class Scip
190 {
191 public:
192 Scip() = default;
193 Scip(ScipPtr p, LibPtr lib);
194 ~Scip();
195 Scip(const Scip&) = delete;
196 Scip& operator=(const Scip&) = delete;
197 Scip(Scip&& other) noexcept;
198 Scip& operator=(Scip&& other) noexcept;
199
200 ScipPtr get() const { return _ptr; }
201 explicit operator bool() const { return _ptr != nullptr; }
202
203 private:
204 ScipPtr _ptr = nullptr;
205 LibPtr _lib;
206 };
207
210 Scip create_scip();
211
212private:
213 LibPtr _lib_ptr;
214 std::string _unavailable_reason;
215 SCIPApi();
216 ~SCIPApi() = default;
217 SCIPApi(const SCIPApi&) = delete;
218 SCIPApi& operator=(const SCIPApi&) = delete;
219
220 ZonoOptScipLibHandle load_library();
221 ZonoOptScipLibHandle load_library_from_path(const std::string& path, const std::vector<std::string>& names);
222};
223
224} // namespace detail
225} // namespace ZonoOpt
226
227#endif
void * ZonoOptScipLibHandle
Definition SCIPApi.hpp:19
Definition ZonoOpt.hpp:58