def test_setitem_fail(self):
        data = np.array([2., 0., -1.])
        matching_graph = mm.MatchingGraph(edges=[(1, 1), (1, 2), (2, 2)], nb_demand_classes=2, nb_supply_classes=2)
        ed = mm.EdgesData(data=data, matching_graph=matching_graph)

        with pytest.raises(ValueError):
            ed[(3, 2)] = 9.
class TestValueFunction:
    N_graph = mm.MatchingGraph(edges=[(1, 1), (1, 2), (2, 2)], nb_demand_classes=2, nb_supply_classes=2)
    epsilon = 0.1
    alpha = np.array([(1. / 2.) + epsilon, (1. / 2.) - epsilon])
    beta = np.array([(1. / 2.) - epsilon, (1. / 2.) + epsilon])
    arrival_dist = mm.NodesData.items(demand_items=alpha, supply_items=beta, matching_graph=N_graph)
    costs = mm.NodesData(data=np.array([10., 1., 1., 10.]), matching_graph=N_graph)
    capacity = 2.
    x0 = mm.State.zeros(matching_graph=N_graph, capacity=capacity)
    N_model = mm.Model(matching_graph=N_graph, arrival_dist=arrival_dist, costs=costs, init_state=x0, capacity=capacity,
                       penalty=100., state_space="state_with_arrival")

    def test_init(self):
        N_value_function_a = rl.ValueFunction(model=TestValueFunction.N_model)

        assert N_value_function_a.model is TestValueFunction.N_model
        assert np.all(N_value_function_a.complete_arrival_graph_edges_list == [(1, 1), (1, 2), (2, 1), (2, 2)])

        x_0_b = mm.State.zeros(matching_graph=TestValueFunction.N_graph, capacity=np.inf)
        N_model_b = mm.Model(matching_graph=TestValueIteration.N_graph,
                             arrival_dist=TestValueIteration.arrival_dist, costs=TestValueIteration.costs,
                             init_state=x_0_b, capacity=np.inf,
                             penalty=100., state_space="state_with_arrival")
        with pytest.raises(AssertionError):
            rl.ValueFunction(model=N_model_b)

    def test_build_state_space(self):
        N_value_function_a = rl.ValueFunction(model=TestValueFunction.N_model)
        state_space_theory_state_tuples = [(0., 0., 0., 0.), (0., 1., 0., 1.), (0., 2., 0., 2.), (0., 1., 1., 0.),
                                           (0., 2., 1., 1.), (0., 2., 2., 0.), (1., 0., 0., 1.), (1., 1., 0., 2.),
                                           (1., 1., 1., 1.), (1., 2., 1., 2.), (1., 2., 2., 1.), (2., 0., 0., 2.),
                                           (2., 1., 1., 2.), (2., 2., 2., 2.), (1., 0., 1., 0.), (1., 1., 2., 0.),
                                           (2., 0., 1., 1.), (2., 1., 2., 1.), (2., 0., 2., 0.)]
        state_space_theory_arrivals_tuples = [(1., 0., 1., 0.), (1., 0., 0., 1.), (0., 1., 1., 0.), (0., 1., 0., 1.)]
        state_space_theory = [(mm.State(values=np.array(state_tuple), matching_graph=TestValueFunction.N_graph,
                                        capacity=TestValueFunction.capacity),
                               mm.State(values=np.array(arrivals_tuple), matching_graph=TestValueFunction.N_graph,
                                        capacity=TestValueFunction.capacity))
                              for state_tuple, arrivals_tuple in itertools.product(state_space_theory_state_tuples,
                                                                                   state_space_theory_arrivals_tuples)]

        assert np.all(N_value_function_a.state_space == state_space_theory)

    def test_initialise_values(self):
        N_value_function_a = rl.ValueFunction(model=TestValueFunction.N_model)

        for state in N_value_function_a.state_space:
            assert N_value_function_a[state] == 0.

    def test_get_item(self):
        # TODO
        pass

    def test_set_item(self):
        # TODO
        pass

    def test_copy(self):
        # TODO
        pass
