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 Overflow
🌐
Cvxpy
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
🌐
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.
Find elsewhere
🌐
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 ·
Top answer
1 of 1
1

Looking at the docs we find that this is expected:

As another example, the quotient expr / p is not DPP-compliant when p is a parameter, but this can be rewritten as expr * p_tilde, where p_tilde is a parameter that represents 1/p.

But in your case we need both p and 1 / p? Keeping those two synchronized is challenging. We can use a class we'll call ReciprocalParameter to make it easier.

import cvxpy as cp
from typing import Optional

class ReciprocalParameter:
    """Used for times when you want a cvxpy Parameter and its ratio"""

    def __init__(self, *args, **kwargs) -> None:
        self._p = cp.Parameter(*args, **kwargs)
        # Reciprocal of the above
        self._rp = cp.Parameter(*args, **kwargs)

    @property
    def value(self) -> Optional[float]:
        """Return the value of the Parameter"""
        return self._p.value

    @value.setter
    def value(self, val: Optional[float]) -> None:
        """
        Simultaneously set the value of the Parameter (given by `p`)
        and its reciprocal (given by `rp`)
        """
        self._p.value = val
        self._rp.value = 1 / val if val is not None else None

    @property
    def p(self) -> cp.Parameter:
        """Returns the parameter"""
        return self._p

    @property
    def rp(self) -> cp.Parameter:
        """Returns the reciprocal of the parameter"""
        return self._rp

def cp_log_ratio_norm(a, b: ReciprocalParameter):
  # Both `a * cp.inv_pos(b)` and `a / b` make this problem non-DPP
  return cp.maximum(a * b.rp, b.p * cp.inv_pos(a))

var = cp.Variable(pos=True)
param = ReciprocalParameter(pos=True)
param.value = 5
objective = cp.Minimize(cp_log_ratio_norm(var, param))
problem = cp.Problem(objective, [])
objective_value = problem.solve()

print(f"Objective value = {objective_value}")
print(f"Status = {problem.status}")

Note that we treat ReciprocalParameter just like a normal cvxpy.Parameter until the point of use when we are forced to decide whether we want the parameter's value (ReciprocalParameter.p) or its reciprocal (ReciprocalParameter.rp).