Example #1
0
def constantsInDomain():
    all_actions, init_atoms, goal_atoms = info_from_file(FILE_NAME)
    constants_in_domain = []
    length_init = len(init_atoms)
    for i in range(0, length_init):
        atom = init_atoms[i]
        if "," not in atom:
            for j in range(atom.index('(') + 1, atom.index(')')):
                letter = atom[j]
                if letter not in constants_in_domain and letter.isupper():
                    constants_in_domain.append(letter)
        else:
            letter_before_comma = ""
            for j in range(atom.index('(') + 1, atom.index(',')):
                startswith = atom[(atom.index('(') + 1)]
                letter_before_comma += atom[j]
            if letter_before_comma not in constants_in_domain and startswith.isupper(
            ):
                constants_in_domain.append(letter_before_comma)
            letter_after_comma = ""
            for j in range(atom.index(',') + 1, atom.index(')')):
                startswith = atom[(atom.index(',') + 1)]
                letter_after_comma += atom[j]
            if letter_after_comma not in constants_in_domain and startswith.isupper(
            ):
                constants_in_domain.append(letter_after_comma)
    return constants_in_domain
Example #2
0
def encodingHandler(file_name):
        global FILE_NAME
        FILE_NAME = file_name
        setFileName(FILE_NAME) #defines file_name in hebrand base

        atoms_to_numbers_dict, numbers_to_atoms_dict = createConversionDicts()
        hebrand_base = hebrandBase()
        action_schemas, init_state, goal_state = info_from_file(FILE_NAME)
        all_action_combinations = allVariationsOfActionNames(action_schemas)
        print('All action combinations: ', all_action_combinations)

        initial_state_CNF = createInitialStateCnfSentence(atoms_to_numbers_dict, init_state, all_action_combinations)
        goal_state_CNF = createGoalStateCnfSentence(atoms_to_numbers_dict, goal_state, '0')
        print('Initial state CNF is: ', initial_state_CNF)
        print('Goal states: ', goal_state_CNF)
        print('Length of initial CNF is: ', len(initial_state_CNF))

        at_most_one_axioms_CNF, at_least_one_axioms_CNF = extendOnlyOneActionAxioms(atoms_to_numbers_dict, all_action_combinations, '0')
        print('At-most-one axioms: ', at_most_one_axioms_CNF)
        print('At-least-one axioms: ', at_least_one_axioms_CNF)

        sat_sentence = []
        sat_sentence.extend(initial_state_CNF)
        sat_sentence.extend(goal_state_CNF)
        sat_sentence.extend(at_most_one_axioms_CNF)
        sat_sentence.extend(at_least_one_axioms_CNF)

        print('First sat-sentence is: ', sat_sentence)
Example #3
0
def groundAtoms_init_goal(
):  #finding the ground atoms defined in the init and goal lines
    all_actions, init_atoms, goal_atoms = info_from_file(FILE_NAME)
    num_of_actions = len(all_actions)
    list_of_atoms = []
    for i in range(0, num_of_actions):
        list_of_atoms += (init_atoms + goal_atoms)
    grounded_atoms = set(list_of_atoms)  #(without duplicates)
    return grounded_atoms
Example #4
0
def createConversionDicts(
):  ##conversion dicts storing the relationship between atom and the assigned number (and vice-versa)
    action_schemas, init_state, goal_state = info_from_file(FILE_NAME)
    actions = allVariationsOfActionNames(action_schemas)
    action_names = []
    for action in range(len(actions)):
        action_names.append(actions[action].name + '0')

    hebrand_base_set = hebrandBase()
    hebrand_base_list = list(hebrand_base_set)
    for i in range(0, len(hebrand_base_list)):
        hebrand_base_list[i] = hebrand_base_list[i] + '0'
    hebrand_base_dict = dict.fromkeys(hebrand_base_list, 0)
    for i in range(0, len(action_names)):
        hebrand_base_dict[action_names[i]] = 0
    value = 1
    #print('This is Hebrand_Base_Dict: ', hebrand_base_dict)
    #print('This is the length of hebrand_base_dict:', len(hebrand_base_dict))
    atoms_to_numbers_dict = {}
    numbers_to_atoms_dict = {}
    for key, val in hebrand_base_dict.items():
        val1 = str(value)
        val2 = '-' + str(value)
        if key.startswith('-'):
            key1 = key
            key2 = key[1:]
            if ((key1 not in atoms_to_numbers_dict)
                    or (key2 not in atoms_to_numbers_dict)):
                atoms_to_numbers_dict[key2] = val1
                atoms_to_numbers_dict[key1] = val2
                numbers_to_atoms_dict[val1] = key2
                numbers_to_atoms_dict[val2] = key1
                value += 1
        else:
            key1 = key
            key2 = '-' + key
            if ((key1 not in atoms_to_numbers_dict)
                    or (key2 not in atoms_to_numbers_dict)):
                atoms_to_numbers_dict[key1] = val1
                atoms_to_numbers_dict[key2] = val2
                numbers_to_atoms_dict[val1] = key1
                numbers_to_atoms_dict[val2] = key2
                value += 1
    #print('SAT_DICTIONARY: ', atoms_to_numbers_dict)
    #print('LENGTH OF ATOMS_TO_NUMBERS_DICT: ', len(atoms_to_numbers_dict))
    #print('NUM_DiCTIONARY: ', numbers_to_atoms_dict)

    return atoms_to_numbers_dict, numbers_to_atoms_dict
