-2

hello guys I'm trying to write code for an optimization problem in python, the problem is I need to heal a person with medicine, medicine1 heals 25 units requires 3 herb A and 2 herb B and medicine2 heals 20 units requires 4 herb A and 1 herb B.I have 25 units of herb A and 10 units of herb B. in the end I want to print the maximum health that I can heal and how many of each medicines 1 and 2 that I used(clearly had no idea how to put it)

I tried to write a knapsack problem but I keep getting the error

TypeError: unsupported operand type(s) for +: 'int' and 'function'

med1= 25
med2= 20
x=25
y=10
health=0
def knapsack(health,med1,med2,x,y):
    if x==0 or y==0:
        return knapsack
    if(med1+knapsack(health+med1,med1,med2,x-3,y- 
   2)>med2+knapsack(health+med2,med1,med2,x-4,y-1)):
        return (med1+knapsack(health+med1,med1,med2,x-3,y-2))
    else:
        return (med2+knapsack(health+med2,med1,med2,x-4,y-1))
print(knapsack(health,med1,med2,x,y))

Can someone help me. Thanks

vanpersie22
  • 63
  • 1
  • 1
  • 7
  • You don't set `med1` anywhere before passing it as an argument to `knapsack`. You probably want to use `<-` instead of `==` when checking `x` and `y`. And `return knapsack` is returning the function itself, not an integer. – B. Shefter Apr 18 '19 at 19:55
  • `return (med1[0]+knapsack(health+med1[0],med1,med2,x-3,y-2))` and this line `return (med2[0]+knapsack(health+med2[0],med1,med2,x-4,y-1))` - for this recursion to work you eventually need to have knapsack return a int (assuming med1[0] is an int. Instead your base case `if x==0 or y==0: return knapsack` returns the function. So you end up with med1[0], an int, + a function, which python does not know how to handle. – bjk116 Apr 18 '19 at 19:57

2 Answers2

0

You are returning inconsistent values from your knapsack() function, esp line:

return knapsack

is causing you problems as this returns callable but you want the integer value as further lines (recursively invked) do

return (med1[0]+knapsack(health+med1[0],med1,med2,x-3,y-2))

which then turns into (you got too many brackets btw):

return int + callable

and then your code fails. So this is either incomplete code, or you copy-paste it from somewhere w/o reading.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
0

Your main problem is that return knapsack is returning the function itself, not an integer, which is why you're getting the TypeError. What you want to return, if I'm reading your code correctly, is the unchanged value of health (since you won't be using any medicine).

Here is a much simpler way to do it, using plain integers rather than single-element lists for med1 and med2, and the built-in function max(), which returns the larger of two values passed to it:

def knapsack(health,med1,med2,x,y):
    if x <= 0 or y <= 0:
        return health
    return max(med1+knapsack(health+med1, med1, med2, x-3, y-2),
               med2+knapsack(health+med2, med1, med2, x-4, y-1))

print(knapsack(0, 25, 10, 25, 10))
B. Shefter
  • 877
  • 7
  • 19