OptimizationVariable
Variable for optimization
Description
An OptimizationVariable
object contains variables for
optimization expressions. Use expressions to represent an objective function,
constraints, or equations. Variables are symbolic in nature, and can be arrays of any
size.
Tip
For the full workflow, see Problem-Based Optimization Workflow or Problem-Based Workflow for Solving Equations.
Creation
Create an OptimizationVariable
object using optimvar
.
Properties
Array-Wide Properties
This property is read-only.
Variable name, specified as a string or character vector.
Name
gives the variable label to be displayed, such as
in show
or write
. Name
also gives the field names in
the solution structure that solve
returns.
Tip
To avoid confusion, set name
to be the MATLAB® variable name. For example,
metal = optimvar("metal")
Data Types: char
| string
Variable type, specified as 'continuous'
,
'integer'
, 'semi-continuous'
, or
'semi-integer'
.
'continuous'
— Real values.'integer'
— Integer values.'semi-continuous'
— Zero or real values between the lower and upper bounds, which must be strictly positive and cannot exceed1e5
. This type applies only to mixed-integer linear programming (intlinprog
).'semi-integer'
— Zero or integer values between the lower and upper bounds, which must be strictly positive and cannot exceed1e5
. This type applies only to mixed-integer linear programming (intlinprog
).
Type
applies to the entire variable array. To have
multiple variable types, create multiple variables.
Tip
To specify a binary variable, use the 'integer'
type and specify LowerBound
= 0
and UpperBound
= 1
.
Data Types: char
| string
Index names, specified as a cell array of strings or character vectors. For information on using index names, see Named Index for Optimization Variables.
Data Types: cell
Element-wise Properties
Lower bound, specified as a real scalar or as a real array having the same
dimensions as the OptimizationVariable
object. Scalar
values apply to all elements of the variable.
The LowerBound
property is always displayed as an
array. However, you can set the property as a scalar that applies to all
elements. For example,
var.LowerBound = 0
Data Types: double
Upper bound, specified as a real scalar or as a real array having the same
dimensions as the OptimizationVariable
object. Scalar
values apply to all elements of the variable.
The UpperBound
property is always displayed as an
array. However, you can set the property as a scalar that applies to all
elements. For example
var.UpperBound = 1
Data Types: double
Object Functions
show | Display information about optimization object |
showbounds | Display variable bounds |
write | Save optimization object description |
writebounds | Save description of variable bounds |
Examples
Create a scalar optimization variable named 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.
Create a 3-by-1 optimization variable vector named x
.
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.
Create an integer optimization variable vector named bolts
that is indexed by the strings "brass"
, "stainless"
, and "galvanized"
. Use the indices of bolts
to create an optimization expression, and experiment with creating bolts
using character arrays or in a different orientation.
Create bolts
using strings in a row orientation.
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.
Create an optimization expression using the string indices.
y = bolts("brass") + 2*bolts("stainless") + 4*bolts("galvanized")
y = Linear OptimizationExpression bolts('brass') + 2*bolts('stainless') + 4*bolts('galvanized')
Use a cell array of character vectors instead of strings to get a variable with the same indices as before.
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.
Use a column-oriented version of bnames
, 3-by-1 instead of 1-by-3, and observe that bolts
has that orientation as well.
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.
Create a 3-by-4-by-2 array of optimization variables named xarray
.
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.
You can also create multidimensional variables indexed by a mixture of names and numeric indices. For example, create a 3-by-4 array of optimization variables where the first dimension is indexed by the strings 'brass'
, 'stainless'
, and 'galvanized'
, and the second dimension is numerically indexed.
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.
Create an optimization variable named x
of size 3-by-3-by-3 that represents binary variables.
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.
Create a semicontinuous optimization variable named x
with a lower bound of and an upper bound of .
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.
Create a semi-integer 3-D variable named y
with lower bounds of [10,20,30]
and upper bounds of [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.
Semicontinuous and semi-integer variables must have strictly positive bounds that do not exceed 1e5
.
More About
For the list of supported operations on optimization variables, see Supported Operations for Optimization Variables and Expressions.
An optimization variable reference is an optimization variable that is a subset of another optimization variable. The reference variable points to, meaning it is an alias of, the original variable. The reference variable does not have an independent existence.
For example, suppose x
is a 3-element optimization
variable:
x = optimvar("x",3,1);
Take y
as the last two elements of x
.
y = x(2:3);
Then y(1)
is an alias of x(2)
, and
y(2)
is an alias of x(3)
. If you use
y
in an optimization expression, the expression includes
x
, not y
. For example,
expr = [1,2]*y
expr = OptimizationExpression x(2) + 2*x(3)
Furthermore, any modification of y
produces a modification of
x
. For example,
y.LowerBound = 2; showbounds(x)
2 <= x(2, 1) 2 <= x(3, 1)
For mixed-integer linear programming problems, you can specify
Type
="semi-continuous"
or
Type
="semi-integer"
for the variables. These
variables must have strictly positive bounds that do not exceed
1e5
.
Semicontinuous and semi-integer variables can take the value 0
or any value
from the lower bound to the upper bound. Semi-integer variables can take only integer values
within the bounds, whereas semicontinuous variables can take any real value within the
bounds.
Tips
OptimizationVariable
objects have handle copy behavior. See Handle Object Behavior and Comparison of Handle and Value Classes. Handle copy behavior means that a copy of anOptimizationVariable
points to the original and does not have an independent existence. For example, create a variablex
, copy it toy
, then set a property ofy
. Note thatx
takes on the new property value.x = optimvar('x','LowerBound',1); y = x; y.LowerBound = 0; showbounds(x)
0 <= x
Version History
Introduced in R2017bFor mixed-integer linear programming problems, variables can have the type 'semi-continuous'
or 'semi-integer'
. See Semicontinuous and Semi-Integer Variables.
You can create optimization variables using the "like"
syntax.
This capability can simplify the initialization of optimization expressions, as
shown in Use "like" Syntax to Initialize the Array.
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.
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: United States.
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)