def test_addition(): from diagram.diagram import MTBDD, EVBDD mat1 = np.array([[1, 7, 0], [0, -1, 0], [2, 8, 1], [1, 5, 0], [1, 5, 0], [1, 5, 0], [1, 15, 0]], dtype=float) print mat1 + mat1 print('MTBDD:') mtbdd = MTBDD() node1 = mtbdd.create(mat1, 0) node2 = mtbdd.create(mat1, 0) from diagram.operations.computations import add_diagrams node3 = add_diagrams(node1, node2) print node3.to_matrix(7, True) print('EVBDD:') evbdd = EVBDD() node4 = evbdd.create(mat1, 0) node5 = evbdd.create(mat1, 0) from diagram.operations.computations import diagram_sum node6 = add_diagrams(node4, node5) print node6.to_matrix(7, True) print('summing the diagrams:') print mat1 print diagram_sum(node1) print diagram_sum(node4) print np.sum(mat1) print node4.nodes node6.plot('ev-add') node3.plot('mt-add')
def test_power_method_sparse(iterations, episodes, max_size, sparsity): for ep in range(episodes): # initialization vec_size = np.random.random_integers(10, high=max_size) vec = np.random.rand(vec_size, 1) mat = np.multiply(np.random.rand(vec_size, vec_size), np.random.rand(vec_size, vec_size) > sparsity) + \ np.random.rand(vec_size, vec_size) * np.identity(vec_size) for it in range(iterations): # multiplying vec = np.dot(mat, vec) # normalizing vec /= np.linalg.norm(vec) # creating the BDD representations from diagram.diagram import MTBDD, EVBDD mtbdd = MTBDD() evbdd = EVBDD() node_mt = mtbdd.create( np.round(config.precision_elements / np.max(vec) * vec, config.precision_round), 0) node_ev = evbdd.create( np.round(config.precision_elements / np.max(vec) * vec, config.precision_round), 0) c_mt = node_mt.complexity() c_ev = node_ev.complexity() # storing the complexity store_results('power-sparse', 'mat', ep, vec_size) store_results('power-sparse', 'mtbdd', ep, c_mt) store_results('power-sparse', 'evbdd', ep, c_ev)
def test_new_implementation(): from diagram.diagram import MTBDD mat1 = np.array([[1, 2, 0], [0, 3, 0], [0, 4, 1], [1, 5, 0], [1, 5, 0], [1, 5, 0], [1, 5, 0]]) mtbdd = MTBDD() node = mtbdd.create(mat1, 0) print node.to_matrix(7) print node.get_subdiagrams(5)
def test_multiplication(): from diagram.diagram import MTBDD, EVBDD mat1 = np.array([[1, 7, 0], [0, -1, 0], [2, 8, 1], [1, 5, 0], [1, 5, 0], [1, 5, 0], [1, 15, 0]], dtype=float) print mat1 print 'Scalar Multiplication: ~~~~~~~~~~~~~~' print mat1 * 2.5 print('MTBDD:') from diagram.operations.computations import scalar_multiply_diagram, elementwise_multiply_diagrams_evbdd, \ elementwise_multiply_diagrams_mtbdd, multiply_by_column_vector, multiply_diagram mtbdd = MTBDD() node1 = mtbdd.create(mat1, 0) node2 = scalar_multiply_diagram(node1, 2.5) print node2.to_matrix(7, True) print('EVBDD:') evbdd = EVBDD() node4 = evbdd.create(mat1, 0) node5 = scalar_multiply_diagram(node4, 2.5) print node5.to_matrix(7, True) print 'Elementwise Multiplication: ~~~~~~~~~' print np.multiply(mat1, mat1) print('MTBDD:') node3 = elementwise_multiply_diagrams_mtbdd(node1, node1) # node3.plot('mtbdd') print node3.to_matrix(7, True) print('EVBDD:') node6 = elementwise_multiply_diagrams_evbdd(node4, node4) print node6.to_matrix(7, True) # node6.plot('evbdd') vec1 = np.array([1.0, 2.0, 3.0]) vnode_mt = mtbdd.create(vec1, 0) vnode_ev = evbdd.create(vec1, 0) print('Column-vector multiplication') print('Reference') print(np.dot(mat1, vec1)) print('MTBDD') node7 = multiply_by_column_vector(node1, vnode_mt) print(node7.to_matrix(7, True)) print('EVBDD') node8 = multiply_by_column_vector(node4, vnode_ev) print(node8.to_matrix(7, True)) print('Matrix-multiplication') mat2 = np.array([[1, -2, 0], [2, -3, 0], [3, -4, -1]]) print('Reference') print(np.dot(mat1, mat2)) print('MTBDD') node11 = mtbdd.create(mat2, 0) node9 = multiply_diagram(node1, node11, mat1.shape[0]) print(node9.to_matrix(3, True)) print('EVBDD') node12 = evbdd.create(mat2, 0) node10 = multiply_diagram(node4, node12, mat1.shape[0]) print(node10.to_matrix(7, True))
def plot_graphs(): # mat1 = np.array([[1, 4], [1, 4], [1, 4], [1, 4]]) mat1 = np.random.random((4, 4)) from diagram.binary import MTBDD, EVBDD mtbdd = MTBDD() evbdd = EVBDD() node1 = mtbdd.create(mat1, 0) node4 = evbdd.create(mat1, 0) # node1.plot('mt-st-r') node4.plot('random')
def test_reduction(): from diagram.binary import MTBDD, EVBDD mat1 = np.array([[1, 2, 0], [0, 1, 0], [2, 4, 1], [1, 5, 0], [1, 5, 0], [1, 5, 0], [1, 5, 0]], dtype=float) print mat1 print('MTBDD:') mtbdd = MTBDD() node = mtbdd.create(mat1, 0) # import code; code.interact(local=dict(locals().items() + globals().items())) # print node.to_matrix(7) # print node.leaves node1 = mtbdd.create(mat1, 0) reduced = mtbdd.reduce(node1) # import code; code.interact(local=dict(locals().items() + globals().items())) red = reduced.leaves print node.to_matrix(7) print len(node.leaves) print reduced.to_matrix(7) print len(red) print('EVBDD:') mtbdd = EVBDD() node = mtbdd.create(mat1, 0) node1 = mtbdd.create(mat1, 0) reduced = mtbdd.reduce(node1) red = reduced.leaves print node.to_matrix(7) print len(node.leaves) print reduced.to_matrix(7) print len(red)
def general_tests(): from diagram.diagram import MTBDD, EVBDD mtbdd = MTBDD() evbdd = EVBDD() mat1 = np.array([[1, 2], [3, 4], [5, 6], [7, 8]]) node1 = mtbdd.create(mat1, 0) node4 = evbdd.create(mat1, 0) node1.plot('mt-st-r') node4.plot('ev-st-r') node1 = mtbdd.create(mat1, 0, False) node4 = evbdd.create(mat1, 0, False) node1.plot('mt-st-nr') node4.plot('ev-st-nr')
def test_power_method(iterations, episodes, max_size): for ep in range(episodes): # initialization vec_size = np.random.random_integers(10, high=max_size) vec = np.random.rand(vec_size, 1) mat = np.random.rand(vec_size, vec_size) for it in range(iterations): # multiplying vec = np.dot(mat, vec) # normalizing vec /= np.linalg.norm(vec) # creating the BDD representations from diagram.diagram import MTBDD, EVBDD mtbdd = MTBDD() evbdd = EVBDD() node_mt = mtbdd.create(np.round(config.precision_elements/np.max(vec)*vec, config.precision_round), 0) node_ev = evbdd.create(np.round(config.precision_elements/np.max(vec)*vec, config.precision_round), 0) c_mt = node_mt.complexity() c_ev = node_ev.complexity() # storing the complexity store_results('power', 'mat', ep, vec_size) store_results('power', 'mtbdd', ep, c_mt) store_results('power', 'evbdd', ep, c_ev)