Knapsack:What is wrong with the following code?
-
I have made a code to test a knapsack problem, I have taken input from a file where i get total no. of items and total capacity into the variables 'items' and capacity 'respectively'. values and weights are array storing the respective items value and weight. Obviously maximize the value that can be contained in Knapsack(0-1 Knapsack prob) is the objective. Here is the code in Python: counter=0 def knapsack(i,j): global counter counter+=1 value=0 if(i==0 ): value=0 else: if v[i][j]<0: if(j<weights[i]): value=knapsack(i-1,j) else: value=max(knapsack(i-1,j),(values[i]+knapsack(i-1,j-weights[i]))) v[i][j]=value return v[i][j] def solveIt(inputData): global counter # parse the input lines = inputData.split('\n') firstLine = lines[0].split() items = int(firstLine[0]) capacity = int(firstLine[1]) global values values= [0] global weights weights= [0] for i in range(1, items+1): line = lines[i] parts = line.split() values.append(int(parts[0])) weights.append(int(parts[1])) # Dynamic Programming Top to Bottm approach for filling the knapsack value = 0 weight = 0 taken1 = [0 for i in range(items+1)] taken=[0 for i in range(items)] global v v=[[-1 for x in range(capacity+1)] for x in range(items+1)] value=knapsack(items,capacity) tempi=items tempc=capacity print capacity sumtotal=0 ####################################Backtracking while (items!=0 and capacity!=0): if(v[items][capacity]!=-1 and v[items-1][capacity]!=-1): if v[items][capacity]!=v[items-1][capacity]: taken1[items]=1 sumtotal+=weights[items] capacity=capacity-weights[items] items-=1 else: taken1[items]=0 items-=1 else: taken1[items]=0 items-=1 # prepare the solution in the specified output format outputData = str(value) + ' ' + str(0) + '\n' outputData += ' '.join(map(str, taken)) print values print weights print "Occuied capacity"+str(sumtotal) print counter print outputData for i in range(1,tempi+1): print taken1[i] #return outputData ###avoid this code; nothing to do with algo import sys if __name__ == '__main__': if len(sys.argv) > 1: fileLocation = sys.argv[1].strip() inputDataFile = open(fileLocation, 'r') inputData = ''.join(inputDataFile.readlines()) inputDataFile.close() print solveIt(inputData) else: print 'This test requires an input file. Please select one from the data directory. (i.e. python http://solver.py ./data/ks_4_0)' ##################################################################################THe problem is that it works for many simple cases bt doesnt for some typical tough cases. Any help would be appreciated. Thanks in advance.
-
Answer:
Better if you post these sort of questions here: http://stackoverflow.com/
Rahul Jain at Quora Visit the source
Related Q & A:
- What's wrong with this PHP Twitter API POST?Best solution by Stack Overflow
- What is wrong with this Laravel 5 response?Best solution by Stack Overflow
- What's wrong with my yahoo 360 page stat counter?Best solution by answers.yahoo.com
- What is wrong with my only tv?Best solution by Yahoo! Answers
- What can I do? "The following message could not be delivered to all recipients?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.