Ejemplo n.º 1
0
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
Ejemplo n.º 2
0
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
Ejemplo n.º 3
0
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
Ejemplo n.º 4
0
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
Ejemplo n.º 5
0
        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


# Test!
# Rule: P(x) => Q(x)
# Facts: P(1)
print("Expected: ", print_formula(make_atom('Q', make_const(1)),
                                  True), "Result:")
for f in apply_rule(
        make_or(make_neg(make_atom("P", make_var("x"))), make_atom("Q", make_var("x"))), \
        [make_atom("P", make_const(1))]):
    print_formula(f)  # should be Q(1)
print("=====")
# Rule: P(x) ^ Q(x) => R(x)
# Facts: P(1), P(2), P(3), Q(3), Q(2)
print("Expected: ", print_formula(make_atom('R', make_const(2)), True), ";",
      print_formula(make_atom('R', make_const(3)), True), "Result:")
for f in apply_rule(
        make_or(
            make_neg(make_atom("P", make_var("x"))),
            make_neg(make_atom("Q", make_var("x"))),
            make_atom("R", make_var("x"))),
Ejemplo n.º 6
0
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]