forked from cvxpy/cvxpy
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathnlp_matrix_stuffing.py
More file actions
97 lines (82 loc) · 3.17 KB
/
nlp_matrix_stuffing.py
File metadata and controls
97 lines (82 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
"""
Copyright 2013 Steven Diamond
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
from __future__ import annotations
import numpy as np
import cvxpy.settings as s
from cvxpy.constraints import (
PSD,
SOC,
Equality,
ExpCone,
Inequality,
NonNeg,
NonPos,
PowCone3D,
Zero,
)
from cvxpy.expressions.variable import Variable
from cvxpy.problems.objective import Minimize
from cvxpy.reductions import InverseData, Solution, cvx_attr2constr
from cvxpy.reductions.matrix_stuffing import (
MatrixStuffing,
extract_lower_bounds,
extract_mip_idx,
extract_upper_bounds,
)
from cvxpy.reductions.solvers.solving_chain_utils import get_canon_backend
from cvxpy.reductions.utilities import (
are_args_affine,
group_constraints,
lower_equality,
lower_ineq_to_nonneg,
nonpos2nonneg,
)
from cvxpy.utilities.coeff_extractor import CoeffExtractor
class NLPMatrixStuffing(MatrixStuffing):
"""Construct matrices for linear cone problems.
Linear cone problems are assumed to have a linear objective and cone
constraints which may have zero or more arguments, all of which must be
affine.
"""
CONSTRAINTS = 'ordered_constraints'
def __init__(self, canon_backend: str | None = None):
self.canon_backend = canon_backend
def accepts(self, problem):
valid_obj_curv = problem.objective.expr.is_affine()
return (type(problem.objective) == Minimize
and valid_obj_curv
and not cvx_attr2constr.convex_attributes(problem.variables())
and are_args_affine(problem.constraints)
and problem.is_dpp())
def apply(self, problem):
pass
def invert(self, solution, inverse_data):
"""Retrieves a solution to the original problem"""
var_map = inverse_data.var_offsets
# Flip sign of opt val if maximize.
opt_val = solution.opt_val
if solution.status not in s.ERROR and not inverse_data.minimize:
opt_val = -solution.opt_val
primal_vars, dual_vars = {}, {}
if solution.status not in s.SOLUTION_PRESENT:
return Solution(solution.status, opt_val, primal_vars, dual_vars,
solution.attr)
# Split vectorized variable into components.
x_opt = list(solution.primal_vars.values())[0]
for var_id, offset in var_map.items():
shape = inverse_data.var_shapes[var_id]
size = np.prod(shape, dtype=int)
primal_vars[var_id] = np.reshape(x_opt[offset:offset+size], shape,
order='F')
return Solution(solution.status, opt_val, primal_vars, dual_vars,
solution.attr)