def read_environment(filename): global env, state, time, readBefore f = open(filename, 'r') line = f.readline() values = line.split() X = int(values[0]) T = int(values[1]) S = int(values[2]) C = int(values[3]) M = int(values[4]) N = int(values[5]) P = int(values[6]) crtIndex = int(values[7]) time = int(T) if readBefore: for a in state: if a[0] == 'location': state.remove(a) break state.append(make_atom('location', make_const(crtIndex))) if not readBefore: env['capacity'].append(make_atom('capacity', make_const(C))) for i in range(S): state.append(make_atom('carries', make_const(i), make_const(C))) line = f.readline() if not readBefore: values = line.split() for i in values: env['isWarehouse'].append(make_atom('isWarehouse', make_const(int(i)))) for i in range(int(P)): line = f.readline() if not readBefore: values = line.split() env['edge'].append(make_atom('edge', make_const(int(values[0])), make_const(int(values[1])), make_const(int(values[2])))) env['edge'].append(make_atom('edge', make_const(int(values[1])), make_const(int(values[0])), make_const(int(values[2])))) env['isDirty'] = [] for i in range(int(N)): line = f.readline() values = line.split() index = int(values[0]) dirty = int(values[1]) dim = int(values[2]) nr_subst = int(values[3]) if not readBefore: env['isRoom'].append(make_atom('isRoom', make_const(index))) env['dimension'].append(make_atom('dimension', make_const(index), make_const(dim))) if dirty == 1: env['isDirty'].append(make_atom('isDirty', make_const(index))) for j in range(4, 4 + 2 * nr_subst, 2): env['substance'].append(make_atom('substance', make_const(index), make_const(int(values[j])), make_const(int(values[j+1])))) readBefore = True
def get_sports_kb(): sports_kb = [] # Predicatul 'Consecutive' add_statement(sports_kb, make_atom('Consecutive', make_const('Luni'), make_const('Marti'))) add_statement(sports_kb, make_atom('Consecutive', make_const('Marti'), make_const('Miercuri'))) add_statement(sports_kb, make_atom('Consecutive', make_const('Miercuri'), make_const('Joi'))) add_statement(sports_kb, make_atom('Consecutive', make_const('Joi'), make_const('Vineri'))) add_statement(sports_kb, make_atom('Consecutive', make_const('Vineri'), make_const('Sambata'))) add_statement(sports_kb, make_atom('Consecutive', make_const('Sambata'), make_const('Duminica'))) # Predicatul 'Weekend' add_statement(sports_kb, make_atom('Weekend', make_const('Sambata'))) add_statement(sports_kb, make_atom('Weekend', make_const('Duminica'))) # Predicatul 'Ploua' add_statement(sports_kb, make_atom('Ploua', make_const('Vineri'))) # TODO 2.1: Dacă a plouat două zile la rând, a treia zi va fi frumos. add_statement(sports_kb, make_atom('Frumos', make_var('day3')), make_atom('Ploua', make_var('day1')), make_atom('Ploua', make_var('day2')), make_atom('Consecutive', make_var('day1'), make_var('day2')), make_atom('Consecutive', make_var('day2'), make_var('day3'))) # Predicatul 'Frumos' add_statement(sports_kb, make_atom('Frumos', make_const('Luni'))) add_statement(sports_kb, make_atom('Frumos', make_const('Marti'))) add_statement(sports_kb, make_atom('Frumos', make_const('Miercuri'))) # TODO 2.2: Dacă a fost frumos trei zile la rând, în cea de-a patra zi va ploua. add_statement(sports_kb, make_atom('Ploua', make_var('day4')), make_atom('Frumos', make_var('day1')), make_atom('Frumos', make_var('day2')), make_atom('Frumos', make_var('day3')), make_atom('Consecutive', make_var('day1'), make_var('day2')), make_atom('Consecutive', make_var('day2'), make_var('day3')), make_atom('Consecutive', make_var('day3'), make_var('day4'))) # Predicatul 'Student' add_statement(sports_kb, make_atom('Student', make_const('Nectarie'))) add_statement(sports_kb, make_atom('Student', make_const('Arsenie'))) # MergeLaMunte (cine, cand) # TODO 2.3: Un student merge întotdeauna la munte dacă este frumos într-o zi de weekend. add_statement(sports_kb, make_atom('MergeLaMunte', make_var('Student'), make_var('when')), make_atom('Weekend', make_var('when')), make_atom('Frumos', make_var('when'))) # Predicatul 'SportDeVara' add_statement(sports_kb, make_atom('SportDeVara', make_const('Volei'))) # Predicatul 'SportDeIarna' add_statement(sports_kb, make_atom('SportDeIarna', make_const('Schi'))) add_statement(sports_kb, make_atom('SportDeIarna', make_const('Sanie'))) # Predicatul 'PracticaSport' add_statement(sports_kb, make_atom('PracticaSport', make_const('Nectarie'), make_const('Schi'))) add_statement(sports_kb, make_atom('PracticaSport', make_const('Nectarie'), make_const('Sanie'))) add_statement(sports_kb, make_atom('PracticaSport', make_const('Arsenie'), make_const('Schi'))) add_statement(sports_kb, make_atom('PracticaSport', make_const('Arsenie'), make_const('Volei'))) # Predicatul 'Activitate' add_statement(sports_kb, make_atom('Activitate', make_var('who'), make_var('what'), make_var('when')), make_atom('MergeLaMunte', make_var('who'), make_var('when')), make_atom('PracticaSport', make_var('who'), make_var('what'))) make_unique_var_names(sports_kb) return sports_kb
for arg in get_args(formula): if is_positive_literal(arg): return arg def is_fact(formula): return is_positive_literal(formula) def is_rule(formula): return get_head(formula) == 'or' or get_head(formula) == 'and' # Test! # formula: P(x) ^ Q(x) -> R(x) f = make_or(make_neg(make_atom("P", make_var("x"))), make_neg(make_atom("Q", make_var("x"))), make_atom("R", make_var("x"))) print(" ; ".join([print_formula(p, True) for p in get_premises(f)])) # Should be P(?x) ; Q(?x) print_formula(get_conclusion(f)) # Should be R(?x) print(is_rule(f)) # must be True print(is_fact(f)) # must be False print(is_fact(get_conclusion(f))) # must be True print(is_rule(get_conclusion(f))) # must be 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):
'edge': [], 'isRoom': [], 'isWarehouse': [], 'capacity': [] } math_predicates = ['isBigger', 'isSmaller', 'equal', 'sum', 'dif'] state = [] newState = [] reward = 0 time = -1 readBefore = False # (action_name, [preconditions], [repetitive preconditions], [LA], [LE]) actions = [ ('Move', [make_var('r1'), make_var('r2')], [make_atom('location', make_var('r1')), make_atom('edge', make_var('r1'), make_var('r2'), make_var('cost'))], [], [make_atom('location', make_var('r2'))], [make_atom('location', make_var('r1'))]), ('Clean', [make_var('r')], [make_atom('location', make_var('r')), make_atom('isRoom', make_var('r')), make_atom('isDirty', make_var('r')), make_atom('dimension', make_var('r'), make_var('dim'))], [make_atom('substance', make_var('r'), make_var('s'), make_var('n')), make_atom('carries', make_var('s'), make_var('m')), make_atom('isBigger', make_var('m'), make_var('n')), make_atom('dif', make_var('m'), make_var('n'), make_var('d'))], [make_atom('carries', make_var('s'), make_var('d'))], [make_atom('isDirty', make_var('r')), make_atom('carries', make_var('s'), make_var('m'))]), ('Refill', [make_var('w')], [make_atom('location', make_var('w')), make_atom('isWarehouse', make_var('w')), make_atom('capacity', make_var('n')), make_atom('carries', make_var('sprim'), make_var('mprim')), make_atom('isSmaller', make_var('mprim'), make_var('n'))],
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]