Beispiel #1
0
'''
Created on Sep 15, 2013

@author: pglauner
'''

from algorithms.routing.shortest_path import shortest_path_search
from algorithms.routing.lowest_cost import lowest_cost_search
from algorithms.routing.a_star.simple import a_star as a_star_simple
from algorithms.routing.a_star.queue_based import a_star as a_star_queue
from algorithms.routing.test_graphs.test_graphs import tricky_graph
from algorithms.routing.test_graphs.phrase_graphs import simple_phrase
from algorithms.routing.test_graphs.graph_utils import action_cost, is_goal, successors

if __name__ == '__main__':
    print shortest_path_search(0, successors(tricky_graph), is_goal(100))
    print lowest_cost_search(0, successors(tricky_graph), is_goal(100),
                             action_cost)
    print a_star_simple(tricky_graph, 0, 100)
    print a_star_queue(tricky_graph, 0, 100)

    print

    print shortest_path_search(0, successors(simple_phrase), is_goal(1))
    print lowest_cost_search(0, successors(simple_phrase), is_goal(1),
                             action_cost)
    print a_star_simple(simple_phrase, 0, 1)
    print a_star_queue(simple_phrase, 0, 1)
Beispiel #2
0
'''
Created on Sep 15, 2013

@author: pglauner
'''

from algorithms.routing.shortest_path import shortest_path_search
from algorithms.routing.lowest_cost import lowest_cost_search
from algorithms.routing.a_star.simple import a_star as a_star_simple
from algorithms.routing.a_star.queue_based import a_star as a_star_queue
from algorithms.routing.test_graphs.test_graphs import tricky_graph
from algorithms.routing.test_graphs.phrase_graphs import simple_phrase
from algorithms.routing.test_graphs.graph_utils import action_cost, is_goal, successors


if __name__ == '__main__':
    print shortest_path_search(0, successors(tricky_graph), is_goal(100))
    print lowest_cost_search(0, successors(tricky_graph), is_goal(100), action_cost)
    print a_star_simple(tricky_graph, 0, 100)
    print a_star_queue(tricky_graph, 0, 100)

    print

    print shortest_path_search(0, successors(simple_phrase), is_goal(1))
    print lowest_cost_search(0, successors(simple_phrase), is_goal(1), action_cost)
    print a_star_simple(simple_phrase, 0, 1)
    print a_star_queue(simple_phrase, 0, 1)
        for z in (x, y):
            if z == "M":
                m1 -= 1
                m2 += 1
                action += "M"
            if z == "C":
                c1 -= 1
                c2 += 1
                action += "C"
        if all(x >= 0 for x in (m1, c1, b1, m2, c2, b2)):
            if B1:
                res[(m1, c1, b1, m2, c2, b2)] = action + "->"
            elif B2:
                res[(m2, c2, b1, m1, c1, b2)] = "<-" + action

    return res


def is_goal(start):
    def g(state):
        return state == (0, 0, 0) + start

    return g


if __name__ == "__main__":
    start = m1, c1, b1, m2, c2, b2 = 3, 3, 1, 0, 0, 0

    print shortest_path_search(start, successors, is_goal(start[0:3]))
    print lowest_cost_search(start, successors, is_goal(start[0:3]), lambda action: 1)
            if z == 'M':
                m1 -= 1
                m2 += 1
                action += 'M'
            if z == 'C':
                c1 -= 1
                c2 += 1
                action += 'C'
        if all(x >= 0 for x in (m1, c1, b1, m2, c2, b2)):
            if B1:
                res[(m1, c1, b1, m2, c2, b2)] = action + '->'
            elif B2:
                res[(m2, c2, b1, m1, c1, b2)] = '<-' + action

    return res


def is_goal(start):
    def g(state):
        return state == (0, 0, 0) + start

    return g


if __name__ == '__main__':
    start = m1, c1, b1, m2, c2, b2 = 3, 3, 1, 0, 0, 0

    print shortest_path_search(start, successors, is_goal(start[0:3]))
    print lowest_cost_search(start, successors, is_goal(start[0:3]),
                             lambda action: 1)
Created on Sep 8, 2013

@author: pglauner
'''

from algorithms.routing.shortest_path import shortest_path_search
from algorithms.routing.lowest_cost import lowest_cost_search


def successors(X, Y):
    def sc(state):
        x, y = state
        assert x <= X and y <= Y
        return {((0, y+x) if y+x <= Y else (x-(Y-y), (Y))): 'x->y',
                ((x+y, 0) if x+y <= X else (X, (y-(X-x)))): 'x<-y',
                (X, y): 'fill x',
                (x, Y): 'fill y',
                (0, y): 'empty x',
                (x, 0): 'empty y'}
    return sc


if __name__ == '__main__':
    res = shortest_path_search((0, 0), successors(418, 986), lambda state: state == (6, 0))
    print res
    print '%s transitions' % (len(res) / 2)
    print
    res = lowest_cost_search((0, 0), successors(418, 986), lambda state: state == (6, 0), lambda action: 1)
    print res
    print '%s transitions' % (len(res) / 2)
Beispiel #6
0
def successors(state):
    _, _, light = state
    return dict(
        successor(state, frozenset([a, b])) for a in state[light]
        for b in state[light])


def successor(state, travellers):
    _, _, light = state
    start = state[light] - travellers
    dest = state[1 - light] | travellers
    if light == 0:
        return (start, dest, 1), (travellers, '->')
    else:
        return (dest, start, 0), (travellers, '<-')


def is_goal(state):
    here, _, _ = state
    return len(here) == 0


if __name__ == '__main__':
    # light: left=0, right=1
    start = here, there, light = frozenset([1, 2, 5, 10]), frozenset([]), 0

    print shortest_path_search(start, successors, is_goal)
    print lowest_cost_search(start, successors, is_goal,
                             lambda action: max(action[0]))
Beispiel #7
0

def successors(state):
    _, _, light = state
    return dict(successor(state, frozenset([a, b]))
                for a in state[light]
                for b in state[light])


def successor(state, travellers):
    _, _, light = state
    start = state[light] - travellers
    dest = state[1-light] | travellers
    if light == 0:
        return (start, dest, 1), (travellers, '->')
    else:
        return (dest, start, 0), (travellers, '<-')


def is_goal(state):
    here, _, _ = state
    return len(here) == 0


if __name__ == '__main__':
    # light: left=0, right=1
    start = here, there, light = frozenset([1, 2, 5, 10]), frozenset([]), 0

    print shortest_path_search(start, successors, is_goal)
    print lowest_cost_search(start, successors, is_goal, lambda action: max(action[0]))