简化

Reduction 是一种将问题转化为等价问题的转换过程。 如果一个问题的解可以通过适度的努力转化为另一个问题的解,那么这两个问题是等价的。 CVXPY使用简化来将问题重写为求解器能够接受的形式。

免责声明

大多数用户不需要了解CVXPY的简化系统的任何信息。 实际上,大多数用户甚至不需要知道简化的存在!

为了确保我们在保持向后兼容性的同时改进CVXPY的速度和功能,我们决定将简化系统 不纳入CVXPY的公共API。因此,该系统的某些方面可能会在未来版本中无通知地发生变化。

我们为CVXPY的贡献者、好奇者和那些愿意在可能不适用于未来版本的CVXPY上构建的人提供这份文档。 如果您有一个有趣的应用程序需要直接访问简化系统,并且您关心与未来版本的CVXPY的前向兼容性,请与我们联系。

简化类型

简化允许CVXPY支持不同的问题类别,例如凸编程和对数凸编程。它们还帮助CVXPY针对不同的求解器类别, 如二次规划求解器和锥体求解器。从软件编译器中借用术语,我们将简化分为中端简化和后端简化。一个不考虑目标求解器的简化源问题称为 中间端简化 ,而将源问题转换为特定类型求解器可接受的形式的简化称为 后端简化 。每个求解器(以及它被调用的模式)都被称为*后端*或*目标*。

下面是CVXPY简化的详细说明:

核心类

简化通过以下类进行数据交换和操作。 也可以使用其他数据结构,如以字符串为键的字典,但这些是定义简化系统的主要类。

解决方案

class cvxpy.reductions.solution.Solution(status, opt_val, primal_vars, dual_vars, attr)[source]

A solution to an optimization problem.

status

The status code.

Type:

str

opt_val

The optimal value.

Type:

float

primal_vars

A map from variable ids to optimal values.

Type:

dict of id to NumPy ndarray

dual_vars

A map from constraint ids to dual values.

Type:

dict of id to NumPy ndarray

attr

Miscelleneous information propagated up from a solver.

Type:

dict

简化

class cvxpy.reductions.reduction.Reduction(problem=None)[source]

Abstract base class for reductions.

A reduction is an actor that transforms a problem into an equivalent problem. By equivalent we mean that there exists a mapping between solutions of either problem: if we reduce a problem \(A\) to another problem \(B\) and then proceed to find a solution to \(B\), we can convert it to a solution of \(A\) with at most a moderate amount of effort.

A reduction that is instantiated with a non-None problem offers two key methods: reduce and retrieve. The reduce() method converts the problem the reduction was instantiated with to an equivalent problem. The retrieve() method takes as an argument a Solution for the equivalent problem and returns a Solution for the problem owned by the reduction.

Every reduction offers three low-level methods: accepts, apply, and invert. The accepts method of a particular reduction specifies the types of problems that it is applicable to; the apply method takes a problem and reduces it to an equivalent form, and the invert method maps solutions from reduced-to problems to their problems of provenance.

Parameters:

problem (Problem) – A problem owned by this reduction; possibly None.

__init__(problem=None) None[source]

Construct a reduction for reducing problem.

If problem is not None, then a subsequent invocation of reduce() will reduce problem and return an equivalent one.

accepts(problem)[source]

States whether the reduction accepts a problem.

Parameters:

problem (Problem) – The problem to check.

Returns:

True if the reduction can be applied, False otherwise.

Return type:

bool

abstract apply(problem)[source]

Applies the reduction to a problem and returns an equivalent problem.

Parameters:

problem (Problem) – The problem to which the reduction will be applied.

Returns:

  • Problem or dict – An equivalent problem, encoded either as a Problem or a dict.

  • InverseData, list or dict – Data needed by the reduction in order to invert this particular application.

abstract invert(solution, inverse_data)[source]

Returns a solution to the original problem given the inverse_data.

Parameters:
  • solution (Solution) – A solution to a problem that generated the inverse_data.

  • inverse_data – The data encoding the original problem.

Returns:

A solution to the original problem.

Return type:

Solution

reduce()[source]

Reduces the owned problem to an equivalent problem.

Returns:

An equivalent problem, encoded either as a Problem or a dict.

Return type:

Problem or dict

Raises:

ValueError – If this Reduction was constructed without a Problem.

retrieve(solution)[source]

Retrieves a solution to the owned problem.

Parameters:

solution (Solution) – A solution to the problem emitted by reduce().

Returns:

A solution to the owned problem.

Return type:

Solution

Raises:

ValueError – If self.problem is None, or if reduce() was not previously called.

class cvxpy.reductions.chain.Chain(problem=None, reductions=None)[source]

Bases: Reduction

A logical grouping of multiple reductions into a single reduction.

reductions

A list of reductions.

Type:

list[Reduction]

accepts(problem) bool[source]

A problem is accepted if the sequence of reductions is valid.

In particular, the i-th reduction must accept the output of the i-1th reduction, with the first reduction (self.reductions[0]) in the sequence taking as input the supplied problem.

Parameters:

problem (Problem) – The problem to check.

Returns:

True if the chain can be applied, False otherwise.

Return type:

bool

apply(problem, verbose: bool = False)[source]

Applies the chain to a problem and returns an equivalent problem.

Parameters:
  • problem (Problem) – The problem to which the chain will be applied.

  • verbose (bool, optional) – Whehter to print verbose output.

Returns:

  • Problem or dict – The problem yielded by applying the reductions in sequence, starting at self.reductions[0].

  • list – The inverse data yielded by each of the reductions.

invert(solution, inverse_data)[source]

Returns a solution to the original problem given the inverse_data.

求解链

class cvxpy.reductions.solvers.solving_chain.SolvingChain(problem=None, reductions=None)[source]

Bases: Chain

A reduction chain that ends with a solver.

Parameters:

reductions (list[Reduction]) – A list of reductions. The last reduction in the list must be a solver instance.

reductions

A list of reductions.

Type:

list[Reduction]

solver

The solver, i.e., reductions[-1].

Type:

Solver

prepend(chain) SolvingChain[source]

Create and return a new SolvingChain by concatenating chain with this instance.

solve(problem, warm_start: bool, verbose: bool, solver_opts)[source]

Solves the problem by applying the chain.

Applies each reduction in the chain to the problem, solves it, and then inverts the chain to return a solution of the supplied problem.

Parameters:
  • problem (Problem) – The problem to solve.

  • warm_start (bool) – Whether to warm start the solver.

  • verbose (bool) – Whether to enable solver verbosity.

  • solver_opts (dict) – Solver specific options.

Returns:

solution – A solution to the problem.

Return type:

Solution

solve_via_data(problem, data, warm_start: bool = False, verbose: bool = False, solver_opts={})[source]

Solves the problem using the data output by the an apply invocation.

The semantics are:

data, inverse_data = solving_chain.apply(problem)
solution = solving_chain.invert(solver_chain.solve_via_data(data, ...))

which is equivalent to writing

solution = solving_chain.solve(problem, ...)
Parameters:
  • problem (Problem) – The problem to solve.

  • data (map) – Data for the solver.

  • warm_start (bool) – Whether to warm start the solver.

  • verbose (bool) – Whether to enable solver verbosity.

  • solver_opts (dict) – Solver specific options.

Returns:

The information returned by the solver; this is not necessarily a Solution object.

Return type:

raw solver solution