Пример #1
0
def convert_sokoban_instance(filename):
    asp_instance = asp_fact_parser.parse(filename)

    print "(define (problem sokoban-problem)"
    print "  (:domain sokoban-sequential)"
    print "  (:objects"
    print "    dir-down - direction"
    print "    dir-left - direction"
    print "    dir-right - direction"
    print "    dir-up - direction"
    print "    player-01 - player"

    locations = set()
    box_locations = asp_instance["box"].objects()
    goal_locations = set(asp_instance["solution"].objects())

    for predicate in ["top", "right", "box", "solution", "sokoban"]:
        locations.update(asp_instance[predicate].objects())

    for location in sorted(locations):
        print "    loc-%s - location" % location

    for box_location in sorted(box_locations):
        print "    box-%s - stone" % box_location
    print "  )"

    print "  (:init"
    for location in sorted(goal_locations):
        print "    (IS-GOAL loc-%s)" % location
    for location in sorted(locations - goal_locations):
        print "    (IS-NONGOAL loc-%s)" % location

    for left_pos, right_pos in sorted(asp_instance["right"].tuples()):
        print "    (MOVE-DIR loc-%s loc-%s dir-right)" % (left_pos, right_pos)
        print "    (MOVE-DIR loc-%s loc-%s dir-left)" % (right_pos, left_pos)

    for up_pos, down_pos in sorted(asp_instance["top"].tuples()):
        print "    (MOVE-DIR loc-%s loc-%s dir-up)" % (up_pos, down_pos)
        print "    (MOVE-DIR loc-%s loc-%s dir-down)" % (down_pos, up_pos)

    player_location, = asp_instance["sokoban"].objects()
    print "    (at player-01 loc-%s)" % player_location

    for box_location in sorted(box_locations):
        print "    (at box-%s loc-%s)" % (box_location, box_location)

    for location in sorted(locations - box_locations - set([player_location])):
        print "    (clear loc-%s)" % location

    print "    (= (total-cost) 0)"
    print "  )"
    print "  (:goal (and"

    for box_location in sorted(box_locations):
        print "    (at-goal box-%s)" % box_location

    print "  ))"
    print "  (:metric minimize (total-cost))"
    print ")"
Пример #2
0
def convert_hanoi_instance(filename):
    asp_instance = asp_fact_parser.parse(filename)
   
    time_bound = len(asp_instance["time"].objects()) - 1    
    
    disks = sorted(map(disk_to_num, asp_instance["disk"].objects()))
    num_disks = len(disks) - NUM_PEGS

    print "(define (problem hanoi-problem)"
    print "  (:domain hanoi)"
    print "  (:objects "
    for disk in disks:
        print "d" + str(disk),
    print ""
    print "  )"
    print ""
      
    print "  (:init "
    for p in xrange(1, NUM_PEGS + 1):
        for d in xrange(1, num_disks + 1):
            print "(smaller d" + str(p) + " d" + str(d+NUM_PEGS) + ")",
        print ""
    print ""
    for d1 in xrange(1, num_disks + 1):
        for d2 in xrange(d1 + 1, num_disks + 1):
            print "(smaller d" + str(d1+NUM_PEGS) + " d" + str(d2+NUM_PEGS) + ")",
        print ""
    print ""
    
    clear = set(disks)
    for tup in asp_instance["on0"].tuples():
        first = disk_to_num(tup[0])
        second = disk_to_num(tup[1])
        clear.remove(second)
        print "(on d" + str(first) + " d" + str(second) + ")"
    for disk in clear:
        print "(clear d" + str(disk) + ")"

    print "  )"
    print ""
    print "  (:goal (and "

    for tup in asp_instance["ongoal"].tuples():
        first = disk_to_num(tup[0])
        second = disk_to_num(tup[1])
        print "(on d" + str(first) + " d" + str(second) + ")"

    print "  ))"
    print ""
    print ")"