Exemplo n.º 3
0
def demo_N_with_capacity():
    np.random.seed(42)
    N_graph = mm.MatchingGraph(edges=[(1, 1), (1, 2), (2, 2)], nb_demand_classes=2, nb_supply_classes=2)
    epsilon = 0.1
    alpha = np.array([(1. / 2.) + epsilon, (1. / 2.) - epsilon])
    beta = np.array([(1. / 2.) - epsilon, (1. / 2.) + epsilon])
    arrival_dist = mm.NodesData.items(demand_items=alpha, supply_items=beta, matching_graph=N_graph)
    costs = mm.NodesData(data=np.array([1., 10., 10., 1.]), matching_graph=N_graph)
    P = [po.Threshold_N(threshold=0.),
         po.Threshold_N(threshold=1.),
         po.MaxWeight(costs=costs),
         po.NoMatchings()]
    capacity = 5.
    x0 = mm.State.zeros(matching_graph=N_graph, capacity=capacity)
    test_model = mm.Model(matching_graph=N_graph, arrival_dist=arrival_dist, costs=costs, init_state=x0,
                          capacity=capacity, penalty=100., state_space="state_with_arrival")

    plt.ion()

    res = test_model.run(nb_iter=1000, policies=P, plot=True)

    t = time.time()
    N = 10000
    c, x = test_model.average_cost(N, P, plot=True)
    print(time.time() - t)
    for i in range(len(P)):
        print(P[i], ": ", c[i][N])

    plt.ioff()
    plt.show()
Exemplo n.º 4
0
def demo_N():
    np.random.seed(42)
    N_graph = mm.MatchingGraph([(1, 1), (1, 2), (2, 2)], 2, 2)
    epsilon = 0.1
    alpha = np.array([(1. / 2.) + epsilon, (1. / 2.) - epsilon])
    beta = np.array([(1. / 2.) - epsilon, (1. / 2.) + epsilon])
    arrival_dist = mm.NodesData.items(alpha, beta, N_graph)
    costs = mm.NodesData(np.array([10., 1., 1., 10.]), N_graph)
    P = [po.Threshold_N(threshold=3.),
         po.MaxWeight(costs=costs)]
    x0 = mm.State.zeros(N_graph)
    test_model = mm.Model(N_graph, arrival_dist, costs, x0)

    plt.ion()

    res = test_model.run(nb_iter=1000, policies=P, plot=True)

    t = time.time()
    N = 10000
    c, x = test_model.average_cost(N, P, plot=True)
    print(time.time() - t)
    for i in range(len(P)):
        print(P[i], ": ", c[i][N])

    plt.ioff()
    plt.show()
Exemplo n.º 5
0
def demo_W():
    np.random.seed(42)
    W = mm.MatchingGraph([(1, 1), (2, 1), (2, 2), (3, 2)], 3, 2)
    epsilon = 0.1
    alpha = np.array([(1./2.)-epsilon, (1./4.)+epsilon, (1./4.)])
    beta = np.array([1./2., 1./2.])
    arrival_dist = mm.NodesData.items(alpha, beta, W)
    costs = mm.NodesData(np.array([10., 10., 1., 1., 1000.]), W)
    P = [po.P14T23(thresholds=np.array([11, 0])),
         po.P13T24D2(thresholds=np.array([5, 14]))]
    # P = [po.P14T23(thresholds=np.array([14, 0])),
    #      po.OptimalW(thresholds=np.array([14])),
    #      po.OptimalWBis(thresholds=np.array([14])),
    #      po.P13T24D2(thresholds=np.array([5, 14]))]
    # P = [po.P13T24D2(thresholds=np.array([5, 14])),
    #      po.P13T24D2(thresholds=np.array([5, 15])),
    #      po.OptimalW(thresholds=np.array([14])),
    #      po.OptimalW(thresholds=np.array([15]))]
    x0 = mm.State.zeros(W)
    test_model = mm.Model(W, arrival_dist, costs, x0)

    plt.ion()

    res = test_model.run(nb_iter=1000, policies=P, plot=True)

    t = time.time()
    N = 10000
    c, x = test_model.average_cost(N, P, plot=True)
    print(time.time()-t)
    for i in range(len(P)):
        print(P[i], ": ", c[i][N])

    plt.ioff()
    plt.show()
    def test_init(self):
        values_a = np.array([5, 3, 3, 5])
        matchingGraph = TestMatchingGraph.N_graph
        N_state_a = mm.State(values=values_a, matching_graph=matchingGraph)

        assert np.all(N_state_a.data == values_a)
        assert N_state_a.matching_graph == matchingGraph
        assert N_state_a.capacity == np.inf

        capacity = 10
        N_state_b = mm.State(values=values_a, matching_graph=matchingGraph, capacity=capacity)

        assert N_state_b.capacity == capacity

        values_b = np.array([-2, 3, 3, 5])
        with pytest.raises(ValueError):
            _ = mm.State(values=values_b, matching_graph=matchingGraph)

        values_c = np.array([2, 3, 3, 5])
        with pytest.raises(ValueError):
            _ = mm.State(values=values_c, matching_graph=matchingGraph)

        values_d = np.array([11, 3, 3, 11])
        with pytest.raises(ValueError):
            _ = mm.State(values=values_d, matching_graph=matchingGraph, capacity=capacity)
    def test_init(self):
        data = np.array([2., 0., -1.])
        matching_graph = mm.MatchingGraph(edges=[(1, 1), (1, 2), (2, 2)], nb_demand_classes=2, nb_supply_classes=2)
        ed = mm.EdgesData(data=data, matching_graph=matching_graph)

        assert ed.data is data
        assert ed.matching_graph is matching_graph
