Ejemplo n.º 1
0
def test_dc_0():
    c1 = SimpleTemporalConstraint('e1', 'e2', 2, 5, 'c1')
    c2 = SimpleContingentTemporalConstraint('e3', 'e2', 4, 7, 'c2')
    network = TemporalNetwork([c1, c2])

    checker = DCCheckerBE(network)
    feasible, conflict = checker.is_controllable()
    assert (feasible)

    checker = DCCheckerMILP(network)
    feasible, _ = checker.is_controllable()
    assert (feasible)

    c1 = SimpleTemporalConstraint('e1', 'e2', 3, 5, 'c1')
    c2 = SimpleContingentTemporalConstraint('e3', 'e2', 4, 7, 'c2')
    network = TemporalNetwork([c1, c2])

    checker = DCCheckerBE(network)
    feasible, conflict = checker.is_controllable()
    assert (not feasible)
    assert (len(conflict) == 2)
    assert (len(conflict[0]) == 3)
    assert (len(conflict[1]) == 1)
    assert ([c1, 'UB+'] in conflict[0])
    assert ([c1, 'LB-'] in conflict[0])
    assert ([c2, 'UB-', 'LB+'] in conflict[0])
    assert ([c1, 'LB-'] in conflict[1])

    checker = DCCheckerMILP(network)
    feasible, _ = checker.is_controllable()
    assert (not feasible)
Ejemplo n.º 2
0
def test_dc_4():
    c1 = SimpleContingentTemporalConstraint('e1', 'e2', 1, 10, 'c1')
    c2 = SimpleTemporalConstraint('e3', 'e2', 1, 2, 'c2')
    c3 = SimpleContingentTemporalConstraint('e1', 'e3', 1, 8, 'c3')
    network = TemporalNetwork([c1, c2, c3])

    checker = DCCheckerBE(network)
    feasible, conflict = checker.is_controllable()
    assert (not feasible)

    checker = DCCheckerMILP(network)
    feasible, _ = checker.is_controllable()
    assert (not feasible)

    c1 = SimpleContingentTemporalConstraint('e1', 'e2', 0, 10, 'c1')
    c2 = SimpleTemporalConstraint('e3', 'e2', 0, 2, 'c2')
    c3 = SimpleContingentTemporalConstraint('e1c', 'e3', 0, 8, 'c3')
    c4 = SimpleTemporalConstraint('e1', 'e1c', 0, 0, 'c4')
    network = TemporalNetwork([c1, c2, c3, c4])

    checker = DCCheckerBE(network)
    feasible, conflict = checker.is_controllable()
    assert (not feasible)

    checker = DCCheckerMILP(network)
    feasible, _ = checker.is_controllable()
    assert (not feasible)

    c1 = SimpleContingentTemporalConstraint('e1', 'e2', 0, 10, 'c1')
    c2 = SimpleTemporalConstraint('e3', 'e2', 0, 2, 'c2')
    c3 = SimpleContingentTemporalConstraint('e1', 'e3', 0, 8, 'c3')
    network = TemporalNetwork([c1, c2, c3])

    checker = DCCheckerBE(network)
    feasible, conflict = checker.is_controllable()
    assert (not feasible)

    checker = DCCheckerMILP(network)
    feasible, _ = checker.is_controllable()
    assert (not feasible)

    c1 = SimpleContingentTemporalConstraint('e1', 'e2', 1, 10, 'c1')
    c2 = SimpleTemporalConstraint('e3', 'e2', 1, 2, 'c2')
    c3 = SimpleTemporalConstraint('e1', 'e3', 1, 8, 'c3')
    network = TemporalNetwork([c1, c2, c3])

    checker = DCCheckerBE(network)
    feasible, conflict = checker.is_controllable()
    assert (not feasible)

    checker = DCCheckerMILP(network)
    feasible, _ = checker.is_controllable()
    assert (not feasible)
