Advertisement
makispaiktis

Dynamic Programming - HotelProblem - Version3 Regularization

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