class TestValueIteration:
    N_graph = mm.MatchingGraph(edges=[(1, 1), (1, 2), (2, 2)], nb_demand_classes=2, nb_supply_classes=2)
    epsilon = 0.1
    alpha = np.array([(1. / 2.) + epsilon, (1. / 2.) - epsilon])
    beta = np.array([(1. / 2.) - epsilon, (1. / 2.) + epsilon])
    arrival_dist = mm.NodesData.items(demand_items=alpha, supply_items=beta, matching_graph=N_graph)
    costs = mm.NodesData(data=np.array([10., 1., 1., 10.]), matching_graph=N_graph)
    x0 = mm.State.zeros(matching_graph=N_graph, capacity=10.)
    N_model = mm.Model(matching_graph=N_graph, arrival_dist=arrival_dist, costs=costs, init_state=x0, capacity=10.,
                       penalty=100., state_space="state_with_arrival")

    def test_init(self):
        N_value_iteration = rl.ValueIteration(model=TestValueIteration.N_model)

        assert N_value_iteration.model is TestValueIteration.N_model

    def test_bellman_operator_with_matching(self):
        # TODO
        pass

    def test_bellman_operator(self):
        # TODO
        pass

    def test_is_optimal(self):
        # TODO
        pass

    def test_iterate(self):
        # TODO
        pass

    def test_run(self):
        # TODO
        pass
    def test_iadd(self):
        N_nodes_a = mm.NodesData(data=np.array([5, 3, 3, 5]), matching_graph=TestMatchingGraph.N_graph)
        N_nodes_b = mm.NodesData(data=np.array([-5, 4, -8, -1]), matching_graph=TestMatchingGraph.N_graph)
        N_nodes_sum = mm.NodesData(data=np.array([0, 7, -5, 4]), matching_graph=TestMatchingGraph.N_graph)

        N_nodes_a += N_nodes_b
        assert N_nodes_sum == N_nodes_a
    def test_getitem(self):
        data = np.array([2., 0., -1.])
        matching_graph = mm.MatchingGraph(edges=[(1, 1), (1, 2), (2, 2)], nb_demand_classes=2, nb_supply_classes=2)
        ed = mm.EdgesData(data=data, matching_graph=matching_graph)

        assert ed[(1, 1)] == 2.
        assert ed[(1, 2)] == 0.
        assert ed[(2, 2)] == -1.
    def test_init(self):
        matching_graph = mm.MatchingGraph(edges=[(1, 1), (1, 2), (2, 2)],
                                          nb_demand_classes=2,
                                          nb_supply_classes=2)
        costs = mm.NodesData(data=np.array([3., 1., 2., 3.]),
                             matching_graph=matching_graph)
        policy = po.MaxWeight(state_space="state", costs=costs)

        assert policy.costs == costs
    def test_copy(self):
        data = np.array([2., 0., -1.])
        matching_graph = mm.MatchingGraph(edges=[(1, 1), (1, 2), (2, 2)], nb_demand_classes=2, nb_supply_classes=2)
        ed = mm.EdgesData(data=data, matching_graph=matching_graph)
        ed_copy = ed.copy()

        assert ed.matching_graph is ed_copy.matching_graph
        assert ed.data is not ed_copy.data
        assert np.all(ed.data == ed_copy.data)
    def test_eq(self):
        N_state_a = mm.State(values=np.array([5., 3., 3., 5.]), matching_graph=TestMatchingGraph.N_graph,
                             capacity=10)
        N_nodes_a = mm.NodesData(data=np.array([5., 3., 3., 5.]), matching_graph=TestMatchingGraph.N_graph)

        assert np.all(TestState.N_state.data == N_state_a.data)
        assert TestState.N_state.matching_graph == N_state_a.matching_graph
        assert TestState.N_state.capacity == N_state_a.capacity
        assert TestState.N_state == N_state_a
        assert not TestState.N_state == N_nodes_a