Ejemplo n.º 3
0
def test_dc_1():
    c1 = SimpleContingentTemporalConstraint('e1', 'e2', 20, 30, 'c1')
    c2 = SimpleTemporalConstraint('e2', 'e3', 40, 45, 'c2')
    c3 = SimpleTemporalConstraint('e1', 'e3', 0, 50, 'c3')
    network = TemporalNetwork([c1, c2, c3])

    checker = DCCheckerBE(network)
    feasible, conflict = checker.is_controllable()
    assert (not feasible)
    assert (len(conflict) == 1)
    assert (len(conflict[0]) == 4)
    assert ([c3, 'UB+'] in conflict[0])
    assert ([c2, 'LB-'] in conflict[0])
    assert ([c1, 'UB-', 'LB+'] in conflict[0])
    assert ([c1, 'LB-'] in conflict[0])

    checker = DCCheckerMILP(network)
    feasible, _ = checker.is_controllable()
    assert (not feasible)

    c1 = SimpleContingentTemporalConstraint('e1', 'e2', 5, 30, 'c1')
    c2 = SimpleTemporalConstraint('e2', 'e3', 40, 45, 'c2')
    c3 = SimpleTemporalConstraint('e1', 'e3', 0, 50, 'c3')
    network = TemporalNetwork([c1, c2, c3])

    checker = DCCheckerBE(network)
    feasible, conflict = checker.is_controllable()
    assert (not feasible)
    assert (len(conflict) == 1)
    assert (len(conflict[0]) == 4)
    assert ([c3, 'UB+'] in conflict[0])
    assert ([c2, 'LB-'] in conflict[0])
    assert ([c1, 'UB-', 'LB+'] in conflict[0])
    assert ([c1, 'LB-'] in conflict[0])

    checker = DCCheckerMILP(network)
    feasible, _ = checker.is_controllable()
    assert (not feasible)

    c1 = SimpleContingentTemporalConstraint('e1', 'e2', 5, 10, 'c1')
    c2 = SimpleTemporalConstraint('e2', 'e3', 40, 45, 'c2')
    c3 = SimpleTemporalConstraint('e1', 'e3', 0, 50, 'c3')
    network = TemporalNetwork([c1, c2, c3])

    checker = DCCheckerBE(network)
    feasible, conflict = checker.is_controllable()
    assert (feasible)

    checker = DCCheckerMILP(network)
    feasible, conflict = checker.is_controllable()
    assert (feasible)
Ejemplo n.º 4
0
def test_milp_preprocess():
    # A ===> B ====> C ---> D ====> E ----> F
    c1 = SimpleContingentTemporalConstraint('e1', 'e2', 2, 5, 'c1')
    c2 = SimpleContingentTemporalConstraint('e2', 'e3', 2, 5, 'c2')
    c3 = SimpleTemporalConstraint('e3', 'e4', 2, 5, 'c3')
    c4 = SimpleContingentTemporalConstraint('e4', 'e5', 2, 5, 'c4')
    c5 = SimpleTemporalConstraint('e5', 'e6', 2, 5, 'c5')
    network = TemporalNetwork([c1, c2, c3, c4, c5])

    checker = DCCheckerMILP(network)
    processed_network = checker.preprocess_network(network)
    assert (len(processed_network.get_constraints()) == 6)
    assert (len(network.get_constraints()) == 5)
    feasible, _ = checker.is_controllable()
    assert (feasible)
Ejemplo n.º 5
0
def run_random_stnus(num_trials, num_cont=5):
	"""
	Generate NUM_TRIALS networks and run it on BE and MILP checking
	"""

	# Generate networks
	networks = []
	for i in range(num_trials):
		networks.append(generate_network(num_cont))

	# Run BE algorithm
	start = timeit.default_timer()

	for network in networks:
		checker = DCCheckerBE(network)
		controllable, conflict = checker.is_controllable()
		# print(controllable, conflict)

	stop = timeit.default_timer()
	print('BE Time: ', stop - start) 

	# Run MILP algorithm
	start = timeit.default_timer()

	for network in networks:
		checker = DCCheckerMILP(network)
		controllable, _ = checker.is_controllable()
		# print(controllable)

	stop = timeit.default_timer()
	print('MILP Time: ', stop - start) 
