Advertisement
makispaiktis

Dynamic Programming - Hotel Problem

Jun 5th, 2020 (edited)
1,078
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.59 KB | None | 0 0
  1. BOTTOM = 0
  2. RIGHT = 1
  3.  
  4. def min_steps(movement_cost, life):
  5.     rows = len(movement_cost)
  6.     cols = len(movement_cost[0])
  7.     m = [[None for __ in range(cols)] for _ in range(rows)]
  8.     decisions = [[None for __ in range(cols)] for _ in range(rows)]
  9.     life = [[None for __ in range(cols)] for _ in range(rows)]
  10.     m[rows-1][cols-1] = 0   # starting point
  11.     life[rows-1][cols-1] = 4
  12.  
  13.     for i in range(rows-1, -1, -1):
  14.         for j in range(cols-1, -1, -1):
  15.             if i == rows-1 and j == cols-1:
  16.                 continue
  17.             # The above code is in order not to make m[len1, len2] ---> inf
  18.             m[i][j] = float('inf')
  19.             if i < rows-1:
  20.                 value = m[i + 1][j] + movement_cost[i][j]
  21.                 if value < m[i][j]:
  22.                     m[i][j] = value
  23.                     life[i][j] = life[i+1][j] - life_cost[i][j]
  24.                     # Ήρθα από το κάτω κουτί
  25.                     decisions[i][j] = BOTTOM
  26.             if j < cols-1:
  27.                 value = m[i][j + 1] + movement_cost[i][j]
  28.                 if value < m[i][j]:
  29.                     m[i][j] = value
  30.                     # Ήρθα από το δεξιά κουτί
  31.                     decisions[i][j] = RIGHT
  32.                     life[i][j] = life[i][j+1] - life_cost[i][j]
  33.     return m[0][0], decisions, life
  34.  
  35. # Creating the map with river and rocks
  36. movement_cost = [[1 for __ in range(7)] for _ in range(8)]
  37. # river
  38. movement_cost[3][0] = 3
  39. movement_cost[3][2] = 3
  40. movement_cost[3][3] = 3
  41. movement_cost[3][4] = 3
  42. movement_cost[2][3] = 3
  43. movement_cost[2][4] = 3
  44. movement_cost[0][4] = 3
  45. movement_cost[2][6] = 3
  46. # rocks
  47. movement_cost[0][2] = 5
  48. movement_cost[4][2] = 5
  49. movement_cost[4][3] = 5
  50. movement_cost[6][2] = 5
  51. movement_cost[7][2] = 5
  52. movement_cost[5][5] = 5
  53.  
  54. life_cost = [[0 for __ in range(7)] for _ in range(8)]
  55. life_cost[1][0] = 2
  56. life_cost[1][3] = 3
  57. life_cost[3][3] = 3
  58. life_cost[3][6] = 2
  59. life_cost[5][1] = 3
  60. life_cost[5][3] = 3
  61.  
  62. #trees
  63. life_cost[4][0] = -2
  64. life_cost[7][1] = -2
  65.  
  66.  
  67. def get_decisions(decisions, life, i, j):
  68.     describe_square = "("+str(i)+","+str(j)+")  Life: "+str(life[i][j])
  69.     if decisions[i][j] is None:
  70.         return describe_square
  71.     if decisions[i][j]==BOTTOM:
  72.         return get_decisions(decisions, life, i+1, j)+"\n-> "+describe_square
  73.     if decisions[i][j]==RIGHT:
  74.         return get_decisions(decisions, life, i, j+1)+"\n-> "+describe_square
  75.     raise Exception("Shouldn't be called")
  76.  
  77.  
  78.  
  79. steps, decisions, life = min_steps(movement_cost, life_cost)
  80. print(steps)
  81. print(get_decisions(decisions, life, 0, 0))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement