def testRentBook(self):
        client1 = Client(1, "Name1")
        client2 = Client(2, "Name2")

        book1 = Book(1, "Title", "Description", "Author")
        book2 = Book(2, "Title1", "Description1", "Author1")

        clientRepo = Repository()
        bookRepo = Repository()
        functions = ClientController(clientRepo, Statistics(clientRepo))
        functiom = BookController(bookRepo, Statistics(bookRepo))

        functions.addClient(client2.getId(), client2.getName())
        functions.addClient(client1.getId(), client1.getName())

        functiom.addBook(book1.getId(), book1.getTitle(), book1.getDescription(), book1.getAuthor())
        functiom.addBook(book2.getId(), book2.getTitle(), book2.getDescription(), book2.getAuthor())
        rentalRepo = Repository()
        functionsr = RentalController(bookRepo, clientRepo, rentalRepo, Statistics(rentalRepo))

        msg1 = functionsr.rentBook(book1.getId(), client1.getId(), createDateFromString("23.11.2017"), "30.11.2017")

        self.assertTrue(len(msg1) == 0)
        self.assertTrue(functionsr.getRentals()[0].getBookId() == book1.getId())
        self.assertTrue(functionsr.getRentals()[0].getClientId() == client1.getId())

        msg2 = functionsr.rentBook(book2.getId, client2.getId(), createDateFromString("20.11.2017"), "19.11.2017")
        self.assertTrue(msg2 == "Inconsistent dates")
Exemple #2
0
 def __init__(self):
     """
     Initializing the Simulator
     """
     self.world = World()
     self.mdp = []
     self.replay = Replay(self.world.maze, self.world.agents, self.world.get_agents_position())
     self.launch_replay = False
     self.stats = Statistics(len(self.world.agents), len(self.world.adversaries))
     self.distances = []
     self.end = False
     self.coordination = True
Exemple #3
0
 def load_replay(self, file_name):
     """
     Load a maze from a Pickle file
     :param file_name: name of the file
     """
     with open(file_name, "rb") as handle:
         self.replay = pickle.load(handle)
     self.world.maze = self.replay.maze
     self.world.agents = self.replay.agents
     self.world.adversaries = self.replay.adversaries
     self.stats = Statistics(len(self.world.agents), len(self.world.adversaries))
     self.mdp = self.calculate_policies()
     self.distances = self.mdp.value_iteration(1, self.world.maze.end)[1]
    def testRemoveBook(self):
        book1 = Book(1, "Title", "Description", "Author")
        book2 = Book(2, "Title1", "Description1", "Author1")
        book3 = Book(3, "Title2", "Description2", "Author2")
        repo = Repository()
        functions = BookController(repo, Statistics(repo))

        functions.addBook(book1.getId(), book1.getTitle(), book1.getDescription(), book1.getAuthor())
        functions.addBook(book2.getId(), book2.getTitle(), book2.getDescription(), book2.getAuthor())
        functions.addBook(book3.getId(), book3.getTitle(), book3.getDescription(), book3.getAuthor())

        msg1 = functions.removeBook(1)

        self.assertTrue(len(msg1) == 0)
        self.assertTrue(functions.getBooks()[0].getId() == book2.getId())
        self.assertTrue(functions.getBooks()[0].getTitle() == book2.getTitle())
        self.assertTrue(functions.getBooks()[0].getDescription() == book2.getDescription())
        self.assertTrue(functions.getBooks()[0].getAuthor() == book2.getAuthor())

        msg2 = functions.removeBook(1)

        self.assertTrue(msg2 == "The provided ID does not exist")
        self.assertTrue(functions.getBooks()[0].getId() == book2.getId())
        self.assertTrue(functions.getBooks()[0].getTitle() == book2.getTitle())
        self.assertTrue(functions.getBooks()[0].getDescription() == book2.getDescription())
        self.assertTrue(functions.getBooks()[0].getAuthor() == book2.getAuthor())
    def testUtils(self):
        bList = []
        self.assertTrue(len(bList) == 0)
        bookRepo = Repository()
        bc = BookController(bookRepo, Statistics(bookRepo))
        bc.populateBookRepository()

        self.assertTrue(0 < len(bookRepo.getAll()) < 100)
    def testUpdateBook(self):
        bookRepo = Repository()
        bc = BookController(bookRepo, Statistics(bookRepo))
        bc.populateBookRepository()
        bc.addBook(101, "Title", "Description", "Author")
        bc.updateBook(101, "title", "description", "author")

        self.assertTrue(bookRepo.getById(101).getTitle() == "title")
        self.assertTrue(bookRepo.getById(101).getAuthor() == "author")
        self.assertTrue(bookRepo.getById(101).getDescription() == "description")
