def main():

    tasks = Dispenser(after)
    time = 0
    cmd, args = get_cmd(COMMANDS)
    while cmd != QUIT_CMD:
        if cmd == TICK_CMD:
            time += 1
            if not tasks.isEmpty():
                current = tasks.peek()
                current.time_left -= 1
                if current.time_left == 0:
                    print( "\nTask '" + current.name + \
                           "' completed at time " + str( time ) + "." )
                    tasks.remove()
                    if not tasks.isEmpty():
                        print("New task is '" + tasks.peek().name + "'.")
                    else:
                        print("Nothing else to do.")
            else:
                print("Nothing to do.")
        elif cmd == ADD_CMD:
            new_task = Task(args[0], int(args[1]))
            tasks.insert(new_task)
            print( "\nAdded. Current task is '" + tasks.peek().name + \
                   "'." )
        else:
            assert True, "PROGRAM ERROR"
        cmd, args = get_cmd(COMMANDS)

    print("\nTerminating the simulation.")
def main():

    tasks = Dispenser(after)
    time = 0
    cmd, args = get_cmd( COMMANDS )
    while cmd != QUIT_CMD:
        if cmd == TICK_CMD:
            time += 1
            if not tasks.isEmpty():
                current = tasks.peek()
                current.time_left -= 1
                if current.time_left == 0:
                    print( "\nTask '" + current.name + \
                           "' completed at time " + str( time ) + "." )
                    tasks.remove()
                    if not tasks.isEmpty():
                        print( "New task is '" + tasks.peek().name + "'." )
                    else:
                        print( "Nothing else to do." )
            else:
                print( "Nothing to do." )
        elif cmd == ADD_CMD:
            new_task = Task( args[ 0 ], int( args[ 1 ] ) )
            tasks.insert( new_task )
            print( "\nAdded. Current task is '" + tasks.peek().name + \
                   "'." )
        else:
            assert True, "PROGRAM ERROR"
        cmd, args = get_cmd( COMMANDS )

    print( "\nTerminating the simulation." )
