Solving "Knapsack" using DP - python

Here's my code:
def knapsack_dynamic(ws, vs, W):
n = len(ws)
K = [[0] * (W+1)] * (n+1)
for i in range(n+1):
for w in range(W+1):
if i == 0 or w == 0: # knapsack is empty or no more weight to carry
K[i][w] = 0
else:
if ws[i-1] > w:
K[i][w] = K[i-1][w]
else:
K[i][w] = max(vs[i-1] + K[i-1][w-ws[i-1]], K[i-1][w])
return K[n][W]
Here's how to test it:
maxw = 50
ws = [10, 20, 30]
vs = [60, 100, 120]
print(knapsack_dynamic(ws, vs, maxw)) # should print 220
I'm not sure why I'm getting 300 instead of 220.
Can you help me figuring it out please?

The error is made during the matrix initialization:
Replace
K = [[0] * (W+1)] * (n+1)
by
K = [[0] * (W+1) for i in range(n+1)]
or by
K = [[0 for w in range(W+1)] for i in range(n+1)]
When applying the repeat operator * on nested lists, only the reference is repeated not the values.
Try this simple example:
m = [[0] * 4] * 3
print(m) # --> [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
m[0][0] = 5
print(m) # --> [[5, 0, 0, 0], [5, 0, 0, 0], [5, 0, 0, 0]]

Related

Unable to access float object in a 2D array in Python

I need to return the vector solution x of Ux = b for an upper triangular matrix U and vector b using back substitution, but I'm unable to actually access an element of the matrix U.
def BackSub(U, b):
n = len(U)
x = [0 for i in range(n)]
for i in range(n - 1, -1, -1):
s = 0
for j in range(n - 1, i, -1):
s += (U[i][j])*b[j]
b[i] = (b[i] - s)/(U[i][i])
return b
b = [5, 6, 3, 2]
U = [[ 1, 2, 1, 0],
[ 0, 3, -5, 9],
[ 0, 0, 0.5, 1],
[ 0, 0, 0, 7]]
N = GaussElim(U, b)
x = BackSub(N, b)
It returns
TypeError: 'float' object is not subscriptable for U[i][i]
The GaussElim function is this
import numpy as np
def GaussElim(A, b):
n = len(b) #n is matrix size
#Elimination phase
for k in range(0 , n - 1): #k is matrix row
for i in range(k + 1, n): #i is matrix col
if A[i][k] != 0:
factor = A[i][k]/ A[k][k]
A[i][k + 1 : n] = A[i][k + 1 : n] - np.multiply(factor, A[k][k + 1 : n])
b[i] = b[i] - np.multiply(factor, b[k])
#Back substitution
for k in range(n - 1, -1, -1):
b[k] = (b[k] - dot(A[k][k + 1 : n], b[k + 1 : n]))/A[k][k]
return b

How can I create or change polynomial coefficients

I have this code and I want to edit it to do something else:
def pol(poly, n, x):
result = poly[0]
#Using Horner's method
for i in range(1, n):
result = result * x + poly[i]
return result
#Let us evaluate value of
#ax^3 - bx^2 - x - 10 for x = 1
poly = [5, 9, -1, -10]
x = 1
n = len(poly)
print("Value of polynomial is: ", pol(poly, n, x))
I wonder how can I can change the coefficients of the polynomial. And this code just calculates:
x^3 and x^2
How can I make this code calculate for example this polynomial:
p(x) = 5x^10 + 9x - 7x - 10
or any polynomial in Python?
Your code should work, you just need to present the correct input. For
p(x) = 5x^10 + 9x - 7x - 10
you should provide:
poly2 = [5, 0, 0, 0, 0, 0, 0, 0, 0, 9-7, 10]
Alternate pol - implementation:
def pol(poly, x):
n = len(poly) # no need to provide it at call
rp = poly[::-1] # [-10, -1, 9, 5] so they correlate with range(n) as power
print("Poly:",poly, "for x =",x)
result = 0
for i in range(n):
val = rp[i] * x**i
print(rp[i],' * x^', i, ' = ', val, sep='') # debug output
result += val
return result
x = 2 # 1 is a bad test candidate - no differences for 1**10 vs 1**2
# 5x^3 + 9x^2 - x - 10 for x = 1
poly = [5, 9, -1, -10]
print("Value of polynomial is: ", pol(poly, x))
# p(x) = 5x^10 + 9x - 7x - 10
poly2 = [5, 0, 0, 0, 0, 0, 0, 0, 0, 9-7, 10]
print("Value of polynomial is: ", pol(poly2, x))
Output:
Poly: [5, 9, -1, -10] for x = 2
-10 * x^0 = -10
-1 * x^1 = -2
9 * x^2 = 36
5 * x^3 = 40
Value of polynomial is: 64
Poly: [5, 0, 0, 0, 0, 0, 0, 0, 0, 2, -10] for x = 2
-10 * x^0 = -10
2 * x^1 = 4
0 * x^2 = 0
0 * x^3 = 0
0 * x^4 = 0
0 * x^5 = 0
0 * x^6 = 0
0 * x^7 = 0
0 * x^8 = 0
0 * x^9 = 0
5 * x^10 = 5120
Value of polynomial is: 5114