Exemple #7
0
 def new_scenario(self, nb_agents, position_agents, position_goal, nb_adveraries, coordination=True):
     """
     Create a new scenario without changing the map
     :param nb_agents: the number of agents
     :param position_agents: the positions of the agents
     :param position_goal: the position of the goal
     """
     scenario = Scenario([self.world.maze.nx, self.world.maze.ny], nb_agents, position_agents, position_goal)
     scenario.nb_agents = nb_agents
     scenario.maze = self.world.maze
     scenario.create_agents(self.mdp)
     scenario.random_adversaries(nb_adveraries, self.mdp)
     self.world.agents = scenario.agents
     self.world.adversaries = scenario.adversaries
     self.world.maze.end = position_goal
     self.replay = Replay(self.world.maze, self.world.agents, self.world.get_agents_position(), self.world.adversaries, self.world.get_adversary_position())
     self.stats = Statistics(len(self.world.agents), len(self.world.adversaries))
     self.distances = self.mdp.value_iteration(1, self.world.maze.end)[1]
     self.coordination = coordination
Exemple #8
0
 def create_scenario(self, maze_size, nb_agents, position_agents, position_goal, nb_adveraries):
     """
     Create a new scenario
     :param maze_size: the size of the maze to generate
     :param nb_agents: the number of agents
     :param position_agents: the positions of the agents
     :param position_goal: the position of the goal
     """
     scenario = Scenario(maze_size, nb_agents, position_agents, position_goal)
     scenario.create_maze()
     self.world.maze = scenario.maze
     self.mdp = self.calculate_policies()
     scenario.create_agents(self.mdp)
     scenario.random_adversaries(nb_adveraries, self.mdp)
     self.world.agents = scenario.agents
     self.world.adversaries = scenario.adversaries
     self.replay = Replay(self.world.maze, self.world.agents, self.world.get_agents_position(), self.world.adversaries, self.world.get_adversary_position())
     self.stats = Statistics(len(self.world.agents), len(self.world.adversaries))
     self.distances = self.mdp.value_iteration(1, self.world.maze.end)[1]
    def testAddClient(self):
        client1 = Client(1, "Name1")
        client2 = Client(1, "Name2")
        repo = Repository()
        functions = ClientController(repo, Statistics(repo))

        msg1 = functions.addClient(client1.getId(), client1.getName())
        self.assertTrue(len(msg1) == 0)
        self.assertTrue(functions.getClients()[0].getId() == 1)

        msg2 = functions.addClient(client2.getId(), client2.getName())
        self.assertTrue(msg2 == "Cannot add an existing element")

        self.assertTrue(functions.getClients()[0].getId() == 1)
    def testRemoveClient(self):
        client1 = Client(1, "Name1")
        client2 = Client(2, "Name2")
        repo = Repository()
        functions = ClientController(repo, Statistics(repo))

        functions.addClient(client1.getId(), client1.getName())
        functions.addClient(client2.getId(), client2.getName())

        msg1 = functions.removeClient(1)

        self.assertTrue(len(msg1) == 0)
        self.assertTrue(functions.getClients()[0].getId() == client2.getId())
        self.assertTrue(functions.getClients()[0].getName() == client2.getName())

        msg2 = functions.removeClient(1)

        self.assertTrue(msg2 == "The provided ID does not exist")
        self.assertTrue(functions.getClients()[0].getId() == client2.getId())
        self.assertTrue(functions.getClients()[0].getName() == client2.getName())
    def testAddBook(self):
        book1 = Book(1, "Title", "Description", "Author")
        book2 = Book(2, "Title1", "Description1", "Author1")
        book3 = Book(1, "Title2", "Description2", "Author2")
        repo = Repository()
        functions = BookController(repo, Statistics(repo))

        msg1 = functions.addBook(book1.getId(), book1.getTitle(), book1.getDescription(), book1.getAuthor())

        self.assertTrue(msg1 == "")
        self.assertTrue(functions.getBooks()[0].getId() == book1.getId())
        self.assertTrue(functions.getBooks()[0].getTitle() == book1.getTitle())
        self.assertTrue(functions.getBooks()[0].getDescription() == book1.getDescription())
        self.assertTrue(functions.getBooks()[0].getAuthor() == book1.getAuthor())

        msg2 = functions.addBook(book2.getId(), book2.getTitle(), book2.getDescription(), book2.getAuthor())

        self.assertTrue(msg2 == "")
        self.assertTrue(functions.getBooks()[1].getId() == book2.getId())
        self.assertTrue(functions.getBooks()[1].getTitle() == book2.getTitle())
        self.assertTrue(functions.getBooks()[1].getDescription() == book2.getDescription())
        self.assertTrue(functions.getBooks()[1].getAuthor() == book2.getAuthor())

        msg3 = functions.addBook(book3.getId(), book3.getTitle(), book3.getDescription(), book3.getAuthor())

        self.assertTrue(msg3 == "Cannot add an existing element")

        self.assertTrue(functions.getBooks()[1].getId() == book2.getId())
        self.assertTrue(functions.getBooks()[1].getTitle() == book2.getTitle())
        self.assertTrue(functions.getBooks()[1].getDescription() == book2.getDescription())
        self.assertTrue(functions.getBooks()[1].getAuthor() == book2.getAuthor())

        self.assertTrue(functions.getBooks()[0].getId() == book1.getId())
        self.assertTrue(functions.getBooks()[0].getTitle() == book1.getTitle())
        self.assertTrue(functions.getBooks()[0].getDescription() == book1.getDescription())
        self.assertTrue(functions.getBooks()[0].getAuthor() == book1.getAuthor())
