knapsack-problem

knapsack problem (classic)

落爺英雄遲暮 提交于 2019-12-23 19:54:34
问题 So I have to solve the knapsack problem for class. So far, I've come up with the following. My comparators are functions that determine which of two subjects will be the better option (by looking at the corresponding (value,work) tuples). I decided to iterate over the possible subjects with work less than maxWork, and in order to find which subject is the best option at any given turn, I compared my most recent subject to all the other subjects that we have not used yet. def greedyAdvisor

Dynamic Programming Knapsack K-exact items

荒凉一梦 提交于 2019-12-23 18:30:06
问题 I found this very handy example code which implements a DP solution to the knapsack problem (kudos to the person who posted it). https://codereview.stackexchange.com/questions/20569/dynamic-programming-solution-to-knapsack-problem I am trying to modify it to include a constraint on the number of items k in the knapsack. I added a third argument def knapsack(items, maxweight, maxitems): and modified the reconstruction as follows: while i > 0: if bestvalues[i][j] != bestvalues[i - 1][j] and len

1/0 Knapsack Variation with Weighted Edges

你。 提交于 2019-12-23 03:15:21
问题 I'm currently investigating a routing problem (finding a subset of places [each with a certain score] I want to visit while not exceeding a maximum travel time) and came up with a variation of the 1/0 knapsack problem that seems to solve my original problem. According to Wikipedia the 1/0 knapsack is described as: Given a set of items, each with a mass and a value, determine the number of each item to include in a collection so that the total weight is less than or equal to a given limit and

1/0 Knapsack Variation with Weighted Edges

女生的网名这么多〃 提交于 2019-12-23 03:15:01
问题 I'm currently investigating a routing problem (finding a subset of places [each with a certain score] I want to visit while not exceeding a maximum travel time) and came up with a variation of the 1/0 knapsack problem that seems to solve my original problem. According to Wikipedia the 1/0 knapsack is described as: Given a set of items, each with a mass and a value, determine the number of each item to include in a collection so that the total weight is less than or equal to a given limit and

Trying to figure out the classic Knapsack recurrence

こ雲淡風輕ζ 提交于 2019-12-21 21:53:57
问题 I am reading about the Knapsack Problem (unbounded) which is, as I understand a classic in DP. Although I think I understand the solution as I read it, I am not clear how I can translate it to actual code. For example in the following recurrence "formula": M(j) = MAX {M(j-1), MAX i = 1 to n (M(j - Si) + Vi) } for j >=1 I am not sure how I can translate this to code, since it is not clear to me if the inner MAX should be there or it should just be instead: M(j) = MAX {M(j-1), M(j - Si) + Vi }

Subset-sum problem in PHP with MySQL

时光总嘲笑我的痴心妄想 提交于 2019-12-21 21:32:17
问题 following Problem: I have a MySQL database with songs in it. The database has the following structure: id INT(11)(PRIMARY) title VARCHAR(255) album VARCHAR(255) track INT(11) duration INT(11) The user should be able to enter a specific time into a php form and the php function should give him a list of all possible combinations of songs which add up to the given time ±X min. So if the user wants to listen to 1 hour of music ±5 minutes he would enter 60 minutes and 5 minutes of threshold into

Get the most efficient combination of a large List of objects based on a field

匆匆过客 提交于 2019-12-21 11:57:40
问题 I'm looking to maximize the number of stars given a certain budget and max limit on the combination. Example question: With a budget of 500 euro, visiting only the maximum allowed restaurants or less, dine and collect the most stars possible. I'm looking to write an efficient algorithm, that could potentially process 1 million Restaurant instances for up to 10 max Restaurants. Note, this is a cross post from a question I asked yesterday: Java: Get the most efficient combination of a large

Knapsack with minimum cost

与世无争的帅哥 提交于 2019-12-21 06:42:31
问题 I've got several types of coins, each have weight (wi) and cost (ci). So I've got to make a knapsack with weight==W and (!) minimum cost of coins in it. I can`t make recurrence relation to use DP. 回答1: Just formulate the usual recurrence relation... Designate the minimum cost achievable with total weight k as Min_cost(k). Bootstrap the memoization with: Min_cost(0) = cost of empty set = 0 Then solve for increasing values of k using: Min_cost(i+1) = min [Min_cost(i) + min [ci, for all items

Optimisation/knapsack algorithm with multiple contraints in JavaScript

限于喜欢 提交于 2019-12-21 06:40:12
问题 I'm looking for a solution to the knapsack problem where multiple constraints are in place. Say our knapsack has a maximum weight of 30kg and we have a set of 100 objects, each with a weight and each with a benefit. These objects could look like this: { name: 'water bottle', weight: 2, benefit: 5 }, { name: 'potatoes', weight: 10, benefit: 6 } Finding the combination of objects with the highest benefit within the maximum weight is simple enough. Here is a nodeJS plugin showing how it can be

Optimisation/knapsack algorithm with multiple contraints in JavaScript

自作多情 提交于 2019-12-21 06:40:06
问题 I'm looking for a solution to the knapsack problem where multiple constraints are in place. Say our knapsack has a maximum weight of 30kg and we have a set of 100 objects, each with a weight and each with a benefit. These objects could look like this: { name: 'water bottle', weight: 2, benefit: 5 }, { name: 'potatoes', weight: 10, benefit: 6 } Finding the combination of objects with the highest benefit within the maximum weight is simple enough. Here is a nodeJS plugin showing how it can be