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.7.1: Copyright (c) 2024 HiGHS under MIT licence terms Coefficient ranges: Matrix [4e-02, 1e+00] Cost [1e-01, 1e+00] Bound [0e+00, 0e+00] RHS [1e+01, 4e+01] Presolving model 3 rows, 12 cols, 12 nonzeros 0s 3 rows, 12 cols, 12 nonzeros 0s 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.00 (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
运送橙子,只将 70 份浆果运至 NYC
,同时将 980 份橙子运至 LAX
,而不向 LAX
运送浆果。
列出以下最佳运量:
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;
为以下三个点设置初始值。
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];
输入参数
优化变量,指定为 OptimizationVariable
对象。使用 optimvar
创建 var
。
示例: var = optimvar('var',4,6)
命名索引,指定为字符向量元胞数组、字符向量、字符串向量或整数向量。strindex
参量的数目必须等于 var
中的维数。
示例: ["small","medium","large"]
数据类型: double
| char
| string
| cell
输出参量
等效数值索引,以整数向量形式返回。输出参量的数目必须为下列值之一:
var
中的维数。每个输出向量numindexj
是对应输入参量strindexj
的等效数值索引。1。在这种情况下,对于所有
j
,每个输入strindex
j
的大小必须相同,并且输出满足线性索引条件var(numindex(j)) = var(strindex1(j),...,strindexk(j))
(对于全部j
)。
版本历史记录
在 R2018a 中推出
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.
选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- 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)