def test_thresholds_with_priorities_match_n_end_priority():
    matching_graph = mm.MatchingGraph(edges=[(1, 1), (1, 2), (2, 2)],
                                      nb_demand_classes=2,
                                      nb_supply_classes=2)
    matching_order = mm.EdgesData(data=np.array([0, 2, 1]),
                                  matching_graph=matching_graph)
    thresholds = mm.EdgesData(data=np.array([3., 0., 0.]),
                              matching_graph=matching_graph)
    policy = po.ThresholdsWithPriorities(state_space="state",
                                         matching_order=matching_order,
                                         thresholds=thresholds)

    x = mm.State(values=np.array([5., 5., 5., 5.]),
                 matching_graph=matching_graph)
    u = policy.match(x)
    u_theory = np.array([2., 5., 2., 5.])
    assert np.all(u.to_nodesdata() == u_theory)

    x = mm.State(values=np.array([0., 5., 5., 0.]),
                 matching_graph=matching_graph)
    u = policy.match(x)
    u_theory = np.array([0., 0., 0., 0.])
    assert np.all(u.to_nodesdata() == u_theory)

    x = mm.State(values=np.array([5., 0., 5., 0.]),
                 matching_graph=matching_graph)
    u = policy.match(x)
    u_theory = np.array([2., 0., 2., 0.])
    assert np.all(u.to_nodesdata() == u_theory)

    x = mm.State(values=np.array([0., 5., 0., 5.]),
                 matching_graph=matching_graph)
    u = policy.match(x)
    u_theory = np.array([0., 5., 0., 5.])
    assert np.all(u.to_nodesdata() == u_theory)

    x = mm.State(values=np.array([5., 0., 0., 5.]),
                 matching_graph=matching_graph)
    u = policy.match(x)
    u_theory = np.array([5., 0., 0., 5.])
    assert np.all(u.to_nodesdata() == u_theory)

    x = mm.State(values=np.array([2., 5., 2., 5.]),
                 matching_graph=matching_graph)
    u = policy.match(x)
    u_theory = np.array([0., 5., 0., 5.])
    assert np.all(u.to_nodesdata() == u_theory)

    x = mm.State(values=np.array([3., 4., 2., 5.]),
                 matching_graph=matching_graph)
    u = policy.match(x)
    u_theory = np.array([1., 4., 0., 5.])
    assert np.all(u.to_nodesdata() == u_theory)
def test_thresholds_with_priorities_str():
    matching_graph = mm.MatchingGraph(edges=[(1, 1), (1, 2), (2, 2)],
                                      nb_demand_classes=2,
                                      nb_supply_classes=2)
    matching_order = mm.EdgesData(data=np.array([0, 2, 1]),
                                  matching_graph=matching_graph)
    thresholds = mm.EdgesData(data=np.array([3., 0., 0.]),
                              matching_graph=matching_graph)
    policy = po.ThresholdsWithPriorities(state_space="state",
                                         matching_order=matching_order,
                                         thresholds=thresholds)

    assert str(policy) == 'TwP p={} t={}'.format(matching_order, thresholds)
