[mlpack-svn] r16186 - in mlpack/trunk/src/mlpack/core/optimizers: . lrsdp
fastlab-svn at coffeetalk-1.cc.gatech.edu
fastlab-svn at coffeetalk-1.cc.gatech.edu
Tue Jan 28 15:42:11 EST 2014
Author: rcurtin
Date: Tue Jan 28 15:42:10 2014
New Revision: 16186
Log:
Add CMake configuration for LRSDP, and split it properly into cpp/hpp/_impl.hpp
files.
Added:
mlpack/trunk/src/mlpack/core/optimizers/lrsdp/CMakeLists.txt
mlpack/trunk/src/mlpack/core/optimizers/lrsdp/lrsdp.cpp
- copied, changed from r16183, /mlpack/trunk/src/mlpack/core/optimizers/lrsdp/lrsdp_impl.hpp
Modified:
mlpack/trunk/src/mlpack/core/optimizers/CMakeLists.txt
mlpack/trunk/src/mlpack/core/optimizers/lrsdp/lrsdp.hpp
mlpack/trunk/src/mlpack/core/optimizers/lrsdp/lrsdp_impl.hpp
Modified: mlpack/trunk/src/mlpack/core/optimizers/CMakeLists.txt
==============================================================================
--- mlpack/trunk/src/mlpack/core/optimizers/CMakeLists.txt (original)
+++ mlpack/trunk/src/mlpack/core/optimizers/CMakeLists.txt Tue Jan 28 15:42:10 2014
@@ -1,6 +1,7 @@
set(DIRS
aug_lagrangian
lbfgs
+ lrsdp
sgd
)
Added: mlpack/trunk/src/mlpack/core/optimizers/lrsdp/CMakeLists.txt
==============================================================================
--- (empty file)
+++ mlpack/trunk/src/mlpack/core/optimizers/lrsdp/CMakeLists.txt Tue Jan 28 15:42:10 2014
@@ -0,0 +1,12 @@
+set(SOURCES
+ lrsdp.hpp
+ lrsdp.cpp
+ lrsdp_impl.hpp
+)
+
+set(DIR_SRCS)
+foreach(file ${SOURCES})
+ set(DIR_SRCS ${DIR_SRCS} ${CMAKE_CURRENT_SOURCE_DIR}/${file})
+endforeach()
+
+set(MLPACK_SRCS ${MLPACK_SRCS} ${DIR_SRCS} PARENT_SCOPE)
Copied: mlpack/trunk/src/mlpack/core/optimizers/lrsdp/lrsdp.cpp (from r16183, /mlpack/trunk/src/mlpack/core/optimizers/lrsdp/lrsdp_impl.hpp)
==============================================================================
--- /mlpack/trunk/src/mlpack/core/optimizers/lrsdp/lrsdp_impl.hpp (original)
+++ mlpack/trunk/src/mlpack/core/optimizers/lrsdp/lrsdp.cpp Tue Jan 28 15:42:10 2014
@@ -1,18 +1,15 @@
/**
- * @file lrsdp_impl.hpp
+ * @file lrsdp.cpp
* @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 {
+using namespace mlpack;
+using namespace mlpack::optimization;
+using namespace std;
LRSDP::LRSDP(const size_t numConstraints,
const arma::mat& initialPoint) :
@@ -51,7 +48,7 @@
void LRSDP::Gradient(const arma::mat& /*coordinates*/,
arma::mat& /*gradient*/) const
{
- Log::Fatal << "LRSDP::Gradient() called! Uh-oh..." << std::endl;
+ Log::Fatal << "LRSDP::Gradient() called! Not implemented! Uh-oh..." << std::endl;
}
double LRSDP::EvaluateConstraint(const size_t index,
@@ -70,108 +67,14 @@
}
}
-void LRSDP::GradientConstraint(const size_t /*index*/,
- const arma::mat& /*coordinates*/,
- arma::mat& /*gradient*/) const
+void LRSDP::GradientConstraint(const size_t /* index */,
+ const arma::mat& /* coordinates */,
+ arma::mat& /* gradient */) const
{
- Log::Fatal << "LRSDP::GradientConstraint() called! Uh-oh..." << std::endl;
+ Log::Fatal << "LRSDP::GradientConstraint() called! Not implemented! Uh-oh..." << std::endl;
}
const arma::mat& LRSDP::GetInitialPoint()
{
return initialPoint;
}
-
-// 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
Modified: mlpack/trunk/src/mlpack/core/optimizers/lrsdp/lrsdp.hpp
==============================================================================
--- mlpack/trunk/src/mlpack/core/optimizers/lrsdp/lrsdp.hpp (original)
+++ mlpack/trunk/src/mlpack/core/optimizers/lrsdp/lrsdp.hpp Tue Jan 28 15:42:10 2014
@@ -14,6 +14,15 @@
namespace mlpack {
namespace optimization {
+/**
+ * An implementation of a low-rank semidefinite program solver, based on
+ * Monteiro and Burer's formulation. The optimizer is the augmented Lagrangian
+ * algorithm, and thus this class has Evaluate(), Gradient(),
+ * EvaluateConstraint(), and GradientConstraint() functions for that purpose.
+ * However, you should not call the Gradient() or GradientConstraint() functions
+ * by hand, because they are only implemented as part of the augmented
+ * Lagrangian optimizer.
+ */
class LRSDP
{
public:
@@ -133,7 +142,8 @@
}; // namespace optimization
}; // namespace mlpack
-// Include implementation.
+// Include implementation (template specializations for the augmented Lagrangian
+// function).
#include "lrsdp_impl.hpp"
#endif
Modified: mlpack/trunk/src/mlpack/core/optimizers/lrsdp/lrsdp_impl.hpp
==============================================================================
--- mlpack/trunk/src/mlpack/core/optimizers/lrsdp/lrsdp_impl.hpp (original)
+++ mlpack/trunk/src/mlpack/core/optimizers/lrsdp/lrsdp_impl.hpp Tue Jan 28 15:42:10 2014
@@ -14,74 +14,6 @@
namespace mlpack {
namespace optimization {
-LRSDP::LRSDP(const size_t numConstraints,
- const arma::mat& initialPoint) :
- a(numConstraints),
- b(numConstraints),
- aModes(numConstraints),
- initialPoint(initialPoint),
- augLagInternal(*this),
- augLag(augLagInternal)
-{ }
-
-LRSDP::LRSDP(const size_t numConstraints,
- const arma::mat& initialPoint,
- AugLagrangian<LRSDP>& augLag) :
- a(numConstraints),
- b(numConstraints),
- aModes(numConstraints),
- initialPoint(initialPoint),
- augLagInternal(*this),
- augLag(augLag)
-{ }
-
-double LRSDP::Optimize(arma::mat& coordinates)
-{
- augLag.Sigma() = 20;
- augLag.Optimize(coordinates, 1000);
-
- return Evaluate(coordinates);
-}
-
-double LRSDP::Evaluate(const arma::mat& coordinates) const
-{
- return -accu(coordinates * trans(coordinates));
-}
-
-void LRSDP::Gradient(const arma::mat& /*coordinates*/,
- arma::mat& /*gradient*/) const
-{
- Log::Fatal << "LRSDP::Gradient() called! Uh-oh..." << std::endl;
-}
-
-double LRSDP::EvaluateConstraint(const size_t index,
- const arma::mat& coordinates) const
-{
- arma::mat rrt = coordinates * trans(coordinates);
- if (aModes[index] == 0)
- return trace(a[index] * rrt) - b[index];
- else
- {
- double value = -b[index];
- for (size_t i = 0; i < a[index].n_cols; ++i)
- value += a[index](2, i) * rrt(a[index](0, i), a[index](1, i));
-
- return value;
- }
-}
-
-void LRSDP::GradientConstraint(const size_t /*index*/,
- const arma::mat& /*coordinates*/,
- arma::mat& /*gradient*/) const
-{
- Log::Fatal << "LRSDP::GradientConstraint() called! Uh-oh..." << std::endl;
-}
-
-const arma::mat& LRSDP::GetInitialPoint()
-{
- return initialPoint;
-}
-
// Custom specializations of the AugmentedLagrangianFunction for the LRSDP case.
template<>
double AugLagrangianFunction<LRSDP>::Evaluate(const arma::mat& coordinates)
More information about the mlpack-svn
mailing list