popso 0.8.2__py3-none-win_amd64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
popso/__init__.py ADDED
@@ -0,0 +1,54 @@
1
+ """popso: Python bindings (ctypes) for the popso LP/MILP/QP/MIQP solver.
2
+
3
+ import popso
4
+ m = popso.Model("example")
5
+ x = m.add_var(ub=10, vtype=popso.INTEGER, name="x")
6
+ y = m.add_var(name="y")
7
+ m.add_constr(x + 2 * y <= 14)
8
+ m.add_constr(3 * x - y >= 0)
9
+ m.set_objective(3 * x + 4 * y, sense=popso.MAXIMIZE)
10
+ r = m.solve(time_limit=10)
11
+ print(r.status, r.objective, r[x], r["y"])
12
+
13
+ The shared library is searched in $POPSO_LIBRARY (a file or a directory), next to this
14
+ package, in the build/ directory of a source checkout, and on the system library path.
15
+ """
16
+
17
+ from ._lib import path as library_path
18
+ from .model import (
19
+ AT_LOWER,
20
+ AT_UPPER,
21
+ AT_ZERO,
22
+ BASIC,
23
+ BINARY,
24
+ CONTINUOUS,
25
+ INF,
26
+ INTEGER,
27
+ MAXIMIZE,
28
+ MINIMIZE,
29
+ PARAMS,
30
+ SEMICONTINUOUS,
31
+ SEMIINTEGER,
32
+ SOS1,
33
+ SOS2,
34
+ Constr,
35
+ Constraint,
36
+ LinExpr,
37
+ Model,
38
+ Params,
39
+ PopsoError,
40
+ QuadExpr,
41
+ Result,
42
+ Var,
43
+ read_lp,
44
+ read_mps,
45
+ version,
46
+ )
47
+
48
+ __version__ = version()
49
+
50
+ __all__ = [
51
+ "AT_LOWER", "AT_UPPER", "AT_ZERO", "BASIC", "BINARY", "CONTINUOUS", "INF", "INTEGER", "MAXIMIZE", "MINIMIZE", "PARAMS", "SEMICONTINUOUS", "SEMIINTEGER",
52
+ "SOS1", "SOS2", "Constr", "Constraint", "LinExpr", "Model", "Params", "PopsoError", "QuadExpr", "Result", "Var",
53
+ "library_path", "read_lp", "read_mps", "version",
54
+ ]
popso/_lib.py ADDED
@@ -0,0 +1,143 @@
1
+ """Locating and loading the popso shared library (ctypes prototypes of include/popso.h)."""
2
+
3
+ import ctypes
4
+ import ctypes.util
5
+ import os
6
+ import sys
7
+
8
+ if sys.platform == "win32":
9
+ _LIB_NAMES = ["popso.dll", "libpopso.dll"]
10
+ elif sys.platform == "darwin":
11
+ _LIB_NAMES = ["libpopso.dylib"]
12
+ else:
13
+ _LIB_NAMES = ["libpopso.so"]
14
+
15
+ _HERE = os.path.dirname(os.path.abspath(__file__))
16
+
17
+
18
+ def _candidates():
19
+ """Places to look for the library, in order."""
20
+ env = os.environ.get("POPSO_LIBRARY")
21
+ if env:
22
+ if os.path.isdir(env):
23
+ for name in _LIB_NAMES:
24
+ yield os.path.join(env, name)
25
+ else:
26
+ yield env
27
+ return # an explicit choice is not second-guessed
28
+ for name in _LIB_NAMES:
29
+ yield os.path.join(_HERE, name)
30
+ # A source checkout: python/popso/ -> <repo>/build[/Release]
31
+ repo = os.path.dirname(os.path.dirname(_HERE))
32
+ for sub in ("build", os.path.join("build", "Release")):
33
+ for name in _LIB_NAMES:
34
+ yield os.path.join(repo, sub, name)
35
+ found = ctypes.util.find_library("popso")
36
+ if found:
37
+ yield found
38
+
39
+
40
+ def _load():
41
+ tried = []
42
+ for cand in _candidates():
43
+ if os.path.isabs(cand) and not os.path.exists(cand):
44
+ tried.append(cand)
45
+ continue
46
+ try:
47
+ return ctypes.CDLL(cand), cand
48
+ except OSError as e:
49
+ tried.append("%s (%s)" % (cand, e))
50
+ raise ImportError(
51
+ "popso: cannot find the popso shared library. Build it with CMake (it is built by default as "
52
+ "build/%s), then set POPSO_LIBRARY to its path or copy it into %s. Tried: %s"
53
+ % (_LIB_NAMES[0], _HERE, "; ".join(tried) or "nothing")
54
+ )
55
+
56
+
57
+ lib, path = _load()
58
+
59
+ _c_int = ctypes.c_int
60
+ _c_double = ctypes.c_double
61
+ _c_ll = ctypes.c_longlong
62
+ _c_char_p = ctypes.c_char_p
63
+ _vp = ctypes.c_void_p
64
+ _pd = ctypes.POINTER(ctypes.c_double)
65
+ _pi = ctypes.POINTER(ctypes.c_int)
66
+ _pll = ctypes.POINTER(ctypes.c_longlong)
67
+ _pcp = ctypes.POINTER(ctypes.c_char_p)
68
+
69
+ # int (*)(void* user_data, double objective, double bound, const double* x, int num_vars)
70
+ INCUMBENT_CALLBACK = ctypes.CFUNCTYPE(_c_int, _vp, _c_double, _c_double, _pd, _c_int)
71
+
72
+ _PROTOTYPES = {
73
+ "popso_version": (_c_char_p, []),
74
+ "popso_status_string": (_c_char_p, [_c_int]),
75
+ "popso_last_error": (_c_char_p, [_vp]),
76
+ "popso_create": (_vp, []),
77
+ "popso_free": (None, [_vp]),
78
+ "popso_read_mps": (_c_int, [_vp, _c_char_p]),
79
+ "popso_write_mps": (_c_int, [_vp, _c_char_p]),
80
+ "popso_read_lp": (_c_int, [_vp, _c_char_p]),
81
+ "popso_add_sos": (_c_int, [_vp, _c_int, _c_int, _pi, _pd, _c_char_p, _pi]),
82
+ "popso_add_indicator": (_c_int, [_vp, _c_int, _c_int, _c_int, _pi, _pd, _c_double, _c_double, _c_char_p, _pi]),
83
+ "popso_num_sos": (_c_int, [_vp]),
84
+ "popso_num_indicators": (_c_int, [_vp]),
85
+ "popso_set_model_name": (_c_int, [_vp, _c_char_p]),
86
+ "popso_get_model_name": (_c_char_p, [_vp]),
87
+ "popso_add_var": (_c_int, [_vp, _c_double, _c_double, _c_double, _c_int, _c_char_p, _pi]),
88
+ "popso_add_vars": (_c_int, [_vp, _c_int, _pd, _pd, _pd, _pi, _pcp]),
89
+ "popso_add_row": (_c_int, [_vp, _c_int, _pi, _pd, _c_double, _c_double, _c_char_p, _pi]),
90
+ "popso_add_rows": (_c_int, [_vp, _c_int, _pi, _pi, _pd, _pd, _pd, _pcp]),
91
+ "popso_set_var_bounds": (_c_int, [_vp, _c_int, _c_double, _c_double]),
92
+ "popso_set_var_type": (_c_int, [_vp, _c_int, _c_int]),
93
+ "popso_set_row_bounds": (_c_int, [_vp, _c_int, _c_double, _c_double]),
94
+ "popso_set_objective_sense": (_c_int, [_vp, _c_int]),
95
+ "popso_set_objective_constant": (_c_int, [_vp, _c_double]),
96
+ "popso_set_obj_coef": (_c_int, [_vp, _c_int, _c_double]),
97
+ "popso_set_objective": (_c_int, [_vp, _c_int, _pi, _pd]),
98
+ "popso_set_q": (_c_int, [_vp, _c_int, _pi, _pi, _pd]),
99
+ "popso_add_q_entries": (_c_int, [_vp, _c_int, _pi, _pi, _pd]),
100
+ "popso_num_q_nonzeros": (_c_int, [_vp]),
101
+ "popso_get_q": (_c_int, [_vp, _c_int, _pi, _pi, _pd]),
102
+ "popso_add_qrow_terms": (_c_int, [_vp, _c_int, _c_int, _pi, _pi, _pd]),
103
+ "popso_num_qrow_terms": (_c_int, [_vp, _c_int]),
104
+ "popso_get_qrow_terms": (_c_int, [_vp, _c_int, _c_int, _pi, _pi, _pd]),
105
+ "popso_num_vars": (_c_int, [_vp]),
106
+ "popso_num_rows": (_c_int, [_vp]),
107
+ "popso_num_nonzeros": (_c_ll, [_vp]),
108
+ "popso_get_objective_sense": (_c_int, [_vp]),
109
+ "popso_get_objective_constant": (_c_double, [_vp]),
110
+ "popso_get_var": (_c_int, [_vp, _c_int, _pd, _pd, _pd, _pi]),
111
+ "popso_get_row": (_c_int, [_vp, _c_int, _pd, _pd]),
112
+ "popso_get_var_name": (_c_char_p, [_vp, _c_int]),
113
+ "popso_get_row_name": (_c_char_p, [_vp, _c_int]),
114
+ "popso_find_var": (_c_int, [_vp, _c_char_p]),
115
+ "popso_set_param": (_c_int, [_vp, _c_char_p, _c_double]),
116
+ "popso_get_param": (_c_int, [_vp, _c_char_p, _pd]),
117
+ "popso_set_start": (_c_int, [_vp, _c_int, _pi, _pd]),
118
+ "popso_read_start": (_c_int, [_vp, _c_char_p]),
119
+ "popso_clear_start": (_c_int, [_vp]),
120
+ "popso_solve": (_c_int, [_vp]),
121
+ "popso_get_status": (_c_int, [_vp]),
122
+ "popso_has_solution": (_c_int, [_vp]),
123
+ "popso_get_objective": (_c_int, [_vp, _pd]),
124
+ "popso_get_bound": (_c_int, [_vp, _pd]),
125
+ "popso_get_gap": (_c_int, [_vp, _pd]),
126
+ "popso_get_node_count": (_c_int, [_vp, _pll]),
127
+ "popso_get_lp_iterations": (_c_int, [_vp, _pll]),
128
+ "popso_get_solve_time": (_c_int, [_vp, _pd]),
129
+ "popso_get_solution": (_c_int, [_vp, _pd, _c_int]),
130
+ "popso_write_solution": (_c_int, [_vp, _c_char_p]),
131
+ "popso_has_duals": (_c_int, [_vp]),
132
+ "popso_get_duals": (_c_int, [_vp, _pd, _c_int]),
133
+ "popso_get_reduced_costs": (_c_int, [_vp, _pd, _c_int]),
134
+ "popso_has_basis": (_c_int, [_vp]),
135
+ "popso_get_basis": (_c_int, [_vp, _pi, _pi]),
136
+ "popso_interrupt": (_c_int, [_vp]),
137
+ "popso_set_incumbent_callback": (_c_int, [_vp, INCUMBENT_CALLBACK, _vp]),
138
+ }
139
+
140
+ for _name, (_res, _args) in _PROTOTYPES.items():
141
+ _f = getattr(lib, _name)
142
+ _f.restype = _res
143
+ _f.argtypes = _args