Example #1
0
 def getDepthTrainNodes(self, x, y, depth):
     DarkLogic.getActions()
     DarkLogic.apply(self._actionId)
     # print("explore " + str(self.actionId()) + " at depth = " + str(depth) + " with value = " + str(self.value()))
     for key in self._sons:
         node = self._sons[key]
         if node:
             if node.isEvaluated():
                 if node.value() != Node.VAL_INIT and not (
                         node.value() == Node.VAL_MAX
                         and not node.isLoss()):
                     """print("train with node id = " + str(node.actionId()) + " at depth = " + str(
                         depth + 1) + " with value = " + str(node.value()))"""
                     Node._ai.getTrainingStates(node.value(), x, y)
                 node.getDepthTrainNodes(x, y, depth + 1)
     DarkLogic.unapply()
Example #2
0
 def getDbStates(self):
     # print("get all nodes from current search tree")
     ret = []
     DarkLogic.getActions()
     name = DarkLogic.theoremName()
     content = DarkLogic.toNormStrTheorem()
     if self.value() != Node.VAL_INIT and not (self.value() == Node.VAL_MAX
                                               and not self.isLoss()):
         ret.append(State(name=name, content=content, value=self._value))
     else:
         ret.append(State(name=name, content=content))
     for key in self._sons:
         node = self._sons[key]
         if node:
             node.getDeepDbStates(ret, 1)
     return ret
Example #3
0
    def preExplore(self, nodeList, states):
        # play crt move
        DarkLogic.apply(self._threadId, self._actionId)

        # check if it is a node which leads to loss
        if DarkLogic.isAlreadyPlayed(self._threadId):
            self._value = Node.VAL_MAX
        elif not DarkLogic.canBeDemonstrated(self._threadId):
            self._value = Node.VAL_MAX
            if not DarkLogic.isEvaluated(self._threadId):
                self._isLoss = True
        # check if it is a node which leads to win
        elif DarkLogic.isDemonstrated(self._threadId):
            self._value = 0
            # stop reflexion because AI found a demonstration
            Node._ai.stopThread(self._threadId)
        else:
            actions = DarkLogic.getActions(self._threadId)
            for action in actions:
                self._sons[action] = None
            nodeList.append(self)
            if self._ai.canEvaluate(DarkLogic.getState(self._threadId)):
                states.append(Node._ai.getTrueState(self._threadId))
            else:
                self._aiValue = Node.VAL_INIT
        self._isEvaluated = True

        # unplay crt move
        DarkLogic.unapply(self._threadId)
Example #4
0
 def getTrainNodes(self, x, y):
     DarkLogic.getActions()
     print(str(len(self._sons)) + " nodes to explore")
     for key in self._sons:
         node = self._sons[key]
         if node:
             if node.isEvaluated():
                 print("explore " + str(node.actionId()) + " at depth = " +
                       str(1) + " with value = " + str(node.value()))
                 if node.value() != Node.VAL_INIT and not (
                         node.value() == Node.VAL_MAX
                         and not node.isLoss()):
                     """print("train with node id = " + str(node.actionId()) + " at depth = " + str(
                         1) + " with value = " + str(node.value()))"""
                     Node._ai.getTrainingStates(node.value(), x, y)
                 node.getDepthTrainNodes(x, y, 1)
Example #5
0
    def exploreDepthDeep(self, nodeList, states):
        DarkLogic.apply(self._threadId, self._actionId)
        actions = DarkLogic.getActions(self._threadId)
        for action in actions:
            if action not in self._sons or not self._sons[action]:
                self._sons[action] = Node(actionId=action,
                                          threadId=self._threadId,
                                          depth=self._depth + 1)
            node = self._sons[action]
            if not node.isEvaluated():
                node.preExplore(nodeList, states)
                if node.value() < Node.VAL_INIT:
                    self._value = node.value() + 1
            if Node._ai.mustStop(self._threadId):
                break
            if len(states) == Node._ai.MaxNbNode:
                break

        if not Node._ai.mustStop(
                self._threadId) and len(states) != Node._ai.MaxNbNode:
            # We need to go deeper!
            nodes = self.getDeepExploreNodes(self._sons.keys())
            rand.shuffle(nodes)
            for node in nodes:
                node.exploreDepthDeep(nodeList, states)
                if node.value() < Node.VAL_INIT:
                    self._value = node.value() + 1
                if Node._ai.mustStop(self._threadId):
                    break
                if len(states) == Node._ai.MaxNbNode:
                    break

        DarkLogic.unapply(self._threadId)
