Problems

The Problem class is the entry point to specifying and solving optimization problems. Each cvxpy.problems.problem.Problem instance encapsulates an optimization problem, i.e., an objective and a set of constraints, and the solve() method either solves the problem encoded by the instance, returning the optimal value and setting variables values to optimal points, or reports that the problem was in fact infeasible or unbounded. You can construct a problem, solve it, and inspect both its value and the values of its variables like so:

problem = Problem(Minimize(expression), constraints)
problem.solve()
if problem.status not in ["infeasible", "unbounded"]:
    # Otherwise, problem.value is inf or -inf, respectively.
    print "Optimal value: %s" % problem.value
for variable in problem.variables():
    print "Variable %s: value %s" % (variable.name(), variable.value)

Problems are immutable, except through the specification of Parameter values. This means that you cannot modify a problem’s objective or constraints after you have created it. If you find yourself wanting to add a constraint to an existing problem, you should instead create a new problem using, for example, the following idiom:

problem = Problem(Minimize(expression), constraints)
problem = Problem(problem.objective, problem.constraints + new_constraints)

Most users need not know anything about the Problem class except how to instantiate it, how to solve problem instances (solve()), and how to query the solver results (status and value).

Information about the size of a problem instance and statistics about the most recent solve invocation are captured by the SizeMetrics and SolverStats classes, respectively, and can be accessed via the size_metrics() and solver_stats() properties of the Problem class.

Minimize

class cvxpy.problems.objective.Minimize(expr)[source]

Bases: cvxpy.problems.objective.Objective

An optimization objective for minimization.

Parameters:expr (Expression) – The expression to minimize. Must be a scalar.
Raises:ValueError – If expr is not a scalar.
is_dcp()[source]

The objective must be convex.

Maximize

class cvxpy.problems.objective.Maximize(expr)[source]

Bases: cvxpy.problems.objective.Objective

An optimization objective for maximization.

Parameters:expr (Expression) – The expression to maximize. Must be a scalar.
Raises:ValueError – If expr is not a scalar.
is_dcp()[source]

The objective must be concave.

SizeMetrics

class cvxpy.problems.problem.SizeMetrics(problem)[source]

Bases: object

Reports various metrics regarding the problem.

num_scalar_variables

integer – The number of scalar variables in the problem.

num_scalar_data

integer – The number of scalar constants and parameters in the problem. The number of constants used across all matrices, vectors, in the problem. Some constants are not apparent when the problem is constructed: for example, The sum_squares expression is a wrapper for a quad_over_lin expression with a constant 1 in the denominator.

num_scalar_eq_constr

integer – The number of scalar equality constraints in the problem.

num_scalar_leq_constr

integer – The number of scalar inequality constraints in the problem.

max_data_dimension

integer – The longest dimension of any data block constraint or parameter.

max_big_small_squared

integer – The maximum value of (big)(small)^2 over all data blocks of the problem, where (big) is the larger dimension and (small) is the smaller dimension for each data block.

SolverStats

class cvxpy.problems.problem.SolverStats(results_dict, solver_name)[source]

Bases: object

Reports some of the miscellaneous information that is returned by the solver after solving but that is not captured directly by the Problem instance.

solve_time

double – The time (in seconds) it took for the solver to solve the problem.

setup_time

double – The time (in seconds) it took for the solver to setup the problem.

num_iters

int – The number of iterations the solver had to go through to find a solution.

Problem

class cvxpy.problems.problem.Problem(objective, constraints=None)[source]

Bases: cvxpy.utilities.canonical.Canonical

A convex optimization problem.

Problems are immutable, save for modification through the specification of Parameter

Parameters:
  • objective (Minimize or Maximize) – The problem’s objective.
  • constraints (list) – The constraints on the problem variables.
atoms()[source]

Accessor method for atoms.

Returns:A list of the atom types in the problem; note that this list contains classes, not instances.
Return type:list of Atom
constants()[source]

Accessor method for parameters.

Returns:A list of the constants in the problem.
Return type:list of Constant
constraints

A shallow copy of the problem’s constraints.

Note that constraints cannot be reassigned, appended to, or otherwise modified after creation, except through parameters.

get_problem_data(solver)[source]

Returns the problem data used in the call to the solver.

When a problem is solved, a chain of reductions, called a SolvingChain, compiles it to some low-level representation that is compatible with the targeted solver. This method returns that low-level representation.

For some solving chains, this low-level representation is a dictionary that contains exactly those arguments that were supplied to the solver; however, for other solving chains, the data is an intermediate representation that is compiled even further by libraries other than CVXPY.

A solution to the equivalent low-level problem can be obtained via the data by invoking the solve_via_data method of the returned solving chain, a thin wrapper around the code external to CVXPY that further processes and solves the problem. Invoke the unpack_results method to recover a solution to the original problem.

Parameters:solver (str) – The solver the problem data is for.
Returns:
  • dict or object – lowest level representation of problem
  • SolvingChain – The solving chain that created the data.
  • list – The inverse data generated by the chain.
is_dcp()[source]

Does the problem satisfy DCP rules?

is_qp()[source]

Is problem a quadratic program?

objective

Minimize or Maximize – The problem’s objective.

Note that the objective cannot be reassigned after creation, and modifying the objective after creation will result in undefined behavior.

parameters()[source]

Accessor method for parameters.

Returns:A list of the parameters in the problem.
Return type:list of Parameter
classmethod register_solve(name, func)[source]

Adds a solve method to the Problem class.

Parameters:
  • name (str) – The keyword for the method.
  • func (function) – The function that executes the solve method. This function must take as its first argument the problem instance to solve.
size_metrics

SizeMetrics – Information about the problem’s size.

solve(*args, **kwargs)[source]

Solves the problem using the specified method.

Parameters:
  • method (function) – The solve method to use.
  • solver (str, optional) – The solver to use.
  • verbose (bool, optional) – Overrides the default of hiding solver output.
  • solver_specific_opts (dict, optional) – A dict of options that will be passed to the specific solver. In general, these options will override any default settings imposed by cvxpy.
Returns:

The optimal value for the problem, or a string indicating why the problem could not be solved.

Return type:

float

Raises:
  • DCPError – Raised if the problem is not DCP.
  • SolverError – Raised if no suitable solver exists among the installed solvers, or if an unanticipated error is encountered.
solver_stats

SolverStats – Information returned by the solver.

status

str – The status from the last time the problem was solved; one of optimal, infeasible, or unbounded.

unpack_results(solution, chain, inverse_data)[source]

Updates the problem state given the solver results.

Updates problem.status, problem.value and value of primal and dual variables.

Parameters:
  • solution (Solution) – The solution returned by applying the chain to the problem and invoking the solver on the resulting data.
  • chain (SolvingChain) – A solving chain that was used to solve the problem.
  • inverse_data (list) – The inverse data returned by applying the chain to the problem.
value

float – The value from the last time the problem was solved (or None if not solved).

variables()[source]

Accessor method for variables.

Returns:A list of the variables in the problem.
Return type:list of Variable