Exemple #12
0
    clientRepo = Repository()
    rentalRepo = Repository()
elif mode == "text":
    bookRepo = CSVRepository(path + "bookRepo.csv", Book)
    clientRepo = CSVRepository(path + "clientRepo.csv", Client)
    rentalRepo = CSVRepository(path + "rentalRepo.csv", Rental)
elif mode == "binary":
    bookRepo = PickleRepository(path + "binaryBookRepo.pickle")
    clientRepo = PickleRepository(path + "binaryClientRepo.pickle")
    rentalRepo = PickleRepository(path + "binaryRentalRepo.pickle")
elif mode == "custom":
    bookRepo = CustomRepository(Book)
    clientRepo = CustomRepository(Client)
    rentalRepo = CustomRepository(Rental)
else:
    raise ValueError("Invalid option")

validator = Validator()
stats = Statistics(bookRepo)
bookController = BookController(bookRepo, stats)
bookController.populateBookRepository()

clientController = ClientController(clientRepo, stats)
clientController.populateClientRepository()

rentalController = RentalController(bookRepo, clientRepo, rentalRepo, stats)
#rentalController.populateRentals()

ui = Ui(bookController, clientController, rentalController, validator, stats)

ui.runUi()
    def run(self):
     
        self.__printMenu()
        
        while True:
           
            try:
               
                cmd = int(input("Command: "))
                while cmd < 0 or cmd > 21:
                    cmd = int(input("Please enter a number between 0-21!"))
                    
                if cmd == 1:
                    
                    self.__printMenu()
                     
                elif cmd == 2:
                    
                    self.__undoController.newOperation()
                    if not self.__students.store(self.__readStudent()):
                        self.__undoController._index -= 1
                        
                elif cmd == 3:
                    
                    self.__undoController.newOperation()
                    if not self.__disciplines.store(self.__readDiscipline()):
                        self.__undoController._index -= 1
                
                elif cmd == 4:
                    
                    self.__undoController.newOperation()
                    self.__grades.store(self.__readGrade())
              
                
                elif cmd == 5:
                    
                    ID = self.__getNumber()
                    self.__undoController.newOperation()
                    if not self.__students.delete(ID):
                        self.__undoController._index -= 1
                        
                elif cmd == 6:

                    ID = self.__getNumber()
                    self.__undoController.newOperation()
                    if not self.__disciplines.delete(ID):
                        self.__undoController._index -= 1
                    
                elif cmd == 7:
                    
                    self.__undoController.newOperation()
                    if not self.__students.update(self.__readStudent()):
                        self.__undoController._index -= 1
                                      
                elif cmd == 8:
                    
                    self.__undoController.newOperation()
                    if not self.__disciplines.update(self.__readDiscipline()):
                        self.__undoController._index -= 1
                    
                    
                elif cmd == 9:
                    
                    print(str(self.__students))
                    
                elif cmd == 10:
                    
                    print(str(self.__disciplines))
                    
                elif cmd == 11:
                    s = ""
                    for i in self.__students.getAll():
                        s += str(i) + ":\n"
                        for j in self.__disciplines.getAll():
                            s += "\t" + j.getName() + ": "
                            for k in self.__grades.getAll():
                                if i.getID() == k.getID_student() and j.getID() == k.getID_discipline():
                                    s += str(k.getGrade()) + ", "
                            s +="\n"
                        s +="\n"
                    print (s)
                    
                elif cmd == 12:
                    
                    ID = self.__getNumber()
                    print(self.__disciplines.find(ID))
                    
                elif cmd == 13:
                    
                    ID = self.__getNumber()
                    print(self.__students.find(ID))
                
                elif cmd == 14:
                    
                    nameDiscipline = input("Discipline Name: ") 
                    self.__disciplines.findByName(nameDiscipline)
                    
                elif cmd == 15:
                    
                    nameStud = input("Student Name: ")  
                    self.__students.findByName(nameStud)
                    
                elif cmd == 16 or cmd == 17 or cmd == 18 or cmd == 19:
                    
                    statistics = Statistics(self.__grades)
                    print(statistics.statisticsMenu(cmd))
                
                elif cmd == 20:
                    if not self.__undoController.undo():
                        print("Cannot perform undo command")
                
                elif cmd == 21:
                    if not self.__undoController.redo():
                        print("Cannot perform redo command")
                else:
                    break
            except ValueError:
                print("Please enter a number between 0-21!")
