Main Content

本页翻译不是最新的。点击此处可查看最新英文版本。

OptimizationVariable

优化变量

说明

OptimizationVariable 对象包含优化表达式的变量。使用表达式来表示目标函数、约束或方程。变量本质上是符号,可以是任何大小的数组。

提示

有关完整的工作流,请参阅基于问题的优化工作流基于问题的方程求解工作流

创建对象

使用 optimvar 创建一个 OptimizationVariable 对象。

属性

全部展开

数组范围的属性

此 属性 为只读。

变量名称,指定为字符串或字符向量。

Name 给出要在 showwrite 等中显示的变量标签。Name 还给出 solve 返回的解结构体中的字段名称。

提示

为避免混淆,请将 name 设置为 MATLAB® 变量名称。例如,

metal = optimvar('metal')

数据类型: char | string

变量类型,指定为 'continuous''integer'

  • 'continuous' - 实数值

  • 'integer' - 整数值

变量类型适用于数组中的所有变量。如果需要多种变量类型,请创建多个变量。

提示

要指定二元变量,请使用 'integer' 类型,并指定 LowerBound = 0UpperBound = 1

数据类型: char | string

索引名称,指定为字符串元胞数组或字符向量。有关使用索引名称的信息,请参阅Named Index for Optimization Variables

数据类型: cell

按元素属性

下界,指定为实数标量或实数数组,数组的维度与 OptimizationVariable 对象相同。标量值适用于变量的所有元素。

LowerBound 属性始终显示为数组。但是,您可以将属性设置为适用于所有元素的标量。例如,

var.LowerBound = 0

数据类型: double

上界,指定为实数标量或实数数组,数组的维度与 OptimizationVariable 对象相同。标量值适用于变量的所有元素。

UpperBound 属性始终显示为数组。但是,您可以将属性设置为适用于所有元素的标量。例如:

var.UpperBound = 1

数据类型: double

对象函数

show显示有关优化对象的信息
showbounds显示变量边界
write保存优化对象描述
writebounds保存变量边界描述

示例

全部折叠

创建一个名为 dollars 的标量优化变量。

dollars = optimvar('dollars')
dollars = 
  OptimizationVariable with properties:

          Name: 'dollars'
          Type: 'continuous'
    IndexNames: {{}  {}}
    LowerBound: -Inf
    UpperBound: Inf

  See variables with show.
  See bounds with showbounds.

创建一个名为 x 的 3×1 优化变量向量。

x = optimvar('x',3)
x = 
  3x1 OptimizationVariable array with properties:

  Array-wide properties:
          Name: 'x'
          Type: 'continuous'
    IndexNames: {{}  {}}

  Elementwise properties:
    LowerBound: [3x1 double]
    UpperBound: [3x1 double]

  See variables with show.
  See bounds with showbounds.

创建一个名为 bolts 的整数优化变量向量,该向量按字符串 "brass""stainless""galvanized" 进行索引。使用 bolts 的索引创建一个优化表达式,并使用字符数组或以不同方向创建 bolts 进行试验。

使用字符串行向向量创建 bolts

bnames = ["brass","stainless","galvanized"];
bolts = optimvar('bolts',bnames,'Type','integer')
bolts = 
  1x3 OptimizationVariable array with properties:

  Array-wide properties:
          Name: 'bolts'
          Type: 'integer'
    IndexNames: {{}  {1x3 cell}}

  Elementwise properties:
    LowerBound: [-Inf -Inf -Inf]
    UpperBound: [Inf Inf Inf]

  See variables with show.
  See bounds with showbounds.

使用字符串索引创建一个优化表达式。

y = bolts("brass") + 2*bolts("stainless") + 4*bolts("galvanized")
y = 
  Linear OptimizationExpression

    bolts('brass') + 2*bolts('stainless') + 4*bolts('galvanized')

使用字符向量元胞数组而不是字符串来获得与之前索引相同的变量。

bnames = {'brass','stainless','galvanized'};
bolts = optimvar('bolts',bnames,'Type','integer')
bolts = 
  1x3 OptimizationVariable array with properties:

  Array-wide properties:
          Name: 'bolts'
          Type: 'integer'
    IndexNames: {{}  {1x3 cell}}

  Elementwise properties:
    LowerBound: [-Inf -Inf -Inf]
    UpperBound: [Inf Inf Inf]

  See variables with show.
  See bounds with showbounds.

使用列向 bnames(即 3×1 而不是 1×3),请注意,bolts 也具有该方向。

bnames = ["brass";"stainless";"galvanized"];
bolts = optimvar('bolts',bnames,'Type','integer')
bolts = 
  3x1 OptimizationVariable array with properties:

  Array-wide properties:
          Name: 'bolts'
          Type: 'integer'
    IndexNames: {{1x3 cell}  {}}

  Elementwise properties:
    LowerBound: [3x1 double]
    UpperBound: [3x1 double]

  See variables with show.
  See bounds with showbounds.

创建一个名为 xarray 的 3×4×2 优化变量数组。

xarray = optimvar('xarray',3,4,2)
xarray = 
  3x4x2 OptimizationVariable array with properties:

  Array-wide properties:
          Name: 'xarray'
          Type: 'continuous'
    IndexNames: {{}  {}  {}}

  Elementwise properties:
    LowerBound: [3x4x2 double]
    UpperBound: [3x4x2 double]

  See variables with show.
  See bounds with showbounds.

您还可以创建按名称和数值索引混合进行索引的多维变量。例如,创建一个 3×4 优化变量数组,其中第一个维度按字符串 'brass''stainless''galvanized' 进行索引,第二个维度按数值进行索引。

bnames = ["brass","stainless","galvanized"];
bolts = optimvar('bolts',bnames,4)
bolts = 
  3x4 OptimizationVariable array with properties:

  Array-wide properties:
          Name: 'bolts'
          Type: 'continuous'
    IndexNames: {{1x3 cell}  {}}

  Elementwise properties:
    LowerBound: [3x4 double]
    UpperBound: [3x4 double]

  See variables with show.
  See bounds with showbounds.

创建一个表示二元变量的名为 x、大小为 3×3×3 的优化变量。

x = optimvar('x',3,3,3,'Type','integer','LowerBound',0,'UpperBound',1)
x = 
  3x3x3 OptimizationVariable array with properties:

  Array-wide properties:
          Name: 'x'
          Type: 'integer'
    IndexNames: {{}  {}  {}}

  Elementwise properties:
    LowerBound: [3x3x3 double]
    UpperBound: [3x3x3 double]

  See variables with show.
  See bounds with showbounds.

详细信息

全部展开

提示

  • OptimizationVariable 对象具有句柄复制行为。请参阅句柄对象行为句柄类和值类的比较。句柄复制行为意味着 OptimizationVariable 的副本指向原始变量,并且不独立存在。例如,创建变量 x,将其复制到 y,然后设置 y 的属性。请注意,x 采用新属性值。

    x = optimvar('x','LowerBound',1);
    y = x;
    y.LowerBound = 0;
    showbounds(x)
        0 <= x

版本历史记录

在 R2017b 中推出