How could I distribute obstacles to my grid without writing them manually?

I'm working on A star algorithm and as my code below shown the gird is written manually and I'm thinking to make a grid with 100* 100 size. So, it will be so awful to write them manually. I need to put my starting point at (0,0) location and my goal point at (99,99) location.
I'm trying to make the grid with this line below
grid1 = [[0 for i in range(100)]for j in range(100)]
But how could I assign obstacles to this grid randomly or not randomly without touching the location of starting point and goal point?
This is below my code:
from __future__ import print_function
import random
grid = [[0, 1, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0],#0 are free path whereas 1's are obstacles
[0, 1, 0, 0, 0, 0],
[0, 1, 0, 0, 1, 0],
[0, 0, 0, 0, 1, 0]]
'''
heuristic = [[9, 8, 7, 6, 5, 4],
[8, 7, 6, 5, 4, 3],
[7, 6, 5, 4, 3, 2],
[6, 5, 4, 3, 2, 1],
[5, 4, 3, 2, 1, 0]]'''
init = [0, 0]
goal = [len(grid)-1, len(grid[0])-1] #all coordinates are given in format [y,x]
cost = 1
drone_h = 60
#the cost map which pushes the path closer to the goal
heuristic = [[0 for row in range(len(grid[0]))] for col in range(len(grid))]
for i in range(len(grid)):
for j in range(len(grid[0])):
heuristic[i][j] = abs(i - goal[0]) + abs(j - goal[1])
#if grid[i][j] == 1:
#heuristic[i][j] = 99 #added extra penalty in the heuristic map
print(heuristic)
elevation = [[0 for row in range(len(grid[0]))] for col in range(len(grid))]
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
elevation[i][j] = random.randint(1,100)
else:
elevation[i][j] = 0
#the actions we can take
delta = [[-1, 0 ], # go up
[ 0, -1], # go left
[ 1, 0 ], # go down
[ 0, 1 ]] # go right
#function to search the path
def search(grid,init,goal,cost,heuristic):
closed = [[0 for col in range(len(grid[0]))] for row in range(len(grid))]# the referrence grid
closed[init[0]][init[1]] = 1
action = [[0 for col in range(len(grid[0]))] for row in range(len(grid))]#the action grid
x = init[0]
y = init[1]
g = 0
f = g + heuristic[init[0]][init[0]] + elevation[init[0]][init[0]]
cell = [[f, g, x, y]]
found = False # flag that is set when search is complete
resign = False # flag set if we can't find expand
while not found and not resign:
if len(cell) == 0:
resign = True
return "FAIL"
else:
cell.sort()#to choose the least costliest action so as to move closer to the goal
cell.reverse()
next = cell.pop()
x = next[2]
y = next[3]
g = next[1]
f = next[0]
if x == goal[0] and y == goal[1]:
found = True
else:
for i in range(len(delta)):#to try out different valid actions
x2 = x + delta[i][0]
y2 = y + delta[i][1]
if x2 >= 0 and x2 < len(grid) and y2 >=0 and y2 < len(grid[0]):
if closed[x2][y2] == 0 and grid[x2][y2] == 0 and elevation[x2][y2] < drone_h :
g2 = g + cost
f2 = g2 + heuristic[x2][y2] + elevation[x2][y2]
cell.append([f2, g2, x2, y2])
closed[x2][y2] = 1
action[x2][y2] = i
invpath = []
x = goal[0]
y = goal[1]
invpath.append([x, y])#we get the reverse path from here
while x != init[0] or y != init[1]:
x2 = x - delta[action[x][y]][0]
y2 = y - delta[action[x][y]][1]
x = x2
y = y2
invpath.append([x, y])
path = []
for i in range(len(invpath)):
path.append(invpath[len(invpath) - 1 - i])
print("ACTION MAP")
for i in range(len(action)):
print(action[i])
return path
a = search(grid,init,goal,cost,heuristic)
for i in range(len(a)):
print(a[i])
You could assign the grid randomly and afterwards make sure both starting and end point don't contain obstacles. For neighboring fields you could just do the same as for those two.
import random
grid1 = [[random.randint(0,1) for i in range(100)]for j in range(100)]
# clear starting and end point of potential obstacles
grid1[0][0] = 0
grid1[99][99] = 0