class TestModel:
    # TODO: do more tests based on the modification to state space and discounted costs.
    N_model = mm.Model(matching_graph=TestMatchingGraph.N_graph,
                       arrival_dist=mm.NodesData.items(demand_items=np.array([0.6, 0.4]),
                                                       supply_items=np.array([0.4, 0.6]),
                                                       matching_graph=TestMatchingGraph.N_graph),
                       costs=mm.NodesData(data=np.ones(4), matching_graph=TestMatchingGraph.N_graph),
                       init_state=mm.State.zeros(matching_graph=TestMatchingGraph.N_graph), state_space="state")

    def test_init(self):
        matching_graph = TestMatchingGraph.N_graph
        alpha = np.array([0.6, 0.4])
        beta = np.array([0.4, 0.6])
        arrival_dist = mm.NodesData.items(demand_items=alpha, supply_items=beta, matching_graph=matching_graph)
        costs = mm.NodesData(data=np.ones(4), matching_graph=matching_graph)
        init_state = mm.State.zeros(matching_graph=matching_graph)
        N_model_a = mm.Model(matching_graph=matching_graph, arrival_dist=arrival_dist, costs=costs,
                             init_state=init_state, state_space="state")

        assert N_model_a.matching_graph == matching_graph
        assert N_model_a.arrival_dist == arrival_dist
        assert N_model_a.costs == costs
        assert N_model_a.init_state == init_state

    def test_sample_arrivals(self):
        N = 10000.
        total_arrivals = mm.State.zeros(TestModel.N_model.matching_graph)
        for _ in np.arange(N):
            arrivals = TestModel.N_model.sample_arrivals()
            total_arrivals += arrivals
        mean_arrivals = total_arrivals.data / N

        assert np.allclose(mean_arrivals, TestModel.N_model.arrival_dist.data, atol=1e-1)

    def test_iterate(self):
        # costs = mm.NodesData(data=np.array([3., 1., 2., 3.]), matching_graph=TestMatchingGraph.N_graph)
        # policies = [po.Threshold_N(threshold=3), po.MaxWeight(costs=costs)]
        # states_list = []
        # # We initialize each state to the initial state of the model init_state and reset each policy
        # for policy in policies:
        #     states_list.append(TestModel.N_model.init_state.copy())
        #     policy.reset_policy(TestModel.N_model.init_state)
        #
        # states_list = TestModel.N_model.iterate(states_list=states_list, policies=policies)
        # state_threshold_a = mm.State(values=np.array([0., 0.]))
        # assert np.all(states_list == [])
        pass

    def test_run(self):
        pass
def test_thresholds_with_priorities_init():
    matching_graph = mm.MatchingGraph(edges=[(1, 1), (1, 2), (2, 2)],
                                      nb_demand_classes=2,
                                      nb_supply_classes=2)
    matching_order = mm.EdgesData(data=np.array([0, 2, 1]),
                                  matching_graph=matching_graph)
    thresholds = mm.EdgesData(data=np.array([3., 0., 0.]),
                              matching_graph=matching_graph)
    policy = po.ThresholdsWithPriorities(state_space="state",
                                         matching_order=matching_order,
                                         thresholds=thresholds)

    assert policy.matching_order is matching_order
    assert policy.thresholds is policy.thresholds
    def test_init(self):
        matching_graph = TestMatchingGraph.N_graph
        alpha = np.array([0.6, 0.4])
        beta = np.array([0.4, 0.6])
        arrival_dist = mm.NodesData.items(demand_items=alpha, supply_items=beta, matching_graph=matching_graph)
        costs = mm.NodesData(data=np.ones(4), matching_graph=matching_graph)
        init_state = mm.State.zeros(matching_graph=matching_graph)
        N_model_a = mm.Model(matching_graph=matching_graph, arrival_dist=arrival_dist, costs=costs,
                             init_state=init_state, state_space="state")

        assert N_model_a.matching_graph == matching_graph
        assert N_model_a.arrival_dist == arrival_dist
        assert N_model_a.costs == costs
        assert N_model_a.init_state == init_state
    def test_matchings_available(self):
        matchings = [(1, 1), (1, 2), (2, 2)]
        assert np.all(TestState.N_state.matchings_available() == matchings)

        N_state_a = mm.State(values=np.array([5, 0, 5, 0]), matching_graph=TestMatchingGraph.N_graph)
        matchings_a = [(1, 1)]
        assert np.all(N_state_a.matchings_available() == matchings_a)

        N_state_b = mm.State(values=np.array([5, 0, 0, 5]), matching_graph=TestMatchingGraph.N_graph)
        matchings_b = [(1, 2)]
        assert np.all(N_state_b.matchings_available() == matchings_b)

        N_state_c = mm.State(values=np.array([2, 3, 0, 5]), matching_graph=TestMatchingGraph.N_graph)
        matchings_c = [(1, 2), (2, 2)]
        assert np.all(N_state_c.matchings_available() == matchings_c)
