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 は列ベクトルです。

x = optimvar(name,cstr1,n2,...,cstrk) または x = optimvar(name,{cstr1,cstr2,...,cstrk}) または x = optimvar(name,[n1,n2,...,nk]) は、正の整数 nj と名前 cstrk のすべての組み合わせについて、次元が整数 nj とエントリ cstr1k の長さに等しい最適化変数の配列を作成します。

x = optimvar(___,Name,Value) では、前の構文のいずれかについて、1 つ以上の 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.

"brass""stainless"、および "galvanized" という string によってインデックス付けされる bolts という名前の整数最適化変数ベクトルを作成します。bolts のインデックスを使用して最適化式を作成し、さらに、文字配列を使用して、または異なる方向で bolts の作成を試します。

行方向で string を使用して 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.

string インデックスを使用して最適化式を作成します。

y = bolts("brass") + 2*bolts("stainless") + 4*bolts("galvanized")
y = 
  Linear OptimizationExpression

    bolts('brass') + 2*bolts('stainless') + 4*bolts('galvanized')

string の代わりに文字ベクトルの cell 配列を使用して、前出と同じインデックスをもつ変数を取得します。

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.

1 行 3 列ではなく 3 行 1 列の列方向バージョンの bnames を使用して、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 x 4 x 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 列の配列を作成します。1 番目の次元は string 'brass''stainless'、および 'galvanized' によってインデックス付けされ、2 番目の次元は数値的にインデックス付けされます。

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.

サイズが 3 x 3 x 3 で 2 値変数を表す x という名前の最適化変数を作成します。

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.

入力引数

すべて折りたたむ

変数名。文字ベクトルまたは string として指定します。

ヒント

どの名前が変数のどの側面に関連しているかについて混乱を避けるために、ワークスペース変数名を変数名に設定します。たとえば、

truck = optimvar('truck');

例: "Warehouse"

例: 'truck'

データ型: char | string

変数の次元。正の整数として指定します。

例: 4

データ型: double

インデックス用の名前。文字ベクトルの cell 配列または string ベクトルとして指定します。

メモ

cstr"Tp" などの string スカラーにすることはできず、["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]) によって、xx(2) <= 2 および x(3) <= 4 の 3 要素の非負ベクトルとして作成します。

変数の型。'continuous' または 'integer' で指定します。

  • 'continuous' - 実数値

  • 'integer' - 整数値

変数の型は、配列のすべての変数に適用されます。複数の変数の型をもつには、複数の変数を作成します。

ヒント

2 値変数を指定するには、'integer' 型を使用して、LowerBound = 0UpperBound = 1 を指定します。

例: 'integer'

下限。x と同じサイズの配列または実数スカラーとして指定します。LowerBound がスカラーの場合、値は x のすべての要素に適用されます。

例: 下限 0x のすべての要素に設定するには、スカラー値 0 を指定します。

データ型: double

上限。x と同じサイズの配列または実数スカラーとして指定します。UpperBound がスカラーの場合、値は x のすべての要素に適用されます。

例: 上限 2x のすべての要素に設定するには、スカラー値 2 を指定します。

データ型: double

出力引数

すべて折りたたむ

最適化変数。OptimizationVariable 配列として返されます。配列の次元は、cstr1cstr2 列などの、対応する入力変数の次元と同じです。

ヒント

  • OptimizationVariable オブジェクトには、"ハンドル" コピー動作があります。詳細については、ハンドル オブジェクトの動作ハンドル クラスと値クラスの比較を参照してください。ハンドル コピー動作とは、OptimizationVariable のコピーがオリジナルを指しており、独立して存在していないことを意味します。たとえば、変数 x を作成し、それを y にコピーして、y のプロパティを設定します。x に新しいプロパティ値が反映される点に注意してください。

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

バージョン履歴

R2017b で導入