[mlpack-git] master: Slight code cleanups for style. No functionality changes. (2969438)

gitdub at big.cc.gt.atl.ga.us gitdub at big.cc.gt.atl.ga.us
Thu Mar 5 22:15:11 EST 2015


Repository : https://github.com/mlpack/mlpack

On branch  : master
Link       : https://github.com/mlpack/mlpack/compare/904762495c039e345beba14c1142fd719b3bd50e...f94823c800ad6f7266995c700b1b630d5ffdcf40

>---------------------------------------------------------------

commit 29694386bb22899ddd9eb2cdd38378bd1627ec35
Author: Ryan Curtin <ryan at ratml.org>
Date:   Mon Feb 2 15:15:16 2015 -0500

    Slight code cleanups for style.
    No functionality changes.


>---------------------------------------------------------------

29694386bb22899ddd9eb2cdd38378bd1627ec35
 src/mlpack/core/optimizers/sdp/primal_dual.hpp     | 24 +++---
 .../core/optimizers/sdp/primal_dual_impl.hpp       | 87 +++++++++++-----------
 2 files changed, 57 insertions(+), 54 deletions(-)

diff --git a/src/mlpack/core/optimizers/sdp/primal_dual.hpp b/src/mlpack/core/optimizers/sdp/primal_dual.hpp
index ed2ac9b..67c901b 100644
--- a/src/mlpack/core/optimizers/sdp/primal_dual.hpp
+++ b/src/mlpack/core/optimizers/sdp/primal_dual.hpp
@@ -25,7 +25,7 @@ class PrimalDualSolver
    * Construct a new solver instance from a given SDP instance.
    * Uses a random, positive initialization point.
    *
-   * @param sdp
+   * @param sdp Initialized SDP to be solved.
    */
   PrimalDualSolver(const SDPType& sdp);
 
@@ -34,30 +34,30 @@ class PrimalDualSolver
    * positive initialization point. Both initialX and initialZ need to be
    * positive definite matrices.
    *
-   * @param sdp
+   * @param sdp Initialized SDP to be solved.
    * @param initialX
