コード例 #1
0
ファイル: abduction.py プロジェクト: sahil02235/symbolic-rl
def add_start_state(start_state):
    '''
    Add the current state of the agent to ASP program
    '''
    start_state = "%AAA\n" + "state_at((" + str(int(
        start_state[0])) + ", " + str(int(
            start_state[1])) + "), 1).\n" + "%BBB\n"
    helper.append_to_file(start_state, cf.CLINGOFILE)
コード例 #2
0
def generate_pos(hypothesis, previous_state, next_state, action, wall_list,
                 cell_range):
    '''
    Generate a pos in the exploration phase

    Output: #pos({state_after((3,6))}, {state_after((4,6)), ...}, {state_before((3,6)). action(non). }).
    '''

    walls = add_surrounding_walls(int(previous_state[0]),
                                  int(previous_state[1]), wall_list)

    state_after_str = "state_after((" + str(int(next_state[0])) + "," + str(
        int(next_state[1])) + "))"
    state_before_str = "state_before((" + str(int(
        previous_state[0])) + "," + str(int(previous_state[1])) + "))."
    action_str = "action(" + action + ")."

    all_walls = get_seen_walls(cf.CLINGOFILE)

    # If the agent moved to a cell other than adjacent, there must be a link
    link_detected, exclusions = get_exclusions(previous_state, next_state)

    all_exclusions = exclusions

    sub_exclusion = ""
    inclusion = ""
    if link_detected:
        predict_x, predict_y, link = get_link(previous_state, next_state,
                                              action)

    if link_detected and (not cf.ALREADY_LINK):
        helper.append_to_file(link + "\n", cf.CLINGOFILE)
        helper.append_to_file(link + "\n", cf.LASFILE)
        cf.ALREADY_LINK = link

    inclusion, sub_exclusion = get_inc_exc(hypothesis, state_before_str,
                                           state_after_str, action_str,
                                           all_walls, cell_range,
                                           cf.ALREADY_LINK)

    if sub_exclusion != "":
        all_exclusions = all_exclusions + "," + sub_exclusion

    if link_detected:
        # TODO double-check if the exclusions for pos1 is fine
        pos1 = "#pos({" + "state_after(({},{}))".format(
            predict_x, predict_y
        ) + "}, {" "}, {" + state_before_str + action_str + walls + "})."
        pos2 = "#pos({" + inclusion + "}, {" + all_exclusions + "}, {" + "state_before(({},{})).".format(
            predict_x, predict_y) + action_str + walls + "})."
        helper.append_to_file(pos1 + "\n", cf.LASFILE)
        helper.append_to_file(pos2 + "\n", cf.LASFILE)
        return pos1, pos2, link

    pos = "#pos({" + inclusion + "}, {" + all_exclusions + "}, {" + state_before_str + action_str + walls + "})."
    helper.append_to_file(pos + "\n", cf.LASFILE)
    return pos, None, None
コード例 #3
0
ファイル: abduction.py プロジェクト: sahil02235/symbolic-rl
def add_goal_state(goal_state):
    '''
    Add goal spec to ASP program
    '''
    goal_state = "state_at((" + str(int(goal_state[0])) + ", " + str(
        int(goal_state[1])) + "), T),"
    goal = "finished(T):- goal(T2), time(T), T >= T2.\n goal(T):- " + goal_state + " not finished(T-1).\n" + \
    "goalMet:- goal(T).\n:- not goalMet.\n"
    helper.append_to_file(goal, cf.CLINGOFILE)
コード例 #4
0
ファイル: abduction.py プロジェクト: sahil02235/symbolic-rl
def make_lp_base(cell_range):
    '''
    Collect all info necessary to run clingo and send them to "cf.CLINGOFILE"
    '''
    actions = "1{action(down, T); action(up, T); action(right, T); action(left, T)}1 :- time(T), not finished(T).\n"
    show = "#show state_at/2.\n #show action/2.\n"
    time = "%CCC\n" + "time(0.." + str(cf.TIME_RANGE) + ").\n" + "%DDD\n"
    minimize = "#minimize{1, X, T: action(X,T)}.\n"

    kb = actions + show + time + cell_range + minimize + cf.ADJACENT
    helper.append_to_file(kb, cf.CLINGOFILE)
コード例 #5
0
ファイル: abduction.py プロジェクト: sahil02235/symbolic-rl
def update_time_range(agent_position, t):
    '''
    Update planning starting point based on the location of the agent
    '''
    # Replace everything between "CCC" and "DDD" in clingo file with a new agent position
    t = "%CCC\n" + "time(" + str(t) + ".." + str(
        cf.TIME_RANGE) + ").\n" + "%DDD\n"
    flag = False
    with open(cf.CLINGOFILE) as f:
        for line in f:
            if line == "%CCC\n":
                flag = True
            if flag == False:
                with open("temp.lp", "a") as newfile:
                    newfile.write(line)
            if line == "%DDD\n":
                flag = False
    os.rename("temp.lp", cf.CLINGOFILE)

    helper.append_to_file(t, cf.CLINGOFILE)
コード例 #6
0
ファイル: abduction.py プロジェクト: sahil02235/symbolic-rl
def update_agent_position(agent_position, t):
    '''
    Update planning starting point based on the location of the agent
    '''
    # Replace everything between "AAA" and "BBB" in clingo file with a new agent position
    start_state = "%AAA\n" + "state_at((" + str(int(
        agent_position[0])) + ", " + str(int(
            agent_position[1])) + "), " + str(t) + ").\n" + "%BBB\n"
    flag = False
    with open(cf.CLINGOFILE) as f:
        for line in f:
            if line == "%AAA\n":
                flag = True
            if flag == False:
                with open("temp.lp", "a") as newfile:
                    newfile.write(line)
            if line == "%BBB\n":
                flag = False
    os.rename("temp.lp", cf.CLINGOFILE)

    helper.append_to_file(start_state, cf.CLINGOFILE)
