Exemplo n.º 1
0
 def test_opt_attack_rate(self):
     network = MAS.Network(*generate_asymmetric())
     attack_routing = np.array([[0.0, 0.0, 1.0], [0.5, 0.0, 0.5], [0.5, 0.5, 0.0]])
     nu_init = np.array([1.0, 0.0, 0.0])
     k = 2
     network.opt_attack_rate(attack_routing, k, nu_init)
     print network.new_availabilities()
Exemplo n.º 2
0
 def test_availabilities(self):
     # compute and check availabilities for asymmetric network
     rates, routing, travel_times = generate_asymmetric()
     rates[0] = 2.
     network = MAS.Network(rates, routing, travel_times)
     self.assertTrue(
         is_equal(network.availabilities(), np.array([.25, .5, 1.])))
 def test_to_cplex_lp_file(self):
     # test if it generates the right string
     network = MAS.Network(*generate_asymmetric())
     target = np.array([ 0.25, 0.5, 1.])
     cost = np.ones((3,3))
     coeff, sources = MinAttackSolver(network, target, cost).min_cost_flow_init()
     string = to_cplex_lp_file(coeff, sources, network.full_adjacency)
Exemplo n.º 4
0
 def test_opt_attack_rate(self):
     network = MAS.Network(*generate_asymmetric())
     attack_routing = np.array([[0., 0., 1.], [.5, 0., .5], [.5, .5, 0.]])
     nu_init = np.array([1., 0., 0.])
     k = 2
     network.opt_attack_rate(attack_routing, k, nu_init)
     print network.new_availabilities()
Exemplo n.º 5
0
 def generate_solver(self):
     # generate asymmetric network
     network = MAS.Network(*generate_asymmetric())
     attack_routing = np.array([[0., 0., 1.],[.5, 0., .5],[.5, .5, 0.]])
     nu = np.array([1., 0., 0.])
     k = 2
     return AttackRateSolver(network, attack_routing, k, nu)
Exemplo n.º 6
0
 def test_balance(self):
     # test if the balance strategy effectively balance the network
     network = MAS.Network(*generate_asymmetric())
     tmp = np.array([.5, .5, 1.])
     self.assertTrue(is_equal(network.new_availabilities(), tmp))
     opt_rates, opt_routing = network.balance()
     tmp = np.array([1., 1., 1.])
     self.assertTrue(is_equal(network.new_availabilities(), tmp))
Exemplo n.º 7
0
 def test_balance(self):
     # test if the balance strategy effectively balance the network
     network = MAS.Network(*generate_asymmetric())
     tmp = np.array([0.5, 0.5, 1.0])
     self.assertTrue(is_equal(network.new_availabilities(), tmp))
     opt_rates, opt_routing = network.balance()
     tmp = np.array([1.0, 1.0, 1.0])
     self.assertTrue(is_equal(network.new_availabilities(), tmp))
 def test_to_cplex_lp_file_adj(self):
     network = MAS.Network(*generate_asymmetric())
     network.adjacency_1 = np.array([[0, 1, 1],
                                     [1, 0, 0],
                                     [1, 0, 0]])
     target = np.array([ 0.25, 0.5, 1.])
     cost = np.ones((3,3))
     coeff, sources = MinAttackSolver(network, target, cost, full_adj=True).min_cost_flow_init()
     print to_cplex_lp_file(coeff, sources, network.adjacency_1)
 def test_constraints(self):
     # generate asymmetric network with availabilities = [ 0.5  0.5  1. ]
     network = MAS.Network(*generate_asymmetric())
     # an attack strategy that results in availabilities = [ 0.25  0.5   1. ]
     attack_rates = np.array([1., 0., 0.])
     attack_routing = np.array([[0., 0., 1.], [.5, 0., .5], [.5, .5, 0.]])
     network.update(attack_rates, attack_routing)
     # fix the availability at station 2 to be equal to 1
     k = 2
     b, A = AttackRoutingSolver(network, attack_rates, k).constraints()
 def test_constraints(self):
     # generate asymmetric network with availabilities = [ 0.5  0.5  1. ]
     network = MAS.Network(*generate_asymmetric())
     # an attack strategy that results in availabilities = [ 0.25  0.5   1. ]
     attack_rates = np.array([1., 0., 0.])
     attack_routing = np.array([[0., 0., 1.],[.5, 0., .5],[.5, .5, 0.]])
     network.update(attack_rates, attack_routing)
     # fix the availability at station 2 to be equal to 1
     k = 2
     b, A = AttackRoutingSolver(network, attack_rates, k).constraints()
Exemplo n.º 11
0
 def test_min_attack_solver(self):
     # generate asymmetric network
     network = MAS.Network(*generate_asymmetric())
     target = np.ones((3,))
     cost = np.ones((3,3))
     opt_rates, opt_routing = MinAttackSolver(network, target, cost).solve()
     self.assertTrue(is_equal(opt_rates, np.array([0.,0.,1.])))
     tmp = .5 * np.ones((3, 3))
     tmp[range(3), range(3)] = 0.0
     self.assertTrue(is_equal(opt_routing, tmp))
