背包连续(非明显的)限制背包、明显

2023-09-11 03:12:41 作者:一味宠爱

我看着动态规划 - Kapsack问题(YouTube)的。不过,我解决一个稍微不同的问题,即该约束的预算价格,在双,而不是整数。所以,我想知道我怎么可以修改?双是连续不像整数,我可以有1,2,3 ....我不认为我做的0.0,0.1,0.2 ...?

更新1

我想转换两倍乘以为int由100钱的只有2位小数。但是,这将意味着值的范围会非常大?

更新2

我需要解决的问题是:

  

产品有价格(双)及满意度(整数)值。我有一个预算约束,我需要最大限度地提高满意度值。

     

在YouTube视频中,笔者创建了两个二维数组,如int [numItems的] [possibleCapacity(重量)。在这里,我不能因为预算是一个双非整

解决方案

如果你想使用浮点数任意precision(即没有小数固定数量的),而这些都不是分数,动态规划将无法正常工作。

动态规划的基础是存储计算的具体投入previous结果。因此,如果您使用浮点数任意precision,你需要几乎无限怀念为每个可能的浮点数,当然,做无限的计算,而这是不可能的,最优的。

但是,如果这些数字有一个固定的precision(如用这笔钱,其中只有两位十进制数),则可以通过将它们相乘(如你提到的),将它们转换为整数,然后解决背包问题照旧。

微星显卡 一线微星超强GTX275到货 送NV限量版背包

I watched Dynamic Programming - Kapsack Problem (YouTube). However, I am solving a slightly different problem where the constraint is the budget, price, in double, not integer. So I am wondering how can I modify that? Double is "continuous" unlike integer where I can have 1,2,3 .... I don't suppose I do 0.0, 0.1, 0.2 ...?

UPDATE 1

I thought of converting double to int by multiply by 100. Money is only 2 decimal places. But that will mean the range of values will be very large?

UPDATE 2

The problem I need to solve is:

Items have a price (double) & satisfaction (integer) value. I have a budget as a constraint and I need to maximize satisfaction value.

In the youtube video, the author created two 2d array like int[numItems][possibleCapacity(weight)]. Here, I can't as budget is a double not integer

解决方案

If you want to use floating point numbers with arbitrary precision (i.e., don't have a fixed number of decimals), and these are not fractions, dynamic programming won't work.

The basis of dynamic programming is to store previous results of a calculation for specific inputs. Therefore, if you used floating point numbers with arbitrary precision, you would need practically infinite memory for each of the possible floating point numbers and, of course, do infinite calculations, something that is impossible and non-optimal.

However, if these numbers have a fixed precision (as with the money, which only have two decimal numbers), you can convert these into integers by multiplying them (as you've mentioned), and then solve the knapsack problem as usual.