Exemple #1
0
 def test_ast2(self):
     solver = Solver("ast", self.puzzle_3_2)
     _ = solver.solve()
     print(round(solver.statistics.total_time, 3), " s")
     self.assertEqual(solver.statistics.max_depth, 20)
     self.assertEqual(solver.statistics.moves, [
         'D', 'R', 'U', 'U', 'L', 'D', 'R', 'D', 'L', 'U', 'L', 'U', 'R',
         'R', 'D', 'D', 'L', 'L', 'U', 'U'
     ])
Exemple #2
0
 def test_ast3(self):
     solver = Solver("ast", self.puzzle_4_zl, zl=True)
     _ = solver.solve()
     print(round(solver.statistics.total_time, 3), " s")
     self.assertEqual(solver.statistics.max_depth, 35)
     self.assertEqual(solver.statistics.moves, [
         'D', 'L', 'L', 'L', 'U', 'U', 'R', 'R', 'R', 'D', 'D', 'L', 'L',
         'L', 'U', 'R', 'R', 'D', 'L', 'U', 'U', 'R', 'R', 'D', 'D', 'L',
         'L', 'U', 'L', 'U', 'R', 'R', 'R', 'D', 'D'
     ])
Exemple #3
0
 def test_bfs2(self):
     solver = Solver("bfs", self.puzzle_3_2)
     _ = solver.solve()
     print(round(solver.statistics.total_time, 3), " s")
     self.assertEqual(solver.statistics.max_depth, 21)
     self.assertEqual(solver.statistics.nodes, 54094)
     self.assertEqual(solver.statistics.moves, [
         'D', 'R', 'U', 'U', 'L', 'D', 'R', 'D', 'L', 'U', 'L', 'U', 'R',
         'R', 'D', 'D', 'L', 'L', 'U', 'U'
     ])
Exemple #4
0
 def test_bfs1(self):
     solver = Solver("bfs", self.puzzle_3_1)
     _ = solver.solve()
     print(round(solver.statistics.total_time, 3), " s")
     self.assertEqual(solver.statistics.max_depth, 27)
     self.assertEqual(solver.statistics.nodes, 166786)
     self.assertEqual(solver.statistics.moves, [
         'L', 'U', 'U', 'L', 'D', 'R', 'D', 'L', 'U', 'R', 'R', 'U', 'L',
         'L', 'D', 'R', 'R', 'U', 'L', 'D', 'D', 'R', 'U', 'L', 'U', 'L'
     ])
def run_test(map_path, timeout):
    start = clock()
    world = World.from_file(map_path)
    print 'solving {:50}'.format(map_path),
    
    start = clock()
    solver = Solver(world, timeout=timeout)
    solver.solve()
    score, solution = solver.get_best()
    solver.log_stats()
    t = clock()-start    
    
    
    world = World.from_file(map_path)
    for cmd in solution:
        world = world.apply_command(cmd)
        if world.terminated:
            break
    validated_score = world.score
    
    assert score == validated_score, (score, validated_score)
    print '{:>10} {:>10.3f}s'.format(score, t)
Exemple #6
0
 def test_dfs2(self):
     solver = Solver("dfs", self.puzzle_3_2)
     _ = solver.solve()
     print(round(solver.statistics.total_time, 3), " s")
     self.assertEqual(solver.statistics.max_depth, 46142)
     self.assertEqual(solver.statistics.nodes, 51015)
Exemple #7
0
 def test_dfs1(self):
     solver = Solver("dfs", self.puzzle_3_1)
     _ = solver.solve()
     print(round(solver.statistics.total_time, 3), " s")
     self.assertEqual(solver.statistics.max_depth, 9612)
     self.assertEqual(solver.statistics.nodes, 9869)
Exemple #8
0
                        action='store_true',
                        help="print the path")
    parser.add_argument("-n",
                        "--nodes",
                        default='200000',
                        help="maximum number of nodes to visit,\
                             default 200 000")
    parser.add_argument("-zl",
                        "--zerolast",
                        action='store_true',
                        help="WRITE ME")
    args = parser.parse_args()

    init_state = list(map(int, args.initial.split(",")))
    if (list(range(len(init_state))) != sorted(init_state)):
        raise ValueError("Wrong initial state! Check if all numbers are here")
    if (math.sqrt(len(init_state)) != round(math.sqrt(len(init_state)))):
        raise ValueError("Wrong initial state! Check the array size")
    solver = Solver(args.method, init_state, zl=args.zerolast)
    final_state = solver.solve(maxnodes=int(args.nodes))
    solver.print_stats(args.final)
    if not final_state:
        print("Solution is not found")

# 15,14,1,6,9,11,4,12,0,10,7,3,13,8,5,2   PROBLEM
# 1,2,3,4,13,9,14,5,12,10,15,0,11,8,7,6 -n 100000 -g 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0

# tests
# 6,1,8,4,0,2,7,3,5
# 8,6,4,2,1,3,5,7,0
    print 'genetic done', score
    if score > 0:
        solver.best_score = score
        solver.best_solution = solution
        


if __name__ == '__main__':
    realout = sys.stdout
    sys.stdout = sys.stderr

    signal.signal(signal.SIGINT, handler)
    
    data = sys.stdin.read()
    world = World.from_string(data)
    
    world.show()
    
    solver = Solver(world, timeout=1000000)
    
    greedy(World(world))
    
    call_genetic(World(world))
    
    solver.solve()
    
    print>>realout, solver.get_best()[1]
    print 'final score', solver.get_best()[0]
    
    sys.stdout = realout