Esempio n. 1
0
    def _game(self):
        nbAttempts = 0
        maxNbAttempts = 10
        hasWon = False
        self._nbGames += 1
        print("Game n°" + str(self._nbGames))
        while not DarkLogic.isOver():
            print("Attempt n°" + str(nbAttempts + 1) + "/" +
                  str(maxNbAttempts))
            action = self._player.play()
            LogicGame._actionSwitcher[action.fun()](self, action)
            print(
                "____________________________________________________________________________"
            )
            if action.fun() == EnumFun.PUSH_ACTION:
                nbAttempts += 1
                if nbAttempts == maxNbAttempts:
                    break

        if DarkLogic.hasAlreadyPlayed():
            if DarkLogic.isDemonstrated():
                print(self._player.name() + " won! " + self._player.name() +
                      " finished the demonstration!")
                hasWon = True
            elif DarkLogic.isAlreadyPlayed():
                print(self._player.name() + " lost! Repetition of theorem!")
            elif DarkLogic.isEvaluated():
                print(
                    self._player.name() +
                    "lost! Cannot (\"back-\")demonstrate that a theorem is false with "
                    "implications")
            elif not DarkLogic.canBeDemonstrated():
                print(
                    self._player.name() +
                    " lost! This theorem cannot be demonstrated! " +
                    "It can be true or false according to the values of its variables"
                )
            elif nbAttempts == maxNbAttempts:
                print(self._player.name() + " lost! Too much attempts!")

            # update player's elo
            W = 1 if hasWon else 0
            exElo = self._player.elo()
            newElo = round(exElo + 30 *
                           (W - 1 / (1 + 10**((self._eloThm - exElo) / 400))))
            self._player.setElo(newElo)
        else:
            if DarkLogic.isDemonstrated():
                print("The demonstration is already finished!")
            elif not DarkLogic.canBeDemonstrated():
                print(
                    "This theorem cannot be demonstrated! " +
                    "It can be true or false according to the values of its variables"
                )

        # clear Logic State
        DarkLogic.clearAll()

        # let player meditates the last game
        self._player.meditate()
Esempio n. 2
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)
Esempio n. 3
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)
Esempio n. 4
0
def humanIdentity():
    DarkLogic.init(0)
    print("Identity Demonstration")
    DarkLogic.makeTheorem("identity", "a<=>a")
    DarkLogic.printTheorem()
    print("__________________________________________________")

    apply("arr,[0]")
    apply("arr,[0]")
    apply("arr,[1]")
    apply("arr_True,[]")
    assert DarkLogic.isDemonstrated(), "identity theorem was not demonstrated"
Esempio n. 5
0
def humanDoubleNot():
    DarkLogic.init(0)
    print("DoubleNot Demonstration")
    DarkLogic.makeTheorem("doubleNot", "a<=>!!a")
    DarkLogic.printTheorem()
    print("__________________________________________________")

    apply("FE,[1]")
    apply("arr,[0]")
    apply("arr,[0]")
    apply("arr_True")
    assert DarkLogic.isDemonstrated(), "DoubleNot theorem was not demonstrated"
Esempio n. 6
0
def humanExcludedMiddle():
    DarkLogic.init(0)
    print("ExcludedMiddle Demonstration")
    DarkLogic.makeTheorem("ExcludedMiddle", "p||!p")
    DarkLogic.printTheorem()
    print("__________________________________________________")

    apply("arr")
    apply("FI!")
    apply("||Ig_Annexe_0")
    apply("FE")
    apply("FI!_Annexe_7")
    apply("||Ig")
    apply("ax_Annexe_1")

    assert DarkLogic.isDemonstrated(
    ), "ExcludedMiddle theorem was not demonstrated"
Esempio n. 7
0
    def performance(self, thName, thContent, maxTime):
        print("CheckPerformance Test on " + thName + " theorem : " + thContent)
        nbThreads = multiprocessing.cpu_count()
        realMaxTime = (maxTime * 1.15) * (4.0 / nbThreads)

        # start AI
        self._thread = TestThread(self, thName, thContent, nbThreads)

        with self._condition_var:
            while not self._hasEvents:
                hasTimedOut = not self._condition_var.wait(20)
                if hasTimedOut:
                    break

        if len(self._eventQueue):
            self._eventQueue.pop()
        self._hasEvents = False
        assert DarkLogic.isDemonstrated(), thName + " cannot be demonstrated"
        assert self._elapsed_seconds < realMaxTime, "Demonstration of " + thName + " theorem must be "
        "done in less than " + str(realMaxTime) + " seconds"
        DarkLogic.clearAll()
Esempio n. 8
0
    def eval(self, threadIdx):
        # play crt move
        DarkLogic.apply(threadIdx, self._actionId)

        # check if it is a node which leads to loss
        if DarkLogic.isAlreadyPlayed(threadIdx):
            self._value = Node.VAL_MAX
        elif not DarkLogic.canBeDemonstrated(threadIdx):
            self._value = Node.VAL_MAX
            if not DarkLogic.isEvaluated(threadIdx):
                self._isLoss = True
        # check if it is a node which leads to win
        elif DarkLogic.isDemonstrated(threadIdx):
            self._value = 0
            # stop reflexion because AI found a demonstration
            Node._ai.stopThread(threadIdx)
        else:
            self._subValue = Node._ai.eval([DarkLogic.getState(threadIdx)],
                                           threadIdx)
        self._isEvaluated = True

        # unplay crt move
        DarkLogic.unapply(threadIdx)
Esempio n. 9
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