예제 #1
0
 def generate(self):
     pg = problem_generator.ProblemGenerator(25, 15, 1, 3)
     problem = pg.generate_problem()
     yml = yaml_from_problem(problem)
     print(yml)
     problem_from_yml = problem_from_input(yml)
     self.assertEqual(problem.graph.number_of_nodes(), problem_from_yml.graph.number_of_nodes())
     self.assertEqual(problem.graph.number_of_edges(), problem_from_yml.graph.number_of_edges())
예제 #2
0
 def dijkstra(self):
     with open("tests/test.yml") as f:
         str = f.read()
     pp = problem_from_input(str)
     dijkstra = Dijkstra(pp)
     solved = dijkstra.solve_graph()
     print(solved)
     self.assertTrue(solved == ['Ulsan', 'Daegu', 'Gumi', 'Seoul', 'Pyeongyang']
                     or solved == ['Ulsan', 'Daegu', 'Daejeon', 'Seoul', 'Pyeongyang'])
예제 #3
0
 def parser(self):
     with open("tests/test.yml") as f:
         str = f.read()
     pp = problem_from_input(str)
     print(pp.graph.nodes, pp.graph.edges, pp.start, pp.end, pp.trunk_size)
     for node in pp.graph.nodes:
         print(pp.graph.nodes[node].get("treasure", None))
     self.assertEqual(pp.start, "Ulsan")
     self.assertEqual(pp.end, "Pyeongyang")
예제 #4
0
 def draw(self):
     with open("tests/test.yml") as f:
         str = f.read()
     pp = problem_from_input(str)
     dijkstra = Dijkstra(pp)
     solved = dijkstra.solve_graph()
     treasure_list = treasureparser.treasure_from_graph_with_path(pp.graph, solved)
     to_take = greedy(treasure_list, pp.trunk_size)
     solution = Solution(pp, solved, to_take)
     problemgui.draw_plot(solution)
예제 #5
0
 def bfs(self):
     with open("tests/test.yml") as f:
         str = f.read()
     pp = problem_from_input(str)
     bfs = BFS(pp)
     solved_set = set()
     for i in range(20):
         solved = bfs.solve_graph()
         solved_set.add(tuple(solved))
         self.assertEqual(len(solved_set), 1)
     print(solved)
     self.assertTrue(solved == ['Ulsan', 'Daegu', 'Gumi', 'Seoul', 'Pyeongyang']
                     or solved == ['Ulsan', 'Daegu', 'Daejeon', 'Seoul', 'Pyeongyang'])
예제 #6
0
 def greedy(self):
     with open("tests/test.yml") as f:
         str = f.read()
     pp = problem_from_input(str)
     dijkstra = Dijkstra(pp)
     solved = dijkstra.solve_graph()
     print(solved)
     pp.treasure_list = treasureparser.treasure_from_graph_with_path(pp.graph, solved)
     print("treasures: ", pp.treasure_list)
     treasure_list = treasureparser.treasure_from_graph_with_path(pp.graph, solved)
     to_take = greedy(treasure_list, pp.trunk_size)
     print("take: ", to_take)
     self.assertEqual(sum(taken.value for taken in to_take), 766 if "Gumi" in solved else 720)
     self.assertLessEqual(sum(taken.size for taken in to_take), pp.trunk_size)
예제 #7
0
파일: solver.py 프로젝트: Spaqin/AAL-2017L
    def main(self):
        result_list = []
        graph_solutions = set()
        max_value = 0
        best_algo = None
        best_path = None
        best_set = None
        best_size = 0
        total_time_start = perf_counter()
        problem = problemparser.problem_from_input(self.problem_stream)
        logging.basicConfig(level=logging.INFO)
        if self.bfs_on:
            logging.info("Running BFS...")
            solver = bfs.BFS(problem)
            path, time = self.bench(solver.solve_graph)
            graph_solutions.add(tuple(path))
            r = self.Result("BFS", time, len(path))
            result_list.append(r)
            logging.info("BFS finished")

        if self.dijkstra_on:
            logging.info("Running Dijkstra")
            solver = dijkstra.Dijkstra(problem)
            path, time = self.bench(solver.solve_graph)
            graph_solutions.add(tuple(path))
            value = len(path)
            r = self.Result("Dijkstra", time, value)
            result_list.append(r)
            logging.info("Dijkstra finished")

        if self.mitm_on:
            for path in graph_solutions:
                logging.info("Running MITM for path: {}".format(path))
                treasure_list = treasureparser.treasure_from_graph_with_path(
                    problem.graph, path)
                treasures, time = self.bench(mitm.meet_in_the_middle,
                                             treasure_list, problem.trunk_size)
                total_value = 0
                total_size = 0
                for tr in treasures:
                    total_value += tr.value
                    total_size += tr.size
                r = self.Result("Mitm", time, total_value)
                result_list.append(r)
                if max_value < total_value:
                    max_value = total_value
                    best_algo = "MITM"
                    best_path = path
                    best_set = treasures
                    best_size = total_size
                logging.info("MITM iteration ended")
            logging.info("MITM ended")

        if self.greedy_on:
            for path in graph_solutions:
                logging.info("Running Greedy for path: {}".format(path))
                treasure_list = treasureparser.treasure_from_graph_with_path(
                    problem.graph, path)
                treasures, time = self.bench(greedy.greedy, treasure_list,
                                             problem.trunk_size)
                total_value = 0
                total_size = 0
                for tr in treasures:
                    total_value += tr.value
                    total_size += tr.size
                r = self.Result("Greedy", time, total_value)
                result_list.append(r)
                if max_value < total_value:
                    max_value = total_value
                    best_algo = "Greedy"
                    best_path = path
                    best_set = treasures
                    best_size = total_size
                logging.info("Greedy iteration ended")
            logging.info("Greedy ended")

        total_time_end = perf_counter()
        print(20 * "=", "STATS", 21 * "=")
        print("Name    | Time taken [s]| Score (path len/value)")
        for result in result_list:
            print("{:8}|{:>4.13f}|{:>23}".format(result.name, result.time,
                                                 result.value))
        print(48 * "=")
        print("Total time taken: {}s".format(total_time_end -
                                             total_time_start))
        print("Best path: {}".format(best_path))
        print("Best knapsack algorithm: {}".format(best_algo))
        print("Best set: {}".format(best_set))
        print("Total value: {}".format(max_value))
        print("Total size: {} out of max {}".format(best_size,
                                                    problem.trunk_size))
        stdout.flush()
        if self.display_results:
            solution = Solution(problem, path=best_path, to_take=best_set)
            problemgui.draw_plot(solution)