Ejemplo n.º 6
0
def test_dc_2():
    c1 = SimpleContingentTemporalConstraint('e1', 'e2', 5, 30, 'c1')
    c2 = SimpleTemporalConstraint('e3', 'e2', 1, 1, 'c2')
    network = TemporalNetwork([c1, c2])

    checker = DCCheckerBE(network)
    feasible, conflict = checker.is_controllable()
    assert (not feasible)

    checker = DCCheckerMILP(network)
    feasible, _ = checker.is_controllable()
    assert (not feasible)
Ejemplo n.º 7
0
def test_dc_7():
    c1 = SimpleContingentTemporalConstraint('e1', 'e2', 3, 3.5, 'c1')
    c2 = SimpleTemporalConstraint('e1', 'e2', 4, 6, 'c2')
    c3 = SimpleTemporalConstraint('e1', 'e2', 2, 3.5, 'c3')
    network = TemporalNetwork([c1, c2, c3])

    checker = DCCheckerBE(network)
    feasible, conflict = checker.is_controllable()
    assert (not feasible)

    checker = DCCheckerMILP(network)
    feasible, _ = checker.is_controllable()
    assert (not feasible)
Ejemplo n.º 8
0
def test_dc_6():
    c1 = SimpleContingentTemporalConstraint('e1', 'e5', 0.6294, 18.8554, 'c1')
    c2 = SimpleTemporalConstraint('e1', 'e2', 1, 100, 'c2')
    c3 = SimpleTemporalConstraint('e2', 'e5', 0, 100, 'c3')
    c4 = SimpleTemporalConstraint('e2', 'e3', 1, 100, 'c4')
    c5 = SimpleTemporalConstraint('e3', 'e4', 1.5, 100, 'c5')
    c6 = SimpleTemporalConstraint('e1', 'e4', 1, 3.5, 'c6')
    network = TemporalNetwork([c1, c2, c3, c4, c5, c6])

    checker = DCCheckerBE(network)
    feasible, conflict = checker.is_controllable()
    assert (not feasible)

    checker = DCCheckerMILP(network)
    feasible, _ = checker.is_controllable()
    assert (not feasible)
Ejemplo n.º 9
0
def test_dc_3():
    c1 = SimpleContingentTemporalConstraint('e1', 'e2', 3, 100000, 'c1')
    c2 = SimpleTemporalConstraint('e2', 'e3', -1, 100000, 'c2')
    c3 = SimpleContingentTemporalConstraint('e3', 'e4', 1, 5.5, 'c3')
    c4 = SimpleTemporalConstraint('e4', 'e5', 0, None, 'c4')
    c5 = SimpleContingentTemporalConstraint('e5', 'e6', 10, 14.5, 'c5')
    c6 = SimpleTemporalConstraint('e6', 'e7', 0, 100000, 'c6')
    c7 = SimpleTemporalConstraint('e2', 'e7', 5, 18, 'c7')
    network = TemporalNetwork([c1, c2, c3, c4, c5, c6, c7])

    checker = DCCheckerBE(network)
    feasible, conflict = checker.is_controllable()
    assert (not feasible)

    checker = DCCheckerMILP(network)
    feasible, _ = checker.is_controllable()
    assert (not feasible)