def convert_sokoban_instance(filename):
    asp_instance = asp_fact_parser.parse(filename)

    print "(define (problem solitaire-problem)"
    print "  (:domain pegsolitaire-sequential)"
    print "  (:objects"
    
    locations = sorted(asp_instance["full"].tuples() + asp_instance["empty"].tuples())
    
    for loc1, loc2 in locations:
        print "    pos-%s-%s - location" % (loc1,loc2)
    
    print "    t0 - time-step"
    for time in sorted(asp_instance["time"].objects(), key=int):
        print "    t%s - time-step" % time
    
    print "  )"
    print "  (:init"
    print "    (at-time t0)"
    print "    (= (total-cost) 0)"
    
    for time in range(len(asp_instance["time"].objects())):
        print "    (consecutive t%s t%s)" % (time, time+1)
    
    for loc1,loc2 in asp_instance["full"].tuples():
        print "    (occupied pos-%s-%s)" % (loc1,loc2)
    
    for loc1,loc2 in asp_instance["empty"].tuples():
        print "    (free pos-%s-%s)" % (loc1,loc2)
    
    max_height = int(max(locations)[0])
    max_width = max(int(location[1]) for location in locations)
    
    for index_height in range(max_height)[1:]:
        for index_width in range(max_width)[1:]:
            if [str(index_height-1),str(index_width)] in locations and [str(index_height),str(index_width)] in locations and [str(index_height+1),str(index_width)] in locations:
                print "    (IN-LINE pos-%s-%s pos-%s-%s pos-%s-%s)" % (index_height-1,index_width,index_height,index_width,index_height+1,index_width)
                print "    (IN-LINE pos-%s-%s pos-%s-%s pos-%s-%s)" % (index_height+1,index_width,index_height,index_width,index_height-1,index_width)
            if [str(index_height),str(index_width-1)] in locations and [str(index_height),str(index_width)] in locations and [str(index_height),str(index_width+1)] in locations:
                print "    (IN-LINE pos-%s-%s pos-%s-%s pos-%s-%s)" % (index_height,index_width-1,index_height,index_width,index_height,index_width+1)
                print "    (IN-LINE pos-%s-%s pos-%s-%s pos-%s-%s)" % (index_height,index_width+1,index_height,index_width,index_height,index_width-1)
    
    print "  )"
    print "  (:goal (at-time t%s))" % len(asp_instance["time"].objects())
    print "  (:metric minimize (total-cost))"
    print ")"
def convert_hydraulic_leaking_instance(filename):
    asp_instance = asp_fact_parser.parse(filename)

    print "(define (problem hydraulic-leaking-problem)"
    print "  (:domain hydraulic-leaking)"
    print "  (:objects"

    for tank in sorted(asp_instance["tank"].objects()):
        print "    %s - tank" % tank
    for jet in sorted(asp_instance["jet"].objects()):
        print "    %s - jet" % jet
    for junction in sorted(asp_instance["junction"].objects()):
        print "    %s - junction" % junction
    for valve in sorted(asp_instance["valve"].objects()):
        print "    %s - valve" % valve

    print "  )"
    print "  (:init"

    for linked in sorted(asp_instance["link"].tuples()):
        print "    (link %s %s %s)" % (linked[0], linked[1], linked[2])

    for valve in sorted(asp_instance["leaking"].objects()):
        print "    (leaking %s)" % valve

    for tank in sorted(asp_instance["full"].objects()):
        print "    (full %s)" % tank
        print "    (pressurized %s)" % tank

    print "    (= (total-cost) 0)"
    print "  )"
    print "  (:goal (and"

    for goal in sorted(asp_instance["goal"].objects()):
        print "    (pressurized %s)" % goal

    print "  ))"
    print "  (:metric minimize (total-cost))"
    print ")"
