when I run this I get NameError name state is not defined error How to solve this

0 votes
class AdversarialNode(State):

  

    def _init_(self, value, name, isMax, children = []):

        self._name = name

        self._utility = value

        self._isMax = isMax

        self._children = children

        self._move = 1

        self._action = None

         

    def isLeaf(self):

        return len(self._children) == 0

    def isMax(self):

        return self._isMax

     

    def isNextAgentMax(self):

        return not self._isMax

    def addChild(self,child):

        self._children.append(child);

         

    def getAction(self):

        return self._action

         

    def _str_(self):

        s = "Name is %s, value is %d" %(self._name,self._utility)

        s += "\n children are "

        for ch in range(len(self._children)):

            s+= str(self._children[ch])

        return s

from abc import abstractmethod

class Game(object):

  @abstractmethod

  def _init_(self, params): pass  

  @abstractmethod

  def getInitialState(self):pass  

  @abstractmethod

  def getPlayer(self,state):pass  

  @abstractmethod

  def getActions(self,state): pass    

  @abstractmethod

  def getResult(self, state, action): pass  

  @abstractmethod

  def terminalTest(self,state): pass  

  @abstractmethod

  def utility(self,state,player): pass  

  @abstractmethod

  def getAgentCount(self): pass

class State(object):

    

    def _init_(self, params):pass

     

    def isMax(self): pass      

    def isNextAgentMax(self): pass

     

    def getAction(self):pass

import sys

class MinimaxTreeGame(Game):

    def _init_(self):

      bottom1 = [AdversarialNode(3,"E",True,[]),

                   AdversarialNode(12,"F",True,[]),

                   AdversarialNode(8,"G",True,[])]          

      bottom2 = [AdversarialNode(2,"H",True,[]),

                   AdversarialNode(4,"I",True,[]),

                   AdversarialNode(6,"J",True,[])]

      bottom3 = [AdversarialNode(14,"K",True,[]),

                AdversarialNode(5,"L",True,[]),

                AdversarialNode(2,"M",True,[])]

         

      b = AdversarialNode(-sys.maxsize-1,"B",False,bottom1)

      c = AdversarialNode(-sys.maxsize-1,"C",False,bottom2)         

      d = AdversarialNode(-sys.maxsize-1,"D",False,bottom3)

         

      a = AdversarialNode(-sys.maxsize-1,"A",True,[b,c,d])

         

      self._root = a

         

    def getInitialState(self):         

      return self._root

     

    def getPlayer(self,state):         

      return state.isMax()

     

    def getActions(self,state):

        return [x for x  in range(len(state._children))]

          

    def getResult(self, state, action):

        return state._children[action]

      

    def terminalTest(self,state):         

      return state.isLeaf()

      

    def utility(self,state,player):

        return state._value

             

    def getAgentCount(self):  

        return 2

     

    def printState(self,state):

        toPrintNodes = []

        toPrintNodes.append(state)

        while len(toPrintNodes) > 0:

            node = toPrintNodes[0]

            del toPrintNodes[0]

            print("Name = %s, value = %d"%(node._name,node._utility))

            toPrintNodes += node._children

class SimpleMinimax(object):

   

  def _init_(self, game, listeners = []):        

        

    self._game = game

    self.listeners = listeners         

    self._expandedNodes = 0

    self._duplicateStates = {}

     

  def minimax_decision(self,state):

    self._duplicateStates[str(state)] = state

         

    if self._game.terminalTest(state):             

      return state._utility

         

    if state.isMax():

      return self.maxvalue(state)         

    else:

      return self.minvalue(state)

     

    

   

  def minvalue( self,state):         

    ss = str(state)

    if ss in self._duplicateStates and self._duplicateStates[ss]._utility > state._utility:

      return state._utility

    else:

      self._duplicateStates[str(state)] = state

    self._expandedNodes += 1          

    retValue = 1000000000000

         

#         player = self._game.getPlayer(state)

    actions = self._game.getActions(state)

         

    for action in actions:             

      tempValue = self.minimax_decision(self._game.getResult(state,action))             

      if tempValue < retValue:

        retValue = tempValue     

        state._utility = retValue

        state._action = action

                    

    return retValue

             

  def maxvalue(self,state):

         

    ss = str(state)

    if ss in self._duplicateStates and self._duplicateStates[ss]._utility > state._utility:

      return state._utility

    else:

      self._duplicateStates[str(state)] = state

             

    self._expandedNodes += 1

         

    retValue=-1000000000000

         

#         player = self._game.getPlayer(state)

    actions = self._game.getActions(state)

         

    for action in actions:             

      tempValue = self.minimax_decision(self._game.getResult(state,action))             

      if tempValue > retValue:

        retValue = tempValue     

        state._utility = retValue                 

        state._action = action

         

    return retValue

if _name_ == "_main_":     

  game = MinimaxTreeGame()

  minimax = SimpleMinimax(game)

  initialState = game.getInitialState()

  minimax.minimax_decision(initialState)  

  game.printState(initialState)
Sep 6, 2020 in Python by MUHAMMAD
• 160 points

edited Sep 7, 2020 by Niroj 2,863 views

Hi, @Muhammad,

In which line of the code you shared is the error? 

1 answer to this question.

0 votes

Hi@MUHAMMAD,

I tried to run the exact code you pasted above. But I didn't get the same error that you pasted. You are trying to pass State in your class. But where you get this value. First, check that one. Also, you have written if _name_ == "_main_": in your code. Here you have missed one underscore. It should be like this if __name__ == "__main__":

answered Sep 8, 2020 by MD
• 95,460 points

Related Questions In Python

0 votes
0 answers

But when I run this, I get NameError: name 'alphabeta' is not defined error. How to solve this?

tree = [[[5, 1, 2], [8, -8, ...READ MORE

Sep 6, 2020 in Python by MUHAMMAD
• 160 points

retagged Sep 7, 2020 by Gitika 1,304 views
0 votes
1 answer

i write this python to read dataset into panda data frame but im getting NameError: name 'true' is not defined. Below is the code:

change  def parse(path):   g = gz ...READ MORE

answered Mar 15, 2020 in Python by Abhishek Shingadiya
3,706 views
0 votes
1 answer
0 votes
1 answer
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,464 views
0 votes
1 answer
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,790 points
4,122 views
0 votes
2 answers

NameError: name 'raw_input' is not defined

Hi, There may a problem with your python ...READ MORE

answered Jun 25, 2020 in Python by MD
• 95,460 points
32,737 views
0 votes
1 answer

how to solve the error : "ImportError: No module named Tkinter" in python.

Hi@Umama, If you are using Linux system, then ...READ MORE

answered Apr 27, 2020 in Python by MD
• 95,460 points
13,515 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP