主要内容

本页采用了机器翻译。点击此处可查看最新英文版本。

OptimizationVariable

优化变量

说明

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

提示

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

创建对象

使用 optimvar 创建一个 OptimizationVariable 对象。

属性

全部展开

数组范围的属性

此 属性 为只读。

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

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

提示

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

metal = optimvar("metal")

数据类型: char | string

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

  • 'continuous' - 实数值。

  • 'integer' - 整数值。

  • 'semi-continuous' - 下界与上界之间的零值或实数值,这些值必须是严格意义上的正值,且不能超过 1e5。该类型仅适用于混合整数线性规划 (intlinprog)。

  • 'semi-integer' - 下界与上界之间的零值或整数值,这些值必须是严格意义上的正值,且不能超过 1e5。该类型仅适用于混合整数线性规划 (intlinprog)。

Type 适用于整个变量数组。如果需要多种变量类型,请创建多个变量。

提示

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

数据类型: char | string

索引名称,指定为字符串元胞数组或字符向量。有关使用索引名称的信息,请参阅优化变量的命名索引

数据类型: 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 = 
  3×1 OptimizationVariable array with properties:

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

  Elementwise properties:
    LowerBound: [3×1 double]
    UpperBound: [3×1 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 = 
  1×3 OptimizationVariable array with properties:

  Array-wide properties:
          Name: 'bolts'
          Type: 'integer'
    IndexNames: {{}  {1×3 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 = 
  1×3 OptimizationVariable array with properties:

  Array-wide properties:
          Name: 'bolts'
          Type: 'integer'
    IndexNames: {{}  {1×3 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 = 
  3×1 OptimizationVariable array with properties:

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

  Elementwise properties:
    LowerBound: [3×1 double]
    UpperBound: [3×1 double]

  See variables with show.
  See bounds with showbounds.

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

xarray = optimvar("xarray",3,4,2)
xarray = 
  3×4×2 OptimizationVariable array with properties:

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

  Elementwise properties:
    LowerBound: [3×4×2 double]
    UpperBound: [3×4×2 double]

  See variables with show.
  See bounds with showbounds.

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

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

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

  Elementwise properties:
    LowerBound: [3×4 double]
    UpperBound: [3×4 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 = 
  3×3×3 OptimizationVariable array with properties:

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

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

  See variables with show.
  See bounds with showbounds.

创建一个名为 x 的半连续优化变量,其下界为 π/2,上界为 2π

x = optimvar("x",Type="semi-continuous",...
    LowerBound=pi/2,UpperBound=2*pi)
x = 
  OptimizationVariable with properties:

          Name: 'x'
          Type: 'semi-continuous'
    IndexNames: {{}  {}}
    LowerBound: 1.5708
    UpperBound: 6.2832

  See variables with show.
  See bounds with showbounds.

创建一个名为 y 的半整数三维变量,其下界为 [10,20,30],上界为 [20,40,60]

y = optimvar("y",3,Type="semi-integer",...
    LowerBound=[10,20,30],UpperBound=[20,40,60])
y = 
  3×1 OptimizationVariable array with properties:

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

  Elementwise properties:
    LowerBound: [3×1 double]
    UpperBound: [3×1 double]

  See variables with show.
  See bounds with showbounds.

半连续变量和半整数变量必须具有严格意义上的正边界,该边界不得超过 1e5

详细信息

全部展开

提示

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

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

版本历史记录

在 R2017b 中推出

全部展开