Example #5
0
def groundActions():
    all_actions, init_atoms, goal_atoms = info_from_file(FILE_NAME)
    grounded_actions = set()
    num_of_actions = len(all_actions)

    for n in range(0, num_of_actions):  #Go through each action
        name_of_preconds_and_effects = []
        preconds = all_actions[n].preconds
        one_precondition = []
        for i in range(0, len(preconds)):  #go through each precondition
            one_precondition += preconds[i]
            name = []  # list for creating name for new precondtion
            for j in range(0, one_precondition.index('(')):
                name += one_precondition[j]
            #print('List of names: ', name_of_preconds_and_effects)
            #print('Name: ', name)
            if name not in name_of_preconds_and_effects:  # Excluding already excisting names (hoping its not possible to get on(a,b) AND on(a,b,c))
                name_of_preconds_and_effects.append(
                    name)  #Add name to list (eks. on, clear,..)
                variable_list = []
                for j in range(
                        one_precondition.index('(') + 1,
                        one_precondition.index(
                            ')')):  #go through elements between parentheses
                    if one_precondition[j].isalpha():
                        variable_list.append(
                            one_precondition[j]
                        )  #Add all letters between the parantheses to a list
                howManyCharacters = len(
                    variable_list)  #how many letters do we have
                list_of_combinations = allCombinations(
                    howManyCharacters
                )  #get all the combinations of the constants in the domain
                #print('LIST OF COMBINATIONS: ', list_of_combinations)
                howManyCombinations = len(
                    list_of_combinations)  #How many combinations do we get
                for j in range(
                        0, howManyCombinations):  #go through each combination
                    for k in range(
                            0, len(name_of_preconds_and_effects)
                    ):  #go through all the different names we have
                        create_new_precond = []
                        for l in range(0,
                                       len(name_of_preconds_and_effects[k])):
                            create_new_precond.append(
                                name_of_preconds_and_effects[k]
                                [l])  #add the name to a new precond
                        if '(' not in create_new_precond:
                            create_new_precond.append(
                                '('
                            )  #add the start parenthese to a new precond
                        for m in range(0, len(list_of_combinations[j])):
                            create_new_precond.append(
                                list_of_combinations[j][m]
                            )  #add the different combinations with a comma between
                            if m != (len(list_of_combinations[j]) - 1):
                                create_new_precond.append(',')
                        create_new_precond.append(
                            ')')  #Add the end paranthese to the new precond
                        joined_create_new_precond = "".join(create_new_precond)
                        if joined_create_new_precond not in grounded_actions:
                            grounded_actions.add(joined_create_new_precond)
            one_precondition = []
        effects = all_actions[n].effects
        one_effect = []
        for i in range(
                0, len(effects)
        ):  #same as above, just for effects in stead of preconds
            one_effect += effects[i]
            name = []
            for j in range(0, one_effect.index('(')):
                name += one_effect[j]
            if name not in name_of_preconds_and_effects:
                name_of_preconds_and_effects.append(name)
                variable_list = []
                for j in range(
                        one_effect.index('(') + 1, one_effect.index(')')):
                    if one_effect[j].isalpha():
                        variable_list.append(one_effect[j])
                howManyCharacters = len(variable_list)
                list_of_combinations = allCombinations(howManyCharacters)
                howManyCombinations = len(list_of_combinations)
                for j in range(0, howManyCombinations):
                    for k in range(0, len(name_of_preconds_and_effects)):
                        create_new_effect = []
                        for l in range(0,
                                       len(name_of_preconds_and_effects[k])):
                            create_new_effect.append(
                                name_of_preconds_and_effects[k][l])
                        if '(' not in create_new_effect:
                            create_new_effect.append('(')
                        for m in range(0, len(list_of_combinations[j])):
                            create_new_effect.append(
                                list_of_combinations[j][m])
                            if m != (len(list_of_combinations[j]) - 1):
                                create_new_effect.append(',')
                        create_new_effect.append(')')
                        joined_create_new_effect = "".join(create_new_effect)
                        if joined_create_new_effect not in grounded_actions:
                            grounded_actions.add(joined_create_new_effect)
            one_effect = []
    return grounded_actions
