def test_coord_cost(self): d_mat = np.array([[1, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [1, 0, 1, 0, 0, 1, 0, 0], [0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 1, 1, 1, 0, 0, 0], [0, 0, 0, 0, 1, 1, 0, 0], [0, 0, 0, 1, 0, 1, 1, 0], [0, 0, 0, 0, 0, 0, 1, 1]]) d_list = ["a", "b", "c", "d", "e", "f", "g", "h"] d = DSMMatrix(d_mat, activity_labels=d_list) c_mat = np.diag(np.ones([8])) c = ClusterMatrix.from_mat(c_mat) pow_cc = 1 cg = ClusterGenerator(dsm_mat=d) initial_cost = ClusterGenerator._coord_cost(d, c, cg.params) assert (initial_cost == 64) d_mat = np.array([[1, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [1, 0, 1, 0, 0, 0, 0, 0], [1, 0, 0, 1, 0, 0, 1, 0], [0, 0, 1, 1, 1, 0, 0, 0], [0, 0, 0, 1, 0, 1, 0, 0], [0, 0, 0, 0, 1, 1, 1, 0], [0, 0, 0, 0, 0, 0, 1, 1]]) d_list = ["a", "b", "c", "d", "e", "f", "g", "h"] d = DSMMatrix(d_mat, activity_labels=d_list) c_mat = np.diag(np.ones([8])) c = ClusterMatrix.from_mat(c_mat) pow_cc = 1 # cg = ClusterGenerator(dsm_mat = d) initial_cost = ClusterGenerator._coord_cost(d, c, cg.params) assert (initial_cost == 80)
async def new_dsm(dsm_item: DSMItem): d_mat = np.array(dsm_item.mat) labels = dsm_item.labels system_elements = dsm_item.system_elements try: d = DSMMatrix(d_mat, activity_labels=labels) d.clear_elements(system_elements) app_state.dsm = d except AssertionError as err: return {"result": "AssertionError: " + str(err)} except: return {"result": "Unexpected error: " + str(sys.exc_info()[0])} return dsm_item
def test_bid(self): d_mat = np.array([[1, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [1, 0, 1, 0, 0, 1, 0, 0], [1, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]]) d_list = ["a", "b", "c", "d", "e", "f", "g", "h"] d = DSMMatrix(d_mat, activity_labels=d_list) c_mat = np.diag(np.ones([8])) c = ClusterMatrix.from_mat(c_mat) cg = ClusterGenerator(dsm_mat=d) cg.params = ClusterParameters(pow_cc=1, pow_bid=1, pow_dep=4, max_cluster_size=16, rand_accept=32, rand_bid=32, times=2, stable_limit=2, max_repeat=10) elmt = 7 cluster_bid = cg._make_bid(elmt, d, c) # print(cluster_bid) assert np.equal(cluster_bid, np.zeros([8, 1])).all() d_mat2 = np.array([[1, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [1, 0, 1, 0, 0, 0, 0, 0], [1, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]]) d2 = DSMMatrix(d_mat2, activity_labels=d_list) cg2 = ClusterGenerator(dsm_mat=d2) cg2.params = ClusterParameters(pow_cc=1, pow_bid=1, pow_dep=4, max_cluster_size=16, rand_accept=32, rand_bid=32, times=2, stable_limit=2, max_repeat=10) elmt = 0 cluster_bid_2 = cg2._make_bid(elmt, d, c) # print("CB2: ", cluster_bid_2) assert np.equal(cluster_bid_2, np.array([[0], [0], [16], [1], [0], [0], [0], [0]])).all()
def test_coord_cost_2(self): d_mat = np.array([[1, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [1, 0, 1, 0, 0, 0, 0, 0], [1, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]]) d_list = ["a", "b", "c", "d", "e", "f", "g", "h"] d = DSMMatrix(d_mat, activity_labels=d_list) c_mat = np.array([[1., 0., 1., 0., 0., 0., 0., 0.], [0., 1., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 1., 0., 0., 0., 0.], [0., 0., 0., 0., 1., 0., 0., 0.], [0., 0., 0., 0., 0., 1., 0., 0.], [0., 0., 0., 0., 0., 0., 1., 0.], [0., 0., 0., 0., 0., 0., 0., 1.], [0., 0., 0., 0., 0., 0., 0., 0.]]) c = ClusterMatrix.from_mat(c_mat) pow_cc = 1 cg = ClusterGenerator(dsm_mat=d) # import pdb; pdb.set_trace() initial_cost = ClusterGenerator._coord_cost(d, c, cg.params) print(initial_cost) assert (initial_cost == 20)
def test_cluster(self): d_mat = np.array([[1, 0, 0, 0, 0, 0, 0, 0], [1, 1, 0, 0, 0, 0, 0, 0], [1, 0, 1, 0, 0, 1, 0, 0], [0, 1, 0, 1, 0, 0, 0, 0], [0, 0, 1, 1, 1, 0, 0, 0], [0, 0, 0, 0, 1, 1, 0, 0], [0, 0, 0, 1, 0, 1, 1, 0], [0, 0, 0, 0, 0, 0, 1, 1]]) d_list = ["a", "b", "c", "d", "e", "f", "g", "h"] d = DSMMatrix(d_mat, activity_labels=d_list) # print(d.mat.shape) cg = ClusterGenerator(dsm_mat=d)
async def do_cluster(): app_state.generator = ClusterGenerator(dsm_mat=app_state.dsm) c_orig = app_state.generator.cluster(app_state.dsm) total_coord_cost = app_state.generator.total_coord_cost cost_history = app_state.generator.cost_history c = ClusterMatrix.reorder(c_orig) new_dsm = DSMMatrix.reorder_by_cluster(app_state.dsm, c) d_new_g = DSMMatrix.place_diag(new_dsm) app_state.cluster_result = c app_state.dsm_result = d_new_g app_state.dsm_annotated = DSMMatrix.annotate_clusters(d_new_g, c) return { "dsm": app_state.dsm_result.tojson(), "labels": app_state.dsm_result.labels, "cluster": app_state.cluster_result.tojson(), "dsm_a": app_state.dsm_annotated.tojson(), }
def test_place_diag(self): d_mat = np.array([[0, 0, 1, 0, 0, 1, 0, 1], [1, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0, 1], [0, 0, 1, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 1, 0, 0], [0, 1, 0, 0, 0, 0, 1, 0], [1, 0, 0, 1, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0]]) d_list = ["a", "b", "c", "d", "e", "f", "g", "h"] d = DSMMatrix(d_mat, activity_labels=d_list) d_diag = DSMMatrix.place_diag(d) d_mat_result = np.array([[1, 0, 1, 0, 0, 1, 0, 1], [1, 1, 0, 0, 0, 0, 0, 0], [0, 0, 1, 1, 0, 0, 0, 1], [0, 0, 1, 1, 0, 0, 0, 1], [0, 0, 0, 0, 1, 1, 0, 0], [0, 1, 0, 0, 0, 1, 1, 0], [1, 0, 0, 1, 0, 0, 1, 0], [0, 0, 1, 0, 0, 0, 0, 1]]) d_result = DSMMatrix(d_mat_result, activity_labels=d_list) assert np.equal(d_diag.mat, d_result.mat).all()
def test_reorder2(self): d_mat_2 = np.array([[1, 0, 0, 1], [0, 0, 0, 0], [1, 0, 1, 0], [0, 0, 1, 1]]) d_list_2 = ["1", "2", "3", "4"] d_2 = DSMMatrix(d_mat_2, d_list_2) # print("Original DSM: ") # print(d_2.mat) c_mat_2 = np.array([[0, 0, 0, 1], [0, 0, 1, 0], [0, 1, 0, 0], [1, 0, 0, 0]]) c_2 = ClusterMatrix.from_mat(c_mat_2) # print("Original Cluster: ") # print(c_2.mat) new_d_mat_2 = DSMMatrix.reorder_by_cluster(d_2, c_2) # print("Reordered DSM: ") # print(new_d_mat_2.mat) # print(new_d_mat_2.labels) val_comp_2 = new_d_mat_2.mat == np.array([[0, 1, 0, 0], [0, 0, 0, 1], [0, 0, 0, 0], [1, 0, 0, 0]]) lab_comp_2 = new_d_mat_2.labels == ['4', '3', '2', '1'] assert val_comp_2.all() assert lab_comp_2
def test_reorder1(self): # d_mat = np.identity(3) d_mat = np.array([[1, 0, 0, 0], [0, 0, 0, 0], [1, 0, 1, 0], [0, 0, 1, 1]]) d_list = ["1", "2", "3", "4"] d = DSMMatrix(d_mat, d_list) # print("Original DSM: ") # print(d.mat) c_mat = np.array([[1, 0, 1, 1], [0, 1, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]) c = ClusterMatrix.from_mat(c_mat) # print("Original Cluster: ") # print(c.mat) new_d_mat = DSMMatrix.reorder_by_cluster(d, c) # print("Reordered DSM: ") # print(new_d_mat.mat) # print(new_d_mat.labels) val_comp_1 = new_d_mat.mat == np.array([[0, 0, 0, 0], [1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 0]]) lab_comp_1 = new_d_mat.labels == ['1', '3', '4', '2'] assert val_comp_1.all() assert lab_comp_1
def test_clear_systemelems(self): d_mat = np.array([[1, 0, 1, 0], [1, 1, 1, 0], [1, 1, 1, 0], [0, 0, 1, 1]]) d = DSMMatrix(d_mat) d.clear_elements([0]) assert (d.mat == np.array([[0, 0, 0, 0], [0, 1, 1, 0], [0, 1, 1, 0], [0, 0, 1, 1]])).all() d.clear_elements([1, 3]) # print(d.mat) assert (d.mat == np.array([[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 1, 0], [0, 0, 0, 0]])).all() d.clear_elements([]) assert (d.mat == np.array([[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 1, 0], [0, 0, 0, 0]])).all() self.assertRaises(AssertionError, d.clear_elements, ["e"]) self.assertRaises(AssertionError, d.clear_elements, ["2", 1]) self.assertRaises(AssertionError, d.clear_elements, [6])
def __init__(self): self.params = ClusterParameters(pow_cc=1, pow_bid=1, pow_dep=4, max_cluster_size=8, rand_accept=16, rand_bid=16, times=2, stable_limit=2, max_repeat=10) self.dsm = DSMMatrix.from_size(4) self.dsm_result = None self.cluster_result = None self.dsm_annotated = None self.generator = None
def test_setunset(self): d_mat = np.array([[1, 0, 0, 0], [0, 0, 0, 0], [1, 0, 1, 0], [0, 0, 1, 1]]) d_list = ["a", "b", "c", "d"] d = DSMMatrix(d_mat, activity_labels=d_list) d_mat[1, 0] = 1 d.set(1, 0) assert (d.mat == np.array([[1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 1, 0], [0, 0, 1, 1]])).all() d_mat[2, 1] = 1 d.set(2, 1) assert (d.mat == np.array([[1, 0, 0, 0], [1, 0, 0, 0], [1, 1, 1, 0], [0, 0, 1, 1]])).all() d_mat[3, 2] = 0 d.unset(3, 2) assert (d.mat == np.array([[1, 0, 0, 0], [1, 0, 0, 0], [1, 1, 1, 0], [0, 0, 0, 1]])).all() # d_mat[3,2] = 0 self.assertRaises(ArithmeticError, d.unset, 4, 6) self.assertRaises(ArithmeticError, d.unset, -1, 0)
def test_cluster(self): d_mat = np.array([[1, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [1, 0, 1, 0, 0, 0, 0, 0], [1, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]]) d_list = ["a", "b", "c", "d", "e", "f", "g", "h"] d = DSMMatrix(d_mat, activity_labels=d_list) cg = ClusterGenerator(default_size=8) # try: cluster_matrix = cg.cluster(d) total_coord_cost = cg.total_coord_cost cost_history = cg.cost_history # except: # extype, value, tb = sys.exc_info() # traceback.print_exc() # pdb.post_mortem(tb) # pdb.runcall(DSMMatrix.reorder_by_cluster, DSM_matrix, cluster_matrix) c_mat_result = np.array([[1, 0, 1, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 1, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]]) c_result = ClusterMatrix.from_mat(c_mat_result) c_size_result = np.array([[2], [1], [2], [1], [1], [1], [0], [0]]) print(cluster_matrix.mat) # import pdb; pdb.set_trace() # assert np.equal(cluster_matrix.mat, c_result.mat).all() initial_cost = ClusterGenerator._coord_cost(d, cluster_matrix, cg.params) print("Cost: " + str(initial_cost)) assert (initial_cost == 14 or initial_cost == 16)
def __init__(self, dsm_mat=None, default_size=2): if isinstance(dsm_mat, DSMMatrix): self._dsm_mat = dsm_mat else: self._dsm_mat = DSMMatrix.from_size(default_size) # Set clustering control parameters max_size = self._dsm_mat.mat.shape[0] self._params = set_default_cluster_parameters(max_size) # Initialise matrices and arrays self._DSM_size = self._dsm_mat.mat.shape[1] self._n_clusters = max_size self._cost_history = np.zeros([10000, 1]) self._history_index = 0 self._cluster_matrix_history = [] self._cluster_size_history = [] self._total_coord_cost_history = [] self._cost_history = np.array([]) self._total_coord_cost = 0 self._cluster_result = ClusterMatrix.from_mat( np.zeros([max_size, max_size]))
def graph_matrix(gen_matrix, cluster_matrix=None, x_title='Element', y_title='Element', graph_title='DSM Matrix', x_tcklabel=None, y_tcklabel=None, print_flag=0): """ Graph the DSM and cluster matrix. """ assert isinstance(gen_matrix, DSMMatrix) or isinstance(gen_matrix, ClusterMatrix) # if not isinstance(cluster_matrix, ClusterMatrix) ds_mat = gen_matrix.mat (row_in, col_out) = np.where(ds_mat) m_value = ds_mat[ds_mat != 0] max_row = np.max(row_in) max_col = np.max(col_out) axis = np.array([0, max_col, max_row]); max_value = np.max(m_value) data_scale = np.ceil(500/max_value)/20 if x_tcklabel is None and isinstance(gen_matrix, DSMMatrix): x_tcklabel = gen_matrix.labels elif isinstance(x_tcklabel, list): if len(x_tcklabel) != ds_mat.shape[0]: x_tcklabel = [str(x+1) for x in range(ds_mat.shape[0])] else: x_tcklabel = [str(x+1) for x in range(ds_mat.shape[0])] if y_tcklabel is None and isinstance(gen_matrix, DSMMatrix): y_tcklabel = gen_matrix.labels elif isinstance(y_tcklabel, list): if len(y_tcklabel) != ds_mat.shape[0]: y_tcklabel = [str(x+1) for x in range(ds_mat.shape[0])] else: y_tcklabel = [str(x+1) for x in range(ds_mat.shape[0])] if isinstance(cluster_matrix, ClusterMatrix): new_gen_matrix = DSMMatrix.annotate_clusters(gen_matrix, cluster_matrix) orig_ds_mat = gen_matrix.mat ds_mat = new_gen_matrix.mat fig, ax = plt.subplots() im = ax.imshow(ds_mat, cmap=plt.cm.Blues) ax.set_xticks(np.arange(len(x_tcklabel))) ax.set_yticks(np.arange(len(y_tcklabel))) ax.set_xticklabels(x_tcklabel) ax.set_yticklabels(y_tcklabel) ax.set_xlabel(x_title) ax.set_ylabel(y_title) ax.xaxis.set_label_position('top') ax.tick_params(top=True, bottom=False, labeltop=True, labelbottom=False) # plt.setp(ax.get_xticklabels(), rotation=90, ha="right", # rotation_mode="anchor") ax.set_title(graph_title) fig.tight_layout() plt.draw()
def block_exit(): plt.show() if __name__ == "__main__": d_mat = np.array([ [1, 0, 1, 0, 0, 1, 0, 1], [1, 1, 0, 0, 0, 0, 0, 0], [0, 0, 1, 1, 0, 0, 0, 1], [0, 0, 1, 1, 0, 0, 0, 1], [0, 0, 0, 0, 1, 1, 0, 0], [0, 1, 0, 0, 0, 1, 1, 0], [1, 0, 0, 1, 0, 0, 1, 0], [0, 0, 1, 0, 0, 0, 0, 1] ]) d = DSMMatrix(d_mat) c_mat = np.array([ [0, 0, 0, 1, 0, 1, 0, 0], [1, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 1, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0] ]) c = ClusterMatrix.from_mat(c_mat) graph_matrix(d, cluster_matrix=c) graph_matrix(c)
def _coord_cost(DSM_matrix, cluster_matrix, params): """ Calculate the coordination cost of the cluster matrix. This routine checks all DSM interactions and adds to a total the cost of all intra-cluster interactions. Interactions outside of clusters are assigned a higher cost. As per Thebeau (2000) the cost formula is, for each interdependent/coupled activity i and j: (In cluster) (DSM[i] + DSM[j])*cluster_size[cluster_n]^pow_cc (Out of cluster) (DSM[i] + DSM[j])*DSM_size^pow_cc Sum each term to obtain the total. """ assert isinstance(DSM_matrix, DSMMatrix) assert isinstance(cluster_matrix, ClusterMatrix) assert isinstance(params, ClusterParameters) if params: pow_cc = params.pow_cc else: pow_cc = 1 # get the number of clusters and size of the DSM n_clusters = cluster_matrix.mat.shape[0] DSM_size = cluster_matrix.mat.shape[1] # initialise coordination costs total_coord_cost = 0 coordination_cost = np.zeros([1, DSM_size]) # dummy variable for reorder function DSM_labels = [str(x + 1) for x in range(DSM_size)] # reorder the DSM according to the cluster matrix new_DSM_matrix = DSMMatrix.reorder_by_cluster(DSM_matrix, cluster_matrix) new_DSM_size = new_DSM_matrix.mat.shape[0] num_cluster_elements = np.sum(cluster_matrix.mat, axis=1, dtype=np.int) n = 0 # indexing starts from zero new_cluster_mat = ClusterMatrix.from_mat( np.zeros([new_DSM_size, new_DSM_size])) # Create a new cluster matrix that matches the reordered DSM matrix # # formerly n_clusters - changed due to shape # Check again to replicate MATLAB functionality - this is not a permanent fix for i in range(n_clusters): if i < new_DSM_size: new_cluster_mat.mat[i, n:n + num_cluster_elements[i]] = np.ones( [1, num_cluster_elements[i]]) n += num_cluster_elements[i] else: new_cluster_mat.mat = np.vstack([ new_cluster_mat.mat, np.zeros(new_cluster_mat.mat.shape[1]) ]) new_cluster_mat.mat[i, n:n + num_cluster_elements[i]] = np.ones( [1, num_cluster_elements[i]]) n += num_cluster_elements[i] # import pdb; pdb.set_trace() # get new cluster size array matching matrix # new_cluster_size = np.sum(new_cluster_mat.mat, axis=1) new_cluster_mat.update_cluster_size() # replace old data with new data for cost calculation DSM_matrix = new_DSM_matrix DSM_size = new_DSM_size cluster_matrix = new_cluster_mat # cluster_size = new_cluster_size # get the number of clusters and size of the DSM n_clusters = cluster_matrix.mat.shape[0] DSM_size = cluster_matrix.mat.shape[1] total_coord_cost = 0 coordination_cost = np.zeros([DSM_size, 1]) # Calculate the cost of the solution for i in range(DSM_size): for j in range(i + 1, DSM_size): if DSM_matrix.mat[i, j] > 0 or DSM_matrix.mat[j, i] > 0: cost_total = 0 for cluster_index in range(n_clusters): if cluster_matrix.mat[cluster_index, i] + cluster_matrix.mat[ cluster_index, j] == 2: # cost_total += (DSM_matrix.mat[i,j] + DSM_matrix.mat[j,i]) * cluster_size[cluster_index]**pow_cc cost_total += (DSM_matrix.mat[i, j] + DSM_matrix.mat[j, i] ) * cluster_matrix.cluster_size[ cluster_index]**pow_cc if cost_total > 0: cost_c = cost_total else: cost_c = (DSM_matrix.mat[i, j] + DSM_matrix.mat[j, i]) * DSM_size**pow_cc coordination_cost[i] += cost_c total_coord_cost = np.sum(coordination_cost) return total_coord_cost
def test_manual_labels(self): d_mat = np.array([[1, 0, 0, 0], [0, 0, 0, 0], [1, 0, 1, 0], [0, 0, 1, 1]]) d_list = ["a", "b", "c", "d"] d = DSMMatrix(d_mat, activity_labels=d_list)
def test_auto_labels(self): d2_mat = np.array([[1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 1, 0], [0, 0, 1, 1]]) d2 = DSMMatrix(d2_mat) # print(d2.labels) assert d2.labels == ['1', '2', '3', '4']
def test_from_size(self): d = DSMMatrix.from_size(3)