OptimizationVariable
优化变量
说明
OptimizationVariable 对象包含优化表达式的变量。使用表达式来表示目标函数、约束或方程。变量本质上是符号,可以是任何大小的数组。
提示
有关完整的工作流,请参阅基于问题的优化工作流或基于问题的方程求解工作流。
创建对象
使用 optimvar 创建一个 OptimizationVariable 对象。
属性
数组范围的属性
变量类型,指定为 'continuous'、'integer'、'semi-continuous' 或 'semi-integer'。
'continuous'- 实数值。'integer'- 整数值。'semi-continuous'- 下界与上界之间的零值或实数值,这些值必须是严格意义上的正值,且不能超过1e5。该类型仅适用于混合整数线性规划 (intlinprog)。'semi-integer'- 下界与上界之间的零值或整数值,这些值必须是严格意义上的正值,且不能超过1e5。该类型仅适用于混合整数线性规划 (intlinprog)。
Type 适用于整个变量数组。如果需要多种变量类型,请创建多个变量。
提示
要指定二元变量,请使用 'integer' 类型,并指定 LowerBound = 0 和 UpperBound = 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 的半连续优化变量,其下界为 ,上界为 。
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。
详细信息
有关优化变量支持的运算列表,请参阅优化变量和表达式支持的运算。
优化变量引用是一个优化变量,它是另一个优化变量的子集。引用变量指向原始变量,即它是原始变量的别名。引用变量不独立存在。
例如,假设 x 是一个 3 元素优化变量:
x = optimvar("x",3,1);取 y 作为 x 的最后两个元素。
y = x(2:3);
那么 y(1) 就是 x(2) 的别名,而 y(2) 是 x(3) 的别名。如果在优化表达式中使用 y,则该表达式包含的是 x,而不是 y。例如,
expr = [1,2]*y
expr = OptimizationExpression x(2) + 2*x(3)
此外,对 y 的任何修改都会产生对 x 的修改。例如,
y.LowerBound = 2; showbounds(x)
2 <= x(2, 1) 2 <= x(3, 1)
对于混合整数线性规划问题,可以为变量指定 Type="semi-continuous" 或 Type="semi-integer"。这些变量必须具有严格意义上的正边界,该边界不得超过 1e5。
半连续变量和半整数变量可以取 0 或下界与上界之间的任何值。半整数变量只能取边界内的整数值,而半连续变量可以取边界内的任何实数值。
提示
版本历史记录
在 R2017b 中推出对于混合整数线性规划问题,变量可以是类型 'semi-continuous' 或 'semi-integer'。请参阅半连续变量和半整数变量。
您可以使用 "like" 语法创建优化变量。此功能可以简化优化表达式的初始化,如使用 "like" 语法初始化数组所示。
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)