SOS约束
(类来自 pyomo.core.base.sos)
- class pyomo.core.base.sos.SOSConstraint(*args, **kwds)[source]
-
为特殊有序集(SOS)实现约束。
- Parameters:
sos (int) – SOS的类型。
var (pyomo.environ.Var) – 将从中创建SOS的变量组。
index (pyomo.environ.Set, list 或 dict, 可选) – 一个数据结构,包含要成为SOS(s)成员的变量的索引。索引可以作为pyomo Set提供:如果SOS是索引的,则提供索引;否则提供非索引。或者,索引可以作为列表提供,用于非索引的SOS,或作为字典提供,用于索引的SOS(s)。
weights (pyomo.environ.Param 或 dict, 可选) – 一个包含SOS(s)每个成员权重的数据结构。这些权重可以作为pyomo Param或dict提供。如果未提供,权重将使用var索引集自动确定。
规则 (可选) – 一个返回包含变量列表和相应权重列表的2元组的方法,或者一个变量列表,其权重随后根据它们在列表中的位置确定,或者,如果不应将约束包含在模型/实例中,则使用pyomo.environ.Constraint.Skip。此参数不能与var、index或weights结合使用。
示例
1 - 一个由pyomo Var组件的所有成员组成的N类型的SOS:
>>> # import pyomo >>> import pyomo.environ as pyo >>> # declare the model >>> model = pyo.AbstractModel() >>> # define the SOS type >>> N = 1 # 2, 3, ... >>> # the set that indexes the variables >>> model.A = pyo.Set() >>> # the variables under consideration >>> model.x = pyo.Var(model.A) >>> # the sos constraint >>> model.mysos = pyo.SOSConstraint(var=model.x, sos=N)
2 - 一个由所有pyomo Var组件成员组成的类型为N的SOS,每个成员都有一个特定的权重:
>>> # declare the model >>> model = pyo.AbstractModel() >>> # define the SOS type >>> N = 1 # 2, 3, ... >>> # the set that indexes the variables >>> model.A = pyo.Set() >>> # the variables under consideration >>> model.x = pyo.Var(model.A) >>> # the weights for each variable used in the sos constraints >>> model.mysosweights = pyo.Param(model.A) >>> # the sos constraint >>> model.mysos = pyo.SOSConstraint( ... var=model.x, ... sos=N, ... weights=model.mysosweights ... )
3 - 由Var组件中选定的成员组成的N类型的SOS:
>>> # declare the model >>> model = pyo.AbstractModel() >>> # define the SOS type >>> N = 1 # 2, 3, ... >>> # the set that indexes the variables >>> model.A = pyo.Set() >>> # the variables under consideration >>> model.x = pyo.Var(model.A) >>> # the set that indexes the variables actually used in the constraint >>> model.B = pyo.Set(within=model.A) >>> # the sos constraint >>> model.mysos = pyo.SOSConstraint(var=model.x, sos=N, index=model.B)
4 - 由Var组件的选定成员组成的类型为N的SOS,每个成员都有特定的权重:
>>> # declare the model >>> model = pyo.AbstractModel() >>> # define the SOS type >>> N = 1 # 2, 3, ... >>> # the set that indexes the variables >>> model.A = pyo.Set() >>> # the variables under consideration >>> model.x = pyo.Var(model.A) >>> # the set that indexes the variables actually used in the constraint >>> model.B = pyo.Set(within=model.A) >>> # the weights for each variable used in the sos constraints >>> model.mysosweights = pyo.Param(model.B) >>> # the sos constraint >>> model.mysos = pyo.SOSConstraint( ... var=model.x, ... sos=N, ... index=model.B, ... weights=model.mysosweights ... )
5 - 一组由pyomo Var组件成员组成的类型为N的SOS(s):
>>> # declare the model >>> model = pyo.AbstractModel() >>> # define the SOS type >>> N = 1 # 2, 3, ... >>> # the set that indexes the variables >>> model.A = pyo.Set() >>> # the variables under consideration >>> model.x = pyo.Var(model.A) >>> # the set indexing the sos constraints >>> model.B = pyo.Set() >>> # the sets containing the variable indexes for each constraint >>> model.mysosvarindexset = pyo.Set(model.B) >>> # the sos constraints >>> model.mysos = pyo.SOSConstraint( ... model.B, ... var=model.x, ... sos=N, ... index=model.mysosvarindexset ... )
6 - 一组类型为N的SOS(s),由pyomo Var组件的成员组成,每个成员都有特定的权重:
>>> # declare the model >>> model = pyo.AbstractModel() >>> # define the SOS type >>> N = 1 # 2, 3, ... >>> # the set that indexes the variables >>> model.A = pyo.Set() >>> # the variables under consideration >>> model.x = pyo.Var(model.A) >>> # the set indexing the sos constraints >>> model.B = pyo.Set() >>> # the sets containing the variable indexes for each constraint >>> model.mysosvarindexset = pyo.Set(model.B) >>> # the set that indexes the variables used in the sos constraints >>> model.C = pyo.Set(within=model.A) >>> # the weights for each variable used in the sos constraints >>> model.mysosweights = pyo.Param(model.C) >>> # the sos constraints >>> model.mysos = pyo.SOSConstraint( ... model.B, ... var=model.x, ... sos=N, ... index=model.mysosvarindexset, ... weights=model.mysosweights, ... )
7 - 使用规则参数创建的N类型的简单SOS:
>>> # declare the model >>> model = pyo.AbstractModel() >>> # define the SOS type >>> N = 1 # 2, 3, ... >>> # the set that indexes the variables >>> model.A = pyo.Set() >>> # the variables under consideration >>> model.x = pyo.Var(model.A, domain=pyo.NonNegativeReals) >>> # the rule method creating the constraint >>> def rule_mysos(m): ... var_list = [m.x[a] for a in m.x] ... weight_list = [i+1 for i in range(len(var_list))] ... return (var_list, weight_list) >>> # the sos constraint(s) >>> model.mysos = pyo.SOSConstraint(rule=rule_mysos, sos=N)
8 - 使用规则参数创建的N类型的简单SOS,其中权重是自动确定的:
>>> # declare the model >>> model = pyo.AbstractModel() >>> # define the SOS type >>> N = 1 # 2, 3, ... >>> # the set that indexes the variables >>> model.A = pyo.Set() >>> # the variables under consideration >>> model.x = pyo.Var(model.A, domain=pyo.NonNegativeReals) >>> # the rule method creating the constraint >>> def rule_mysos(m): ... return [m.x[a] for a in m.x] >>> # the sos constraint(s) >>> model.mysos = pyo.SOSConstraint(rule=rule_mysos, sos=N)
9 - 一组类型为N的SOS(s),涉及不同pyomo Var组件的成员,每个成员都有特定的权重。这需要rule参数:
>>> # declare the model >>> model = pyo.AbstractModel() >>> # define the SOS type >>> N = 1 # 2, 3, ... >>> # the set that indexes the x variables >>> model.A = pyo.Set() >>> # the set that indexes the y variables >>> model.B = pyo.Set() >>> # the set that indexes the SOS constraints >>> model.C = pyo.Set() >>> # the x variables, which will be used in the constraints >>> model.x = pyo.Var(model.A, domain=pyo.NonNegativeReals) >>> # the y variables, which will be used in the constraints >>> model.y = pyo.Var(model.B, domain=pyo.NonNegativeReals) >>> # the x variable indices for each constraint >>> model.mysosindex_x = pyo.Set(model.C) >>> # the y variable indices for each constraint >>> model.mysosindex_y = pyo.Set(model.C) >>> # the weights for the x variable indices >>> model.mysosweights_x = pyo.Param(model.A) >>> # the weights for the y variable indices >>> model.mysosweights_y = pyo.Param(model.B) >>> # the rule method with which each constraint c is built >>> def rule_mysos(m, c): ... var_list = [m.x[a] for a in m.mysosindex_x[c]] ... var_list.extend([m.y[b] for b in m.mysosindex_y[c]]) ... weight_list = [m.mysosweights_x[a] for a in m.mysosindex_x[c]] ... weight_list.extend([m.mysosweights_y[b] for b in m.mysosindex_y[c]]) ... return (var_list, weight_list) >>> # the sos constraint(s) >>> model.mysos = pyo.SOSConstraint( ... model.C, ... rule=rule_mysos, ... sos=N ... )
方法
__init__(*args, **kwargs)构造函数
activate()将活动属性设置为True
add(index, variables[, weights])为指定索引添加组件数据。
clear()清除此组件中的数据
clear_suffix_value(suffix_or_name[, expand])清除此组件数据的后缀值
cname(*args, **kwds)已弃用。
construct([data])构建此组件
将活动属性设置为False
dim()返回索引的维度
display([ostream, verbose, prefix])get_suffix_value(suffix_or_name[, default])获取此组件数据的后缀值
getname([fully_qualified, name_buffer, ...])返回与此对象关联的组件名称。
返回所有ComponentData实例的字典id->索引。
返回索引集
如果此类是Pyomo组件,则返回True
如果这个类已经被构造,则返回True
is_expression_type([expression_system])如果此数值是一个表达式,则返回 True
如果此组件已索引,则返回true
如果此类是Pyomo布尔对象,则返回True。
如果此数值是一个命名表达式,则返回 True
如果此类是Pyomo数值对象,则返回True
除非此类是参数对象,否则返回 False
如果此组件是引用,则返回True,其中“引用”被解释为任何不拥有自己数据的组件。
除非此类是变量对象,否则返回 False
items([排序, 有序])返回一个包含(index, data)组件数据元组的迭代器
已弃用。
iterkeys()已弃用。
已弃用。
keys([sort, ordered])返回组件数据键的迭代器
model()返回与此对象关联的模型。
返回此对象的父对象。
返回与此对象关联的组件。
pprint([ostream, verbose, prefix])待办事项
reconstruct([data])已移除:reconstruct() 在 Pyomo 6.0 中被移除。
返回 self.model()
set_suffix_value(suffix_or_name, value[, expand])设置此组件数据的后缀值
set_value(value)设置标量组件的值。
待办事项
type()已弃用。
如果这可以用作模型组件,则返回True。
values([sort, ordered])返回组件数据对象的迭代器
属性
Skip返回活动属性
返回此组件的类类型
仅在直接父容器的上下文中获取组件名称。
获取完全限定的组件名称。
成员文档
- activate()
将活动属性设置为True
- clear()
清除此组件中的数据
- clear_suffix_value(suffix_or_name, expand=True)
清除此组件数据的后缀值
- cname(*args, **kwds)
已弃用。
自版本5.0起已弃用:cname() 方法已重命名为 getname()。 获取组件名称的首选方法是使用 .name 属性,该属性返回完全限定的组件名称。 .local_name 属性将仅在直接父容器的上下文中返回组件名称。
- deactivate()
将活动属性设置为False
- dim()
返回索引的维度
- get_suffix_value(suffix_or_name, default=None)
获取此组件数据的后缀值
- getname(fully_qualified=False, name_buffer=None, relative_to=None)
返回与此对象关联的组件名称。
- id_index_map()
返回所有ComponentData实例的字典id->索引。
- index_set()
返回索引集
- is_component_type()
如果此类是Pyomo组件,则返回True
- is_constructed()
如果这个类已经被构造,则返回True
- is_expression_type(expression_system=None)
如果此数值是一个表达式,则返回 True
- is_indexed()
如果此组件已索引,则返回true
- is_logical_type()
如果此类是Pyomo布尔对象,则返回True。
布尔对象包括常量、变量或逻辑表达式。
- is_named_expression_type()
如果此数值是一个命名表达式,则返回 True
- is_numeric_type()
如果此类是Pyomo数值对象,则返回True
- is_parameter_type()
除非此类是参数对象,否则返回 False
- is_reference()
如果此组件是引用,则返回True,其中“引用”被解释为不拥有自己数据的任何组件。
- is_variable_type()
除非此类是变量对象,否则返回 False
- items(sort=<SortComponents.UNSORTED: 0>, ordered=NOTSET)
返回一个包含(index, data)组件数据元组的迭代器
- Parameters:
sort (bool 或 SortComponents) – 以指定的排序顺序迭代声明的组件项。有关有效选项和描述,请参见
SortComponents。ordered (bool) – 已弃用:请使用 sort=SortComponents.ORDERED_INDICES。 如果为 True,则项目将以确定性顺序返回 (使用底层集合的 ordered_iter()。
- iteritems()
已弃用。
从字典返回一个包含(索引,数据)元组的列表
自版本6.0起已弃用:iteritems方法已被弃用。请使用dict.items()。
- iterkeys()
已弃用。
返回字典中的键列表
自版本6.0起已弃用:iterkeys方法已被弃用。请使用dict.keys()。
- itervalues()
已弃用。
返回字典中组件数据对象的列表
自版本6.0起已弃用:itervalues方法已被弃用。请使用dict.values()。
- keys(sort=<SortComponents.UNSORTED: 0>, ordered=NOTSET)
返回组件数据键的迭代器
此方法设置此IndexedComponent容器内组件数据对象的排序。为了保持一致性,
__init__(),values(), 和items()都利用此方法来确保一致的排序。- Parameters:
sort (bool 或 SortComponents) – 以指定的排序顺序迭代声明的组件键。有关有效选项和描述,请参见
SortComponents。ordered (bool) – 已弃用:请使用 sort=SortComponents.ORDERED_INDICES。 如果为 True,则键将以确定性顺序返回 (使用底层集合的 ordered_iter())。
- model()
返回与此对象关联的模型。
- parent_block()
返回此对象的父对象。
- parent_component()
返回与此对象关联的组件。
- reconstruct(data=None)
已移除:reconstruct() 在 Pyomo 6.0 中被移除。
重新构建模型组件是脆弱的,并且不能正确更新在其他组件或上下文中使用的组件实例(这对于Var、Param和Set尤其成问题)。希望重现reconstruct()旧行为的用户,如果愿意操作非公开接口,并且愿意花时间验证他们的模型是否正确执行,可以通过以下方式近似实现reconstruct的旧行为:
component.clear() component._constructed = False component.construct()
- root_block()
返回 self.model()
- set_suffix_value(suffix_or_name, value, expand=True)
设置此组件数据的后缀值
- set_value(value)
设置标量组件的值。
- to_dense_data()
待办事项
- type()
已弃用。
返回此组件的类类型
自版本5.7起已弃用:Component.type() 方法已被 .ctype 属性取代。
- valid_model_component()
如果这可以用作模型组件,则返回True。
- values(sort=<SortComponents.UNSORTED: 0>, ordered=NOTSET)
返回组件数据对象的迭代器
- Parameters:
sort (bool 或 SortComponents) – 以指定的排序顺序迭代声明的组件值。有关有效选项和描述,请参见
SortComponents。ordered (bool) – 已弃用:请使用 sort=SortComponents.ORDERED_INDICES。 如果为 True,则值将以确定性顺序返回 (使用底层集合的 ordered_iter()。
- property active
返回活动属性
- property ctype
返回此组件的类类型
- property local_name
仅在直接父容器的上下文中获取组件名称。
- property name
获取完全限定的组件名称。