Ejemplo n.º 1
0
class UI:
    def __init__(self):
        self.__initC = Configuration([-1] * 4)
        self.__p = Problem(self.__initC)
        self.__ctrl = Controller(self.__p)
        self.__n = 4

    def printMenu(self):
        str = ''
        str += "0 - exit \n"
        str += "1 - read the number of queens \n"
        str += "2 - find a solution with DFS \n"
        str += "3 - find a soultion with Greedy \n"
        print(str)

    def readCommand(self):
        n = 4
        try:
            print("Input the number of queens")
            n = int(input("n = "))
        except:
            print("Invalid number, the implicit value is 4.")
            n = 4
        self.__initC = Configuration([-1] * n)
        self.__p = Problem(self.__initC)
        self.__ctrl = Controller(self.__p)
        self.__n = n

    def findSolDFS(self):
        startClock = time()
        print(str(self.__ctrl.DFS(self.__p.getRoot(), self.__n)))
        print('execution time = ', time() - startClock, " seconds")

    def findSolGreedy(self):
        startClock = time()
        print(str(self.__ctrl.greedy(self.__p.getRoot(), self.__n)))
        print('execution time = ', time() - startClock, " seconds")

    def run(self):
        run = True
        self.printMenu()
        while run:
            try:
                command = int(input(">> "))
                if command == 0:
                    run = False
                elif command == 1:
                    self.readCommand()
                elif command == 2:
                    self.findSolDFS()
                elif command == 3:
                    self.findSolGreedy()
            except:
                print("Invalid command!")
Ejemplo n.º 2
0
class UI:
    def __init__(self, size):
        self._initialConfig = Configuration(size)
        self._problem = Problem(self._initialConfig)
        self._controller = Controller(self._problem)

    def printMenu(self):
        print("\n---------------------\n" + "0 - exit\n" +
              "1 - solve with DFS\n" + "2 - solve with Greedy\n" +
              "3 - give new size\n")

    def run(self):
        runM = True
        self.printMenu()
        while runM:

            command = int(input(">"))
            if command == 0:
                runM = False
            elif command == 1:
                self.solveDFS()
            elif command == 2:
                self.solveBestFS()
            elif command == 3:
                self.reconfigure()
            self.printMenu()

    def solveDFS(self):
        startClock = time()
        print(self._controller.DFS(self._problem.getRoot()))
        print("execution time = ", time() - startClock, " seconds")

    def solveBestFS(self):
        startClock = time()
        print(self._controller.Greedy(self._problem.getRoot()))
        print("execution time = ", time() - startClock, " seconds")

    def reconfigure(self):
        size = 5
        try:
            print(
                "Input the size of the board and number of queens (implicit 5)"
            )
            size = int(input("size = "))
        except:
            print("Invalid input, implicit value is still in place")
        self._initialConfig = Configuration(size)
        self._problem = Problem(self._initialConfig)
        self._controller = Controller(self._problem)
Ejemplo n.º 3
0
class UI:
    def __init__(self):
        try:
            n = int(input("Input the size of the board (implicit n=4)"))
        except:
            print("Invalid number. Size set to implict: 4.")
            n = 4
        matrix = np.zeros((n, n), dtype=int).tolist()
        self.__initialState = Matrix(n, matrix)
        self.__problem = Problem(self.__initialState)
        self.__controller = Controller(self.__problem)

    def mainMenu(self):
        print("Please choose an option from below:")
        print("0.Exit")
        print("1.Solve with DFS")
        print("2.Solve with greedy")

    def runBFS(self):
        solution = self.__controller.DFS(self.__problem.getRoot())
        if solution == []:
            print("No solution found")
        else:
            for s in solution:
                print(str(s))

    def runGBFS(self):
        solution = self.__controller.Greedy(self.__problem.getRoot())
        if solution == []:
            print("No solution found")
        else:
            print(str(solution))

    def run(self):
        self.mainMenu()
        finished = False
        while not finished:
            try:
                choice = int(input(">>"))
                if choice == 0:
                    finished = True
                elif choice == 1:
                    self.runBFS()
                elif choice == 2:
                    self.runGBFS()
            except ValueError:
                print("Invalid choice. Please choose one of 0,1 or 2")
Ejemplo n.º 4
0
class UI:
    def __init__(self):
        self.__config = Board([0, 0, 0, 0])
        self.__p = Problem(self.__config)
        self.__cont = Controller(self.__p)

    def printMenu(self):
        print("N-Queens Problem")
        print("Current configuration is: n = " + str(self.__config.getSize()))
        print("0 - Exit")
        print("1 - Change Configuration")
        print("2 - DFS")
        print("3 - greedy")

    def changeConfig(self):
        self.__config = Board([0 for y in range(int(input("n = ")))])
        self.__p = Problem(self.__config)
        self.__cont = Controller(self.__p)

    def findDFS(self):
        startClock = time()
        print(str(self.__cont.DFS(self.__p.getRoot())))
        print('execution time = ', time() - startClock, " seconds")

    def findGreedy(self):
        startClock = time()
        print(str(self.__cont.greedy(self.__p.getRoot())))
        print('execution time = ', time() - startClock, " seconds")

    def run(self):
        while True:
            self.printMenu()
            command = int(input(">>"))
            if command == 0:
                break
            elif command == 1:
                self.changeConfig()
            elif command == 2:
                self.findDFS()
            elif command == 3:
                self.findGreedy()