[mlpack-git] master, mlpack-1.0.x: Move things around so LRSDP can actually be included in stuff. (5d63c78)
gitdub at big.cc.gt.atl.ga.us
gitdub at big.cc.gt.atl.ga.us
Thu Mar 5 21:43:08 EST 2015
Repository : https://github.com/mlpack/mlpack
On branches: master,mlpack-1.0.x
Link : https://github.com/mlpack/mlpack/compare/904762495c039e345beba14c1142fd719b3bd50e...f94823c800ad6f7266995c700b1b630d5ffdcf40
>---------------------------------------------------------------
commit 5d63c781cb278cc334ac1f3ba709d88b721963be
Author: Ryan Curtin <ryan at ratml.org>
Date: Mon Feb 10 14:40:16 2014 +0000
Move things around so LRSDP can actually be included in stuff.
>---------------------------------------------------------------
5d63c781cb278cc334ac1f3ba709d88b721963be
src/mlpack/core/optimizers/lrsdp/CMakeLists.txt | 1 -
src/mlpack/core/optimizers/lrsdp/lrsdp.cpp | 108 +++++++++++++++++++++
src/mlpack/core/optimizers/lrsdp/lrsdp.hpp | 5 -
src/mlpack/core/optimizers/lrsdp/lrsdp_impl.hpp | 121 ------------------------
4 files changed, 108 insertions(+), 127 deletions(-)
diff --git a/src/mlpack/core/optimizers/lrsdp/CMakeLists.txt b/src/mlpack/core/optimizers/lrsdp/CMakeLists.txt
index be78e1a..f981ed9 100644
--- a/src/mlpack/core/optimizers/lrsdp/CMakeLists.txt
+++ b/src/mlpack/core/optimizers/lrsdp/CMakeLists.txt
@@ -1,7 +1,6 @@
set(SOURCES
lrsdp.hpp
lrsdp.cpp
- lrsdp_impl.hpp
)
set(DIR_SRCS)
diff --git a/src/mlpack/core/optimizers/lrsdp/lrsdp.cpp b/src/mlpack/core/optimizers/lrsdp/lrsdp.cpp
index 3e321ca..be24921 100644
--- a/src/mlpack/core/optimizers/lrsdp/lrsdp.cpp
+++ b/src/mlpack/core/optimizers/lrsdp/lrsdp.cpp
@@ -78,3 +78,111 @@ const arma::mat& LRSDP::GetInitialPoint()
{
return initialPoint;
}
+
+// Convert the object to a string.
+std::string LRSDP::ToString() const
+{
+ std::ostringstream convert;
+ convert << "LRSDP [" << this << "]" << std::endl;
+ convert << "Matrix Size: " << c.n_rows << "x" << c.n_cols << std::endl;
+ convert << "Initial point Size : " << initialPoint.n_rows << "x"
+ << initialPoint.n_cols << std::endl;
+ //convert << "AugLagrangian Info: " << std::endl << augLag;
+ return convert.str();
+}
+
+namespace mlpack {
+namespace optimization {
+
+// Custom specializations of the AugmentedLagrangianFunction for the LRSDP case.
+template<>
+double AugLagrangianFunction<LRSDP>::Evaluate(
+ const arma::mat& coordinates) const
+{
+ // We can calculate the entire objective in a smart way.
+ // L(R, y, s) = Tr(C * (R R^T)) -
+ // sum_{i = 1}^{m} (y_i (Tr(A_i * (R R^T)) - b_i)) +
+ // (sigma / 2) * sum_{i = 1}^{m} (Tr(A_i * (R R^T)) - b_i)^2
+
+ // Let's start with the objective: Tr(C * (R R^T)).
+ // Simple, possibly slow solution.
+ arma::mat rrt = coordinates * trans(coordinates);
+ double objective = trace(function.C() * rrt);
+
+ // Now each constraint.
+ for (size_t i = 0; i < function.B().n_elem; ++i)
+ {
+ // Take the trace subtracted by the b_i.
+ double constraint = -function.B()[i];
+
+ if (function.AModes()[i] == 0)
+ {
+ constraint += trace(function.A()[i] * rrt);
+ }
+ else
+ {
+ for (size_t j = 0; j < function.A()[i].n_cols; ++j)
+ {
+ constraint += function.A()[i](2, j) *
+ rrt(function.A()[i](0, j), function.A()[i](1, j));
+ }
+ }
+
+ objective -= (lambda[i] * constraint);
+ objective += (sigma / 2) * std::pow(constraint, 2.0);
+ }
+
+ return objective;
+}
+
+template<>
+void AugLagrangianFunction<LRSDP>::Gradient(
+ const arma::mat& coordinates,
+ arma::mat& gradient) const
+{
+ // We can calculate the gradient in a smart way.
+ // L'(R, y, s) = 2 * S' * R
+ // with
+ // S' = C - sum_{i = 1}^{m} y'_i A_i
+ // y'_i = y_i - sigma * (Trace(A_i * (R R^T)) - b_i)
+ arma::mat rrt = coordinates * trans(coordinates);
+ arma::mat s = function.C();
+
+ for (size_t i = 0; i < function.B().n_elem; ++i)
+ {
+ double constraint = -function.B()[i];
+
+ if (function.AModes()[i] == 0)
+ {
+ constraint += trace(function.A()[i] * rrt);
+ }
+ else
+ {
+ for (size_t j = 0; j < function.A()[i].n_cols; ++j)
+ {
+ constraint += function.A()[i](2, j) *
+ rrt(function.A()[i](0, j), function.A()[i](1, j));
+ }
+ }
+
+ double y = lambda[i] - sigma * constraint;
+
+ if (function.AModes()[i] == 0)
+ {
+ s -= (y * function.A()[i]);
+ }
+ else
+ {
+ // We only need to subtract the entries which could be modified.
+ for (size_t j = 0; j < function.A()[i].n_cols; ++j)
+ {
+ s(function.A()[i](0, j), function.A()[i](1, j)) -= y;
+ }
+ }
+ }
+
+ gradient = 2 * s * coordinates;
+}
+
+}; // namespace optimization
+}; // namespace mlpack
diff --git a/src/mlpack/core/optimizers/lrsdp/lrsdp.hpp b/src/mlpack/core/optimizers/lrsdp/lrsdp.hpp
index f87a5eb..bba3a09 100644
--- a/src/mlpack/core/optimizers/lrsdp/lrsdp.hpp
+++ b/src/mlpack/core/optimizers/lrsdp/lrsdp.hpp
@@ -145,9 +145,4 @@ class LRSDP
}; // namespace optimization
}; // namespace mlpack
-// Include implementation (template specializations for the augmented Lagrangian
-// function).
-#include "lrsdp_impl.hpp"
-
#endif
-
diff --git a/src/mlpack/core/optimizers/lrsdp/lrsdp_impl.hpp b/src/mlpack/core/optimizers/lrsdp/lrsdp_impl.hpp
deleted file mode 100644
index 830aa3a..0000000
--- a/src/mlpack/core/optimizers/lrsdp/lrsdp_impl.hpp
+++ /dev/null
@@ -1,121 +0,0 @@
-/**
- * @file lrsdp_impl.hpp
- * @author Ryan Curtin
- *
- * An implementation of Monteiro and Burer's formulation of low-rank
- * semidefinite programs (LR-SDP).
- */
-#ifndef __MLPACK_CORE_OPTIMIZERS_LRSDP_LRSDP_IMPL_HPP
-#define __MLPACK_CORE_OPTIMIZERS_LRSDP_LRSDP_IMPL_HPP
-
-// In case it hasn't already been included.
-#include "lrsdp.hpp"
-
-namespace mlpack {
-namespace optimization {
-
-// convert the object to a string
-std::string LRSDP::ToString() const{
- std::ostringstream convert;
- convert << "LRSDP [" << this << "]" << std::endl;
- convert << "Matrix Size: " << c.n_rows << "x" << c.n_cols << std::endl;
- convert << "Initial point Size : " << initialPoint.n_rows << "x"
- << initialPoint.n_cols << std::endl;
- //convert << "AugLagrangian Info: " << std::endl << augLag;
- return convert.str();
-}
-
-// Custom specializations of the AugmentedLagrangianFunction for the LRSDP case.
-template<>
-double AugLagrangianFunction<LRSDP>::Evaluate(const arma::mat& coordinates)
- const
-{
- // We can calculate the entire objective in a smart way.
- // L(R, y, s) = Tr(C * (R R^T)) -
- // sum_{i = 1}^{m} (y_i (Tr(A_i * (R R^T)) - b_i)) +
- // (sigma / 2) * sum_{i = 1}^{m} (Tr(A_i * (R R^T)) - b_i)^2
-
- // Let's start with the objective: Tr(C * (R R^T)).
- // Simple, possibly slow solution.
- arma::mat rrt = coordinates * trans(coordinates);
- double objective = trace(function.C() * rrt);
-
- // Now each constraint.
- for (size_t i = 0; i < function.B().n_elem; ++i)
- {
- // Take the trace subtracted by the b_i.
- double constraint = -function.B()[i];
-
- if (function.AModes()[i] == 0)
- {
- constraint += trace(function.A()[i] * rrt);
- }
- else
- {
- for (size_t j = 0; j < function.A()[i].n_cols; ++j)
- {
- constraint += function.A()[i](2, j) *
- rrt(function.A()[i](0, j), function.A()[i](1, j));
- }
- }
-
- objective -= (lambda[i] * constraint);
- objective += (sigma / 2) * std::pow(constraint, 2.0);
- }
-
- return objective;
-}
-
-template<>
-void AugLagrangianFunction<LRSDP>::Gradient(const arma::mat& coordinates,
- arma::mat& gradient) const
-{
- // We can calculate the gradient in a smart way.
- // L'(R, y, s) = 2 * S' * R
- // with
- // S' = C - sum_{i = 1}^{m} y'_i A_i
- // y'_i = y_i - sigma * (Trace(A_i * (R R^T)) - b_i)
- arma::mat rrt = coordinates * trans(coordinates);
- arma::mat s = function.C();
-
- for (size_t i = 0; i < function.B().n_elem; ++i)
- {
- double constraint = -function.B()[i];
-
- if (function.AModes()[i] == 0)
- {
- constraint += trace(function.A()[i] * rrt);
- }
- else
- {
- for (size_t j = 0; j < function.A()[i].n_cols; ++j)
- {
- constraint += function.A()[i](2, j) *
- rrt(function.A()[i](0, j), function.A()[i](1, j));
- }
- }
-
- double y = lambda[i] - sigma * constraint;
-
- if (function.AModes()[i] == 0)
- {
- s -= (y * function.A()[i]);
- }
- else
- {
- // We only need to subtract the entries which could be modified.
- for (size_t j = 0; j < function.A()[i].n_cols; ++j)
- {
- s(function.A()[i](0, j), function.A()[i](1, j)) -= y;
- }
- }
- }
-
- gradient = 2 * s * coordinates;
-}
-
-}; // namespace optimization
-}; // namespace mlpack
-
-#endif
-
More information about the mlpack-git
mailing list