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