Main Content

optimvar

创建优化变量

说明

使用 optimvar 创建优化变量。

提示

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

示例

x = optimvar(name) 创建标量优化变量。优化变量是符号对象,您可以使用它来为目标函数和问题约束创建表达式。

提示

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

metal = optimvar('metal')

示例

x = optimvar(name,n) 创建由优化变量组成的 n×1 向量。

示例

x = optimvar(name,cstr) 创建可使用 cstr 进行索引的优化变量向量。x 的元素数与 cstr 向量的长度相同。x 的方向与 cstr 的方向相同:当 cstr 是行向量时,x 也是行向量,当 cstr 是列向量时,x 也是列向量。

示例

对于正整数 nj 和名称 cstrk 的任意组合,x = optimvar(name,cstr1,n2,...,cstrk)x = optimvar(name,{cstr1,cstr2,...,cstrk})x = optimvar(name,[n1,n2,...,nk]) 创建一个优化变量数组,其维数等于整数 nj 和条目 cstr1k 的长度。

示例

对于前面的任意语法,x = optimvar(___,Name,Value) 使用由一个或多个 Name,Value 对组参量指定的其他选项。例如,要指定整数变量,请使用 x = optimvar('x','Type','integer')

示例

全部折叠

创建一个名为 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.

输入参数

全部折叠

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

提示

为了避免混淆哪个名称与变量的哪个方面相关,请将工作区变量名称设置为该变量名称。例如,

truck = optimvar('truck');

示例: "Warehouse"

示例: 'truck'

数据类型: char | string

变量维度,指定为正整数。

示例: 4

数据类型: double

用于索引的名称,指定为字符向量元胞数组或字符串向量。

注意

cstr 不能为诸如 "Tp" 之类的字符串标量,而必须为诸如 ["Tp" "ul"] 之类的向量。要指定单个名称,请使用 {'Tp'} 或等效的 cellstr("Tp")

示例: {'red','orange','green','blue'}

示例: ["red";"orange";"green";"blue"]

数据类型: string | cell

名称-值参数

将可选的参量对组指定为 Name1=Value1,...,NameN=ValueN,其中 Name 是参量名称,Value 是对应的值。名称-值参量必须出现在其他参量之后,但参量对组的顺序无关紧要。

在 R2021a 之前,使用逗号分隔每个名称和值,并用引号将 Name 引起来。

示例: 通过命令 x = optimvar('x',3,'LowerBound',0,'UpperBound',[Inf,2,4]),将 x 创建为 x(2) <= 2x(3) <= 4 的三元素非负向量

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

  • 'continuous' - 实数值

  • 'integer' - 整数值

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

提示

要指定二元变量,请使用 LowerBound 等于 0UpperBound 等于 1'integer' 类型。

示例: 'integer'

下界,指定为与 x 大小相同的数组或实数标量。如果 LowerBound 是标量,则该值适用于 x 的所有元素。

示例: 要为 x 的所有元素设置下界 0,请指定标量值 0

数据类型: double

上界,指定为与 x 大小相同的数组或实数标量。如果 UpperBound 是标量,则该值适用于 x 的所有元素。

示例: 要为 x 的所有元素设置上界 2,请指定标量值 2

数据类型: double

输出参量

全部折叠

优化变量,以 OptimizationVariable 数组形式返回。数组的维度与对应输入变量的维度相同,如 cstr1×cstr2

提示

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

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

版本历史记录

在 R2017b 中推出