-   * @param initialYsparse
-   * @param initialYdense
+   * @param initialYSparse
+   * @param initialYDense
    * @param initialZ
    */
   PrimalDualSolver(const SDPType& sdp,
                    const arma::mat& initialX,
-                   const arma::vec& initialYsparse,
-                   const arma::vec& initialYdense,
+                   const arma::vec& initialYSparse,
+                   const arma::vec& initialYDense,
                    const arma::mat& initialZ);
 
   /**
-   * Invoke the optimization procedure, returning the converged values
-   * for the primal and dual variables.
+   * Invoke the optimization procedure, returning the converged values for the
+   * primal and dual variables.
    *
    * @param X
-   * @param ysparse
-   * @param ydense
+   * @param ySparse
+   * @param yDense
    * @param Z
    */
   double Optimize(arma::mat& X,
-                  arma::vec& ysparse,
-                  arma::vec& ydense,
+                  arma::vec& ySparse,
+                  arma::vec& yDense,
                   arma::mat& Z);
 
   /**
diff --git a/src/mlpack/core/optimizers/sdp/primal_dual_impl.hpp b/src/mlpack/core/optimizers/sdp/primal_dual_impl.hpp
index df77c6a..657ee38 100644
--- a/src/mlpack/core/optimizers/sdp/primal_dual_impl.hpp
+++ b/src/mlpack/core/optimizers/sdp/primal_dual_impl.hpp
@@ -69,33 +69,29 @@ PrimalDualSolver<SDPType>::PrimalDualSolver(const SDPType& sdp,
 
   if (initialX.n_rows != sdp.N() || initialX.n_cols != sdp.N())
     Log::Fatal << "PrimalDualSolver::PrimalDualSolver(): "
-      << "initialX needs to be square n x n matrix"
-      << std::endl;
+      << "initialX needs to be square n x n matrix." << std::endl;
 
   if (!arma::chol(tmp, initialX))
     Log::Fatal << "PrimalDualSolver::PrimalDualSolver(): "
-      << "initialX needs to be symmetric positive definite"
-      << std::endl;
+      << "initialX needs to be symmetric positive definite." << std::endl;
 
   if (initialYsparse.n_elem != sdp.NumSparseConstraints())
     Log::Fatal << "PrimalDualSolver::PrimalDualSolver(): "
-      << "initialYsparse needs to have the same length as the number of sparse constraints"
-      << std::endl;
+      << "initialYsparse needs to have the same length as the number of sparse "
+      << "constraints." << std::endl;
 
   if (initialYdense.n_elem != sdp.NumDenseConstraints())
     Log::Fatal << "PrimalDualSolver::PrimalDualSolver(): "
-      << "initialYdense needs to have the same length as the number of dense constraints"
-      << std::endl;
+      << "initialYdense needs to have the same length as the number of dense "
+      << "constraints." << std::endl;
 
   if (initialZ.n_rows != sdp.N() || initialZ.n_cols != sdp.N())
     Log::Fatal << "PrimalDualSolver::PrimalDualSolver(): "
-      << "initialZ needs to be square n x n matrix"
-      << std::endl;
+      << "initialZ needs to be square n x n matrix."  << std::endl;
 
   if (!arma::chol(tmp, initialZ))
     Log::Fatal << "PrimalDualSolver::PrimalDualSolver(): "
-      << "initialZ needs to be symmetric positive definite"
-      << std::endl;
+      << "initialZ needs to be symmetric positive definite." << std::endl;
 }
 
 /**
@@ -201,7 +197,8 @@ SolveKKTSystem(const arma::sp_mat& Asparse,
 
   // TODO(stephentu): use a more efficient method (e.g. LU decomposition)
   if (!arma::solve(dy, M, rhs))
-    Log::Fatal << "PrimalDualSolver::SolveKKTSystem(): Could not solve KKT system" << std::endl;
+    Log::Fatal << "PrimalDualSolver::SolveKKTSystem(): Could not solve KKT "
+        << "system." << std::endl;
 
   if (Asparse.n_rows)
     dysparse = dy(arma::span(0, Asparse.n_rows - 1));
@@ -209,7 +206,8 @@ SolveKKTSystem(const arma::sp_mat& Asparse,
     dydense = dy(arma::span(Asparse.n_rows, numConstraints - 1));
 
   // Compute dx from (2.13)
-  math::Smat(F * (rd - Asparse.t() * dysparse - Adense.t() * dydense) - rc, Frd_ATdy_rc_Mat);
+  math::Smat(F * (rd - Asparse.t() * dysparse - Adense.t() * dydense) - rc,
+      Frd_ATdy_rc_Mat);
   SolveLyapunov(Einv_Frd_ATdy_rc_Mat, Z, 2. * Frd_ATdy_rc_Mat);
   math::Svec(Einv_Frd_ATdy_rc_Mat, Einv_Frd_ATdy_rc);
   dsx = -Einv_Frd_ATdy_rc;
@@ -222,8 +220,10 @@ namespace private_ {
 
 // TODO(stephentu): should we move this somewhere more general?
 template <typename T> struct vectype { };
-template <typename eT> struct vectype<arma::Mat<eT>> { typedef arma::Col<eT> type; };
-template <typename eT> struct vectype<arma::SpMat<eT>> { typedef arma::SpCol<eT> type; };
+template <typename eT> struct vectype<arma::Mat<eT>>
+{ typedef arma::Col<eT> type; };
+template <typename eT> struct vectype<arma::SpMat<eT>>
+{ typedef arma::SpCol<eT> type; };
 
 } // namespace private_
 
@@ -329,18 +329,15 @@ PrimalDualSolver<SDPType>::Optimize(arma::mat& X,
     // we have to handle each block separately.
     if (sdp.NumSparseConstraints())
     {
-      M.submat(arma::span(0,
-                          sdp.NumSparseConstraints() - 1),
-               arma::span(0,
-                          sdp.NumSparseConstraints() - 1)) =
-        Asparse * Einv_F_AsparseT;
+      M.submat(arma::span(0, sdp.NumSparseConstraints() - 1),
+               arma::span(0, sdp.NumSparseConstraints() - 1)) =
+          Asparse * Einv_F_AsparseT;
       if (sdp.NumDenseConstraints())
       {
-        M.submat(arma::span(0,
-                            sdp.NumSparseConstraints() - 1),
+        M.submat(arma::span(0, sdp.NumSparseConstraints() - 1),
                  arma::span(sdp.NumSparseConstraints(),
                             sdp.NumConstraints() - 1)) =
-          Asparse * Einv_F_AdenseT;
+            Asparse * Einv_F_AdenseT;
       }
     }
     if (sdp.NumDenseConstraints())
@@ -351,13 +348,13 @@ PrimalDualSolver<SDPType>::Optimize(arma::mat& X,
                             sdp.NumConstraints() - 1),
                  arma::span(0,
                             sdp.NumSparseConstraints() - 1)) =
-          Adense * Einv_F_AsparseT;
+            Adense * Einv_F_AsparseT;
       }
       M.submat(arma::span(sdp.NumSparseConstraints(),
                           sdp.NumConstraints() - 1),
                arma::span(sdp.NumSparseConstraints(),
                           sdp.NumConstraints() - 1)) =
-        Adense * Einv_F_AdenseT;
+          Adense * Einv_F_AdenseT;
     }
 
     const double sxdotsz = arma::dot(sx, sz);
@@ -369,7 +366,8 @@ PrimalDualSolver<SDPType>::Optimize(arma::mat& X,
     // This solves step (1) of Section 7, the "predictor" step.
     Rc = -0.5*(X*Z + Z*X);
     math::Svec(Rc, rc);
-    SolveKKTSystem(Asparse, Adense, Z, M, F, rp, rd, rc, dsx, dysparse, dydense, dsz);
+    SolveKKTSystem(Asparse, Adense, Z, M, F, rp, rd, rc, dsx, dysparse, dydense,
+        dsz);
     math::Smat(dsx, dX);
     math::Smat(dsz, dZ);
 
@@ -385,7 +383,8 @@ PrimalDualSolver<SDPType>::Optimize(arma::mat& X,
     // Step (3), the "corrector" step.
     Rc = mu*arma::eye<arma::mat>(n, n) - 0.5*(X*Z + Z*X + dX*dZ + dZ*dX);
     math::Svec(Rc, rc);
-    SolveKKTSystem(Asparse, Adense, Z, M, F, rp, rd, rc, dsx, dysparse, dydense, dsz);
+    SolveKKTSystem(Asparse, Adense, Z, M, F, rp, rd, rc, dsx, dysparse, dydense,
+        dsz);
     math::Smat(dsx, dX);
     math::Smat(dsz, dZ);
     alpha = Alpha(X, dX, tau);
@@ -411,14 +410,16 @@ PrimalDualSolver<SDPType>::Optimize(arma::mat& X,
 
     const double normXZ = arma::norm(X * Z, "fro");
 
-    const double sparsePrimalInfeas = arma::norm(sdp.SparseB() - Asparse * sx, 2);
+    const double sparsePrimalInfeas = arma::norm(sdp.SparseB() - Asparse * sx,
+        2);
     const double densePrimalInfeas = arma::norm(sdp.DenseB() - Adense * sx, 2);
     const double primalInfeas = sqrt(sparsePrimalInfeas * sparsePrimalInfeas +
         densePrimalInfeas * densePrimalInfeas);
 
     primalObj = arma::dot(sdp.C(), X);
 
-    const double dualObj = arma::dot(sdp.SparseB(), ysparse) + arma::dot(sdp.DenseB(), ydense);
+    const double dualObj = arma::dot(sdp.SparseB(), ysparse) +
+        arma::dot(sdp.DenseB(), ydense);
     const double dualityGap = primalObj - dualObj;
 
     // TODO(stephentu): this dual check is quite expensive,
@@ -431,21 +432,23 @@ PrimalDualSolver<SDPType>::Optimize(arma::mat& X,
     const double dualInfeas = arma::norm(DualCheck, "fro");
 
     Log::Debug
-      << "iter=" << iteration + 1 << ", "
-      << "primal=" << primalObj << ", "
-      << "dual=" << dualObj << ", "
-      << "gap=" << dualityGap << ", "
-      << "||XZ||=" << normXZ << ", "
-      << "primalInfeas=" << primalInfeas << ", "
-      << "dualInfeas=" << dualInfeas << ", "
-      << "mu=" << mu
-      << std::endl;
-
-    if (normXZ <= normXzTol && primalInfeas <= primalInfeasTol && dualInfeas <= dualInfeasTol)
+        << "iter=" << iteration + 1 << ", "
+        << "primal=" << primalObj << ", "
+        << "dual=" << dualObj << ", "
+        << "gap=" << dualityGap << ", "
+        << "||XZ||=" << normXZ << ", "
+        << "primalInfeas=" << primalInfeas << ", "
+        << "dualInfeas=" << dualInfeas << ", "
+        << "mu=" << mu
+        << std::endl;
+
+    if (normXZ <= normXzTol && primalInfeas <= primalInfeasTol &&
+        dualInfeas <= dualInfeasTol)
       return primalObj;
   }
 
-  Log::Warn << "Did not converge after " << maxIterations << " iterations!" << std::endl;
+  Log::Warn << "PrimalDualSolver::Optimizer(): Did not converge after "
+      << maxIterations << " iterations!" << std::endl;
   return primalObj;
 }
 



More information about the mlpack-git mailing list