- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我一直在使用动态规划的这种变体来解决背包问题:
KnapsackItem = Struct.new(:name, :cost, :value)
KnapsackProblem = Struct.new(:items, :max_cost)
def dynamic_programming_knapsack(problem)
num_items = problem.items.size
items = problem.items
max_cost = problem.max_cost
cost_matrix = zeros(num_items, max_cost+1)
num_items.times do |i|
(max_cost + 1).times do |j|
if(items[i].cost > j)
cost_matrix[i][j] = cost_matrix[i-1][j]
else
cost_matrix[i][j] = [cost_matrix[i-1][j], items[i].value + cost_matrix[i-1][j-items[i].cost]].max
end
end
end
cost_matrix
end
def get_used_items(problem, cost_matrix)
i = cost_matrix.size - 1
currentCost = cost_matrix[0].size - 1
marked = Array.new(cost_matrix.size, 0)
while(i >= 0 && currentCost >= 0)
if(i == 0 && cost_matrix[i][currentCost] > 0 ) || (cost_matrix[i][currentCost] != cost_matrix[i-1][currentCost])
marked[i] = 1
currentCost -= problem.items[i].cost
end
i -= 1
end
marked
end
这对于上面的结构非常有效,您只需提供名称、成本和值(value)。项目可以像下面这样创建:
items = [
KnapsackItem.new('david lee', 8000, 30) ,
KnapsackItem.new('kevin love', 12000, 50),
KnapsackItem.new('kemba walker', 7300, 10),
KnapsackItem.new('jrue holiday', 12300, 30),
KnapsackItem.new('stephen curry', 10300, 80),
KnapsackItem.new('lebron james', 5300, 90),
KnapsackItem.new('kevin durant', 2300, 30),
KnapsackItem.new('russell westbrook', 9300, 30),
KnapsackItem.new('kevin martin', 8300, 15),
KnapsackItem.new('steve nash', 4300, 15),
KnapsackItem.new('kyle lowry', 6300, 20),
KnapsackItem.new('monta ellis', 8300, 30),
KnapsackItem.new('dirk nowitzki', 7300, 25),
KnapsackItem.new('david lee', 9500, 35),
KnapsackItem.new('klay thompson', 6800, 28)
]
problem = KnapsackProblem.new(items, 65000)
现在,我遇到的问题是我需要为这些玩家中的每一个添加一个位置,并且我必须让背包算法知道它仍然需要最大化所有玩家的值(value),除非有一个新的限制而那个限制就是每个玩家都有一个位置,每个位置只能选择一定的次数。有些位置可以选择两次,有些位置可以选择一次。理想情况下,项目会变成这样:
KnapsackItem = Struct.new(:name, :cost, :position, :value)
职位会有如下限制:
PositionLimits = Struct.new(:position, :max)
限制可能会像下面这样被实例化:
limits = [Struct.new('PG', 2), Struct.new('C', 1), Struct.new('SF', 2), Struct.new('PF', 2), Struct.new('Util', 2)]
让这更棘手的是每个玩家都可以处于 Util 位置。如果我们想禁用 Util 位置,我们只需将 2 设置为 0。
我们原来的项目数组看起来像下面这样:
items = [
KnapsackItem.new('david lee', 'PF', 8000, 30) ,
KnapsackItem.new('kevin love', 'C', 12000, 50),
KnapsackItem.new('kemba walker', 'PG', 7300, 10),
... etc ...
]
如何将位置限制添加到背包算法中,以便仍然保留提供的玩家池的最大值?
最佳答案
ruby 中有一些高效的库可以满足您的任务,很明显您正在寻找一些 constrain based optimization ,ruby 中有一些库是开源的,因此可以免费使用,只需将它们包含在您的项目中即可。您需要做的就是生成 Linear programming在您的约束之外建模目标函数,库的优化器将生成满足您所有约束的解决方案,或者如果无法从给定的约束得出任何结论,则表示不存在解决方案。
ruby 中可用的一些此类库是
OPL 遵循类似于 IBM CPLEX 的 LP 语法,这是一种广泛使用的优化软件,因此您可以很好地引用如何使用它对 LP 建模,而且它是建立在 RGLPK 之上的。
关于ruby - 背包 : how to add item type to existing solution,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19958184/
我已经用 Scala 中的每一项编写了有界背包问题的答案,并尝试将其转换为 Haskell,结果如下: knapsack :: [ ( Int, Int ) ] -> [ ( Int, Int ) ]
我正在解决这个动态编程问题,并且我一直坚持用 Java 编写迭代解决方案 目标是找到花费 X 美分所需的最少卡路里数。如果我们不能恰好花费 X 美分,那么就没有解决方案。我们给定了 N 个元素,每个元
根据维基百科和我浏览过的其他资源,您需要矩阵m[n][W]; n - 元素数量和 W - 背包的总容量。这个矩阵变得非常大,有时大到无法在 C 程序中处理。我知道动态规划的基础是节省内存时间,但是,有
我发现此代码使用蛮力机制解决背包问题(这主要是为了学习,因此无需指出动态更有效)。我得到了可以工作的代码,并且了解了大部分代码。最多。这是问题: 我注意到这两个条件,我不知道它们如何工作以及为什么在代
我必须实现背包问题的以下变体。背包中的每件元素都有优先级和重量。现在我指定一个权重 X。我必须知道计算权重总和至少为 X 并且具有最低优先级的最小项目集。每个项目只能选择一次。示例: Knap
所以我想知道如何计算背包问题的所有解。也就是说,我有兴趣从一组最大大小为 K 的数字中找出可能的子集数。 例如,我们有一组大小为 {3, 2, 5, 6, 7} 的项目,最大大小为 K = 13。因此
我在执行任务时遇到了问题。我有一个数据库,其中包含所有具有“价格”值的项目。它们连接到不同的“轮次”,“轮次”有一个“总值(value)”,其中这些项目“价格”-值(value)全部放在一起定义了“总
我遇到的问题如下: 给定一个包含 N 个元素的队列,每个元素都有一个重量,以及一个包含 K 个容器的队列。我们需要按照元素来的顺序将元素分成容器。例如,第一个项目只能进入第一个容器,第二个可以进入第一
问题如下: You have n trip lengths in km which should be divided among m number of days such that the max
我昨晚在开发一个应用程序时遇到了一个特定的问题,我确信它可能有一个有效的算法来解决它。谁能推荐一下? 问题: TL;DR:也许图片会有所帮助:http://www.custom-foam-insert
如何获取下拉列表,其中占位符显示“选择类别”作为默认选择? 以下代码没有渲染占位符 $this->crud->addField([ // Select2 'label'
刚刚带背包的新品。我在官方网站上搜索并用谷歌搜索,但没有找到答案 在 laravel 7 中,使用 Backpack 4.1 我的数据模型是:客户有很多地址 关系在客户模型中配置: public fu
据我所知(如果我错了请纠正我),背包只处理 $fillable 字段。整个 laravel 的事情不就是 $fillable 和 $guarded 之间的分离吗? MWE: 在 User.php 中:
看到this之后讲座我创建了以下背包代码。在讲座中,教授说从最优值(19:00 分钟)确定集合很容易,但我找不到如何去做。我在代码中提供了一个将值相加为 21 的示例,我如何根据该值确定集合(在本例中
为什么贪心法适用于连续背包问题而不适用于 0-1 背包问题? 最佳答案 对于连续背包,在最佳解决方案中,您不能有 q > 0 的每单位成本为 c 的元素,同时留下 q' > 0 成本为 c' > c
我在理解动态规划时遇到了一些困难,尽管我已经通读了很多资源试图理解。 我理解使用斐波那契算法给出的动态规划示例。我明白如果你使用分而治之的方法,你最终会多次解决一些子问题,而动态编程通过解决这些重叠的
假设一个经典的 0-1 背包问题,但您可以上溢/下溢背包并受到一些惩罚。每单位溢出(重量超过最大容量)扣除X利润,每单位下溢(重量低于最大容量)扣除Y利润。 我想按利润与重量的比率对所有元素进行排序,
我正在编写具有多个约束的背包 0-1 的变体。除了重量限制外,我还有数量限制,但在这种情况下,我想解决背包问题,因为我的背包中需要恰好有 n 件元素,重量小于或等于 W。我我目前正在为基于 Roset
给定一个无限正整数数组或一个正整数流,找出总和为 20 的前五个数。 通过阅读问题陈述,它首先似乎是 0-1 Knapsack 问题,但我很困惑 0-1 Knapsack algo 可以在流上使用的整
我想解决一个3维背包问题。 我有许多不同宽度、高度、长度和值(value)的盒子。我有一个指定的空间,我想把盒子放在那个空间里,这样我就能获得最优的利润。我想使用暴力来做到这一点。 我正在用 Java
我是一名优秀的程序员,十分优秀!