Ejemplo n.º 10
0
def test_dc_12():
    """
    Checker should be able to handle contingent links with lb == ub
    """
    # A =======[10,10]=====> C
    c1 = SimpleContingentTemporalConstraint('e1', 'e3', 10, 10, 'c1')
    network = TemporalNetwork([c1])

    checker = DCCheckerBE(network)
    feasible, conflict = checker.is_controllable()
    assert (feasible)

    checker = DCCheckerMILP(network)
    feasible, _ = checker.is_controllable()
    assert (feasible)

    # A =======[0,0]=====> C
    c1 = SimpleContingentTemporalConstraint('e1', 'e3', 0, 0, 'c1')
    network = TemporalNetwork([c1])

    checker = DCCheckerBE(network)
    feasible, conflict = checker.is_controllable()
    assert (feasible)

    checker = DCCheckerMILP(network)
    feasible, _ = checker.is_controllable()
    assert (feasible)

    # A =======[7,7]=====> C
    #  \--[0,2]->B--[0,5]--/
    c1 = SimpleContingentTemporalConstraint('e1', 'e3', 7, 7, 'c1')
    c2 = SimpleTemporalConstraint('e1', 'e2', 0, 2, 'c2')
    c3 = SimpleTemporalConstraint('e2', 'e3', 0, 5, 'c3')
    network = TemporalNetwork([c1, c2, c3])

    checker = DCCheckerBE(network)
    feasible, conflict = checker.is_controllable()
    assert (feasible)

    checker = DCCheckerMILP(network)
    feasible, _ = checker.is_controllable()
    assert (feasible)

    # A =======[7,7]=====> C
    #  \--[0,1]->B--[0,5]--/
    c1 = SimpleContingentTemporalConstraint('e1', 'e3', 7, 7, 'c1')
    c2 = SimpleTemporalConstraint('e1', 'e2', 0, 1, 'c2')
    c3 = SimpleTemporalConstraint('e2', 'e3', 0, 5, 'c3')
    network = TemporalNetwork([c1, c2, c3])

    checker = DCCheckerBE(network)
    feasible, conflict = checker.is_controllable()
    assert (not feasible)

    checker = DCCheckerMILP(network)
    feasible, _ = checker.is_controllable()
    assert (not feasible)

    # A =======[7,7]=====> C
    #  \--[2,2]->B--[5,5]--/
    c1 = SimpleContingentTemporalConstraint('e1', 'e3', 7, 7, 'c1')
    c2 = SimpleTemporalConstraint('e1', 'e2', 2, 2, 'c2')
    c3 = SimpleTemporalConstraint('e2', 'e3', 5, 5, 'c3')
    network = TemporalNetwork([c1, c2, c3])

    checker = DCCheckerBE(network)
    feasible, conflict = checker.is_controllable()
    assert (feasible)

    checker = DCCheckerMILP(network)
    feasible, _ = checker.is_controllable()
    assert (feasible)

    # A =======[7,7]=====> C
    #  \==[2,2]=>B--[5,5]--/
    c1 = SimpleContingentTemporalConstraint('e1', 'e3', 7, 7, 'c1')
    c2 = SimpleContingentTemporalConstraint('e1', 'e2', 2, 2, 'c2')
    c3 = SimpleTemporalConstraint('e2', 'e3', 5, 5, 'c3')
    network = TemporalNetwork([c1, c2, c3])

    checker = DCCheckerBE(network)
    feasible, conflict = checker.is_controllable()
    assert (feasible)

    checker = DCCheckerMILP(network)
    feasible, _ = checker.is_controllable()
    assert (feasible)

    # A =======[7,7]=====> C
    #  \==[2,3]=>B--[5,5]--/
    c1 = SimpleContingentTemporalConstraint('e1', 'e3', 7, 7, 'c1')
    c2 = SimpleContingentTemporalConstraint('e1', 'e2', 2, 3, 'c2')
    c3 = SimpleTemporalConstraint('e2', 'e3', 5, 5, 'c3')
    network = TemporalNetwork([c1, c2, c3])

    checker = DCCheckerBE(network)
    feasible, conflict = checker.is_controllable()
    assert (not feasible)

    checker = DCCheckerMILP(network)
    feasible, _ = checker.is_controllable()
    assert (not feasible)