Randomly change to 1 p element of a zero list in python

I have a zero list with size of m. I wanna randomly select p indexes and change them to 1.
Y = [0 for x in range(M)]
What is the best way to do this?
You can use Python's random module:
import random
m, p = 5, 2
l = [0] * m
indices = list(range(len(l)))
random.shuffle(indices)
for i in indices[:p]:
l[i] = 1
As mentioned by #MrT in the comments if you don't already have your "zero list" you can do:
l = [0] * (m - p) + [1] * p
random.shuffle(l)
You can use random.sample()
import random
m,p = 10, 5
my_list = [0]*m
index = random.sample(range(len(my_list)), p)
for i in index:
my_list[i] = 1
import random
x = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
x = [1 if bool(random.getrandbits(1)) else 0 for i in x]
print(x) # [1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 1, 1]
-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
import random
leninp = int(input("len"))
raninp = int(input("random"))
x = [0 for i in range(leninp-raninp)] + [1 for i in range(raninp)]
random.shuffle(x)
print(x)

Unable to increment array elements correctly for "Minesweeper"

I'm starting to learn Python, I just started with a simple example. The question was to count mines near each place in a table. Consider input file below:
4 4
*...
....
.*..
....
3 5
**...
.....
.*...
0 0
The output should be like
Field #1:
*100
2210
1*10
1110
Field #2:
**100
33200
1*100
And my code is:
#!/usr/bin/python
import pprint
fin = open("1.2.in")
fout = open("1.2.out")
while True:
i, j = [int(x) for x in fin.readline().split()]
if(i == 0):
break
arr = []
for k in range(0,i):
line = fin.readline();
arr.append(list(line))
pprint.pprint(arr)
resarr = [[0]*j]*i
for row in range(0,i):
for col in range(0,j):
for rowIndex in range(-1,2):
for colIndex in range(-1,2):
# print row,rowIndex, col,colIndex
if (row + rowIndex < i) and (row + rowIndex >= 0) and ( col + colIndex < j) and (col+colIndex >=0) and (rowIndex != 0 or colIndex != 0):
#pprint.pprint(resarr[row][col])
#if arr[row+rowIndex][col+colIndex] == "*":
#print row,rowIndex, col,colIndex, " ", arr[row+rowIndex][col+colIndex]
#print resarr[row][col]
resarr[row][col] += 1
#pprint.pprint(resarr)
# print col+colIndex
print i,j
pprint.pprint(resarr)
I don't know what's wrong, but when I want to increment resarr, a total column is incremented.
Your problem is
resarr = [[0]*j]*i
This means: Take i references to the same list defined by [0]*j and create a list of those.
The thing you want is:
resarr = [[0]*j for _ in range(i)]
This creates a new list ([0, 0, ...]) i times instead.
See this:
>>> a = [0] * 4
>>> a
[0, 0, 0, 0]
>>> b = [a] * 4
>>> b
[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
>>> id(b[0]) # get the "address" of b[0]
42848200
>>> id(b[1]) # b[1] is the same object!
42848200
>>> b[0][0] = 1
>>> b
[[1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0]]

Categories