def test_filter(): G = [[(1,2), (1,3)], [(2,1), (2,3)], [(3,1), (3,2)]] neighbors = [ [v for (u,v) in G[j]] for j in range(3) ] weight = [1, 1, 1] numLayers = 3 domain = lambda j: (0,1) rootState = frozenset([1,2,3]) def trFunc(s,d,j): if d == 1: if j+1 in s: return s - {j+1} - set(neighbors[j]) else: return None else: return s - {j+1} costFunc = lambda s,d,j,ns: d*weight[j] isFeas = lambda s,j: s is not None maxWidth = lambda j: 100 name = 'smisp' def hack_counter(k = [-1]): k[0] += 1 return k[0] # Construct the MDD mymdd = MDD(name) mymdd.compile_trivial(numLayers, domain, lambda d,j: costFunc(None,d,j,None), lambda j: hack_counter()) assert mymdd.numArcLayers == numLayers mymdd.filter_and_refine_constraint(trFunc, rootState, isFeas, lambda s,j: hack_counter(), maxWidth) mymdd.reduce_bottom_up(lambda slist,j: slist[0]) assert mymdd.widthList == [1,2,2,1] assert mymdd.find_shortest_path()[0] == 0 lpath = mymdd.find_longest_path() assert lpath[0] == 1 assert lpath[1] in ([1, 0, 0], [0, 1, 0], [0, 0, 1])
def test_enumerate_paths(): mymdd = MDD() mymdd.loadJSON('data/misp.json') testnode = MDDNode(2, frozenset([3, 4, 5])) assert sorted(mymdd.enumerate_all_suffixes(testnode)) == [(0, [0, 0, 0]), (2, [0, 1, 0]), (2, [1, 0, 0]), (7, [0, 0, 1]), (9, [1, 0, 1])] _assert_clean_tmp(mymdd) testnode = MDDNode(4, frozenset([5])) assert sorted(mymdd.enumerate_all_prefixes(testnode)) == [ (0, [0, 0, 0, 0]), (2, [0, 0, 1, 0]), (3, [1, 0, 0, 0]), (4, [0, 1, 0, 0]) ] _assert_clean_tmp(mymdd) mymdd.loadJSON('data/trivial.json') assert sorted(mymdd.enumerate_all_suffixes(MDDNode(0, ''))) == [] _assert_clean_tmp(mymdd)
def test_pathlist(): feasSolutions = [(0,1,0,1), (0,1,1,0), (0,1,1,1), (1,0,1,1), (1,1,0,0), (1,1,0,1), (1,1,1,0), (1,1,1,1)] cost = [6, 7, 8, 5, 6, 8, 7, 9] # MDD Parameters mergeFunc = lambda slist,i: min(slist) name = 'cac' # Construct the MDD mymdd = MDD(name) # Perform DP-based top-down compilation pathList = list(zip(cost, feasSolutions)) mymdd.compile_pathlist(pathList) assert mymdd.widthList == [1,2,3,5,8] mymdd.reduce_bottom_up(mergeFunc) assert mymdd.widthList == [1,2,3,3,1] assert mymdd.find_shortest_path() == (5, [1,0,1,1]) assert mymdd.find_longest_path() == (9, [1,1,1,1])
def test_properties(): mymdd = MDD() mymdd.loadJSON('data/misp.json') assert mymdd.numNodeLayers == 6 assert mymdd.numArcLayers == 5 assert mymdd.widthList == [1, 2, 3, 2, 2, 1] assert mymdd.maxWidth == 3 mymdd.loadJSON('data/trivial.json') assert mymdd.numNodeLayers == 1 assert mymdd.numArcLayers == 0 assert mymdd.widthList == [1] assert mymdd.maxWidth == 1
def test_find_path(): mymdd = MDD() mymdd.loadJSON('data/misp.json') srcList = list(mymdd.allnodes_in_layer(4)) destList = list(mymdd.allnodes_in_layer(1)) assert mymdd._find_optimal_path(srcList, destList, False) == (0.0, [0, 0, 0]) _assert_clean_tmp(mymdd) assert mymdd._find_optimal_path(srcList, destList, True) == (4.0, [0, 0, 1]) _assert_clean_tmp(mymdd) srcList = [MDDNode(0, frozenset([1, 2, 3, 4, 5]))] destList = [MDDNode(5, frozenset())] assert mymdd._find_optimal_path(srcList, destList, False) == (0.0, [0, 0, 0, 0, 0]) _assert_clean_tmp(mymdd) assert mymdd._find_optimal_path(srcList, destList, True) == (11.0, [0, 1, 0, 0, 1]) _assert_clean_tmp(mymdd) srcList = [MDDNode(2, frozenset([5])), MDDNode(2, frozenset([3, 4, 5]))] destList = [MDDNode(2, frozenset([4, 5]))] assert mymdd._find_optimal_path(srcList, destList, True) == (float('-inf'), []) _assert_clean_tmp(mymdd) destList.append(MDDNode(2, frozenset([3, 4, 5]))) assert mymdd._find_optimal_path(srcList, destList, True) == (0.0, []) _assert_clean_tmp(mymdd)
def test_find_optimal_ix(): mymdd = MDD() mymdd.loadJSON('data/misp.json') testnode = MDDNode(3, frozenset([5])) assert mymdd._find_optimal_ix(testnode, False, False) == (2.0, [0, 0, 1]) _assert_clean_tmp(mymdd) assert mymdd._find_optimal_ix(testnode, False, True) == (4.0, [0, 1, 0]) _assert_clean_tmp(mymdd) assert mymdd._find_optimal_ix(testnode, True, False) == (0.0, [0, 0]) _assert_clean_tmp(mymdd) assert mymdd._find_optimal_ix(testnode, True, True) == (7.0, [0, 1]) _assert_clean_tmp(mymdd) assert mymdd._find_optimal_ix(MDDNode(0, frozenset([1, 2, 3, 4, 5])), False, True) == (0.0, []) _assert_clean_tmp(mymdd) assert mymdd._find_optimal_ix(MDDNode(5, frozenset()), True, False) == (0.0, []) _assert_clean_tmp(mymdd) mymdd.loadJSON('data/trivial.json') assert mymdd._find_optimal_ix(MDDNode(0, ''), False, True) == (0.0, []) _assert_clean_tmp(mymdd) assert mymdd._find_optimal_ix(MDDNode(0, ''), True, False) == (0.0, []) _assert_clean_tmp(mymdd)
def test_find_opt_rtpath(): mymdd = MDD() mymdd.loadJSON('data/misp.json') assert mymdd._find_opt_rtpath(False) == (0.0, [0, 0, 0, 0, 0]) _assert_clean_tmp(mymdd) assert mymdd._find_opt_rtpath(True) == (11.0, [0, 1, 0, 0, 1]) _assert_clean_tmp(mymdd) mymdd.loadJSON('data/trivial.json') assert mymdd._find_opt_rtpath(False) == (0.0, []) _assert_clean_tmp(mymdd) mymdd._append_new_layer() mymdd.add_node(MDDNode(1, 'NEO')) assert mymdd.find_shortest_path() == (float('inf'), []) assert mymdd.find_longest_path() == (float('-inf'), [])
def test_initial(): mymdd = MDD() assert mymdd.numNodeLayers == 0 assert mymdd.numArcLayers == -1 assert list(mymdd.allnodes()) == [] mymdd._append_new_layer() mymdd._add_node(MDDNode(0, 0)) mymdd._append_new_layer() assert mymdd.widthList == [1, 0] mymdd._add_node(MDDNode(1, 1)) mymdd._add_node(MDDNode(1, 2)) assert mymdd.numNodeLayers == 2 assert mymdd.widthList == [1, 2]
def test_enumerate_all_paths(): mymdd = MDD() mymdd.loadJSON('data/misp.json') assert len(mymdd.enumerate_all_paths()) == 10