非线性预处理转换

pyomo.contrib.preprocessing 是一个贡献的预处理转换库,旨在操作非线性和混合整数非线性程序(NLPs 和 MINLPs),以及广义析取程序(GDPs)。

这个贡献包由Qi Chen他在卡内基梅隆大学的同事维护。

以下预处理转换是可用的。然而,根据它们的实用性,一些可能会在以后被弃用或合并。

var_aggregator.VariableAggregator

聚合通过等式约束链接的模型变量。

bounds_to_vars.ConstraintToVarBoundTransform

将约束更改为变量的边界。

induced_linearity.InducedLinearity

重新表述具有诱导线性性的非线性约束。

constraint_tightener.TightenConstraintFromVars

已弃用。

deactivate_trivial_constraints.TrivialConstraintDeactivator

停用无关紧要的约束。

detect_fixed_vars.FixedVarDetector

检测那些实际上是固定的但未被视为固定的变量。

equality_propagate.FixedVarPropagator

传播变量固定以处理类型为 \(x = y\) 的等式。

equality_propagate.VarBoundPropagator

传播类型为 \(x = y\) 的等式的变量边界。

init_vars.InitMidpoint

将非固定变量初始化为其边界的中间点。

init_vars.InitZero

将非固定变量初始化为零。

remove_zero_terms.RemoveZeroTerms

在约束中查找 \(0 v\) 并将其移除。

strip_bounds.VariableBoundStripper

从变量中去除边界。

zero_sum_propagator.ZeroSumPropagator

传播仅正(或负)变量之和的固定到零。

变量聚合器

以下代码片段展示了在具体Pyomo模型上使用变量聚合转换的示例:

>>> from pyomo.environ import *
>>> m = ConcreteModel()
>>> m.v1 = Var(initialize=1, bounds=(1, 8))
>>> m.v2 = Var(initialize=2, bounds=(0, 3))
>>> m.v3 = Var(initialize=3, bounds=(-7, 4))
>>> m.v4 = Var(initialize=4, bounds=(2, 6))
>>> m.c1 = Constraint(expr=m.v1 == m.v2)
>>> m.c2 = Constraint(expr=m.v2 == m.v3)
>>> m.c3 = Constraint(expr=m.v3 == m.v4)
>>> TransformationFactory('contrib.aggregate_vars').apply_to(m)

要查看转换的结果,您可以使用以下命令

>>> m.pprint()
class pyomo.contrib.preprocessing.plugins.var_aggregator.VariableAggregator(**kwds)[source]

聚合通过等式约束链接的模型变量。

之前:

\[\begin{split}x &= y \\ a &= 2x + 6y + 7 \\ b &= 5y + 6 \\\end{split}\]

之后:

\[\begin{split}z &= x = y \\ a &= 8z + 7 \\ b &= 5z + 6\end{split}\]

警告

待办事项:目前尚不清楚“大写E”表达式此时会发生什么。

apply_to(model, **kwds)

将转换应用于给定的模型。

create_using(model, **kwds)

使用此转换创建一个新模型

update_variables(model)[源代码]

更新被聚合替换的变量的值。

待办事项:降低成本

显式约束到变量边界

>>> from pyomo.environ import *
>>> m = ConcreteModel()
>>> m.v1 = Var(initialize=1)
>>> m.v2 = Var(initialize=2)
>>> m.v3 = Var(initialize=3)
>>> m.c1 = Constraint(expr=m.v1 == 2)
>>> m.c2 = Constraint(expr=m.v2 >= -2)
>>> m.c3 = Constraint(expr=m.v3 <= 5)
>>> TransformationFactory('contrib.constraints_to_var_bounds').apply_to(m)
class pyomo.contrib.preprocessing.plugins.bounds_to_vars.ConstraintToVarBoundTransform(**kwds)[source]

将约束更改为变量的边界。

寻找形式为 \(k*v + c_1 \leq c_2\) 的约束。如果结果是一个更紧的界限,则将变量 \(v\) 的下界更改为 \((c_2 - c_1)/k\)。同样,对下界也执行相同的操作。

以下关键字参数是为 apply_tocreate_using 函数指定的。