Ejemplo n.º 11
0
def test_dc_11():
    # A =======[0,10]=====> C
    #           B --[0,2]--/
    c1 = SimpleContingentTemporalConstraint('e1', 'e3', 0, 10, 'c1')
    c2 = SimpleTemporalConstraint('e2', 'e3', 0, 2, 'c2')
    network = TemporalNetwork([c1, c2])

    checker = DCCheckerBE(network)
    feasible, conflict = checker.is_controllable()
    assert (feasible)

    checker = DCCheckerMILP(network)
    feasible, _ = checker.is_controllable()
    assert (feasible)

    # A =======[0,10]=====> C
    #           B --[1,2]--/
    c1 = SimpleContingentTemporalConstraint('e1', 'e3', 0, 10, 'c1')
    c2 = SimpleTemporalConstraint('e2', 'e3', 1, 2, 'c2')
    network = TemporalNetwork([c1, c2])

    checker = DCCheckerBE(network)
    feasible, conflict = checker.is_controllable()
    assert (not feasible)

    checker = DCCheckerMILP(network)
    feasible, _ = checker.is_controllable()
    assert (not feasible)

    # A =======[0,10]=====> C
    #  \--[8,*)->B--[0,2]--/
    c1 = SimpleContingentTemporalConstraint('e1', 'e3', 0, 10, 'c1')
    c2 = SimpleTemporalConstraint('e2', 'e3', 0, 2, 'c2')
    c3 = SimpleTemporalConstraint('e1', 'e2', 8, None, 'c3')
    network = TemporalNetwork([c1, c2, c3])

    checker = DCCheckerBE(network)
    feasible, conflict = checker.is_controllable()
    assert (not feasible)

    checker = DCCheckerMILP(network)
    feasible, _ = checker.is_controllable()
    assert (not feasible)

    # A =======[0,10]=====> C
    #  \--[0,8]->B--[0,2]--/
    c1 = SimpleContingentTemporalConstraint('e1', 'e3', 0, 10, 'c1')
    c2 = SimpleTemporalConstraint('e2', 'e3', 0, 2, 'c2')
    c3 = SimpleTemporalConstraint('e1', 'e2', 0, 8, 'c3')
    network = TemporalNetwork([c1, c2, c3])

    checker = DCCheckerBE(network)
    feasible, conflict = checker.is_controllable()
    assert (feasible)

    checker = DCCheckerMILP(network)
    feasible, _ = checker.is_controllable()
    assert (feasible)

    # A =======[0,10]=====> C
    #  \==[0,8]=>B--[0,2]--/
    c1 = SimpleContingentTemporalConstraint('e1', 'e3', 0, 10, 'c1')
    c2 = SimpleTemporalConstraint('e2', 'e3', 0, 2, 'c2')
    c3 = SimpleContingentTemporalConstraint('e1', 'e2', 0, 8, 'c3')
    network = TemporalNetwork([c1, c2, c3])

    checker = DCCheckerBE(network)
    feasible, conflict = checker.is_controllable()
    assert (not feasible)

    checker = DCCheckerMILP(network)
    feasible, _ = checker.is_controllable()
    assert (not feasible)

    # A =======[0,10]=====> C
    #  \A'==[0,8]=>B-[0,2]-/
    c1 = SimpleContingentTemporalConstraint('e1', 'e3', 0, 10, 'c1')
    c2 = SimpleTemporalConstraint('e2', 'e3', 0, 2, 'c2')
    c3 = SimpleContingentTemporalConstraint('e1c', 'e2', 0, 8, 'c3')
    c4 = SimpleTemporalConstraint('e1', 'e1c', 0, 0, 'c4')
    network = TemporalNetwork([c1, c2, c3, c4])

    checker = DCCheckerBE(network)
    feasible, conflict = checker.is_controllable()
    assert (not feasible)

    checker = DCCheckerMILP(network)
    feasible, _ = checker.is_controllable()
    assert (not feasible)

    # A =======[0,10]=====> C
    #  \==[0,8]=>B
    c1 = SimpleContingentTemporalConstraint('e1', 'e3', 0, 10, 'c1')
    c2 = SimpleContingentTemporalConstraint('e1', 'e2', 0, 8, 'c3')
    network = TemporalNetwork([c1, c2])

    checker = DCCheckerBE(network)
    feasible, conflict = checker.is_controllable()
    assert (feasible)

    checker = DCCheckerMILP(network)
    feasible, _ = checker.is_controllable()
    assert (feasible)

    # A =======[0,10]=====> C ---[0,2]--> B
    c1 = SimpleContingentTemporalConstraint('e1', 'e3', 0, 10, 'c1')
    c2 = SimpleTemporalConstraint('e3', 'e2', 0, 2, 'c2')
    network = TemporalNetwork([c1, c2])

    checker = DCCheckerBE(network)
    feasible, conflict = checker.is_controllable()
    assert (feasible)

    checker = DCCheckerMILP(network)
    feasible, _ = checker.is_controllable()
    assert (feasible)

    # A =======[0,10]=====> C ===[0,2]==> B
    c1 = SimpleContingentTemporalConstraint('e1', 'e3', 0, 10, 'c1')
    c2 = SimpleContingentTemporalConstraint('e3', 'e2', 0, 2, 'c2')
    network = TemporalNetwork([c1, c2])

    checker = DCCheckerBE(network)
    feasible, conflict = checker.is_controllable()
    assert (feasible)

    checker = DCCheckerMILP(network)
    feasible, _ = checker.is_controllable()
    assert (feasible)

    # A =======[2,10]=====> C ===[1,2]==> B
    #                         \==[0,3]==> D
    c1 = SimpleContingentTemporalConstraint('e1', 'e3', 2, 10, 'c1')
    c2 = SimpleContingentTemporalConstraint('e3', 'e2', 1, 2, 'c2')
    c3 = SimpleContingentTemporalConstraint('e3', 'e4', 0, 3, 'c3')
    network = TemporalNetwork([c1, c2, c3])

    checker = DCCheckerBE(network)
    feasible, conflict = checker.is_controllable()
    assert (feasible)

    checker = DCCheckerMILP(network)
    feasible, _ = checker.is_controllable()
    assert (feasible)