Exemple #14
0
class Simulator:

    """
    Class representing the Simulator.
    """

    def __init__(self):
        """
        Initializing the Simulator
        """
        self.world = World()
        self.mdp = []
        self.replay = Replay(self.world.maze, self.world.agents, self.world.get_agents_position())
        self.launch_replay = False
        self.stats = Statistics(len(self.world.agents), len(self.world.adversaries))
        self.distances = []
        self.end = False
        self.coordination = True

    def get_maze(self):
        """
        Return the class Maze contained in the class World
        :return: Return the maze
        """
        return self.world.maze

    def generate_maze(self, size):
        """
        Generate a maze.
        """
        self.world.maze.make_maze(size)
        self.world.create_random_goal()

    def init_simulator(self):
        """
        Initialize the simulator and create the mdp and the agents
        """
        self.generate_maze([15, 15])
        self.mdp = self.calculate_policies()
        self.world.create_agents(2, self.mdp)

    def calculate_policies(self):
        """
        Create the MDP and calculate all the policies
        :return:
        """
        print(" Computing policies... ")
        mdp = MDP(self.world.maze)
        mdp.compute_all_policies(0.99)
        print(" Done. ")
        return mdp

    def create_scenario(self, maze_size, nb_agents, position_agents, position_goal, nb_adveraries):
        """
        Create a new scenario
        :param maze_size: the size of the maze to generate
        :param nb_agents: the number of agents
        :param position_agents: the positions of the agents
        :param position_goal: the position of the goal
        """
        scenario = Scenario(maze_size, nb_agents, position_agents, position_goal)
        scenario.create_maze()
        self.world.maze = scenario.maze
        self.mdp = self.calculate_policies()
        scenario.create_agents(self.mdp)
        scenario.random_adversaries(nb_adveraries, self.mdp)
        self.world.agents = scenario.agents
        self.world.adversaries = scenario.adversaries
        self.replay = Replay(self.world.maze, self.world.agents, self.world.get_agents_position(), self.world.adversaries, self.world.get_adversary_position())
        self.stats = Statistics(len(self.world.agents), len(self.world.adversaries))
        self.distances = self.mdp.value_iteration(1, self.world.maze.end)[1]

    def new_scenario(self, nb_agents, position_agents, position_goal, nb_adveraries, coordination=True):
        """
        Create a new scenario without changing the map
        :param nb_agents: the number of agents
        :param position_agents: the positions of the agents
        :param position_goal: the position of the goal
        """
        scenario = Scenario([self.world.maze.nx, self.world.maze.ny], nb_agents, position_agents, position_goal)
        scenario.nb_agents = nb_agents
        scenario.maze = self.world.maze
        scenario.create_agents(self.mdp)
        scenario.random_adversaries(nb_adveraries, self.mdp)
        self.world.agents = scenario.agents
        self.world.adversaries = scenario.adversaries
        self.world.maze.end = position_goal
        self.replay = Replay(self.world.maze, self.world.agents, self.world.get_agents_position(), self.world.adversaries, self.world.get_adversary_position())
        self.stats = Statistics(len(self.world.agents), len(self.world.adversaries))
        self.distances = self.mdp.value_iteration(1, self.world.maze.end)[1]
        self.coordination = coordination

    def reset(self):
        """
        Reset the simulation
        :return the new world
        """
        self.world.reset()
        self.init_simulator()
        return self.world

    def run(self):
        """
        Run the problem
        """
        if self.launch_replay:
            self.run_replay()
        else:
            positions = []
            positions_adv = []
            for a in self.world.agents:
                if a.position != self.world.get_end():
                    self.run_agent(a)
                positions.append(a.position)
                self.stats.update_distances(a.id, self.distances[a.position[1]][a.position[0]]/2)
            for adv in self.world.adversaries:
                if adv.position != self.world.get_end():
                    self.run_adversary(adv)
                positions_adv.append(adv.position)
                self.stats.update_distances(adv.id, self.distances[adv.position[1]][adv.position[0]]/2)
            self.replay.update_positions(positions)
            self.replay.update_positions_adversary(positions_adv)
            messages = self.exchanging_message()
            self.replay.update_messages(messages)
            self.stats.update_messages(self.world, messages)
            goals_possible, no_goals = self.deduce_goal()
            for a in self.world.agents:
                if self.coordination:
                    a.pomdp.deduce_goal(goals_possible, no_goals)
                else:
                    a.pomdp.deduce_goal([], [])
        self.checkEnd()

    def run_replay(self):
        if self.replay.index < self.replay.index_max:
            positions = self.replay.get_positions()
            positions_adv = self.replay.get_positions_adversaries()
            messages = self.replay.get_messages()
            self.replay.index += 1
            for i in range(len(self.world.agents)):
                self.world.agents[i].position = positions[i]
                self.stats.update_distances(self.world.agents[i].id, self.distances[self.world.agents[i].position[1]][self.world.agents[i].position[0]] / 2)
            for i in range(len(self.world.adversaries)):
                self.world.adversaries[i].position = positions_adv[i]
                self.stats.update_distances(self.world.adversaries[i].id, self.distances[self.world.adversaries[i].position[1]][
                    self.world.adversaries[i].position[0]] / 2)
            self.stats.update_messages(self.world, messages)


    def run_agent(self, agent):
        """
        Run the process for each agent
        :param agent: agent
        """
        # Get the best action following the current policy
        action = agent.pomdp.get_best_action()
        # Execute the action
        new_position = agent.pomdp.execute_action(action)
        # Update the position of the agent
        agent.position = new_position
        # Search with an heuristic the best policy to execute
        agent.pomdp.heuristic_search()

    def run_adversary(self, adversary):
        """
        Run the process for each adversary
        :param adversary: adversary
        """
        # Get the best action following the current policy
        action = adversary.pomdp.get_best_action()
        # Execute the action
        new_position = adversary.pomdp.execute_action(action)
        # Update the position of the agent
        adversary.position = new_position
        # Search with an heuristic the best policy to execute
        adversary.pomdp.heuristic_search()

    def exchanging_message(self):
        """
        Exchange messages between agents.
        """
        messages = []
        for i in range(len(self.world.agents)):
            verifier = self.world.agents[i]
            messages_a = []
            for j in range(len(self.world.agents)):
                if j != i:
                    prover = self.world.agents[j]
                    question = verifier.zkmaze.verifier_send_question(prover.id)
                    answer = prover.zkmaze.prover_send_answer(question)
                    verifier.zkmaze.verifier_receive_answer(prover.id, answer)
                    verifier.zkmaze.estimate_position_agent(prover.id)
                    messages_a.append([question, prover.id, answer])
                else:
                    messages_a.append([])
            for adv in range(len(self.world.adversaries)):
                prover = self.world.adversaries[adv]
                question = verifier.zkmaze.verifier_send_question(prover.id)
                answer = prover.strategy.prover_send_answer(question)
                verifier.zkmaze.verifier_receive_answer(prover.id, answer)
                verifier.zkmaze.estimate_position_agent(prover.id)
                messages_a.append([question, prover.id, answer])
            messages.append(messages_a)
        for i in range(len(self.world.adversaries)):
            verifier = self.world.adversaries[i]
            messages_a = []
            for j in range(len(self.world.agents)):
                prover = self.world.agents[j]
                question = verifier.strategy.verifier_send_question(prover.id)
                answer = prover.zkmaze.prover_send_answer(question)
                verifier.strategy.verifier_receive_answer(prover.id, answer)
                messages_a.append([question, prover.id, answer])
            messages.append(messages_a)
        return messages

    def estimate_position_agent(self, id, positions):
        """
        Estimate the position of the agent
        :param id: Id of the agent
        """
        if id in self.estimated_positions:
            new_positions = self.reduce_position_uncertainty(id, positions)
            self.estimated_positions[id] = new_positions
        else:
            self.estimated_positions[id] = positions

    def reduce_position_uncertainty(self, id, new_estimation):
        """
        Reduce the uncertainty on the estimated positions
        :param id: id of the agent
        :param new_estimation: new positions estimated
        :return: List of positions
        """
        positions = []
        previous_estimation = self.estimated_positions[id]
        for pe in previous_estimation:
            for ne in new_estimation:
                previous_cell = self.world.maze.cell_at(pe[0], pe[1])
                new_cell = self.world.maze.cell_at(ne[0], ne[1])
                if previous_cell.wall_between(new_cell) is not None:
                    if not previous_cell.wall_between(new_cell) and ne not in positions:
                        positions.append(ne)
                if previous_cell == new_cell:
                    positions.append(ne)
        return positions

    def get_estimated_agent_positions(self):
        """
        Return all the expected positions for every agent
        :return: list of expected positions
        """
        expected_positions = {}
        for a in self.world.agents:
            if a.id in self.stats.estimated_position:
                expected_positions[a.id] = self.stats.estimated_position[a.id][self.stats.index-1]
        return expected_positions

    def deduce_goal(self):
        goals = []
        no_goals = []
        for a in self.world.agents:
            if self.stats.index > 1:
                if self.stats.estimated_position[a.id][self.stats.index-1] == self.stats.estimated_position[a.id][self.stats.index-2]:
                    goals += self.stats.estimated_position[a.id][self.stats.index-1]
                    break
                else:
                  if len(self.stats.estimated_position[a.id][self.stats.index-2]) == 1:
                    no_goals += self.stats.estimated_position[a.id][self.stats.index-2]
        return goals, no_goals

    def save_maze(self, file_name):
        """
        Save the maze in a Pickle file
        :param file_name: name of the file
        """
        save = Save(self.world.maze, self.mdp)
        with open(file_name, 'wb') as handle:
            pickle.dump(save, handle)

    def load_maze(self, file_name):
        """
        Load a maze from a Pickle file
        :param file_name: name of the file
        """
        with open(file_name, "rb") as handle:
            save = pickle.load(handle)
            maze = save.maze
            mdp = save.mdp
        self.world.maze = maze
        self.mdp = mdp

    def save_replay(self, file_name):
        """
        Save the replay in a Pickle file
        :param file_name: name of the file
        """
        with open(file_name, 'wb') as handle:
            pickle.dump(self.replay, handle)

    def load_replay(self, file_name):
        """
        Load a maze from a Pickle file
        :param file_name: name of the file
        """
        with open(file_name, "rb") as handle:
            self.replay = pickle.load(handle)
        self.world.maze = self.replay.maze
        self.world.agents = self.replay.agents
        self.world.adversaries = self.replay.adversaries
        self.stats = Statistics(len(self.world.agents), len(self.world.adversaries))
        self.mdp = self.calculate_policies()
        self.distances = self.mdp.value_iteration(1, self.world.maze.end)[1]

    def checkEnd(self):
        end = True
        for a in self.world.agents:
            if a.position != self.world.get_end():
                end = False
        self.end = end