Keyword Arguments:
  • tolerance (NonNegativeFloat, default=1e-13) – 边界相等的容差 (\(LB = UB\))

  • detect_fixed (bool, default=True) – 如果为True,当\(| LB - UB | \leq tolerance\)时,固定变量。

apply_to(model, **kwds)

将转换应用于给定的模型。

create_using(model, **kwds)

使用此转换创建一个新模型

诱导线性重构

class pyomo.contrib.preprocessing.plugins.induced_linearity.InducedLinearity(**kwds)[source]

重新表述具有诱导线性性的非线性约束。

找到连续变量 \(v\),其中 \(v = d_1 + d_2 + d_3\), 其中 \(d\) 是离散变量。这些连续变量可能非线性地参与其他表达式,然后可能被诱导为线性。

整体算法流程可以总结为:

  1. 有效检测离散变量以及暗示离散性的约束条件。

  2. 确定每个有效离散变量的有效值集合

  3. 查找有效离散变量参与的非线性表达式。

  4. 适当地重新表述非线性表达式。

注意

任务1和任务2必须包含范围考虑(Disjuncts)

以下关键字参数是为 apply_tocreate_using 函数指定的。

Keyword Arguments:
  • equality_tolerance (NonNegativeFloat, default=1e-06) – 等式约束的容差。

  • pruning_solver (default='glpk') – 在修剪可能值时使用的求解器。

apply_to(model, **kwds)

将转换应用于给定的模型。

create_using(model, **kwds)

使用此转换创建一个新模型

约束边界收紧器

这个转换是由Sunjeev Kale在卡内基梅隆大学开发的。

class pyomo.contrib.preprocessing.plugins.constraint_tightener.TightenConstraintFromVars[源代码]

已弃用。

根据变量边界收紧约束的上下界。

遍历每个变量,并使用从变量边界推断出的值来收紧约束边界。

目前,这仅适用于线性约束。

自版本5.7起已弃用:约束收紧转换的使用已被弃用。其功能可以通过pyomo.contrib.fbbt.compute_bounds_on_expr(constraint.body)部分复制。

apply_to(model, **kwds)

将转换应用于给定的模型。

create_using(model, **kwds)

使用此转换创建一个新模型

简单约束停用

class pyomo.contrib.preprocessing.plugins.deactivate_trivial_constraints.TrivialConstraintDeactivator(**kwds)[source]

停用无关紧要的约束。

平凡约束的形式为 \(k_1 = k_2\)\(k_1 \leq k_2\),其中 \(k_1\)\(k_2\) 是常数。这些约束通常在变量被固定时出现。

以下关键字参数是为 apply_tocreate_using 函数指定的。

Keyword Arguments:
  • tmp (bool, default=False) – 如果为True,则存储一组转换后的约束,以便将来可以恢复转换。

  • ignore_infeasible (bool, default=False) – 如果为True,则跳过不可行的简单约束,而不是抛出InfeasibleConstraintException异常。

  • return_trivial (default=[]) – 一个列表,停用的 trivialconstraints 将被附加到该列表中(副作用)

  • tolerance (NonNegativeFloat, default=1e-13) – 约束违反的容忍度

apply_to(model, **kwds)

将转换应用于给定的模型。

create_using(model, **kwds)

使用此转换创建一个新模型

revert(instance)[source]

恢复由转换停用的约束。

Parameters:

instance – 之前停用了简单约束的模型实例。

固定变量检测

class pyomo.contrib.preprocessing.plugins.detect_fixed_vars.FixedVarDetector(**kwds)[source]

检测那些实际上是固定的但未被视为固定的变量。

对于模型中的每个变量\(v\),检查其下界\(v^{LB}\)是否在其上界\(v^{UB}\)的某个容差范围内。如果是,则将变量固定为\(v^{LB}\)的值。

以下关键字参数是为 apply_tocreate_using 函数指定的。

Keyword Arguments:
  • tmp (bool, default=False) – 如果为True,则存储转换后的变量集及其旧值,以便可以恢复它们。

  • 容差 (非负浮点数, 默认=1e-13) – 边界相等性的容差 (LB == UB)

apply_to(model, **kwds)

将转换应用于给定的模型。

create_using(model, **kwds)

