Advertisement
Guest User

Untitled

a guest
Dec 10th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.02 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import numpy as np
  4. from tronproblem import *
  5. from trontypes import CellType, PowerupType
  6. import random, math
  7. from queue import Queue, LifoQueue, PriorityQueue
  8.  
  9. # Throughout this file, ASP means adversarial search problem.
  10.  
  11.  
  12. class StudentBot:
  13. def __init__(self):
  14. self.resetCounter = 0
  15. self.turns_elapsed = 0
  16. self.clutch_gene = 0
  17. self.pseudo_clutch_gene = 0
  18.  
  19.  
  20. def decide(self, asp):
  21.  
  22. cutoff = 3
  23. #cutoff = min(4, self.turns_elapsed/3)
  24. if self.turns_elapsed <= 5:
  25. cutoff = 3
  26. self.player = asp.get_start_state().ptm
  27.  
  28. eval_function = self.eval_function_2
  29. if self.clutch_gene >= 12:
  30. #print("clutch")
  31.  
  32. return self.conserve(asp)
  33. if asp.get_start_state().get_remaining_turns_speed(self.player) > 0:
  34. return self.conserve(asp)
  35. #elif self.pseudo_clutch_gene >=12:
  36. # return self.pseudo_clutch(asp)
  37.  
  38. a = self.ab_cut(asp, cutoff, eval_function)
  39.  
  40. return a
  41.  
  42.  
  43.  
  44. def ab_cut(self, asp, cutoff_ply, eval_func):
  45. #print(asp.get_start_state().board[1][1])
  46. #print(asp.get_start_state().board[1][2] == " ")
  47. #print("s")
  48. start = asp.get_start_state()
  49. v = float('-inf')
  50. self.armored = False
  51. action = "D"
  52. # print("ab")
  53. # print(start.ptm)
  54. if start.player_has_armor(start.ptm):
  55. actions = self.armored_actions(start.board, start.player_locs[start.ptm])
  56. else:
  57. actions = asp.get_safe_actions(start.board, start.player_locs[start.ptm])
  58. for a in actions:
  59. val = self.cutoff_min(asp, asp.transition(start, a), v, float('inf'), cutoff_ply-1, eval_func)
  60. #val += self.powerup_value(asp, a)
  61. # print(val)
  62. # print(a)
  63. val += self.powerup_value(asp, a)
  64. if val > v:
  65. v = val
  66. action = a
  67.  
  68.  
  69. self.turns_elapsed += 1
  70. nextsquare = asp.move(start.player_locs[start.ptm], action)
  71. if start.board[nextsquare[0]][nextsquare[1]] == CellType.BOMB:
  72. self.turns_elapsed = 0
  73. # print("y")
  74. return action
  75.  
  76. def cutoff_max(self, asp, state, alfa, balfa, cutoff, e):
  77. ## print("max")
  78. # print(state.ptm)
  79. if asp.is_terminal_state(state):
  80. e = asp.evaluate_state(state)[state.ptm]
  81. if e < 0.1:
  82. return float('-inf')
  83. return float('inf')
  84. if cutoff <= 0:
  85. return e(asp, state)
  86. v = float('-inf')
  87. if state.player_has_armor(state.ptm):
  88. #print('y')
  89. actions = self.armored_actions(state.board, state.player_locs[state.ptm])
  90. else:
  91. actions = asp.get_safe_actions(state.board, state.player_locs[state.ptm])
  92. for a in actions:
  93. v = max(v, self.cutoff_min(asp, asp.transition(state, a), alfa, float('inf'), cutoff-1, e))
  94. if v >= balfa:
  95. return v
  96. alfa = max(v, alfa)
  97. return v
  98.  
  99. def cutoff_min(self, asp, state, alfa, balfa, cutoff, e):
  100. #print("min")
  101. #print(state.ptm)
  102. if asp.is_terminal_state(state):
  103. e = asp.evaluate_state(state)[1-state.ptm]
  104. if e < 0.1:
  105. return float('-inf')
  106. return float('inf')
  107. if cutoff <= 0:
  108. return e(asp, state)
  109. v = float('inf')
  110. if state.player_has_armor(state.ptm):
  111. #print('y')
  112. actions = self.armored_actions(state.board, state.player_locs[state.ptm])
  113. else:
  114. actions = asp.get_safe_actions(state.board, state.player_locs[state.ptm])
  115. for a in actions:
  116.  
  117. v = min(v, self.cutoff_max(asp, asp.transition(state, a), float('-inf'), balfa, cutoff-1, e))
  118. if v <= alfa:
  119. return v
  120. balfa = min(v, balfa)
  121. return v
  122. """
  123. Gets the difference between # accessible spaces for bot 1 and bot 2
  124. """
  125.  
  126.  
  127. def powerup_value(self, asp, action):
  128. board = asp.get_start_state().board
  129. ploc = asp.get_start_state().player_locs[self.player]
  130. p2loc = asp.get_start_state().player_locs[1-self.player]
  131. next_loc = asp.move(ploc, action)
  132. powerup = board[next_loc[0]][next_loc[1]]
  133. separation = (ploc[0]-p2loc[0])**2 + (ploc[1]-p2loc[1])**2
  134.  
  135. if powerup == '!':
  136. return 0
  137. return separation/3
  138. if powerup == '^':
  139.  
  140. return 1
  141. if powerup == '@':
  142. if not asp.get_start_state().player_has_armor(self.player):
  143. return 2
  144. else:
  145. return 0
  146. if powerup == '*':
  147. if separation < 25:
  148. return -1
  149. else:
  150. return 3
  151.  
  152. else:
  153. return 0
  154.  
  155.  
  156.  
  157.  
  158. #Bugged attempt at BDS:
  159. def eval_function_2(self, asp, state):
  160. board = state.board
  161. ptm = self.player
  162. # print("eval")
  163. # print(ptm)
  164. q1 = Queue()
  165. q2 = Queue()
  166. our_util = 0
  167. their_util = 0
  168. q1.put(state.player_locs[ptm])
  169. q2.put(state.player_locs[1-ptm])
  170. our_space = {}
  171. their_space = {}
  172. armored = state.player_has_armor(ptm)
  173. self.clutch_gene += 1
  174. #self.pseudo_clutch_gene += 1
  175. while not q1.empty() and not q2.empty():
  176. if not q1.empty():
  177. front = q1.get()
  178. #if armored:
  179. # steps = self.armored_actions(board, front)
  180. # armored = False
  181. #else:
  182. steps = asp.get_safe_actions(board, front)
  183. for a in steps:
  184. curr = asp.move(front, a)
  185. item = board[curr[0]][curr[1]]
  186. d = self.to_1d(curr, board)
  187. if item == CellType.WALL:
  188. our_util -= 3
  189. if d not in our_space:
  190. if d not in their_space:
  191. q1.put(curr)
  192. our_space[d] = item
  193. our_util +=1
  194.  
  195. if item == '!':
  196.  
  197. counter = 0
  198. for k in range(curr[0]-3, curr[0]+3):
  199. for l in range(curr[1] -3, curr[1] + 3):
  200. if k > 0 and k < len(board):
  201. if l > 0 and l < len(board[0]):
  202. if board[k][l] == CellType.BARRIER:
  203. counter += 1
  204. our_util += counter
  205. if item == CellType.SPEED:
  206. our_util += 3
  207. if item == CellType.ARMOR:
  208. our_util += 1
  209. # self.clutch_gene = 0
  210. if item == CellType.TRAP:
  211. our_util += 3
  212. else:
  213. self.clutch_gene = 0
  214.  
  215. if not q2.empty():
  216. back = q2.get()
  217. steps = asp.get_safe_actions(board, back)
  218. for b in steps:
  219. curr = asp.move(back, b)
  220. item = board[curr[0]][curr[1]]
  221. d = self.to_1d(curr, board)
  222. if d not in their_space:
  223. if d not in our_space:
  224. q2.put(curr)
  225. their_space[d] = item
  226. their_util +=1
  227.  
  228. if item == CellType.BOMB:
  229. counter = 0
  230. for k in range(curr[0]-3, curr[0]+3):
  231. for l in range(curr[1] -3, curr[1] + 3):
  232. if k > 0 and k < len(board):
  233. if l > 0 and l < len(board[0]):
  234. if board[k][l] == CellType.BARRIER:
  235. counter += 1
  236. their_util += counter #self.clutch_gene = 0
  237. if item == CellType.SPEED:
  238. their_util += 0
  239. if item == CellType.ARMOR:
  240. their_util += 1
  241. #self.clutch_gene = 0
  242. if item == CellType.TRAP:
  243.  
  244. their_util += 3
  245. #self.clutch_gene = 0
  246. else:
  247. self.clutch_gene = 0
  248.  
  249. if our_util < 150:
  250. self.resetCounter += 1
  251. else:
  252. self.resetCounter = 0
  253. self.cutoff = 3
  254. if self.resetCounter >= 12:
  255. self.cutoff = 4
  256. #if self.clutch_gene == 12:
  257. # print('clutch')
  258.  
  259. #print("ourspacec")
  260. #print(len(our_space))
  261.  
  262. return our_util - their_util
  263.  
  264. def armored_actions(self, board, loc):
  265. safe = set()
  266. for action in {U, D, L, R}:
  267. r1, c1 = TronProblem.move(loc, action)
  268. if not (
  269. #board[r1][c1] == CellType.BARRIER
  270. board[r1][c1] == CellType.WALL
  271. or TronProblem.is_cell_player(board, (r1, c1))
  272. ):
  273. safe.add(action)
  274. return safe
  275.  
  276.  
  277.  
  278.  
  279. def to_1d(self, location, board):
  280. return len(board)*location[0]+location[1]
  281.  
  282. def to_2d(self, location, board):
  283.  
  284. a = location/len(board[0])
  285. return (int(a), location%len(board[0]))
  286.  
  287. def conserve(self, asp):
  288.  
  289. state = asp.get_start_state()
  290. allActions = asp.get_available_actions(state)
  291. pq = PriorityQueue()
  292. ptm = self.player
  293. # print("conserve")
  294. # print(ptm)
  295. board = state.board
  296. p1 = state.player_locs[ptm]
  297. armored = state.player_has_armor(ptm)
  298. #pq.put((0, p1))
  299. locDic = {}
  300. best = float('inf')
  301. safety = False
  302. armored = False
  303. immediate_actions = asp.get_safe_actions(board, p1)
  304. if armored:
  305. immediate_actions = self.armored_actions(board, p1)
  306. if len(immediate_actions) == 0:
  307. return self.panic(board, p1,asp)
  308. bigA = 'D'
  309. else:
  310. bigA = random.sample(immediate_actions,1)[0]
  311. for act in immediate_actions:
  312.  
  313. loc = asp.move(p1, act)
  314. index = self.to_1d(loc, board)
  315. locDic[index] = act
  316. costDict = {}
  317. #costDict[self.to_1d(p1, board)] = 100
  318. julio = len(asp.get_safe_actions(board, loc))
  319. if julio == 1 or julio == 2:
  320. safety = True
  321. costDict[index] = -2+len(asp.get_safe_actions(board, loc))
  322. else:
  323. #print(act)
  324. if not safety:
  325. if julio == 3:
  326. bigA = act
  327. continue
  328.  
  329. immediate_item = board[loc[0]][loc[1]]
  330. immediate_val = -1
  331. if immediate_item == '!':
  332.  
  333. immediate_val = -100
  334. if immediate_item == '@' and not armored:
  335. immediate_val = -2
  336. if immediate_item == '^':
  337. immediate_val = 3
  338. if immediate_item == '*':
  339. immediate_val = -3
  340. pq.put((immediate_val, index))
  341.  
  342. while not pq.empty():
  343. popped = pq.get()[1]
  344. currLoc = self.to_2d(popped, board)
  345. if popped == self.to_1d(p1, board):
  346. continue
  347.  
  348. actions = asp.get_safe_actions(board, currLoc)
  349. nextLocs = []
  350. for a in actions:
  351. nl = asp.move(currLoc, a)
  352. nextLoc = self.to_1d(nl, board)
  353. if nextLoc not in costDict:
  354. heur = -1
  355. nextItem = board[self.to_2d(nextLoc, board)[0]][self.to_2d(nextLoc, board)[1]]
  356. if nextItem == '*':
  357. heur = -1
  358. if nextItem == '!':
  359. heur = -10
  360. if nextItem == '@':
  361. heur = -1
  362. #if nextItem ==
  363.  
  364. costDict[nextLoc] = heur + costDict[popped]
  365. total_cost = heur + costDict[popped]
  366. pq.put((total_cost, nextLoc))
  367. costDict[index] = min(costDict[index], total_cost)
  368. for act in allActions:
  369. nl = asp.move(currLoc, a)
  370.  
  371. if asp.is_cell_player(board, nl) and self.to_1d(board, p1) != self.to_1d(board, nextloc):
  372. print('ye')
  373. self.clutch_gene = 0
  374. # print(costDict[index])
  375. # print(locDic[index])
  376. #print(costDict[index])
  377. #print(locDic[index])
  378. if costDict[index] < best:
  379. best = costDict[index]
  380. bestLocation = index
  381. bigA = locDic[index]
  382.  
  383. # print(best)
  384. return bigA
  385.  
  386. def panic(self, board, location, asp):
  387. actions = self.armored_actions(board, location)
  388. v = float('inf')
  389. s = set()
  390. best = 'L'
  391. for a in actions:
  392. state = asp.transition(asp.get_start_state(), a)
  393. if self.eval_function_2(asp, state) > v:
  394. v = self.eval_function_2(asp, state)
  395. best = a
  396. return a
  397.  
  398.  
  399.  
  400.  
  401. def cleanup(self):
  402.  
  403. """
  404. Input: None
  405. Output: None
  406.  
  407. This function will be called in between
  408. games during grading. You can use it
  409. to reset any variables your bot uses during the game
  410. (for example, you could use this function to reset a
  411. turns_elapsed counter to zero). If you don't need it,
  412. feel free to leave it as "pass"
  413. """
  414. self.turns_elapsed = 0
  415. self.clutch_gene = 0
  416. self.cutoff = 3
  417. self.resetCounter = 0
  418.  
  419.  
  420. class RandBot:
  421. """Moves in a random (safe) direction"""
  422.  
  423. def decide(self, asp):
  424. """
  425. Input: asp, a TronProblem
  426. Output: A direction in {'U','D','L','R'}
  427. """
  428. state = asp.get_start_state()
  429. locs = state.player_locs
  430. board = state.board
  431. ptm = state.ptm
  432. loc = locs[ptm]
  433. possibilities = list(TronProblem.get_safe_actions(board, loc))
  434. if possibilities:
  435. return random.choice(possibilities)
  436. return "U"
  437.  
  438. def cleanup(self):
  439. pass
  440.  
  441.  
  442. class WallBot:
  443. """Hugs the wall"""
  444.  
  445. def __init__(self):
  446. order = ["U", "D", "L", "R"]
  447. random.shuffle(order)
  448. self.order = order
  449.  
  450. def cleanup(self):
  451. order = ["U", "D", "L", "R"]
  452. random.shuffle(order)
  453. self.order = order
  454.  
  455. def decide(self, asp):
  456. """
  457. Input: asp, a TronProblem
  458. Output: A direction in {'U','D','L','R'}
  459. """
  460. state = asp.get_start_state()
  461. locs = state.player_locs
  462. board = state.board
  463. ptm = state.ptm
  464. loc = locs[ptm]
  465. possibilities = list(TronProblem.get_safe_actions(board, loc))
  466. if not possibilities:
  467. return "U"
  468. decision = possibilities[0]
  469. for move in self.order:
  470. if move not in possibilities:
  471. continue
  472. next_loc = TronProblem.move(loc, move)
  473. if len(TronProblem.get_safe_actions(board, next_loc)) < 3:
  474. decision = move
  475. break
  476. return decision
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement