Skip to contents

constraints_to_penalties(sys, weights = 1) returns a system in which every constraint of sys, including those of its subsystems, is removed and added to the costs as a weighted quadratic penalty: an equation l ~ r contributes w * (l - r)^2 and an inequality with residual res (res <= 0) contributes w * max(res, 0)^2. The result can be solved by optimizers that do not handle constraints. weights is one number or expression for all constraints, or one per constraint; symbolic weights that are not yet parameters of the system are added to its parameters.

Usage

constraints_to_penalties(sys, weights = 1)

Arguments

sys

A System with constraints.

weights

Penalty weights.

Value

A System without constraints.

Examples

x <- Variable("x")
sys <- complete(System(list(), costs = list((x - 2)^2), name = "opt",
                       constraints = list(Inequality(x, 1))))
pen <- constraints_to_penalties(sys, weights = 1e3)
costs(pen)
#> [[1]]
#> (x - 2)^2
#> 
#> [[2]]
#> 1000*Max(0, x - 1)^2
#> 
sol <- solve(OptimizationProblem(pen, list(x ~ 0)))
sol$u
#>        x 
#> 1.000999