Ejemplo n.º 12
0
from dc_checking.temporal_network import TemporalNetwork, SimpleContingentTemporalConstraint, SimpleTemporalConstraint
from dc_checking.dc_milp import DCCheckerMILP
from dc_checking.dc_be import DCCheckerBE

# Controllable
# c1 = SimpleContingentTemporalConstraint('e1', 'e5', 15, 18.8554, 'c1')
# Uncontrollable
c1 = SimpleContingentTemporalConstraint('e1', 'e5', 0.6294, 18.8554, 'c1')
c2 = SimpleTemporalConstraint('e1', 'e2', 1, 100, 'c2')
c3 = SimpleTemporalConstraint('e2', 'e5', 0, 100, 'c3')
c4 = SimpleTemporalConstraint('e2', 'e3', 1, 100, 'c4')
c5 = SimpleTemporalConstraint('e3', 'e4', 1.5, 100, 'c5')
c6 = SimpleTemporalConstraint('e1', 'e4', 1, 3.5, 'c6')
network = TemporalNetwork([c1, c2, c3, c4, c5, c6])

# DC Checker using Bucket Elimination
checker = DCCheckerBE(network)
controllable, conflict = checker.is_controllable(visualize=False,
                                                 visualize_conflict=False)
print(controllable, conflict)

# DC checker using MILP
checker = DCCheckerMILP(network)
controllable, _ = checker.is_controllable(outputIIS=False)
print(controllable)