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(),
    }
Exemple #2
0
    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
Exemple #3
0
    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 _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