pyoptinterface 0.1.0__cp38-cp38-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.
@@ -0,0 +1,63 @@
1
+ from pyoptinterface._src.core_ext import (
2
+ VariableIndex,
3
+ ConstraintIndex,
4
+ ExprBuilder,
5
+ VariableDomain,
6
+ ConstraintSense,
7
+ ConstraintType,
8
+ SOSType,
9
+ ObjectiveSense,
10
+ ScalarAffineFunction,
11
+ ScalarQuadraticFunction,
12
+ )
13
+
14
+ from pyoptinterface._src.attributes import (
15
+ VariableAttribute,
16
+ ModelAttribute,
17
+ TerminationStatusCode,
18
+ ResultStatusCode,
19
+ ConstraintAttribute,
20
+ )
21
+
22
+ from pyoptinterface._src.tupledict import (
23
+ tupledict,
24
+ make_tupledict,
25
+ )
26
+
27
+ from pyoptinterface._src.aml import make_nd_variable, quicksum, quicksum_
28
+
29
+ # Alias of ConstraintSense
30
+ Eq = ConstraintSense.Equal
31
+ """Alias of `ConstraintSense.Equal` for equality constraints.
32
+ """
33
+ Leq = ConstraintSense.LessEqual
34
+ """Alias of `ConstraintSense.LessEqual` for less-than-or-equal-to constraints.
35
+ """
36
+ Geq = ConstraintSense.GreaterEqual
37
+ """Alias of `ConstraintSense.GreaterEqual` for greater-than-or-equal-to constraints.
38
+ """
39
+
40
+ __all__ = [
41
+ "VariableIndex",
42
+ "ConstraintIndex",
43
+ "ExprBuilder",
44
+ "VariableDomain",
45
+ "ConstraintSense",
46
+ "ConstraintType",
47
+ "SOSType",
48
+ "ObjectiveSense",
49
+ "ScalarAffineFunction",
50
+ "ScalarQuadraticFunction",
51
+ "VariableAttribute",
52
+ "ModelAttribute",
53
+ "TerminationStatusCode",
54
+ "ResultStatusCode",
55
+ "ConstraintAttribute",
56
+ "tupledict",
57
+ "make_tupledict",
58
+ "make_nd_variable",
59
+ "quicksum",
60
+ "Eq",
61
+ "Leq",
62
+ "Geq",
63
+ ]
File without changes
@@ -0,0 +1,86 @@
1
+ from .core_ext import ExprBuilder
2
+ from .tupledict import make_tupledict
3
+
4
+ from collections.abc import Collection
5
+
6
+
7
+ def make_nd_variable(
8
+ model,
9
+ *coords: Collection,
10
+ domain=None,
11
+ lb=None,
12
+ ub=None,
13
+ name=None,
14
+ ):
15
+ kw_args = dict()
16
+ if domain is not None:
17
+ kw_args["domain"] = domain
18
+ if lb is not None:
19
+ kw_args["lb"] = lb
20
+ if ub is not None:
21
+ kw_args["ub"] = ub
22
+
23
+ def f(*args):
24
+ if name is not None:
25
+ suffix = str(args)
26
+ kw_args["name"] = f"{name}{suffix}"
27
+ return model.add_variable(**kw_args)
28
+
29
+ td = make_tupledict(*coords, rule=f)
30
+ return td
31
+
32
+
33
+ # def make_nd_variable_batch(
34
+ # model,
35
+ # *coords: Collection,
36
+ # domain=None,
37
+ # lb=None,
38
+ # ub=None,
39
+ # name=None,
40
+ # ):
41
+ # assert model.supports_batch_add_variables()
42
+ #
43
+ # kw_args = dict()
44
+ # if domain is not None:
45
+ # kw_args["domain"] = domain
46
+ # if lb is not None:
47
+ # kw_args["lb"] = lb
48
+ # if ub is not None:
49
+ # kw_args["ub"] = ub
50
+ #
51
+ # N = math.prod(len(c) for c in coords)
52
+ #
53
+ # start_vi = model.add_variables(N, **kw_args)
54
+ # start_index = start_vi.index
55
+ #
56
+ # kvs = []
57
+ # assert len(coords) > 0
58
+ # for i, coord in enumerate(product(*coords)):
59
+ # coord = tuple(flatten_tuple(coord))
60
+ # value = VariableIndex(start_index + i)
61
+ # if len(coord) == 1:
62
+ # coord = coord[0]
63
+ # if value is not None:
64
+ # kvs.append((coord, value))
65
+ #
66
+ # suffix = str(coord)
67
+ # if name is not None:
68
+ # model.set_variable_name(value, f"{name}{suffix}")
69
+ # return tupledict(kvs)
70
+
71
+
72
+ def quicksum_(expr: ExprBuilder, terms, f=None):
73
+ if isinstance(terms, dict):
74
+ iter = terms.values()
75
+ else:
76
+ iter = terms
77
+ if f:
78
+ iter = map(f, iter)
79
+ for v in iter:
80
+ expr += v
81
+
82
+
83
+ def quicksum(terms, f=None):
84
+ expr = ExprBuilder()
85
+ quicksum_(expr, terms, f)
86
+ return expr
@@ -0,0 +1,129 @@
1
+ from enum import Enum, auto
2
+ from .core_ext import VariableDomain, ObjectiveSense
3
+
4
+
5
+ class VariableAttribute(Enum):
6
+ Value = auto()
7
+ LowerBound = auto()
8
+ UpperBound = auto()
9
+ Domain = auto()
10
+ PrimalStart = auto()
11
+ Name = auto()
12
+
13
+
14
+ var_attr_type_map = {
15
+ VariableAttribute.Value: float,
16
+ VariableAttribute.LowerBound: float,
17
+ VariableAttribute.UpperBound: float,
18
+ VariableAttribute.PrimalStart: float,
19
+ VariableAttribute.Domain: VariableDomain,
20
+ VariableAttribute.Name: str,
21
+ }
22
+
23
+
24
+ class ModelAttribute(Enum):
25
+ # ModelLike API
26
+ # NumberOfConstraints = auto()
27
+ # NumberOfVariables = auto()
28
+ Name = auto()
29
+ ObjectiveSense = auto()
30
+
31
+ # AbstractOptimizer API
32
+ DualStatus = auto()
33
+ PrimalStatus = auto()
34
+ RawStatusString = auto()
35
+ TerminationStatus = auto()
36
+ BarrierIterations = auto()
37
+ DualObjectiveValue = auto()
38
+ NodeCount = auto()
39
+ NumberOfThreads = auto()
40
+ ObjectiveBound = auto()
41
+ ObjectiveValue = auto()
42
+ RelativeGap = auto()
43
+ Silent = auto()
44
+ SimplexIterations = auto()
45
+ SolverName = auto()
46
+ SolverVersion = auto()
47
+ SolveTimeSec = auto()
48
+ TimeLimitSec = auto()
49
+ # ObjectiveLimit = auto()
50
+
51
+
52
+ class ResultStatusCode(Enum):
53
+ NO_SOLUTION = auto()
54
+ FEASIBLE_POINT = auto()
55
+ NEARLY_FEASIBLE_POINT = auto()
56
+ INFEASIBLE_POINT = auto()
57
+ INFEASIBILITY_CERTIFICATE = auto()
58
+ NEARLY_INFEASIBILITY_CERTIFICATE = auto()
59
+ REDUCTION_CERTIFICATE = auto()
60
+ NEARLY_REDUCTION_CERTIFICATE = auto()
61
+ UNKNOWN_RESULT_STATUS = auto()
62
+ OTHER_RESULT_STATUS = auto()
63
+
64
+
65
+ class TerminationStatusCode(Enum):
66
+ OPTIMIZE_NOT_CALLED = auto()
67
+ OPTIMAL = auto()
68
+ INFEASIBLE = auto()
69
+ DUAL_INFEASIBLE = auto()
70
+ LOCALLY_SOLVED = auto()
71
+ LOCALLY_INFEASIBLE = auto()
72
+ INFEASIBLE_OR_UNBOUNDED = auto()
73
+ ALMOST_OPTIMAL = auto()
74
+ ALMOST_INFEASIBLE = auto()
75
+ ALMOST_DUAL_INFEASIBLE = auto()
76
+ ALMOST_LOCALLY_SOLVED = auto()
77
+ ITERATION_LIMIT = auto()
78
+ TIME_LIMIT = auto()
79
+ NODE_LIMIT = auto()
80
+ SOLUTION_LIMIT = auto()
81
+ MEMORY_LIMIT = auto()
82
+ OBJECTIVE_LIMIT = auto()
83
+ NORM_LIMIT = auto()
84
+ OTHER_LIMIT = auto()
85
+ SLOW_PROGRESS = auto()
86
+ NUMERICAL_ERROR = auto()
87
+ INVALID_MODEL = auto()
88
+ INVALID_OPTION = auto()
89
+ INTERRUPTED = auto()
90
+ OTHER_ERROR = auto()
91
+
92
+
93
+ model_attr_type_map = {
94
+ ModelAttribute.Name: str,
95
+ ModelAttribute.ObjectiveSense: ObjectiveSense,
96
+ ModelAttribute.DualStatus: ResultStatusCode,
97
+ ModelAttribute.PrimalStatus: ResultStatusCode,
98
+ ModelAttribute.RawStatusString: str,
99
+ ModelAttribute.TerminationStatus: TerminationStatusCode,
100
+ ModelAttribute.BarrierIterations: int,
101
+ ModelAttribute.DualObjectiveValue: float,
102
+ ModelAttribute.NodeCount: int,
103
+ ModelAttribute.NumberOfThreads: int,
104
+ ModelAttribute.ObjectiveBound: float,
105
+ ModelAttribute.ObjectiveValue: float,
106
+ ModelAttribute.RelativeGap: float,
107
+ ModelAttribute.Silent: bool,
108
+ ModelAttribute.SimplexIterations: int,
109
+ ModelAttribute.SolverName: str,
110
+ ModelAttribute.SolverVersion: str,
111
+ ModelAttribute.SolveTimeSec: float,
112
+ ModelAttribute.TimeLimitSec: float,
113
+ }
114
+
115
+
116
+ class ConstraintAttribute(Enum):
117
+ Name = auto()
118
+ # PrimalStart = auto()
119
+ # DualStart = auto()
120
+ Primal = auto()
121
+ Dual = auto()
122
+ # BasisStatus = auto()
123
+
124
+
125
+ constraint_attr_type_map = {
126
+ ConstraintAttribute.Name: str,
127
+ ConstraintAttribute.Primal: float,
128
+ ConstraintAttribute.Dual: float,
129
+ }
@@ -0,0 +1,29 @@
1
+ from .core_ext import ScalarQuadraticFunction, ConstraintSense
2
+
3
+
4
+ def bridge_soc_quadratic_constraint(model, cone_variables, name=""):
5
+ """
6
+ Convert a second order cone constraint to a quadratic constraint.
7
+ x[0] >= sqrt(x[1]^2 + ... + x[n]^2)
8
+ to
9
+ x[0]^2 - x[1]^2 - ... - x[n]^2 >= 0
10
+ """
11
+ N = len(cone_variables)
12
+ if N < 2:
13
+ raise ValueError(
14
+ "Second order cone constraint must have at least two variables"
15
+ )
16
+
17
+ expr = ScalarQuadraticFunction()
18
+ expr.reserve_quadratic(N)
19
+
20
+ x0 = cone_variables[0]
21
+ expr.add_quadratic_term(x0, x0, 1.0)
22
+
23
+ for i in range(1, N):
24
+ xi = cone_variables[i]
25
+ expr.add_quadratic_term(xi, xi, -1.0)
26
+
27
+ con = model.add_quadratic_constraint(expr, ConstraintSense.GreaterEqual, 0.0, name)
28
+
29
+ return con