使用此转换创建一个新模型

revert(instance)[source]

恢复由转换固定的变量。

固定变量等式传播器

class pyomo.contrib.preprocessing.plugins.equality_propagate.FixedVarPropagator(**kwds)[source]

传播变量固定以处理类型为 \(x = y\) 的等式。

如果 \(x\) 是固定的且 \(y\) 不是固定的,那么这个转换将把 \(y\) 固定为 \(x\) 的值。

这种转换也可以作为临时转换执行,转换后的变量会被保存,以后可以取消固定。

以下关键字参数是为 apply_tocreate_using 函数指定的。

Keyword Arguments:

tmp (bool, default=False) – 如果为True,则存储转换后的变量集及其旧状态,以便以后可以恢复。

apply_to(model, **kwds)

将转换应用于给定的模型。

create_using(model, **kwds)

使用此转换创建一个新模型

revert(instance)[source]

恢复由转换固定的变量。

变量边界等式传播器

class pyomo.contrib.preprocessing.plugins.equality_propagate.VarBoundPropagator(**kwds)[源代码]

传播类型为 \(x = y\) 的等式的变量边界。

如果 \(x\) 有一个更紧的边界,那么 \(y\),这个转换将调整 \(y\) 的边界以匹配 \(x\) 的边界。

以下关键字参数是为 apply_tocreate_using 函数指定的。

Keyword Arguments:

tmp (bool, default=False) – 如果为True,则存储转换后的变量集及其旧状态,以便以后可以恢复。

apply_to(model, **kwds)

将转换应用于给定的模型。

create_using(model, **kwds)

使用此转换创建一个新模型

revert(instance)[source]

恢复变量边界。

变量中点初始化器

class pyomo.contrib.preprocessing.plugins.init_vars.InitMidpoint(**kwds)[source]

将非固定变量初始化为其边界的中间点。

  • 如果变量没有边界,将值设为零。

  • 如果变量缺少一个边界,将值设置为现有边界的值。

apply_to(model, **kwds)

将转换应用于给定的模型。

create_using(model, **kwds)

使用此转换创建一个新模型

变量零初始化器

class pyomo.contrib.preprocessing.plugins.init_vars.InitZero(**kwds)[source]

将非固定变量初始化为零。

  • 如果将变量值设置为零会违反边界,则将变量值设置为相关边界值。

apply_to(model, **kwds)

将转换应用于给定的模型。

create_using(model, **kwds)

使用此转换创建一个新模型

零项移除器

class pyomo.contrib.preprocessing.plugins.remove_zero_terms.RemoveZeroTerms(**kwds)[source]

在约束中查找 \(0 v\) 并将其移除。

目前仅限于处理形式为\(x_1 = 0 x_3\)的线性约束,这是由于固定\(x_2 = 0\)而产生的。

注意

待办事项:支持非线性表达式

apply_to(model, **kwds)

将转换应用于给定的模型。

create_using(model, **kwds)

使用此转换创建一个新模型

变量边界移除器

class pyomo.contrib.preprocessing.plugins.strip_bounds.VariableBoundStripper(**kwds)[源代码]

从变量中去除边界。

以下关键字参数是为 apply_tocreate_using 函数指定的。

Keyword Arguments:
  • strip_domains (bool, default=True) – 同时去除离散变量的域

  • reversible (bool, default=False) – 边界剥离是否是临时的。如果是,存储信息以便恢复。

apply_to(model, **kwds)

将转换应用于给定的模型。

create_using(model, **kwds)

使用此转换创建一个新模型

revert(instance)[源代码]

恢复由转换更改的变量边界和域。

零和传播器

class pyomo.contrib.preprocessing.plugins.zero_sum_propagator.ZeroSumPropagator(**kwds)[source]

传播仅正(或负)变量之和的固定到零。

如果 \(z\) 被固定为零,并且 \(z = x_1 + x_2 + x_3\)\(x_1\), \(x_2\), \(x_3\) 都是非负或都是 非正,那么 \(x_1\), \(x_2\), 和 \(x_3\) 将被固定 为零。

apply_to(model, **kwds)

将转换应用于给定的模型。

create_using(model, **kwds)

使用此转换创建一个新模型