def test_thresholds_with_priorities_init_different_matching_graph():
    matching_graph_o = mm.MatchingGraph(edges=[(1, 1), (1, 2), (2, 2)],
                                        nb_demand_classes=2,
                                        nb_supply_classes=2)
    matching_graph_t = mm.MatchingGraph(edges=[(2, 1), (1, 1)],
                                        nb_demand_classes=2,
                                        nb_supply_classes=1)
    matching_order = mm.EdgesData(data=np.array([0, 2, 1]),
                                  matching_graph=matching_graph_o)
    thresholds = mm.EdgesData(data=np.array([3., 0.]),
                              matching_graph=matching_graph_t)
    with pytest.raises(AssertionError):
        _ = po.ThresholdsWithPriorities(state_space="state",
                                        matching_order=matching_order,
                                        thresholds=thresholds)
    def test_zeros(self):
        data = np.zeros(3)
        matching_graph = mm.MatchingGraph(edges=[(1, 1), (1, 2), (2, 2)], nb_demand_classes=2, nb_supply_classes=2)
        ed = mm.EdgesData.zeros(matching_graph=matching_graph)

        assert np.all(ed.data == data)
        assert ed.matching_graph is matching_graph
Exemplo n.º 22
0
 def reset_policy(self, x_0):
     # We reset the previous state, matching and virtual state
     self.previous_x = x_0.copy()
     self.previous_match = mm.Matching.zeros(x_0)
     self.virtual_x = mm.Virtual_State(x_0.data.copy(), x_0.matchingGraph)
     # We empty the list of incomplete matchings
     self.incomplete_matchings = []
    def test_build_state_space(self):
        N_value_function_a = rl.ValueFunction(model=TestValueFunction.N_model)
        state_space_theory_state_tuples = [(0., 0., 0., 0.), (0., 1., 0., 1.), (0., 2., 0., 2.), (0., 1., 1., 0.),
                                           (0., 2., 1., 1.), (0., 2., 2., 0.), (1., 0., 0., 1.), (1., 1., 0., 2.),
                                           (1., 1., 1., 1.), (1., 2., 1., 2.), (1., 2., 2., 1.), (2., 0., 0., 2.),
                                           (2., 1., 1., 2.), (2., 2., 2., 2.), (1., 0., 1., 0.), (1., 1., 2., 0.),
                                           (2., 0., 1., 1.), (2., 1., 2., 1.), (2., 0., 2., 0.)]
        state_space_theory_arrivals_tuples = [(1., 0., 1., 0.), (1., 0., 0., 1.), (0., 1., 1., 0.), (0., 1., 0., 1.)]
        state_space_theory = [(mm.State(values=np.array(state_tuple), matching_graph=TestValueFunction.N_graph,
                                        capacity=TestValueFunction.capacity),
                               mm.State(values=np.array(arrivals_tuple), matching_graph=TestValueFunction.N_graph,
                                        capacity=TestValueFunction.capacity))
                              for state_tuple, arrivals_tuple in itertools.product(state_space_theory_state_tuples,
                                                                                   state_space_theory_arrivals_tuples)]

        assert np.all(N_value_function_a.state_space == state_space_theory)
    def test_init(self):
        data = np.array([5, 3, 3, 5])
        matching_graph = TestMatchingGraph.N_graph
        N_nodes = mm.NodesData(data=data, matching_graph=matching_graph)

        assert np.all(N_nodes.data == data)
        assert N_nodes.matching_graph == matching_graph
    def test_eq(self):
        edges = [(1, 1), (1, 2), (2, 2)]
        nb_demand_classes = 2
        nb_supply_classes = 2
        N_graph_bis = mm.MatchingGraph(edges=edges, nb_demand_classes=nb_demand_classes,
                                       nb_supply_classes=nb_supply_classes)

        assert TestMatchingGraph.N_graph == N_graph_bis
    def test_from_dict_with_default(self):
        data = {(1, 1): 2., (1, 2): 0.}
        data_array = np.array([data[(1, 1)], data[(1, 2)], 0.])
        matching_graph = mm.MatchingGraph(edges=[(1, 1), (1, 2), (2, 2)], nb_demand_classes=2, nb_supply_classes=2)
        ed = mm.EdgesData.from_dict(data=data, matching_graph=matching_graph)

        assert np.all(ed.data == data_array)
        assert ed.matching_graph is matching_graph