Пример #5
0
def convert_knight_instance(filename):
    asp_instance = asp_fact_parser.parse(filename)
   
    size = const_to_num(list(asp_instance["size"].objects())[0])
    
    given_moves = set(map(lambda l: tuple(map(lambda x: const_to_num(x) - 1, l)),  asp_instance["givenmove"].tuples()))
    if len(given_moves) == 0: # If there are no given moves, we might as well start with this move
        given_moves.add( (1,1,2,3) )
    
    
    given_to = {}
    given_from = {}
    
    for (x1, y1, x2, y2) in given_moves:
        given_from[(x1,y1)] = (x2,y2)
        given_to[(x2,y2)] = (x1,y1)
    
    start_pos = given_from.keys()[0]
    
    visited = {}
    did = {}
    
    print "begin_metric"
    print "0"
    print "end_metric"
    print "begin_variables"
    print 2 + (size * size) + len(given_moves)
    print "posX",size,-1
    print "posY",size,-1
    v = 2
    for x in xrange(size):
        for y in xrange(size):
            print "visited_" + str(x) + "_" + str(y),2,-1
            visited[(x,y)] = v
            v = v + 1
    for (m1,m2,m3,m4) in given_moves:
        print "didmove_" + str(m1) + "_" + str(m2) + "_" + str(m3) + "_" + str(m4),2,-1
        did[(m1,m2,m3,m4)] = v
        v = v + 1
    print "end_variables"
    
    print "begin_state"
    print start_pos[0]
    print start_pos[1]
    for x in xrange(size):
        for y in xrange(size):
            print 0
    for (m1,m2,m3,m4) in given_moves:
        print 0
    print "end_state"
    
    print "begin_goal"
    print 2 + (size * size) + len(given_moves)
    print 0, start_pos[0]
    print 1, start_pos[1]
    for var_num in xrange((size * size) + len(given_moves)):
        print var_num + 2, 1
    print "end_goal"
    
    print "NUM_OPS"    
    
    for x1 in xrange(size):
        for y1 in xrange(size):
            for dx,dy in [(1,2),(1,-2),(-1,2),(-1,-2),(2,1),(2,-1),(-2,1),(-2,-1)]:
                x2 = x1 + dx
                y2 = y1 + dy
                if (0 <= x2 < size) and (0 <= y2 < size) and ( (not (x1,y1) in given_from and not (x2,y2) in given_to) or (x1,y1,x2,y2) in given_moves):
                    print "begin_operator"
                    print "move",x1,y1,x2,y2
                    print 0
                    if (x1,y1,x2,y2) in given_moves:
                        print 4
                    else:
                        print 3
                    print 0,0,x1,x2
                    print 0,1,y1,y2
                    print 0,visited[(x2,y2)],0,1
                    if (x1,y1,x2,y2) in given_moves:
                        print 0, did[(x1,y1,x2,y2)], -1, 1
                    print 0
                    print "end_operator"
                    
    print 0                
Пример #6
0
def convert_sokoban_instance(filename):
    asp_instance = asp_fact_parser.parse(filename)

    print "(define (problem airport-problem)"
    print "  (:domain pseudo-airport)"
    print "  (:objects"
    
    for location in asp_instance["location"].objects():
        print "    loc-%s - location" % location
    
    for passenger in asp_instance["passenger"].objects():
        print "    %s - passenger" % passenger
    
    for vehicle in asp_instance["vehicle"].tuples():
        print "    %s - vehicle" % vehicle[0]
    
    max_distance = max(int(driveway[2]) for driveway in asp_instance["driveway"].tuples())
    print "    dist-0 - distance"
    for index_distance in range(max_distance):
        print "    dist-%s - distance" % str(index_distance+1)
    
    max_overall_fuel = max(int(vehicle[1]) for vehicle in asp_instance["vehicle"].tuples())
    print "    fuel-0 - gas_level"
    for index_fuel in range(max_overall_fuel):
        print "    fuel-%s - gas_level" % str(index_fuel+1)
    
    print "  )"
    print "  (:init"
    
    for gas in asp_instance["gasstation"].objects():
        print "    (gas loc-%s)" % gas
    
    for driveway in asp_instance["driveway"].tuples():
        print "    (driveway loc-%s loc-%s dist-%s)" % (driveway[0],driveway[1],driveway[2])
        print "    (driveway loc-%s loc-%s dist-%s)" % (driveway[1],driveway[0],driveway[2])
        
    for vehicle in asp_instance["vehicle"].tuples():
        print "    (max_fuel %s fuel-%s)" % (vehicle[0], vehicle[1])
        
    for init_gas in asp_instance["init_gas"].tuples():
        print "    (current_fuel %s fuel-%s)" % (init_gas[0], init_gas[1])
        
    for init_at in asp_instance["init_at"].tuples():
        print "    (at %s loc-%s)" % (init_at[0], init_at[1])
        
    distances = [int(driveway[2]) for driveway in asp_instance["driveway"].tuples()]
    for index_distance in range(max_distance+1)[1:]:
        if(index_distance in distances):
            for index_fuel in range(max_overall_fuel+1):
                if index_fuel >= index_distance:
                    print "    (fuel_lost dist-%s fuel-%s fuel-%s)" % (index_distance, index_fuel, str(index_fuel-index_distance))
        
    
    print "  )"
    print "  (:goal (and "
    
    airports = asp_instance["airport"].tuples()
    for init_at in asp_instance["init_at"].tuples():
        if init_at[0][0] == "p":
            if init_at[1] == airports[0][0]:
                print "    (at %s loc-%s)" % (init_at[0], airports[1][0])
            else:
                print "    (at %s loc-%s)" % (init_at[0], airports[0][0])    
    
    print "  )  )\n)"