Example #6
0
def actionNames():
    all_actions, init_atoms, goal_atoms = info_from_file(FILE_NAME)
    names = []
    for i in range(0, len(all_actions)):
        names.append(all_actions[i].name)
    return names
Example #7
0
def encodingHandler(file_name):
    global FILE_NAME
    FILE_NAME = file_name
    setFileName(FILE_NAME)  #defines file_name in hebrand base

    atoms_to_numbers_dict, numbers_to_atoms_dict = createConversionDicts(
    )  #conversion dicts storing the relationship between atom and the assigned number (and vice-versa)
    hebrand_base = hebrandBase()
    action_schemas, init_state, goal_state = info_from_file(FILE_NAME)
    all_action_combinations = allVariationsOfActionNames(action_schemas)
    print('All action combinations: ', all_action_combinations)

    #The following code creates the SAT-sentence for horizon = 0 ---------------------------------------------------------------------------
    initial_state_CNF = createInitialStateCnfSentence(atoms_to_numbers_dict,
                                                      init_state,
                                                      all_action_combinations)
    goal_state_CNF = createGoalStateCnfSentence(atoms_to_numbers_dict,
                                                goal_state, '0')
    print('Initial state CNF is: ', initial_state_CNF)
    print('Goal states: ', goal_state_CNF)
    print('Length of initial CNF is: ', len(initial_state_CNF))

    at_most_one_axioms_CNF, at_least_one_axioms_CNF = extendOnlyOneActionAxioms(
        atoms_to_numbers_dict, all_action_combinations, '0')
    #print('At-most-one axioms: ', at_most_one_axioms_CNF)
    #print('At-least-one axioms: ', at_least_one_axioms_CNF)

    sat_sentence = []
    sat_sentence.extend(initial_state_CNF)
    sat_sentence.extend(goal_state_CNF)
    sat_sentence.extend(at_most_one_axioms_CNF)
    sat_sentence.extend(at_least_one_axioms_CNF)

    print('First sat-sentence is: ', sat_sentence)
    nbvar = (len(atoms_to_numbers_dict)) / 2
    nbclauses = len(sat_sentence)

    satisfiability, polarity_of_literals = dpllHandlerWithoutReadingDimacs(
        sat_sentence, nbvar, nbclauses)

    #this is where the SATPLAN idea comes into play by expanding the horizon with 1 until a solution is found
    horizon = 1
    while not satisfiability:
        temp_extended_atoms_dict, temp_extended_numbers_dict = extendConversionDicts(
            horizon, atoms_to_numbers_dict, numbers_to_atoms_dict)
        goal_state_CNF = createGoalStateCnfSentence(temp_extended_atoms_dict,
                                                    goal_state, horizon)
        actions_CNF = extendActions(temp_extended_atoms_dict,
                                    all_action_combinations, horizon)
        at_most_one_axioms_CNF, at_least_one_axioms_CNF = extendOnlyOneActionAxioms(
            temp_extended_atoms_dict, all_action_combinations, horizon)

        sat_sentence.extend(
            goal_state_CNF
        )  #this is wrong, we need to remove the old goal_states first
        sat_sentence.extend(at_most_one_axioms_CNF)
        sat_sentence.extend(at_least_one_axioms_CNF)
        sat_sentence.extend(actions_CNF)

        nbvar = (len(temp_extended_atoms_dict) / 2)
        nbclauses = len(sat_sentence)

        satisfiability, polarity_of_literals = dpllHandlerWithoutReadingDimacs(
            sat_sentence, nbvar, nbclauses)

        horizon += 1

    print('A solution was found after ', horizon - 1, ' steps!!')