Example #6
0
 def demonstration(self, name, content, nbThreads):
     print("Test AI on " + name + " theorem with " + str(nbThreads) +
           " cores")
     DarkLogic.init(nbThreads)
     ai = AI(nbThreads, 60)
     assert DarkLogic.makeTheorem(
         name, content), "cannot make " + name + " theorem"
     DarkLogic.printTheorem()
     start = time.perf_counter()
     while not DarkLogic.isOver():
         action = ai.play()
         DarkLogic.getActions()
         print(ai.name() + " plays action with id " + str(action.id()))
         DarkLogic.apply(action.id())
         DarkLogic.printTheorem()
         print(
             "____________________________________________________________________________"
         )
     end = time.perf_counter()
     if DarkLogic.hasAlreadyPlayed():
         if DarkLogic.isDemonstrated():
             print(ai.name() + " won! " + ai.name() +
                   " finished the demonstration!")
         elif DarkLogic.isAlreadyPlayed():
             print(ai.name() + " lost! Repetition of theorem!")
         elif DarkLogic.isEvaluated():
             print(
                 ai.name() +
                 " lost! Cannot (\"back-\")demonstrate that a theorem is false with implications"
             )
         elif not DarkLogic.canBeDemonstrated():
             print(
                 ai.name() +
                 " lost! This theorem cannot be demonstrated! " +
                 "It can be true or false according to the values of its variables"
             )
     else:
         if DarkLogic.isDemonstrated():
             print("Game Over! the demonstration is already finished!")
             self._elapsed_seconds = end - start
         elif not DarkLogic.canBeDemonstrated():
             print(
                 "Game Over! This theorem cannot be demonstrated! " +
                 "It can be true or false according to the values of its variables"
             )
     self.pushEvent(Event.EventEnum.STOP)
Example #7
0
 def getDeepDbStates(self, dbStates, depth):
     DarkLogic.getActions()
     # print("apply action :"+str(self._actionId)+" at depth = "+str(depth))
     DarkLogic.apply(self._actionId)
     name = DarkLogic.theoremName()
     # print("name:" + str(name))
     # print("content: "+DarkLogic.toStrTheorem())
     content = DarkLogic.toNormStrTheorem()
     # print("NormContent = " + str(content))
     if self.value() != Node.VAL_INIT and not (self.value() == Node.VAL_MAX
                                               and not self.isLoss()):
         dbStates.append(
             State(name=name, content=content, value=self._value))
     else:
         dbStates.append(State(name=name, content=content))
     for key in self._sons:
         node = self._sons[key]
         if node:
             node.getDeepDbStates(dbStates, depth + 1)
     DarkLogic.unapply()