Example #3
0
def test_compute_shortest_path_helper(compute_shortest_path_helper,
                                      calc_key_helper):
    print("Testing compute_shortest_path_helper...")

    # problem_simple, first step
    g = {
        (1, 2): inf,
        (0, 1): inf,
        (3, 2): inf,
        (0, 0): inf,
        (3, 0): inf,
        (3, 1): inf,
        (2, 4): inf,
        (1, 4): inf,
        (0, 2): inf,
        (2, 0): inf,
        (1, 3): inf,
        (2, 3): inf,
        (2, 1): inf,
        (2, 2): inf,
        (1, 0): inf,
        (4, 2): inf,
        (0, 3): inf,
        (0, 4): inf,
        (4, 1): inf,
        (1, 1): inf,
        (4, 0): inf
    }
    rhs = {
        (1, 2): inf,
        (0, 1): inf,
        (3, 2): inf,
        (0, 0): inf,
        (3, 0): inf,
        (3, 1): inf,
        (2, 4): inf,
        (1, 4): inf,
        (0, 2): inf,
        (2, 0): 0,
        (1, 3): inf,
        (2, 3): inf,
        (2, 1): inf,
        (2, 2): inf,
        (1, 0): inf,
        (4, 2): inf,
        (0, 3): inf,
        (0, 4): inf,
        (4, 1): inf,
        (1, 1): inf,
        (4, 0): inf
    }
    graph = graph_simple.copy()
    calc_key = lambda node: calc_key_helper(node, g, rhs, (0, 3), 0)
    queue = PriorityQueue(f=lambda node: calc_key(node))
    queue.insert(goal_simple)
    g_expected = {
        (1, 2): inf,
        (0, 1): inf,
        (3, 2): inf,
        (0, 0): inf,
        (3, 0): inf,
        (3, 1): inf,
        (2, 4): inf,
        (1, 4): inf,
        (0, 2): 2.0,
        (2, 0): 0,
        (1, 3): inf,
        (2, 3): inf,
        (2, 1): 1.0,
        (2, 2): inf,
        (1, 0): inf,
        (4, 2): inf,
        (0, 3): 3.0,
        (0, 4): inf,
        (4, 1): inf,
        (1, 1): 1.0,
        (4, 0): inf
    }
    rhs_expected = {
        (1, 2): inf,
        (0, 1): 2.0,
        (3, 2): inf,
        (0, 0): 2.0,
        (3, 0): inf,
        (3, 1): inf,
        (2, 4): inf,
        (1, 4): 4.0,
        (0, 2): 2.0,
        (2, 0): 0,
        (1, 3): inf,
        (2, 3): inf,
        (2, 1): 1.0,
        (2, 2): 2.0,
        (1, 0): 1.0,
        (4, 2): inf,
        (0, 3): 3.0,
        (0, 4): 4.0,
        (4, 1): inf,
        (1, 1): 1.0,
        (4, 0): inf
    }
    compute_shortest_path_helper(g, rhs, (0, 3), goal_simple, 0, graph, queue)
    assert graph == graph_simple  #not modified
    assert g == g_expected
    assert rhs == rhs_expected
    queue_expected = PriorityQueue(f=lambda node: calc_key(node))
    queue_expected.extend([(1, 0), (0, 1), (2, 2), (0, 0), (0, 4), (1, 4)])
    assert queue == queue_expected

    # problem_simple, second step
    g = {
        (1, 2): inf,
        (0, 1): inf,
        (3, 2): inf,
        (0, 0): inf,
        (3, 0): inf,
        (3, 1): inf,
        (2, 4): inf,
        (1, 4): inf,
        (0, 2): 2.0,
        (2, 0): 0,
        (1, 3): inf,
        (2, 3): inf,
        (2, 1): 1.0,
        (2, 2): inf,
        (1, 0): inf,
        (4, 2): inf,
        (0, 3): 3.0,
        (0, 4): inf,
        (4, 1): inf,
        (1, 1): 1.0,
        (4, 0): inf
    }
    rhs = {
        (1, 2): inf,
        (0, 1): 3.0,
        (3, 2): inf,
        (0, 0): inf,
        (3, 0): inf,
        (3, 1): inf,
        (2, 4): inf,
        (1, 4): 4.0,
        (0, 2): 4.0,
        (2, 0): 0,
        (1, 3): inf,
        (2, 3): inf,
        (2, 1): 1.0,
        (2, 2): 2.0,
        (1, 0): 1.0,
        (4, 2): inf,
        (0, 3): 3.0,
        (0, 4): 4.0,
        (4, 1): inf,
        (1, 1): inf,
        (4, 0): inf
    }
    graph = graph_simple_step2.copy()
    calc_key = lambda node: calc_key_helper(node, g, rhs, (0, 2), 1)
    queue = PriorityQueue(f=lambda node: calc_key(node))
    queue.extend([(1, 1), (0, 2), (1, 0), (2, 2), (0, 1), (0, 4), (1, 4)])
    g_expected = {
        (1, 2): inf,
        (0, 1): 2.0,
        (3, 2): inf,
        (0, 0): inf,
        (3, 0): inf,
        (3, 1): inf,
        (2, 4): inf,
        (1, 4): inf,
        (0, 2): 3.0,
        (2, 0): 0,
        (1, 3): inf,
        (2, 3): inf,
        (2, 1): 1.0,
        (2, 2): inf,
        (1, 0): 1.0,
        (4, 2): inf,
        (0, 3): 3.0,
        (0, 4): inf,
        (4, 1): inf,
        (1, 1): inf,
        (4, 0): inf
    }
    rhs_expected = {
        (1, 2): inf,
        (0, 1): 2.0,
        (3, 2): inf,
        (0, 0): 2.0,
        (3, 0): inf,
        (3, 1): inf,
        (2, 4): inf,
        (1, 4): 4.0,
        (0, 2): 3.0,
        (2, 0): 0,
        (1, 3): inf,
        (2, 3): inf,
        (2, 1): 1.0,
        (2, 2): 2.0,
        (1, 0): 1.0,
        (4, 2): inf,
        (0, 3): 4.0,
        (0, 4): 4.0,
        (4, 1): inf,
        (1, 1): inf,
        (4, 0): inf
    }
    compute_shortest_path_helper(g, rhs, (0, 2), goal_simple, 1, graph, queue)
    assert graph == graph_simple_step2  #not modified
    assert g == g_expected
    assert rhs == rhs_expected
    queue_expected = PriorityQueue(f=lambda node: calc_key(node))
    queue_expected.extend([(0, 0), (2, 2), (0, 3), (0, 4), (1, 4)])
    assert queue == queue_expected