OptimizationVariable
优化变量
说明
OptimizationVariable
对象包含优化表达式的变量。使用表达式来表示目标函数、约束或方程。变量本质上是符号,可以是任何大小的数组。
提示
有关完整的工作流,请参阅基于问题的优化工作流或基于问题的方程求解工作流。
创建对象
使用 optimvar
创建一个 OptimizationVariable
对象。
属性
数组范围的属性
Type
— 变量类型
'continuous'
(默认) | 'integer'
变量类型,指定为 'continuous'
或 'integer'
。
'continuous'
- 实数值'integer'
- 整数值
变量类型适用于数组中的所有变量。如果需要多种变量类型,请创建多个变量。
提示
要指定二元变量,请使用 'integer'
类型,并指定 LowerBound
= 0
和 UpperBound
= 1
。
数据类型: char
| string
IndexNames
— 索引名称
''
(默认) | 字符串元胞数组 | 字符向量元胞数组
索引名称,指定为字符串元胞数组或字符向量。有关使用索引名称的信息,请参阅Named Index for Optimization Variables。
数据类型: cell
按元素属性
LowerBound
— 下界
-Inf
(默认) | 实数标量 | 实数数组
下界,指定为实数标量或实数数组,数组的维度与 OptimizationVariable
对象相同。标量值适用于变量的所有元素。
LowerBound
属性始终显示为数组。但是,您可以将属性设置为适用于所有元素的标量。例如,
var.LowerBound = 0
数据类型: double
UpperBound
— 上界
Inf
(默认) | 实数标量 | 实数数组
上界,指定为实数标量或实数数组,数组的维度与 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.
详细信息
算术运算
有关优化变量支持的运算列表,请参阅Supported Operations for Optimization Variables and Expressions。
提示
版本历史记录
在 R2017b 中推出
MATLAB 命令
您点击的链接对应于以下 MATLAB 命令:
请在 MATLAB 命令行窗口中直接输入以执行命令。Web 浏览器不支持 MATLAB 命令。
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- 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)