Exemplo n.º 27
0
    def match_D(self, x):
        # We compute the workload w
        w = np.inner(x.data, self.XiD)
        # We computed the pertubated states and workload
        xtil = x.data + self.beta * (np.exp(-x.data / self.beta) - 1.)
        wtil = np.sign(w) * (np.abs(w) + self.beta *
                             (np.exp(-np.abs(w) / self.beta) - 1.))

        # The function h is the sum of two function: \hat{h}(w) et h_c(state)
        # We compute the gradient of h_c(state)
        grad_ctil = np.multiply(self.costs.data,
                                (1. - np.exp(-x.data / self.beta)))
        # print('grad_ctil',grad_ctil)
        if w >= 0:
            grad_barCtil = self.barC_plus * (1. -
                                             np.exp(-w / self.beta)) * self.XiD
        else:
            grad_barCtil = -self.barC_minus * (
                1. - np.exp(w / self.beta)) * self.XiD
        # print('xtil',xtil)
        # print('cxtil',np.inner(self.costs.data, xtil))
        grad_h_c = 2. * self.kappa * (
            np.inner(self.costs.data, xtil) -
            np.maximum(self.barC_plus * wtil, -self.barC_minus * wtil)) * (
                grad_ctil - grad_barCtil)
        # We compute the derivative of \hat{h}(w)
        if w >= 0:
            hat_hprime = 2 * self.Aplus * w + self.Bplus
        elif w < 0 and w >= -self.tau_star:
            # hat_hprime = 2*self.Aminus + self.Bminus + self.Dminus*self.THETA*np.exp(self.THETA*w) # Ana code version
            hat_hprime = 2 * self.Aminus * w + self.Bminus + self.Dminus * self.THETA * np.exp(
                self.THETA * w)  # My version
        else:
            ws = self.tau_star + w
            hat_hprime = (self.barC_minus /
                          self.delta_plus) * (ws + (1 / self.theta) *
                                              (1 - np.exp(self.theta * ws)))
        # Finally, we compute the gradient of h(state)
        grad_h = mm.NodesData(hat_hprime * self.XiD + grad_h_c,
                              x.matchingGraph)
        grad_h_index = np.array([
            np.sum(grad_h[x.matchingGraph.edges[i]])
            for i in np.arange(len(x.matchingGraph.edges))
        ])

        prob = hMWT("hMWT")
        prob.model(x, grad_h_index, w, self.tau_star, self.Idle_index,
                   self.NUmax)
        prob.optimize(False)
        if prob.is_solution == True:
            u_star = mm.Matching.zeros(x)
            for i in np.arange(len(x.matchingGraph.edges)):
                u_star[x.matchingGraph.edges[i]] += prob.u[i].val
            return u_star
        else:
            raise ValueError('The MIP optimizer has not found a solution')
    def test_init(self):
        values_a = np.array([1., 1., 1.])
        N_matching_a = mm.Matching(state=TestState.N_state, values=values_a)

        assert N_matching_a.state == TestState.N_state
        assert np.all(N_matching_a.data == values_a)
        assert N_matching_a.matching_graph == TestState.N_state.matching_graph

        values_b = np.array([-2., 1., 1.])
        with pytest.raises(ValueError):
            _ = mm.Matching(state=TestState.N_state, values=values_b)

        values_c = np.array([4., 0., 0.])
        with pytest.raises(ValueError):
            _ = mm.Matching(state=TestState.N_state, values=values_c)

        values_d = np.array([1., 1.])
        with pytest.raises(AssertionError):
            _ = mm.Matching(state=TestState.N_state, values=values_d)
