def checkMathPredicates(name, atom): args = get_args(atom) if is_variable(args[0]) or is_variable(args[1]): return False a = get_value(args[0]) b = get_value(args[1]) if name == 'isBigger': return a >= b elif name == 'equal': return a == b elif name == 'sum': if is_constant(args[2]) and (a + b == get_value(args[2])): return True elif is_variable(args[2]): args[2] = substitute(args[2], {get_name(args[2]): make_const(a + b)}) return True elif name == 'dif': if is_constant(args[2]) and (a - b == get_value(args[2])): return True elif is_variable(args[2]): args[2] = substitute(args[2], {get_name(args[2]): make_const(a - b)}) return True return False
def constructAction(availableActions, action, index): newElem = action[0] + '(' for arg in action[1]: arg = substitute(arg, availableActions[action[0]][index][0]) newElem = newElem + str(get_value(arg)) + ',' newElem = newElem[:-1] + ')' return newElem
def findAllSubstitutions(action, atoms, subst): if not atoms: print(subst) applyLE(action, subst) applyLA(action, subst) return atom = atoms[0] name = get_head(atom) if name in env: facts = env[name] elif name in math_predicates: return findAllSubstitutions(action, atoms[1:], subst) else: facts = state for fact in facts: newSubst = unify(atom, fact) if newSubst != False and newSubst != None: copySubst = deepcopy(subst) copySubst.update(newSubst) remaining = deepcopy(atoms[1:]) for i in range(len(remaining)): remaining[i] = substitute(remaining[i], newSubst) findAllSubstitutions(action, remaining, copySubst)
def findAvailableActions(state): availableActions = {} for action in actions: allSubst = [] findSubstitutions(action, action[2], {}, allSubst, state) if not allSubst: continue for subst in allSubst: repetitive = [] for i in range(len(action[3])): repetitive.append(substitute(action[3][i], subst)) allSubstRepetitive = [] res = findSubstitutions(action, repetitive, {}, allSubstRepetitive, state) #print("repetitive subst " + str(allSubstRepetitive)) #print("are all unifing " + str(res)) if res == False: continue if action[0] in availableActions: availableActions[action[0]].append((subst, allSubstRepetitive)) else: availableActions[action[0]] = [(subst, allSubstRepetitive)] return availableActions
def applyLA(action, substitution): global newState LA = deepcopy(action[4]) subst = substitution[0] for i in range(len(LA)): LA[i] = substitute(LA[i], subst) name = get_head(LA[i]) if not substitution[1] and name not in env and name not in math_predicates: if LA[i] not in newState: newState.append(LA[i]) for subst in substitution[1]: for a in LA: a = substitute(a, subst) name = get_head(a) if name not in env and name not in math_predicates: if a not in newState: newState.append(a)
def applyLA(action, subst): global newState LA = action[3] for a in LA: a = substitute(a, subst) name = get_head(a) if name not in env and name not in math_predicates: if a not in newState: newState.append(a)
def applyLE(action, subst): global newState LE = action[4] for e in LE: e = substitute(e, subst) name = get_head(e) if e in newState: #print("found atom to remove " + str(e)) newState.remove(e)
def applyLE(action, substitution): global newState LE = deepcopy(action[5]) subst = substitution[0] for i in range(len(LE)): LE[i] = substitute(LE[i], subst) name = get_head(LE[i]) if name == 'isDirty': env['isDirty'].remove(LE[i]) if LE[i] in newState: #print("found atom to remove " + str(e)) newState.remove(LE[i]) for subst in substitution[1]: for e in LE: e = substitute(e, subst) name = get_head(e) if e in newState: #print("found atom to remove " + str(e)) newState.remove(e)
def forward(path): if len(env['isDirty']) == 0 or time <= 0: return path #search actions with satisfied preconditions availableActions = {} for action in actions: allSubst = [] findSubstitutions(action, action[2], {}, allSubst) if not allSubst: continue #print("basic substs " + str(allSubst)) for subst in allSubst: repetitive = [] for i in range(len(action[3])): repetitive.append(substitute(action[3][i], subst)) allSubstRepetitive = [] res = findSubstitutions(action, repetitive, {}, allSubstRepetitive) #print("repetitive subst " + str(allSubstRepetitive)) #print("are all unifing " + str(res)) if res == False: continue if action[0] in availableActions: availableActions[action[0]].append((subst, allSubstRepetitive)) else: availableActions[action[0]] = [(subst, allSubstRepetitive)] #print("AVAILABLE ACTIONS") #print(availableActions.keys()) #print(availableActions) if len(availableActions) == 0: return path #choose best action #TODO [actionName, index] = findBestAction(availableActions, path) #TODO should be removed if actionName == None: return path if time <= 0: return path A = [a for a in actions if a[0] == actionName][0] #print("APPLY ACTION " + actionName) #print("APPLY SUBST " + str(availableActions[actionName][index])) applyAction(A, availableActions[actionName][index]) path.append(constructAction(availableActions, A, index)) return forward(path)
def preconditionsSatisfied(atoms): atom = atoms[0] #print("checking " + str(atom)) name = get_head(atom) if name in env: #print("NAME IN ENV") facts = env[name] elif name in math_predicates: res = checkMathPredicates(name, atom) if res == True and len(atoms) == 1: return True elif res == True: return preconditionsSatisfied(atoms[1:]) else: return False else: facts = state #print(facts) hasUnified = False for fact in facts: s = unify(atom, fact) #print("with " + str(fact) + " unify is " + str(s)) if s != False and s != None: hasUnified = True #print("unify " + str(atom) + " with " + str(fact) + " under ") #print(s) if len(atoms) == 1: return True remaining = deepcopy(atoms[1:]) for i in range(len(remaining)): remaining[i] = substitute(remaining[i], s) #print("remaining " + str(remaining)) res = preconditionsSatisfied(remaining) if res == False: return False if hasUnified: return True return False
def findSubstitutions(action, atoms, subst, allSubst, state): if not atoms: #print("found subst " + str(subst)) if len(subst) != 0: allSubst.append(subst) return True atom = atoms[0] name = get_head(atom) if name in env: facts = env[name] elif name in math_predicates: newSubst = {} res = checkMathPredicates(name, atom, newSubst) if res == False: return False else: if newSubst != {}: #TODO update the remaining with the newSubst? subst.update(newSubst) return findSubstitutions(action, atoms[1:], subst, allSubst, state) else: facts = state hasUnifiedWithAll = True for fact in facts: #TODO pot sa aplic newSubst in unify si sa nu mai fac remaining? newSubst = unify(atom, fact) if newSubst != False and newSubst != None: copySubst = deepcopy(subst) copySubst.update(newSubst) remaining = deepcopy(atoms[1:]) for i in range(len(remaining)): remaining[i] = substitute(remaining[i], newSubst) res = findSubstitutions(action, remaining, copySubst, allSubst, state) if not res: hasUnifiedWithAll = False return hasUnifiedWithAll
def apply_rule(rule, facts): print(facts) resulting_facts = [] premises = list(map(lambda x: get_args(x)[0], get_premises(rule))) print premises substitutions = [] for premise in premises: print('*** premise ***') print(premise) print('*** end premise ***') new_substitutions = [] for fact in facts: print('*** premise ***') print(fact) print('*** end premise ***') res = unify(premise, fact) if res: substitutions.append(res) for substitution in substitutions: res = unify(premise, fact, substitution) if res: new_substitutions.append(res) for new_subst in new_substitutions: substitutions.append(new_subst) print(substitutions) for subst in substitutions: res = substitute(get_conclusion(rule), subst) if res and res not in resulting_facts: resulting_facts.append(res) return resulting_facts
def apply_rule(rule, facts): # TODO subst = {} prem = {} ans = [] values = {} fact_values = {} for fact in facts: vals = [] for arg in get_args(fact): vals.append(get_value(arg)) if get_head(fact) not in values: values[get_head(fact)] = [tuple(vals)] else: values[get_head(fact)].append(tuple(vals)) if get_head(fact) not in fact_values: fact_values[get_head(fact)] = [fact] else: fact_values[get_head(fact)].append(fact) for r in get_premises(rule): if isinstance(r, Sentence): var = get_name(get_args(get_args(r)[0])[0]) prm = get_head(get_args(r)[0]) prem[(prm, var)] = get_args(r)[0] continue var = get_name(get_args(r)[0]) prem[(get_head(r), var)] = [r] index_h = {} max_index_h = {} for (pred, var) in prem: index_h[(pred, var)] = 0 max_index_h[(pred, var)] = 0 if pred in values: max_index_h[(pred, var)] = len(values[pred]) pred = [] for x in prem: pred.append(x) current_p = 0 subst = {} while 1: p = pred[current_p] prd = p[0] val = p[1] p_index = index_h[p] if p_index == max_index_h[p]: index_h[p] = 0 if current_p == 0: break current_p = current_p - 1 p = pred[current_p] for x in get_args(prem[p]): name = get_name(x) if name in subst: del subst[name] index_h[p] = index_h[p] + 1 continue b_subst = deepcopy(subst) aux_subst = unify(prem[p], fact_values[prd][p_index], subst) if aux_subst!=False: subst = aux_subst if current_p == len(pred) - 1: ans.append(substitute(get_conclusion(rule),subst)) subst = {} current_p = 0 next_index = index_h[p] + 1 index_h[p] = next_index p = pred[current_p] continue else: subst = b_subst index_h[p] = index_h[p] + 1 if index_h[p] == max_index_h[p]: index_h[p] = 0 current_p = current_p - 1 index_h[pred[current_p]] = index_h[pred[current_p]] + 1 p = pred[current_p] for x in get_args(prem[p]): name = get_name(x) if name in subst: del subst[name] continue if current_p < len(pred) - 1: current_p = current_p + 1 continue index_h[p] = index_h[p] + 1 return ans