Question: Python help required, I need help getting commandline input working. I have two test cases and two parsers, first parse, parses through the filename(.txt) and
Python help required,
I need help getting commandline input working. I have two test cases and two parsers, first parse, parses through the filename(.txt) and second one is not working, becuase I am trying to read them in through the command line. So for the first test.txt, which has this test input inside it: ['A', ['B', ('D', 3), ('E', 5)], ['C', ['F', ['I',('K',0), ('L', 7)],('J',5)], ['G', ('M',7), ('N',8)], ('H',4)]]
Works through the file reader parser, but through command line.
So when I compile like so: pythong mycode.py test.txt
I get my result, but when I try to read it command line it is not working and throws an error, which I'll paste below. The most IMPORTANT test case that needs to be working is test2.txt which has the input of: [[3,12,8],[2,4,6],[14,5,2]]
So when I try to run it: python mycode.py [[3,12,8],[2,4,6],[14,5,2]]
It does not read it nor does it work in commandline. The error message I get is:
ERROR after running the following: python minimax_a_b.py [[3,12,8],[2,4,6],[14,5,2]] Traceback (most recent call last): File "minimax_a_b.py", line 144, in
The main things I want my program to do is read in from the user promt or like so: python mycode.py [[3,12,8],[2,4,6],[14,5,2]]
The structure of the list represents a nested tree list, with the outer brackets([]) representing the root, and the inner brackets present the subtrees with their values.
Then my program creates the tree, runs minimax on it and outputs the max value, which in test2 case is 3. So if you can help me out achieve that, then that would be highly appreciated.
Here is my code, I tried maintaing the indetion by pasting it in MS word and then copying it and pasting here:
[Code]
from ast import literal_eval
import sys
class MiniMax:
# print utility value of root node (assuming it is max)
# print names of all nodes visited during search
def __init__(self, game_tree):
self.game_tree = game_tree # GameTree
self.root = game_tree.root # GameNode
self.currentNode = None # GameNode
self.successors = [] # List of GameNodes
return
def minimax(self, node):
# first, find the max value
best_val = self.max_value(node) # should be root node of tree
# second, find the node which HAS that max value
# --> means we need to propagate the values back up the
# tree as part of our minimax algorithm
successors = self.getSuccessors(node)
print "Max Value: " + str(best_val)
# find the node with our best move
best_move = None
for elem in successors: # ---> Need to propagate values up tree for this to work
if elem.value == best_val:
best_move = elem
break
# return that best value that we've found
return best_move
def max_value(self, node):
if self.isTerminal(node):
return self.getUtility(node)
infinity = float('inf')
max_value = -infinity
successors_states = self.getSuccessors(node)
for state in successors_states:
max_value = max(max_value, self.min_value(state))
return max_value
def min_value(self, node):
if self.isTerminal(node):
return self.getUtility(node)
infinity = float('inf')
min_value = infinity
successor_states = self.getSuccessors(node)
for state in successor_states:
min_value = min(min_value, self.max_value(state))
return min_value
# #
# UTILITY METHODS #
# #
# successor states in a game tree are the child nodes...
def getSuccessors(self, node):
assert node is not None
return node.children
# return true if the node has NO children (successor states)
# return false if the node has children (successor states)
def isTerminal(self, node):
assert node is not None
return len(node.children) == 0
def getUtility(self, node):
assert node is not None
return node.value
#Parse from filename
def parse_data_as_list(fname):
with open(fname, "r") as f:
data_as_string = f.read()
data_list = literal_eval(data_as_string)
return data_list
#Parse from commandline
def parse_input_data_as_list(data_as_string):
data_list = literal_eval(data_as_string)
return data_list
class GameNode:
def __init__(self, name, value=0, parent=None):
self.Name = name # a char
self.value = value # an int
self.parent = parent # a node reference
self.children = [] # a list of nodes
def addChild(self, childNode):
self.children.append(childNode)
class GameTree:
def __init__(self):
self.root = None
def build_tree(self, data_list):
self.root = GameNode(data_list.pop(0))
for elem in data_list:
self.parse_subtree(elem, self.root)
def parse_subtree(self, data_list, parent):
# base case
if type(data_list) is tuple:
# make connections
leaf_node = GameNode(data_list[0])
leaf_node.parent = parent
parent.addChild(leaf_node)
# if we're at a leaf, set the value
if len(data_list) == 2:
leaf_node.value = data_list[1]
return
# recursive case
tree_node = GameNode(data_list.pop(0))
# make connections
tree_node.parent = parent
parent.addChild(tree_node)
for elem in data_list:
self.parse_subtree(elem, tree_node)
# return from entire method if base case and recursive case both done running
return
##########################
#### MAIN ENTRY POINT ####
##########################
def main():
data_as_string = sys.argv[1]
#filename = sys.argv[1]
#print "FILENAME: " + filename
#data_list = parse_data_as_list(filename)
data_list = parse_input_data_as_list(data_as_string)
data_tree = GameTree()
data_tree.build_tree(data_list)
minimax = MiniMax(data_tree)
best_move = minimax.minimax(minimax.root)
if __name__ == "__main__":
main()
[End Code]
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