Exemplo n.º 12
0
 def test_min_cost_flow_init(self):
     # generate asymmetric network
     network = MAS.Network(*generate_asymmetric())
     # target for the availabilities
     target = np.array([.5, 1., 1.])
     # cost is uniform
     cost = np.ones((3,3))
     coeff, sources = MinAttackSolver(network, target, cost).min_cost_flow_init()
     # check coefficients
     tmp = np.ones((3,3))
     tmp[0,:] = np.array([2., 2., 2.])
     self.assertTrue(is_equal(coeff, tmp))
     # check sources
     self.assertTrue(is_equal(sources, np.array([.0, .5, -.5])))
Exemplo n.º 13
0
 def test_flow_to_rates_routing(self):
     network = MAS.Network(*generate_asymmetric())
     flow = np.zeros((3, 3))
     flow[0,1] = .25
     flow[0,2] = .25
     flow[1,0] = .5
     flow[1,2] = .5
     cost = np.ones((3,3))
     target = np.array([.5, 1., 1.])
     rates, routing = MinAttackSolver(network, target, cost).flow_to_rates_routing(flow)
     # check rates
     self.assertTrue(is_equal(rates, np.array([1., 1., 0.])))
     # check routing
     tmp = .5 * np.ones((3, 3))
     tmp[range(3), range(3)] = 0.0
     self.assertTrue(is_equal(routing, tmp))
 def test_single_destination_attack(self):
     # generate asymmetric MAS_network with availabilities [0.5  0.5  1.0]
     # and attack on station 2 with budget = 1.0 (default)
     network = MAS.Network(*generate_asymmetric())
     sol = SingleDestinationAttack(network, 2).apply()
     attack_rates, attack_routing = sol['attack_rates'], sol['attack_routing']
     network.update(attack_rates, attack_routing)
     self.assertTrue(is_equal(network.new_availabilities(), np.array([1./3, 1./3, 1.])))
     self.assertTrue(is_equal(sol['alpha'], 1.5))
     # now attack on station 1
     sol = SingleDestinationAttack(network, 1).apply()
     attack_rates, attack_routing = sol['attack_rates'], sol['attack_routing']
     network.update(attack_rates, attack_routing)
     self.assertTrue(is_equal(network.new_availabilities(), np.array([1./3, 1., 2./3])))
     self.assertTrue(is_equal(sol['alpha'], 1.5))
     # now attack on station 1 with 0.49 budget -> inefficient attack
     network.budget = 0.49
     sol = SingleDestinationAttack(network, 1).apply()
     attack_rates, attack_routing = sol['attack_rates'], sol['attack_routing']
     network.update(attack_rates, attack_routing)
     self.assertTrue(is_equal(network.new_availabilities(), np.array([.5, .5, 1.])))        
Exemplo n.º 15
0
 def test_single_destination_attack(self):
     network = MAS.Network(*generate_asymmetric())
     network.single_destination_attack(2)
     self.assertTrue(
         is_equal(network.new_availabilities(),
                  np.array([1. / 3, 1. / 3, 1.])))
Exemplo n.º 16
0
 def test_throughputs_2(self):
     # compute and check throughputs for asymmetric network
     network = MAS.Network(*generate_asymmetric())
     self.assertTrue(is_equal(network.throughputs(), np.array([0.25, 0.25, 0.5])))
Exemplo n.º 17
0
 def test_min_attack(self):
     # test if the attacks affectively achieve the target availabilities
     target = np.array([.25, .5, 1.])
     network = MAS.Network(*generate_asymmetric())
     opt_rates, opt_routing = network.min_attack(target)
     self.assertTrue(is_equal(network.new_availabilities(), target))
Exemplo n.º 18
0
 def test_throughputs_2(self):
     # compute and check throughputs for asymmetric network
     network = MAS.Network(*generate_asymmetric())
     self.assertTrue(
         is_equal(network.throughputs(), np.array([.25, .25, .5])))
Exemplo n.º 19
0
 def test_cplex_balance_small_network(self):
     network = MAS.Network(*generate_asymmetric())
     tmp = np.array([0.5, 0.5, 1.0])
     self.assertTrue(is_equal(network.new_availabilities(), tmp))
     network.balance(cplex=True)
     self.assertTrue(is_equal(network.new_availabilities(), np.ones((3,))))
Exemplo n.º 20
0
 def test_single_destination_attack(self):
     network = MAS.Network(*generate_asymmetric())
     network.single_destination_attack(2)
     self.assertTrue(is_equal(network.new_availabilities(), np.array([1.0 / 3, 1.0 / 3, 1.0])))
Exemplo n.º 21
0
 def test_cplex_balance_small_network(self):
     network = MAS.Network(*generate_asymmetric())
     tmp = np.array([.5, .5, 1.])
     self.assertTrue(is_equal(network.new_availabilities(), tmp))
     network.balance(cplex=True)
     self.assertTrue(is_equal(network.new_availabilities(), np.ones((3, ))))
Exemplo n.º 22
0
 def test_availabilities(self):
     # compute and check availabilities for asymmetric network
     rates, routing, travel_times = generate_asymmetric()
     rates[0] = 2.0
     network = MAS.Network(rates, routing, travel_times)
     self.assertTrue(is_equal(network.availabilities(), np.array([0.25, 0.5, 1.0])))
Exemplo n.º 23
0
 def test_min_attack(self):
     # test if the attacks affectively achieve the target availabilities
     target = np.array([0.25, 0.5, 1.0])
     network = MAS.Network(*generate_asymmetric())
     opt_rates, opt_routing = network.min_attack(target)
     self.assertTrue(is_equal(network.new_availabilities(), target))