yoshuanava / dd2380_project Goto Github PK
View Code? Open in Web Editor NEWAI for playing Tetris, employing Monte Carlo Tree Search
AI for playing Tetris, employing Monte Carlo Tree Search
I modified the GetChildren method of GameNode, so that the GameNodes are self-contained, and we don't need to go back and forth between state and nodes. Hence, our successor function is part of the GameNode definition. The new method is:
def getChildren(self):
children = []
# Translations relative to the middle of the board
for translation in range(-5, 6, 1):
for rotation in range(0, 4, 1): # Number of rotations
# Simulate the game by dong a few rotations and translations: get the new state (for each performed move)
new_state = GameState.TetrisGame(self.state.grid, self.state.curr_piece, self.state.next_piece, rotation, translation)
action = (rotation, translation)
child = GameNode(new_state, self, action)
children.append(child)
# Calculate the hash value
# hash_val = abs(hash(new_state_string))
# Check for hash collision
# if (self.hashtable[hash_val % 1000000] == 0):
# self.hashtable[hash_val % 1000000] = 1
# child = GameNode(new_state.getGrid(), piece, next_piece, new_state.getActions())
# self.children.append(child)
return children
However, I'm getting about 44 children out of each state. I think we should only take into account some of them. But which ones?
Furthermore, this function takes quite some CPU time. I think it could be because we run deepcopy while creating new GameState objects. Could this be the case?
The root node should be initialized in game.py
inside the method new_piece
, as this method is called when a new brick is spawned. Currently, the root node is initialized in core.py
(even before the game is started, which implies that the root is "Null").
There are some issues in the MCST class. If a node has no children, the commands
random.choice(node.children)
and
random.choice(unexplored_children)
returns an index error.
When I try to call the heuristic function in the simulation method of MCTS, as follows:
print("-------- Simulation --------")
points = 0
itr = 1
child = node
while (itr <= self.max_length):
# Get all the possible actions, and choose a random one. Predict the next state and evaluate it with the heuristic function
children = child.getChildren()
child = random.choice(children)
points += th.heuristic(child.state.grid) # Run heuristic here
itr += 1
I get the following output:
************ MCTS ************
Length of future nodes list = 44
##### Iteration 0 #####
-------- Selection --------
Tree depth explored = 1
Time elapsed = 0.101644039154
-------- Simulation --------
Traceback (most recent call last):
File "tetris.py", line 14, in <module>
Core(gfx).run()
File "/home/alfredoso/GitHub/DD2380_Project/tetris-master/tetris/core.py", line 40, in run
self.play() # Our playing method
File "/home/alfredoso/GitHub/DD2380_Project/tetris-master/tetris/core.py", line 108, in play
mcts.run()
File "/home/alfredoso/GitHub/DD2380_Project/tetris-master/tetris/TreeSearch.py", line 240, in run
node = self.simulation(node)
File "/home/alfredoso/GitHub/DD2380_Project/tetris-master/tetris/TreeSearch.py", line 195, in simulation
points += th.heuristic(child.state.grid) # Run heuristic here
File "/home/alfredoso/GitHub/DD2380_Project/tetris-master/tetris/heuristic.py", line 9, in heuristic
h1 = nHoles(gamefield)
File "/home/alfredoso/GitHub/DD2380_Project/tetris-master/tetris/heuristic.py", line 26, in nHoles
maxheights = getMaxHeights(gamefield)
File "/home/alfredoso/GitHub/DD2380_Project/tetris-master/tetris/heuristic.py", line 76, in getMaxHeights
if (gamefield[row][col] > 0):
IndexError: list index out of range
Maybe I'm not using it appropriately. Is this the case?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.