optimvar - Create optimization variables - MATLAB (original) (raw)
Create optimization variables
Syntax
Description
Use optimvar
to create optimization variables.
[x](#d126e149857) = optimvar([name](#d126e149548))
creates a scalar optimization variable. An optimization variable is a symbolic object that enables you to create expressions for the objective function and the problem constraints in terms of the variable.
Tip
To avoid confusion, set name
to be the MATLAB® variable name. For example,
metal = optimvar("metal")
[x](#d126e149857) = optimvar([name](#d126e149548),[n](#d126e149585))
creates an n
-by-1 vector of optimization variables.
[x](#d126e149857) = optimvar([name](#d126e149548),[cstr](#d126e149610))
creates a vector of optimization variables that can use cstr
for indexing. The number of elements of x
is the same as the length of the cstr
vector. The orientation ofx
is the same as the orientation ofcstr
: x
is a row vector whencstr
is a row vector, and x
is a column vector when cstr
is a column vector.
[x](#d126e149857) = optimvar([name](#d126e149548),[cstr](#d126e149610)1,[n](#d126e149585)2,...,[cstr](#d126e149610)k)
or`x` = optimvar(`name`,{`cstr`1,`cstr`2,...,`cstr`k})
or`x` = optimvar(`name`,[`n`1,`n`2,...,`n`k])
, for any combination of positive integers n
j and namescstr
k, creates an array of optimization variables with dimensions equal to the integers n
j and the lengths of the entries cstr1
k.
[x](#d126e149857) = optimvar(___,[Name,Value](#namevaluepairarguments))
, for any previous syntax, uses additional options specified by one or moreName,Value
pair arguments. For example, to specify an integer variable, use x = optimvar("x",Type="integer")
.
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 = 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") + 2bolts("stainless") + 4bolts("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 π/2 and an upper bound of 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.
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
.
Input Arguments
Variable name, specified as a character vector or string.
Tip
To avoid confusion about which name relates to which aspect of a variable, set the workspace variable name to the variable name. For example,
truck = optimvar('truck');
Example: "Warehouse"
Example: 'truck'
Data Types: char
| string
Variable dimension, specified as a positive integer.
Example: 4
Data Types: double
Names for indexing, specified as a cell array of character vectors or a string vector.
Note
cstr
cannot be a string scalar such as "Tp"
, but must be a vector such as ["Tp" "ul"]
. To specify a single name, use {'Tp'}
or the equivalentcellstr("Tp")
.
Example: {'red','orange','green','blue'}
Example: ["red";"orange";"green";"blue"]
Data Types: string
| cell
Name-Value Arguments
Specify optional pairs of arguments asName1=Value1,...,NameN=ValueN
, where Name
is the argument name and Value
is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.
Before R2021a, use commas to separate each name and value, and enclose Name
in quotes.
Example: Create x
as a 3-element nonnegative vector withx(2) <= 2
and x(3) <= 4
by the command x = optimvar('x',3,'LowerBound',0,'UpperBound',[Inf,2,4])
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 binary variables, use the "integer"
type with LowerBound equal to0
and UpperBound equal to 1
.
Example: Type="integer"
Data Types: char
| string
Lower bounds, specified as an array of the same size asx or as a real scalar. IfLowerBound
is a scalar, the value applies to all elements of x
.
Example: To set a lower bound of 0
to all elements of x
, specify the scalar value0
.
Data Types: double
Upper bounds, specified as an array of the same size asx or as a real scalar. IfUpperBound
is a scalar, the value applies to all elements of x
.
Example: To set an upper bound of 2
to all elements of x
, specify the scalar value2
.
Data Types: double
Output Arguments
Optimization variable, returned as an OptimizationVariable array. The dimensions of the array are the same as those of the corresponding input variables, such ascstr1
-by-cstr2
.
More About
For mixed-integer linear programming problems, you can specifyType
="semi-continuous"
orType
="semi-integer"
for the variables. These variables must have strictly positive bounds that do not exceed1e5
.
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)
Version History
Introduced in R2017b