Example #8
0
    def exploreDepthEval(self):
        # play crt move
        DarkLogic.apply(self._threadId, self._actionId)
        # get actions
        actions = DarkLogic.getActions(self._threadId)
        # no need to go deeper
        if len(self._sons) != len(actions):
            minSubValue = Node.INIT_SUBVALUE
            updateValue = True
            for action in actions:
                # node = None
                if action not in self._sons:
                    node = Node(actionId=action,
                                threadId=self._threadId,
                                depth=self._depth + 1)
                    self._sons[action] = node
                    node.eval(self._threadId)
                    if node.value() < Node.VAL_MAX:
                        idx = self._dbNode.push(action)
                        self._dbNode.updateValue(idx, node.realValue())
                else:
                    node = self._sons[action]
                subValue = node.subValue()
                if subValue < minSubValue:
                    minSubValue = subValue

                # if must stop exploration, stop it
                if Node._ai.mustStop(self._threadId):
                    updateValue = False
                    break
            if updateValue:
                if self._dbNode.size() == 0:
                    # all actions lead to loss
                    self._value = Node.VAL_MAX
                else:
                    self._subValue = minSubValue + 1
        else:
            action = self._dbNode.getBestAction()
            node = self._sons[action]
            # self._dbNode.updateValue(idx, self.realValue(), True)  # delete this line
            value = node.exploreDepthEval()
            if node.value() < Node.VAL_MAX:
                self._dbNode.updateValue(action, value)
                action = self._dbNode.getBestAction()
                node = self._sons[action]
                self._subValue = node.subValue() + 1
            else:
                self._dbNode.removeIdx(action)

        DarkLogic.unapply(self._threadId)
        return self.realValue()
    def _start(self):
        # Dispatch actions between slave threads
        nbActions = 0
        foundDemo = False
        actions = DarkLogic.getActions(0)
        for action in actions:
            threadIdx = nbActions % len(self._slaveThreads)
            self._ai.pushCrtAction([action], threadIdx)
            val = self._ai.valueOfActions([action])
            if val == 0:
                foundDemo = True
                break
            elif val == MasterAIThread.VAL_MAX_NODE:
                continue
            else:
                self._slaveThreads[threadIdx].pushAction(action)
                nbActions += 1
                """DarkLogic.apply(0, action)
                subActions = DarkLogic.getActions(0)
                for subAction in subActions:
                    threadIdx = nbActions % len(self._slaveThreads)
                    self._ai.pushCrtAction([action, subAction], threadIdx)
                    subVal = self._ai.valueOfActions([action, subAction])
                    if subVal == 0:
                        foundDemo = True
                        break
                    elif subVal == MasterAIThread.VAL_MAX_NODE:
                        continue
                    else:
                        self._slaveThreads[threadIdx].pushAction(action, subAction)
                        nbActions += 1
                DarkLogic.unapply(0)
                DarkLogic.getActions(0)
                if foundDemo:
                    break"""

        if not foundDemo:
            # start slave threads
            if nbActions > len(self._slaveThreads):
                for slaveThread in self._slaveThreads:
                    slaveThread._start()
                    self._threadAlive[
                        slaveThread.instanceId()] = slaveThread.instanceId()
            else:
                for k in range(nbActions):
                    self._slaveThreads[k]._start()
                    self._threadAlive[self._slaveThreads[k].instanceId(
                    )] = self._slaveThreads[k].instanceId()
        else:
            self._ai.stopFromMasterThread()
Example #10
0
 def _pushAction(self, action):
     DarkLogic.getActions()
     print(self._player.name() + " played action with id " +
           str(action.id()))
     DarkLogic.apply(action.id())
     DarkLogic.printTheorem()
Example #11
0
    def exploreDepthStatic(self, maxDepth):
        # play crt move
        DarkLogic.apply(self._threadId, self._actionId)
        """print("crt action id: " + str(self._actionId) + ", at depth="
              + str(self._depth) + ", crt theorem is: " + DarkLogic.toStrTheorem(self._threadId))"""

        # no need to go deeper
        if self._depth == maxDepth:
            # check if it is a node which leads to loss
            if DarkLogic.isAlreadyPlayed(
                    self._threadId) or not DarkLogic.canBeDemonstrated(
                        self._threadId):
                self._value = Node.VAL_MAX
            # check if it is a node which leads to win
            elif DarkLogic.isDemonstrated(self._threadId):
                self._value = 0
                # stop reflexion because AI found a demonstration
                Node._ai.stopThread(self._threadId)
            """else:
                self._subValue = self._ai.eval([DarkLogic.getState(self._threadId)], self._threadId)"""
        elif not Node._ai.mustStop(self._threadId):
            # get actions
            actions = DarkLogic.getActions(self._threadId)

            # add all subnodes if they have not been created yet
            if self._depth == maxDepth - 1:
                for action in actions:
                    self._sons[action] = Node(actionId=action,
                                              threadId=self._threadId,
                                              depth=self._depth + 1)

            # explore subNodes
            hasOnlyLosses = True
            self._subValue = Node.INIT_SUBVALUE
            for action in actions:
                # explore node associated with action
                node = self._sons[action]
                retValue = Node.VAL_MAX
                if node.value() < Node.VAL_MAX:
                    retValue = node.exploreDepthStatic(maxDepth)

                # update m_value
                if retValue == Node.VAL_MAX:
                    if hasOnlyLosses:
                        self._value = Node.VAL_MAX
                else:
                    hasOnlyLosses = False
                    if retValue == Node.VAL_INIT and self._value == Node.VAL_MAX:
                        self._value = Node.VAL_INIT
                    elif self._value > retValue + 1:
                        self._value = retValue + 1

                    # update subValue
                    if self._subValue > node.subValue():
                        self._subValue = node.subValue()

                # if must stop exploration, stop it
                if Node._ai.mustStop(self._threadId):
                    break

        # unplay crt move
        DarkLogic.unapply(self._threadId)

        return self._value