Exemplo n.º 29
0
    def match(self, equilibre=True, tauxEquilibre=0.10, path='',analyzer=None):
        if self.edt_charge and self.parcours_charge and self.voeux_charges:
            if self.affectationFaite:
                # print "2222222222222222222222222222222222222222222222222"
                self.preparer_condition_matching_courant()
            if self.UE_modifiees_significativement:
                self.maj_suite_a_une_modification_significative_ue()
            if tauxEquilibre >= 0 and tauxEquilibre <= 1.0:
                self.tauxEquilibre = tauxEquilibre      #un changement de taux d'equilibre persiste
            MM = MatchingModel(self,equilibre)
            value = MM.match(path)
            if analyzer != None:
                if not MM.infeasible:
                    analyzer.add_MatchingModel(MM)

            self.affectationFaite = True
            print MM
            return value
        else:
            print u"\033[31;1mEtat des chargements indispensables : \nVoeux charges : {}\t EDT charge : {}\t Parcours charges : {}\n\033[37;1m".format(self.voeux_charges, self.edt_charge, self.parcours_charge)
Exemplo n.º 30
0
def demo_N_value_iteration(do_value_iteration=False, nb_value_iterations=100):
    N_graph = mm.MatchingGraph(edges=[(1, 1), (1, 2), (2, 2)], nb_demand_classes=2, nb_supply_classes=2)
    epsilon = 0.1
    alpha = np.array([(1. / 2.) + epsilon, (1. / 2.) - epsilon])
    beta = np.array([(1. / 2.) - epsilon, (1. / 2.) + epsilon])
    arrival_dist = mm.NodesData.items(demand_items=alpha, supply_items=beta, matching_graph=N_graph)
    costs = mm.NodesData(data=np.array([1., 10., 10., 1.]), matching_graph=N_graph)
    capacity = 5.
    x0 = mm.State.zeros(matching_graph=N_graph, capacity=capacity)
    init_arrival = mm.State(values=np.array([0., 1., 1., 0.]), matching_graph=N_graph, capacity=capacity)
    N_model = mm.Model(matching_graph=N_graph, arrival_dist=arrival_dist, costs=costs, init_state=x0,
                       capacity=capacity, penalty=100., state_space="state_with_arrival", discount=0.9,
                       init_arrival=init_arrival)
    value_iteration_result_file = "value_iteration_N_graph.p"

    if do_value_iteration:
        print("Start of value iteration...")
        t = time.time()
        value_iteration = rl.ValueIteration(model=N_model)
        value_iteration.run(nb_iterations=nb_value_iterations, save_file=value_iteration_result_file)
        print("End of value iteration, runtime: {}".format(time.time() - t))

    P = [po.ValueIterationOptimal(state_space="state_and_arrival", model=N_model,
                                  load_file=value_iteration_result_file),
         po.MaxWeight(state_space="state_and_arrival", costs=costs)]
    print("For debug")

    plt.ion()

    res = N_model.run(nb_iter=1000, policies=P, plot=True)

    t = time.time()
    N = 10000
    c, x = N_model.discounted_cost(N, P, plot=True)
    print(time.time() - t)
    for i in range(len(P)):
        print(P[i], ": ", c[i][N])

    plt.ioff()
    plt.show()