Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 背包算法中的min{wi,W−;W}函数 napsack算法(S,W): 输入:设置项目集,以便每个项目∈ S对bi和a有积极的好处 正权重wi;正最大总重量W 输出:每项I的量席∈ 它使总收益最大化,同时 不超过最大总重量W 对于每个项目i∈ 是的 席← 0 不及物动词← bi/wi//i项价值指数 W← 0//总重量 而w_Ruby_Algorithm_Knapsack Problem - Fatal编程技术网

Ruby 背包算法中的min{wi,W−;W}函数 napsack算法(S,W): 输入:设置项目集,以便每个项目∈ S对bi和a有积极的好处 正权重wi;正最大总重量W 输出:每项I的量席∈ 它使总收益最大化,同时 不超过最大总重量W 对于每个项目i∈ 是的 席← 0 不及物动词← bi/wi//i项价值指数 W← 0//总重量 而w

Ruby 背包算法中的min{wi,W−;W}函数 napsack算法(S,W): 输入:设置项目集,以便每个项目∈ S对bi和a有积极的好处 正权重wi;正最大总重量W 输出:每项I的量席∈ 它使总收益最大化,同时 不超过最大总重量W 对于每个项目i∈ 是的 席← 0 不及物动词← bi/wi//i项价值指数 W← 0//总重量 而w,ruby,algorithm,knapsack-problem,Ruby,Algorithm,Knapsack Problem,我正在尝试用Ruby实现上述伪代码,我已经成功实现了一个优先级队列,但我只需要有人为我解释这一行: a← min{wi,W− w} //超过w− w导致重量溢出 min函数到底应该做什么?该如何实施呢?a行← min{wi,W− w} 实际上给‘a’一个值,它是w[i]和w-w的最小值 Algorithm FractionalKnapsack(S, W ): Input: Set S of items, such that each item i ∈ S has a positive benef

我正在尝试用Ruby实现上述伪代码,我已经成功实现了一个优先级队列,但我只需要有人为我解释这一行:

a← min{wi,W− w} //超过w− w导致重量溢出


min函数到底应该做什么?该如何实施呢?

a行← min{wi,W− w} 实际上给‘a’一个值,它是w[i]和w-w的最小值

Algorithm FractionalKnapsack(S, W ):
Input: Set S of items, such that each item i ∈ S has a positive benefit bi and a
positive weight wi; positive maximum total weight W
Output: Amount xi of each item i ∈ S that maximizes the total benefit while
not exceeding the maximum total weight W
for each item i ∈ S do 
 xi ← 0
 vi ← bi/wi // value index of item i 
w ← 0 // total weight
while w < W and S ̸= ∅ do
 remove from S an item i with highest value index
 a ← min{wi, W − w} // more than W − w causes a weight overflow 
 xi ← a
 w←w+a
if w[i] < W-w then a ← w[i]
otherwise a ← W-w
[w[i], W-w].min