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.")
Ejemplo n.º 3
0
    def __init__(self,k,x,y):
        """
        x and y is a list of tuples or lists -> (score,obj1,obj2)
        """
        self.x = x
        self.y = y

        self.C = PriorityQueue()
        self.p = []
        self.k = k
        self.tmp = []
Ejemplo n.º 4
0
class KBest:
    def __init__(self,k,x,y):
        """
        x and y is a list of tuples or lists -> (score,obj1,obj2)
        """
        self.x = x
        self.y = y

        self.C = PriorityQueue()
        self.p = []
        self.k = k
        self.tmp = []

    def _insert_queue(self,i,j):
        self.C.put((self.x[i][0]+self.y[j][0],[i,j]))
        self.tmp.append([i,j])

    def mult(self,k=None):
        if k != None:
            self.k = k

        self._insert_queue(0,0)
        while len(self.p) < self.k and not self.C.empty():
            self._append_next()

        return self.p

    def _append_next(self):
        s,v = self.C.get()
        self.tmp.remove(v)

        self.p.append((s,v))
        for i in range(2):
            vc = copy.deepcopy(v)
            vc[i] += 1
            if vc[0] < len(self.x) and vc[1] < len(self.y) and vc not in self.tmp:
                self._insert_queue(vc[0],vc[1])
Ejemplo n.º 5
0
def test_update_vertex_helper(update_vertex_helper):
    print("Testing update_vertex_helper...")

    # Test 1: triangle problem; A not in queue, don't insert
    g = dict(A=9, B=10, C=2)
    g_original = g.copy()
    rhs = dict(A="value_to_be_overwritten")
    graph = graph_triangle.copy()
    queue = PriorityQueue()  #empty
    update_vertex_helper("A", g, rhs, "non-existent_goal_node", graph, queue)
    assert g == g_original  #not modified
    assert rhs == dict(A=9)
    assert graph == graph_triangle  #not modified
    assert queue == PriorityQueue()

    # Test 2: triangle problem; remove A from queue and don't re-insert
    g = dict(A=9, B=10, C=2)
    g_original = g.copy()
    rhs = dict(A="value_to_be_overwritten")
    graph = graph_triangle.copy()
    queue = PriorityQueue()  #empty
    queue.extend(list("XYAZ"))
    expected_queue = queue.copy()
    expected_queue.remove("A")
    update_vertex_helper("A", g, rhs, "non-existent_goal_node", graph, queue)
    assert g == g_original  #not modified
    assert rhs == dict(A=9)
    assert graph == graph_triangle  #not modified
    #problem here
    assert queue == expected_queue

    # Test 3: triangle problem; A not in queue, do insert
    g = dict(A=8, B=10, C=2)
    g_original = g.copy()
    rhs = dict(A=0)
    graph = graph_triangle.copy()
    queue = PriorityQueue()  #empty
    queue.extend(list("XYZ"))
    expected_queue = queue.copy()
    expected_queue.insert("A")
    update_vertex_helper("A", g, rhs, "non-existent_goal_node", graph, queue)
    assert g == g_original  #not modified
    assert rhs == dict(A=9)
    assert graph == graph_triangle  #not modified
    assert queue == expected_queue

    # Test 4: triangle problem; A in queue, remove and re-insert
    g = dict(A=20, B=10, C=2)
    g_original = g.copy()
    rhs = dict(B="other_stuff")
    graph = graph_triangle.copy()
    queue = PriorityQueue()  #empty
    queue.extend(list("XYAZ"))
    expected_queue = queue.copy()
    update_vertex_helper("A", g, rhs, "non-existent_goal_node", graph, queue)
    assert g == g_original  #not modified
    assert rhs == dict(A=9, B="other_stuff")
    assert graph == graph_triangle  #not modified
    #problem here
    assert queue == expected_queue

    # Test 5: triangle problem; B has only one successor
    g = dict(A=1, B=100, C=10)
    g_original = g.copy()
    rhs = dict()
    queue = PriorityQueue()  #empty
    expected_queue = queue.copy()
    expected_queue.insert("B")
    update_vertex_helper("B", g, rhs, "non-existent_goal_node",
                         graph_triangle.copy(), queue)
    assert rhs == dict(B=13)
    assert queue == expected_queue

    # Test 6: triangle problem; B is goal
    g = dict(A=1, B=0, C=10)
    g_original = g.copy()
    rhs = dict(B=0)
    queue = PriorityQueue()  #empty
    update_vertex_helper("B", g, rhs, "B", graph_triangle.copy(), queue)
    assert rhs == dict(B=0)
    assert queue == PriorityQueue()

    # Test 7: bidirectional graph from problem_simple
    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): 5,
        (2, 3): inf,
        (2, 1): 1.0,
        (2, 2): inf,
        (1, 0): inf,
        (4, 2): inf,
        (0, 3): inf,
        (0, 4): inf,
        (4, 1): inf,
        (1, 1): 1.0,
        (4, 0): inf
    }
    rhs = {
        (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): inf,
        (0, 2): 2.0,
        (2, 0): 0,
        (1, 3): 9,
        (2, 3): inf,
        (2, 1): 1.0,
        (2, 2): 2.0,
        (1, 0): 1.0,
        (4, 2): inf,
        (0, 3): inf,
        (0, 4): inf,
        (4, 1): inf,
        (1, 1): 1.0,
        (4, 0): inf
    }
    queue = PriorityQueue()
    queue.extend([(1, 0), (0, 1), (2, 2), (0, 0)])
    expected_rhs = {
        (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): inf,
        (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): inf,
        (0, 4): inf,
        (4, 1): inf,
        (1, 1): 1.0,
        (4, 0): inf
    }
    expected_queue = queue.copy()
    expected_queue.insert((1, 3))
    update_vertex_helper((1, 3), g, rhs, goal_simple, graph_simple, queue)
    assert rhs == expected_rhs
    assert queue == expected_queue
Ejemplo n.º 6
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