def checkMathPredicates(name, atom, subst = None): 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 == 'isSmaller': 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]): subst[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]): subst[get_name(args[2])] = make_const(a - b) return True return False
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 equal_terms(t1, t2): if is_constant(t1) and is_constant(t2): return get_value(t1) == get_value(t2) if is_variable(t1) and is_variable(t2): return get_name(t1) == get_name(t2) if is_function_call(t1) and is_function(t2): if get_head(t1) != get_head(t2): return all([equal_terms(get_args(t1)[i], get_args(t2)[i]) for i in range(len(get_args(t1)))]) 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 findBestAction(availableActions, path): # TODO global time, reward if 'Clean' in availableActions.keys(): cost = get_value(availableActions['Clean'][0][0]['dim']) time = time - cost reward = reward + cost return ['Clean', 0] if 'Refill' in availableActions.keys(): return ['Refill', 0] if 'Move' in availableActions.keys(): #find the lowest cost minCost = 9999 substIndex = None for subst in availableActions['Move']: cost = get_value(subst[0]['cost']) index = availableActions['Move'].index(subst) if len(path) >= 1 and getActionName(path[len(path) - 1]) == 'Move': args = getActionArgs(path[-1]) #print("ARGS " + str(args)) if int(args[0]) == get_value(availableActions['Move'][index][0]['r2']) and len(availableActions['Move']) > 1: continue if cost < minCost: minCost = cost substIndex = index # substIndex = randint(0, len()) # if len(availableActions['Move']) > 1: # substIndex = randint(0, len(availableActions['Move']) - 1) # if len(path) >= 1 and getActionName(path[len(path) - 1]) == 'Move': # args = getActionArgs(path[-1]) # while int(args[0]) == get_value(availableActions['Move'][substIndex][0]['r2']): # substIndex = randint(0, len(availableActions['Move']) - 1) # args = getActionArgs(path[-1]) # else: # substIndex = 0 # minCost = get_value(availableActions['Move'][substIndex][0]['cost']) time = time - minCost return ['Move', substIndex] return [None, None]
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
def findBestAction(availableActions, path): # TODO global state, time cost = 0 if 'Clean' in availableActions.keys(): cost = get_value(availableActions['Clean'][0][0]['dim']) return ['Clean', 0, cost, cost] if 'Refill' in availableActions.keys(): return ['Refill', 0, 1, 0] if 'Move' in availableActions.keys(): substIndex = 0 maxReward = -1 cost = -1 for subst in availableActions['Move']: moveCost = get_value(subst[0]['cost']) print('move cost ' + str(moveCost) + " time " + str(time)) if moveCost > time: continue index = availableActions['Move'].index(subst) if len(path) >= 1 and getActionName(path[len(path) - 1]) == 'Move': args = getActionArgs(path[-1]) #print("ARGS " + str(args)) if int(args[0]) == get_value( subst[0]['r2']) and len(availableActions['Move']) > 1: continue #TODO apply action # nu merge applyaction pentru ca ar modifica si env; asa schimb manual doar locatia #newState = applyAction(actions[0], subst) newState = deepcopy(state) for atom in newState: if get_head(atom) == 'location': newState.remove(atom) break newState.append( make_atom('location', make_const(get_value(subst[0]['r2'])))) #TODO see available actions for the new states newAvailableActions = findAvailableActions(newState) print(newAvailableActions.keys()) #TODO pick the best move if 'Clean' in newAvailableActions: cleanCost = get_value( newAvailableActions['Clean'][0][0]['dim']) print("Clean cost " + str(cleanCost) + " max reward so far " + str(maxReward)) if 1000 + cleanCost - moveCost > maxReward and moveCost + cleanCost <= time: maxReward = 1000 + cleanCost - moveCost substIndex = index cost = moveCost if 'Refill' in newAvailableActions: print("Refill" + " max reward so far " + str(maxReward)) if 50 - moveCost > maxReward: maxReward = 50 - moveCost substIndex = index cost = moveCost # if 'Move' in newAvailableActions and len(newAvailableActions['Move']) > 1: # daca nu e dead end # if 0 > maxReward: # maxReward = 0 # substIndex = index # cost = moveCost print("chosen cost " + str(cost)) if cost != -1: return ['Move', substIndex, cost, 0] else: substIndex = 0 minCost = 9999 for subst in availableActions['Move']: moveCost = get_value(subst[0]['cost']) if moveCost > time: continue index = availableActions['Move'].index(subst) if len(path) >= 1 and getActionName( path[len(path) - 1]) == 'Move': args = getActionArgs(path[-1]) #print("ARGS " + str(args)) if int(args[0]) == get_value(subst[0]['r2']) and len( availableActions['Move']) > 1: continue if moveCost < minCost: substIndex = index minCost = moveCost print("new chosen cost " + str(minCost)) return ['Move', substIndex, minCost, 0] return [None, None, 0, 0]