コード例 #7
0
def check_ILASP_cover(hypothesis, pos, height, width, link):
    '''
    Check hypothesis needs to be refined 
    '''

    if pos == None:
        return True

    helper.silentremove(cf.BASE_DIR, cf.CHECK_LAS)

    output_las = os.path.join(cf.BASE_DIR, cf.CHECK_LAS)
    if link:
        helper.append_to_file(link, output_las)

    helper.append_to_file(hypothesis, output_las)
    helper.append_to_file(pos, output_las)
    copy_las_base(height, width, output_las)

    remove_mode(output_las)
    print("checking ILASP necessity...")
    hypothesis = run_ILASP(output_las)
    if hypothesis == "":
        return True
    else:
        return False
コード例 #8
0
ファイル: abduction.py プロジェクト: sahil02235/symbolic-rl
def add_hypothesis(hypothesis_asp):
    '''
    Add learnt hypothese to ASP program
    '''
    helper.append_to_file("%START\n", cf.CLINGOFILE)
    helper.append_to_file(hypothesis_asp, cf.CLINGOFILE)
    helper.append_to_file("%END\n", cf.CLINGOFILE)
コード例 #9
0
ファイル: abduction.py プロジェクト: sahil02235/symbolic-rl
def add_new_walls(previous_state, wall_list, file):
    '''
    Check the surrounding and see if there is any new walls

    Output: Boolean to tell whether a new wall has been added to B
    '''
    x = int(previous_state[0])
    y = int(previous_state[1])
    if (((x + 1, y) in wall_list) and (is_wall_in_background(
        (x + 1, y), file) == False)):
        wall = "\nwall({}).\n".format((x + 1, y))
        helper.append_to_file(wall, file)
    if (((x, y + 1) in wall_list) and (is_wall_in_background(
        (x, y + 1), file) == False)):
        wall = "\nwall({}).\n".format((x, y + 1))
        helper.append_to_file(wall, file)
    if (((x - 1, y) in wall_list) and (is_wall_in_background(
        (x - 1, y), file) == False)):
        wall = "\nwall({}).\n".format((x - 1, y))
        helper.append_to_file(wall, file)
    if (((x, y - 1) in wall_list) and (is_wall_in_background(
        (x, y - 1), file) == False)):
        wall = "\nwall({}).\n".format((x, y - 1))
        helper.append_to_file(wall, file)
コード例 #10
0
ファイル: abduction.py プロジェクト: sahil02235/symbolic-rl
def update_h(hypothesis):
    '''
    Update planning starting point based on the location of the agent
    '''

    # Replace everything between "START" and "END" in clingo file with a new H
    flag = False
    with open(cf.CLINGOFILE) as f:
        for line in f:
            if line == "%START\n":
                flag = True
            if flag == False:
                with open("temp.lp", "a") as newfile:
                    newfile.write(line)
            if line == "%END\n":
                flag = False
    os.rename("temp.lp", cf.CLINGOFILE)

    helper.append_to_file("%START\n", cf.CLINGOFILE)
    helper.append_to_file(hypothesis, cf.CLINGOFILE)
    helper.append_to_file("%END\n", cf.CLINGOFILE)
コード例 #11
0
ファイル: helper_test.py プロジェクト: sahil02235/symbolic-rl
 def test_append_to_file(self):
     helper.append_to_file("hello", self.__class__.created_file)
     size = os.stat(self.__class__.created_file).st_size
     self.assertEqual(size, 5)
コード例 #12
0
ファイル: helper_test.py プロジェクト: sahil02235/symbolic-rl
 def test_silentmove(self):
     helper.create_file(BASE_DIR, self.__class__.filename)
     helper.append_to_file("hello", self.__class__.created_file)
     helper.silentremove(BASE_DIR, self.__class__.filename)
     self.assertTrue(os.stat(self.__class__.created_file).st_size == 0)
コード例 #13
0
def get_inc_exc(hypothesis,
                state_before,
                state_after,
                action,
                walls,
                cell_range,
                link=None):
    helper.silentremove(cf.BASE_DIR, cf.GROUNDING)

    helper.append_to_file(hypothesis, cf.GROUNDING_DIR)
    helper.append_to_file(state_before + "\n", cf.GROUNDING_DIR)
    helper.append_to_file(action + "\n", cf.GROUNDING_DIR)
    helper.append_to_file(cf.ADJACENT, cf.GROUNDING_DIR)
    helper.append_to_file(cell_range, cf.GROUNDING_DIR)
    show = "#show state_after/1.\n"
    helper.append_to_file(show, cf.GROUNDING_DIR)
    if link:
        helper.append_to_file(link, cf.GROUNDING_DIR)

    for wall in walls:
        wall = "wall(" + str(wall) + ").\n"
        helper.append_to_file(wall, cf.GROUNDING_DIR)
    answer_sets = abduction.run_clingo(cf.GROUNDING_DIR)

    # The current hypothesis DOES predict the agent is there other than state_after,
    # then they are exclusions
    exclusions = ""
    for sa in answer_sets:
        if (state_after != sa):
            exclusions = exclusions + sa + ","

    # The current hypothesis DOES NOT predict the agent is there other than state_after,
    # then it is inclusion
    inclusion = ""
    if (state_after not in answer_sets):
        inclusion = inclusion + state_after

    if exclusions != "":
        return inclusion, exclusions[0:-1]
    else:
        return inclusion, exclusions