findindex
名前付きインデックス変数と等価な数値インデックスの検索
構文
説明
例
名前付きインデックス変数と等価な数値の検索
加法的な原色の名前と減法的な原色の名前によってインデックス付けされる最適化変数 colors
を作成します。加法的な色の名前として 'black'
と 'white'
を、減法的な色の名前として 'black'
を含めます。
colors = optimvar('colors',["black","white","red","green","blue"],["cyan","magenta","yellow","black"]);
加法的な色 'red'
と 'black'
、および減法的な色 'black'
のインデックス番号を検索します。
[idxadd,idxsub] = findindex(colors,{'red','black'},{'black'})
idxadd = 1×2
3 1
idxsub = 4
名前付きインデックス変数と等価な線形インデックスの検索
加法的な原色の名前と減法的な原色の名前によってインデックス付けされる最適化変数 colors
を作成します。加法的な色の名前として 'black'
と 'white'
を、減法的な色の名前として 'black'
を含めます。
colors = optimvar('colors',["black","white","red","green","blue"],["cyan","magenta","yellow","black"]);
["white","black"]
、["red","cyan"]
、["green","magenta"]
、および ["blue","yellow"]
の組み合わせと等価な線形インデックスを検索します。
idx = findindex(colors,["white","red","green","blue"],["black","cyan","magenta","yellow"])
idx = 1×4
17 3 9 15
インデックス変数による解の表示
名前付きインデックス変数を使用して、最適化問題を作成して解きます。問題は、収益で重み付けされた、さまざまな空港へのフルーツのフローを最大化することです。これはそれぞれの重み付きフローへの制約に従います。
rng(0) % For reproducibility p = optimproblem('ObjectiveSense', 'maximize'); flow = optimvar('flow', ... {'apples', 'oranges', 'bananas', 'berries'}, {'NYC', 'BOS', 'LAX'}, ... 'LowerBound',0,'Type','integer'); p.Objective = sum(sum(rand(4,3).*flow)); p.Constraints.NYC = rand(1,4)*flow(:,'NYC') <= 10; p.Constraints.BOS = rand(1,4)*flow(:,'BOS') <= 12; p.Constraints.LAX = rand(1,4)*flow(:,'LAX') <= 35; sol = solve(p);
Solving problem using intlinprog. Running HiGHS 1.6.0: Copyright (c) 2023 HiGHS under MIT licence terms Presolving model 3 rows, 12 cols, 12 nonzeros 3 rows, 12 cols, 12 nonzeros Solving MIP model with: 3 rows 12 cols (0 binary, 12 integer, 0 implied int., 0 continuous) 12 nonzeros Nodes | B&B Tree | Objective Bounds | Dynamic Constraints | Work Proc. InQueue | Leaves Expl. | BestBound BestSol Gap | Cuts InLp Confl. | LpIters Time 0 0 0 0.00% 1160.150059 -inf inf 0 0 0 0 0.0s S 0 0 0 0.00% 1160.150059 1027.233133 12.94% 0 0 0 0 0.0s Solving report Status Optimal Primal bound 1027.23313332 Dual bound 1027.23313332 Gap 0% (tolerance: 0.01%) Solution status feasible 1027.23313332 (objective) 0 (bound viol.) 0 (int. viol.) 0 (row viol.) Timing 0.01 (total) 0.00 (presolve) 0.00 (postsolve) Nodes 1 LP iterations 3 (total) 0 (strong br.) 0 (separation) 0 (heuristics) Optimal solution found. Intlinprog stopped at the root node because the objective value is within a gap tolerance of the optimal value, options.AbsoluteGapTolerance = 1e-06. The intcon variables are integer within tolerance, options.ConstraintTolerance = 1e-06.
ニューヨークとロサンゼルスへのオレンジとベリーの最適フローを求めます。
[idxFruit,idxAirports] = findindex(flow, {'oranges','berries'}, {'NYC', 'LAX'})
idxFruit = 1×2
2 4
idxAirports = 1×2
1 3
orangeBerries = sol.flow(idxFruit, idxAirports)
orangeBerries = 2×2
0 980
70 0
この表示は、NYC
向けのオレンジは 0、70 のベリーが NYC
向け、980 のオレンジが LAX
向けで、LAX
向けのベリーは 0 であることを示しています。
次の最適なフローをリストします。
Fruit Airports
----- --------
Berries NYC
Apples BOS
Oranges LAX
idx = findindex(flow, {'berries', 'apples', 'oranges'}, {'NYC', 'BOS', 'LAX'})
idx = 1×3
4 5 10
optimalFlow = sol.flow(idx)
optimalFlow = 1×3
70 28 980
この表示は、70 のベリーが NYC
向け、28 のアップルが BOS
向けで、980 のオレンジが LAX
向けであることを示しています。
名前付きインデックス変数による初期点の作成
さまざまな地表タイプ、候補となる作物、耕起方法を伴う問題の名前付きインデックス変数を作成します。
land = ["irr-good","irr-poor","dry-good","dry-poor"]; crops = ["wheat-lentil","wheat-corn","barley-chickpea","barley-lentil","wheat-onion","barley-onion"]; plow = ["tradition","mechanized"]; xcrop = optimvar('xcrop',land,crops,plow,'LowerBound',0);
初期点を正しいサイズのゼロ配列に設定します。
x0.xcrop = zeros(size(xcrop));
従来どおりに耕作され、乾燥状態で植えられる "wheat-onion"
および "wheat-lentil"
作物の場合は、初期値を 3000 に設定します。
[idxLand, idxCrop, idxPlough] = findindex(xcrop, ["dry-good","dry-poor"], ... ["wheat-onion","wheat-lentil"],"tradition"); x0.xcrop(idxLand,idxCrop,idxPlough) = 3000;
以下の 3 つの点の初期値を設定します。
Land Crops Method Value dry-good wheat-corn mechanized 2000 irr-poor barley-onion tradition 5000 irr-good barley-chickpea mechanized 3500
idx = findindex(xcrop,... ["dry-good","irr-poor","irr-good"],... ["wheat-corn","barley-onion","barley-chickpea"],... ["mechanized","tradition","mechanized"]); x0.xcrop(idx) = [2000,5000,3500];
入力引数
var
— 最適化変数
OptimizationVariable
オブジェクト
最適化変数。OptimizationVariable
オブジェクトとして指定します。optimvar
を使用して、var
を作成します。
例: var = optimvar('var',4,6)
strindex
— 名前付きインデックス
文字ベクトルの cell 配列 | 文字ベクトル | string ベクトル | 整数ベクトル
名前付きインデックス。文字ベクトルの cell 配列、文字ベクトル、string ベクトルまたは整数ベクトルとして指定します。strindex
引数の数は、var
の次元の数に一致しなければなりません。
例: ["small","medium","large"]
データ型: double
| char
| string
| cell
出力引数
numindex
— 等価の数値インデックス
整数ベクトル
等価の数値インデックス。整数ベクトルとして返されます。出力引数の数は、次のいずれかでなければなりません。
var
の次元の数。各出力ベクトルnumindexj
は、対応する入力引数strindexj
に等価な数値です。1。この場合、各入力
strindex
j
のサイズはすべてのj
で同じでなければなりません。また、出力は線形インデックス付け条件を満たします。var(numindex(j)) = var(strindex1(j),...,strindexk(j))
(すべてのj
について)
バージョン履歴
R2018a で導入
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)