You can now access the parameters and variables as a dictionary using:
param = cp.Parameter(name='paramname')
problem.param_dict
and
var = cp.Variable(name='varname')
problem.var_dict
where the parameter/variable names are the keys of the dictionary
Answer from jdkworld on Stack OverflowCvxpy
cvxpy.org › api_reference › cvxpy.expressions.html
Expressions -
A leaf node of an expression tree; i.e., a Variable, Constant, or Parameter.
Cvxpy
cvxpy.org › tutorial › intro › index.html
What is CVXPY? -
The purpose of parameters is to change the value of a constant in a problem without reconstructing the entire problem.
Cvxpy
cvxpy.org › tutorial › dpp › index.html
Disciplined Parametrized Programming -
Note: DPP requires CVXPY version 1.1.0 or greater. Parameters are symbolic representations of constants. Using parameters lets you modify the values of constants without reconstructing the entire problem.
Cvxpy
cvxpy.org › _modules › cvxpy › expressions › constants › parameter.html
Source code for cvxpy.expressions.constants.parameter
[docs] class Parameter(Leaf): """Parameters in optimization problems. Parameters are constant expressions whose value may be specified after problem creation. The only way to modify a problem after its creation is through parameters. For example, you might choose to declare the hyper-parameters ...
Snyk
snyk.io › advisor › cvxpy › functions › cvxpy.parameter
How to use the cvxpy.Parameter function in cvxpy | Snyk
def _generate_cvxpy_problem(self): ''' Generate QP problem ''' x = cvxpy.Variable(self.n) y = cvxpy.Variable(self.k) # Create parameters m mu = cvxpy.Parameter(self.n) mu.value = self.mu objective = cvxpy.Minimize(cvxpy.quad_form(x, self.D) + cvxpy.quad_form(y, spa.eye(self.k)) + - 1 / self.gamma * (mu.T * x)) constraints = [np.ones(self.n) * x == 1, self.F.T * x == y, 0 <= x, x <= 1] problem = cvxpy.Problem(objective, constraints) return problem, mu
Top answer 1 of 2
3
You can now access the parameters and variables as a dictionary using:
param = cp.Parameter(name='paramname')
problem.param_dict
and
var = cp.Variable(name='varname')
problem.var_dict
where the parameter/variable names are the keys of the dictionary
2 of 2
2
Following sascha's comments on creating a wrapper class...
from cvxpy import Variable, Parameter, Problem, Minimize
class MyProblem(self, n, ...):
self._var = Variable(n)
self.param = Parameter(n)
costs = #Some complex convex function of var and param
self._problem = Problem(Minimize(costs), constraints)
def solve(self):
self._problem.solve()
return self._target
problem = MyProblem(4, ...)
for param_value in param_values:
problem.param.value = param_value
answer = problem.solve()
This allows for a sweep through the parameter param while standardizing the problem design.
Cvxpy
cvxpy.org › tutorial › advanced › index.html
Advanced Features -
CVXPY now supports N-dimensional expressions. This allows one to define variables, parameters, and constants with arbitrary number of dimensions.
Cvxpy
cvxpy.org › api_reference › cvxpy.problems.html
Problems -
You can specify perturbations in a Parameter by setting its delta attribute (if unspecified, the perturbation defaults to 0). This method populates the delta attribute of the Variables as a side-effect. This method can only be called after calling solve() with requires_grad=True.
Cvxpy
cvxpy.org › tutorial › functions › index.html
Atomic Functions -
If all arguments have known sign but CVXPY can determine that the returned Expression would have different signs in different entries (for example, when stacking a positive Expression and a negative Expression) then the returned Expression will have unknown sign.
Cvxpy
cvxpy.org › version › 1.2 › tutorial › advanced › index.html
Advanced Features — CVXPY 1.2 documentation
Only relevant for problems involving Parameters. Defaults to False. ignore_dpp (bool, optional) – When True, DPP problems will be treated as non-DPP, which may speed up compilation. Defaults to False. kwargs – Additional keyword arguments specifying solver specific options. ... The optimal value for the problem, or a string indicating why the problem could not be solved. We will discuss the optional arguments in detail below. CVXPY is distributed with the open source solvers ECOS, OSQP, and SCS.
Cvxpy
cvxpy.org › version › 1.2 › api_reference › cvxpy.expressions.html
Expressions — CVXPY 1.2 documentation
Parameters are constant expressions whose value may be specified after problem creation. The only way to modify a problem after its creation is through parameters.
Cvxpy
cvxpy.org › tutorial › dcp › index.html
Disciplined Convex Programming -
import cvxpy as cp # Create variables and parameters. x, y = cp.Variable(), cp.Variable() a, b = cp.Parameter(), cp.Parameter() # Examples of CVXPY expressions.
Cvxpy
cvxpy.org › tutorial › constraints › index.html
Advanced Constraints -
By default variables and parameters are real valued. Complex valued variables and parameters can be created by setting the attribute complex=True. Similarly, purely imaginary variables and parameters can be created by setting the attributes imag=True. Expressions containing complex variables, ...
Cvxpy
cvxpy.org › examples › derivatives › fundamentals.html
Derivatives fundamentals -
Notice the keyword argument dpp=True. The parameters must enter in the DGP problem acording to special rules, which we refer to as dpp.
Readthedocs
ajfriendcvxpy.readthedocs.io › en › latest › tutorial › advanced
Advanced Features — CVXPY 0.2.25 documentation
relaxation parameter (default: 1.8). 'normalize' whether to precondition data matrices (default: True). 'use_indirect' whether to use indirect solver for KKT sytem (instead of direct) (default: False). If you are interested in getting the standard form that CVXPY produces for a problem, you can use the get_problem_data method.
Cvxpy
cvxpy.org › api_reference › cvxpy.constraints.html
Constraints -
Parameters:¶ · tolerance : float¶ · The absolute tolerance to impose on the violation. Returns:¶ · True if the violation is less than tolerance, False otherwise. Return type:¶ · bool · Raises:¶ · ValueError – If the constrained expression does not have a value associated with it. ...
Cvxpy
cvxpy.org › version › 1.1 › api_reference › cvxpy.expressions.html
Expressions — CVXPY 1.1.24 documentation
Parameters are constant expressions whose value may be specified after problem creation. The only way to modify a problem after its creation is through parameters.
Cvxpy
cvxpy.org › tutorial › index.html
User Guide - CVXPY
What is CVXPY? Changing the problem · Infeasible and unbounded problems · Other problem statuses · Vectors and matrices · Constraints · Parameters · Atomic Functions · Operators · Scalar functions · Functions along an axis · Elementwise functions ·