Exemple #1
0
    def solve(self, verbose=True, mosek_num_threads=None):
        """Solve the SDP-relaxed max-cut problem.

        Resulting cut, value of the cut and solved matrix
        may be accessed through the `get_solution` method.
        """
        s_time = time()

        # Solve the program. Marginally adjust the matrix to be PSD if needed.
        if verbose: print(f"Using {self.solver} to solve problem...")
        matrix = self._solve_sdp(verbose=verbose,
                                 mosek_num_threads=mosek_num_threads)

        if verbose: print("Finding nearest psd...")
        matrix = nearest_psd(matrix)

        # Get the cut defined by the matrix.
        if verbose: print("Getting the cut defined by the psd...")
        vectors = np.linalg.cholesky(matrix)
        cut = get_partition(vectors)

        # Get the value of the cut. Store results.
        if verbose: print("Storing value of cut and other results...")
        value = get_cut_value(self.graph, cut)
        self._results = {'matrix': matrix, 'cut': cut, 'value': value}

        # Optionally be verbose about the results.
        if verbose:
            print("Solved the SDP-relaxed max-cut problem.\n"
                  "Solution cuts off %f share of total weights." % value)

        if verbose:
            print(
                f"Solving completed! Full Solve took {time() - s_time} seconds!"
            )
 def _get_best_candidate(self, candidates):
     """Select the best solution among a series of candidates.
     Return both the matrix, derived partition and value of
     the latter associated with the best candidate (in terms
     of sum of weights of the edges cut off).
     """
     # Get the partition defined by each candidate and their cut-value.
     partitions = [get_partition(vectors) for vectors in candidates]
     scores = [get_cut_value(self.graph, cut) for cut in partitions]
     # Select the best candidate and return it.
     best = np.argmax([scores])
     return candidates[best], partitions[best], scores[best]
 def solve(self, laplacian, verbose=True):
     """Solve the SDP-relaxed max-cut problem.
     Resulting cut, value of the cut and solved matrix
     may be accessed through the `get_solution` method.
     """
     # Solve the program. Marginally adjust the matrix to be PSD if needed.
     matrix = self._solve_sdp(laplacian)
     matrix = nearest_psd(matrix)
     # Get the cut defined by the matrix.
     vectors = np.linalg.cholesky(matrix)
     cut = get_partition(vectors)
     # Get the value of the cut. Store results.
     value = get_cut_value(self.graph, cut)
     self._results = {'matrix': matrix, 'cut': cut, 'value': value}
     # Optionally be verbose about the results.
     if verbose:
         print("Solved the SDP-relaxed max-cut problem.\n"
               "Solution cuts off %f share of total weights." % value)
     return value