def test_contradictory_independece_Cdi_Rdi(): Cd = [[0,1,3], [2,4]] Ci = [(1,4)] Rd = {1: [[0,1,5], [6,2]]} Ri = {3: [(0,1)]} with pytest.raises(ValueError): vu.validate_crp_constrained_input(5, Cd, Ci, Rd, Ri)
def test_single_customer_dependence_Ri(): Cd = [[0,1], [4,5,2]] Ci = [] Rd = {0: [[1,3,4], [10]]} Ri = {} with pytest.raises(ValueError): vu.validate_crp_constrained_input(6, Cd, Ci, Rd, Ri)
def test_duplicate_dependence_Rd(): Cd = [[2,3], [4,5,0]] Ci = [] Rd = {2: [[0,1], [1,1]]} Ri = {} with pytest.raises(ValueError): vu.validate_crp_constrained_input(6, Cd , Ci, Rd, Ri)
def simulate_crp_constrained(N, alpha, Cd, Ci, Rd, Ri, rng=None): """Simulates a CRP with N customers and concentration alpha. Cd is a list, where each entry is a list of friends. Ci is a list of tuples, where each tuple is a pair of enemies.""" if rng is None: rng = gen_rng() vu.validate_crp_constrained_input(N, Cd, Ci, Rd, Ri) assert N > 0 and alpha > 0. # Initial partition. Z = [-1] * N # Friends dictionary from Cd. friends = {col: block for block in Cd for col in block} # Assign customers. for cust in xrange(N): # If the customer has been assigned, skip. if Z[cust] > -1: continue # Find valid tables for cust and friends. assert all(Z[f] == -1 for f in friends.get(cust, [cust])) prob_table = [0] * (max(Z) + 1) for t in xrange(max(Z) + 1): # Current customers at table t. t_custs = [i for i, z in enumerate(Z) if z == t] prob_table[t] = len(t_custs) # Does f \in {cust \union cust_friends} have an enemy in table t? for tc in t_custs: for f in friends.get(cust, [cust]): if not vu.check_compatible_customers( Cd, Ci, Ri, Rd, f, tc): prob_table[t] = 0 break # Choose from valid tables using CRP. prob_table.append(alpha) assignment = pflip(prob_table, rng=rng) for f in friends.get(cust, [cust]): Z[f] = assignment # At most N tables. assert all(0 <= t < N for t in Z) assert vu.validate_crp_constrained_partition(Z, Cd, Ci, Rd, Ri) return Z
def test_valid_constraints(): Cd = [[0,3], [2,4], [5,6]] Ci = [(0,2), (5,2)] Rd = {0:[[1,4]], 3:[[1,2], [9,5]]} Ri = {0:[(1,19)], 4:[(1,2)]} assert vu.validate_crp_constrained_input(7, Cd, Ci, Rd, Ri)
def test_contradictory_independece_Cdi(): Cd = [[0,1,3], [2,4]] Ci = [(0,1)] Rd = Ri = {} with pytest.raises(ValueError): vu.validate_crp_constrained_input(5, Cd, Ci, Rd, Ri)
def test_single_customer_dependence_Cd(): Cd = [[0], [4,5,2]] Ci = [] Rd = Ri = {} with pytest.raises(ValueError): vu.validate_crp_constrained_input(6, Cd, Ci, Rd, Ri)
def test_duplicate_Cd(): Cd = [[0,2,3], [4,5,0]] Ci = [] Rd = Ri = {} with pytest.raises(ValueError): vu.validate_crp_constrained_input(6, Cd , Ci, Rd, Ri)