knapsack-problem

divide list in two parts that their sum closest to each other

孤人 提交于 2019-12-17 18:41:56
问题 This is a hard algorithms problem that : Divide the list in 2 parts (sum) that their sum closest to (most) each other list length is 1 <= n <= 100 and their(numbers) weights 1<=w<=250 given in the question. For example : 23 65 134 32 95 123 34 1.sum = 256 2.sum = 250 1.list = 1 2 3 7 2.list = 4 5 6 I have an algorithm but it didn't work for all inputs. init. lists list1 = [], list2 = [] Sort elements (given list) [23 32 34 65 95 123 134] pop last one (max one) insert to the list which differs

Variant of Knapsack

随声附和 提交于 2019-12-14 02:53:22
问题 I'm working on a program to solve a variant of the 0/1 Knapsack problem. The original problem is described here: https://en.wikipedia.org/wiki/Knapsack_problem. In case the link goes missing in the future, I will give you a summary of the 0/1 Knapsack problem (if you are familiar with it, jump this paragraph): Let's say we have n items, each with weight wi and value vi . We want to put items in a bag, that supports a maximum weight W , so that the total value inside the bag is the maximum

0-1 knapsack using python cplex

别来无恙 提交于 2019-12-13 19:24:37
问题 I'm trying to solve a slight modification of a 0-1 knapsack problem, where each item is a vector of values from which one value is chosen, instead of a scalar using Python Cplex. This is a variant of a Mixed integer problem. I wrote a IBM OPL solution to this problem, but unable to figure out how to solve it using Python Cplex. My solution using IBM OPL is: int Capacity = 100; // Capacity of the knapsack int k = 2; // Number of items int n = 5; // Number of values range values = 1..n; range

specific Knapsack Algorithm [duplicate]

纵饮孤独 提交于 2019-12-13 09:42:01
问题 This question already has an answer here : How can I solve it? KnapSack - values all the same but each other object has three weights (1 answer) Closed 3 years ago . I have problem with solve the specific knapsack algorithm problem. Is there someone who give me some tips or help me? I solved it by Brute Force method but the execute time is very long (I checked all possible combinations and take the best solution - it works). I need to solve it by Dynamic Programming or Greedy Algorithm (but

Knapsack - save time and memory

老子叫甜甜 提交于 2019-12-12 23:05:30
问题 According to Wikipedia and other sources I had went through, you need matrix m[n][W] ; n - number of items and W - total capacity of knapsack. This matrix get really big, sometimes too big to handle it in C program. I know that dynamic programming is based on saving time for memory but still, is there any solution where can you save time and memory? Pseudo-code for Knapsack problem: // Input: // Values (stored in array v) // Weights (stored in array w) // Number of distinct items (n) //

Use dynamic programming to find a subset of numbers whose sum is closest to given number M

佐手、 提交于 2019-12-12 16:31:18
问题 Given a set A of n positive integers a1, a2,... a3 and another positive integer M, I'm going to find a subset of numbers of A whose sum is closest to M. In other words, I'm trying to find a subset A′ of A such that the absolute value |M - 􀀀 Σ a∈A′| is minimized, where [ Σ a∈A′ a ] is the total sum of the numbers of A′. I only need to return the sum of the elements of the solution subset A′ without reporting the actual subset A′. For example, if we have A as {1, 4, 7, 12} and M = 15. Then, the

Solving the Integer Knapsack

*爱你&永不变心* 提交于 2019-12-12 08:31:29
问题 I a new to dynamic programing and have tried the integer knapsack problem here at SPOJ (http://www.spoj.pl/problems/KNAPSACK/) . However, for the given test cases my solution is not giving the correct output. I'd be thankful to you if you could suggest if the following implementation by me is correct. Please note that the variable back is for backtracking, about which I'm not sure how to do. I hope to have your help in implementing the backtracking too. Thanks. #include <cstdio> #include

Which is the best method between Genetic Algorithm and Dynamic Programming to solve classic 0-1 knapsack? [closed]

≡放荡痞女 提交于 2019-12-12 08:14:20
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 6 years ago . Let's say I have problem like this: Knapsack capacity = 20 million Number of item = 500 Weight of each item is random number between

how to make a list of tuples from a file in python

◇◆丶佛笑我妖孽 提交于 2019-12-11 11:23:26
问题 Ok I have a file like that looks like this. panite,1,1800 ruby,2,100 diamond,0.75,900 emerald,3,250 amethyst,2,50 opal,1,300 sapphire,0.5,750 benitoite,1,2000 malachite,1,60 Our teacher gave us code that uses a try/except to help us open the file. I need to open the file and read each line and make each line a tuple and then put it in a list. The list is supposed to be the last numbe divided by the middle number, and then that value followed by the name of the gem(the middle number is the

Recursive Knapsack Java

风格不统一 提交于 2019-12-11 10:45:21
问题 I'm struggling with a homework assignment and I believe I am vastly over-complicating the solution and need some help from anyone willing to offer it. Let me explain some ground rules for the assignment. Below is a link to another post that has the exact problem informatation. How do I solve the 'classic' knapsack algorithm recursively? A set of numbers will be given such as for example: 15, 11, 8, 7, 6, 5. The first number always corresponds to the target or capacity of the knapsack. What I