Seems to compile , I have to remove from svn at some point
th dual_manifold_objective{_impl}.h
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
librule(name="dual_manifold",
|
||||
headers=["dual_manifold_engine.h", "dual_manifold_engine_impl.h",
|
||||
"mvu_dot_prod_objective.h", "mvu_dot_prod_objective_impl.h"],
|
||||
deplibs=["fastlib:fastlib", "contrib/nvasil/l_bfgs:l_bfgs"])
|
||||
|
||||
binrule(name="test",
|
||||
sources=["dual_manifold_engine_test.cc"],
|
||||
deplibs=[":dual_manifold", "fastlib:fastlib"])
|
||||
@@ -18,7 +18,6 @@
|
||||
|
||||
#ifndef DUAL_MANIFOLD_ENGINE_
|
||||
#define DUAL_MANIFOLD_ENGINE_
|
||||
#include "dual_manifold_objective.h"
|
||||
#include "../l_bfgs/l_bfgs.h"
|
||||
#include "../l_bfgs/optimization_utils.h"
|
||||
/**
|
||||
@@ -58,14 +57,13 @@ class DualManifoldEngine {
|
||||
double norm_grad_tolerance_;
|
||||
index_t iterations_;
|
||||
index_t max_iterations_;
|
||||
datanode *module;
|
||||
double desired_error_;
|
||||
datanode *module_;
|
||||
|
||||
|
||||
LBfgs<OptimizedFunction> lbfgs1_;
|
||||
LBfgs<OptimizedFunction> lbfgs2_;
|
||||
LBfgs<OptimizedFunction> l_bfgs1_;
|
||||
LBfgs<OptimizedFunction> l_bfgs2_;
|
||||
OptimizedFunction optimized_function1_;
|
||||
OptimizedFunction optimized_function2_;
|
||||
|
||||
};
|
||||
|
||||
#include "dual_manifold_engine_impl.h"
|
||||
|
||||
@@ -17,15 +17,15 @@
|
||||
*/
|
||||
|
||||
template<typename OptimizedFunction>
|
||||
DualManifoldEngine<OptimizedFunction>::Init(datanode *module,
|
||||
void DualManifoldEngine<OptimizedFunction>::Init(datanode *module,
|
||||
// index pairs to consider from the matrix (row,column) pairs
|
||||
ArrayList<std::pair<index_t, index_t> > &pairs_to_consider,
|
||||
// The values of the (row, column) values, also known as the dot products
|
||||
ArrayList<double> &dot_prod_values) {
|
||||
|
||||
module_=module;
|
||||
l_bfg1_.Init(&optimized_function1_, fx_param_node(module_, "l_bfgs"));
|
||||
l_bfg2_.Init(&optimized_function2_, fx_param_node(module_, "l_bfgs"));
|
||||
l_bfgs1_.Init(&optimized_function1_, fx_param_node(module_, "l_bfgs"));
|
||||
l_bfgs2_.Init(&optimized_function2_, fx_param_node(module_, "l_bfgs"));
|
||||
|
||||
optimized_function1_.Init(fx_param_node(module_, "opt1"),
|
||||
l_bfgs1_.coordinates(),
|
||||
@@ -41,13 +41,20 @@ DualManifoldEngine<OptimizedFunction>::Init(datanode *module,
|
||||
|
||||
}
|
||||
|
||||
template<typename OptimizedFunction>
|
||||
void DualManifoldEngine<OptimizedFunction>::Destruct() {
|
||||
|
||||
}
|
||||
|
||||
template<typename OptimizedFunction>
|
||||
void DualManifoldEngine<OptimizedFunction>::ComputeLocalOptimum() {
|
||||
for(index_t i=0; i<max_iterations_; i++) {
|
||||
l_bfgs1_.ComputeLocalOptimumBFGS();
|
||||
l_bfgs1_.Reset();
|
||||
l_bfgs2_.ComputeLocalOptimumBFGS();
|
||||
l_bfgs2_.Reset();
|
||||
double error;
|
||||
optimoptimized_function1_.ComputeFeasibilityError(l_bfgs1_.coordinates(), &error);
|
||||
optimized_function1_.ComputeFeasibilityError(l_bfgs1_.coordinates(), &error);
|
||||
if (error<desired_error_) {
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
* =====================================================================================
|
||||
*
|
||||
* Filename: dual_manifold_engine_test.cc
|
||||
*
|
||||
* Description:
|
||||
*
|
||||
* Version: 1.0
|
||||
* Created: 04/11/2008 11:21:53 AM EDT
|
||||
* Revision: none
|
||||
* Compiler: gcc
|
||||
*
|
||||
* Author: Nikolaos Vasiloglou (NV), nvasil@ieee.org
|
||||
* Company: Georgia Tech Fastlab-ESP Lab
|
||||
*
|
||||
* =====================================================================================
|
||||
*/
|
||||
#include <string>
|
||||
#include "fastlib/fastlib.h"
|
||||
#include "dual_manifold_engine.h"
|
||||
#include "mvu_dot_prod_objective.h"
|
||||
#include "errno.h"
|
||||
|
||||
class DualManifoldEngineTest {
|
||||
public:
|
||||
void Init() {
|
||||
std::string filename="/net/hg200/nvasil/dataset/ml-data_0/u.data";
|
||||
FILE *fp=fopen(filename.c_str(), "r");
|
||||
if (fp==NULL) {
|
||||
FATAL("Unable to open file %s, error %s\n", filename.c_str(),
|
||||
strerror(errno));
|
||||
}
|
||||
ArrayList<std::pair<index_t, index_t> > pairs_to_consider;
|
||||
pairs_to_consider.Init();
|
||||
ArrayList<double> dot_prods;
|
||||
dot_prods.Init();
|
||||
while (!feof(fp)) {
|
||||
index_t user_id;
|
||||
index_t movie_id;
|
||||
double rating;
|
||||
fscanf(fp, "%i %i %lg", &user_id, &movie_id, &rating);
|
||||
pairs_to_consider.PushBackCopy(std::make_pair(user_id, movie_id));
|
||||
dot_prods.PushBackCopy(rating);
|
||||
}
|
||||
fclose(fp);
|
||||
engine_.Init(NULL, pairs_to_consider, dot_prods);
|
||||
}
|
||||
void Destruct() {
|
||||
engine_.Destruct();
|
||||
}
|
||||
void Test1() {
|
||||
NOTIFY("Testing Test1..");
|
||||
Init();
|
||||
Test1();
|
||||
Destruct();
|
||||
NOTIFY("Test1 passed!!");
|
||||
}
|
||||
void TestAll() {
|
||||
Test1();
|
||||
}
|
||||
|
||||
private:
|
||||
DualManifoldEngine<MVUDotProdObjective> engine_;
|
||||
};
|
||||
|
||||
int main(int argc, char * argv[]) {
|
||||
fx_init(argc, argv);
|
||||
DualManifoldEngineTest test;
|
||||
test.TestAll();
|
||||
fx_done();
|
||||
}
|
||||
@@ -16,13 +16,13 @@
|
||||
* =====================================================================================
|
||||
*/
|
||||
|
||||
void DualMaxVarianc::Init(datanode *module, Matrix *other_part,
|
||||
ArrayList<std::pair<index_t, index_t> > pairs_to_consider,
|
||||
ArrayList<double> *dot_prod_values) {
|
||||
other_part_=other_part;
|
||||
void DualMaxVariance::Init(datanode *module, Matrix *other_part,
|
||||
ArrayList<std::pair<index_t, index_t> > &pairs_to_consider,
|
||||
ArrayList<double> &dot_prod_values) {
|
||||
auxiliary_mat__=other_part;
|
||||
module_=module;
|
||||
pairs_to_consider_=pairs_to_consider;
|
||||
dot_prod_values_=dot_prod_values;
|
||||
pairs_to_consider_.Copy(pairs_to_consider);
|
||||
dot_prod_values_.Copy(dot_prod_values);
|
||||
eq_lagrange_mult_.Init(pairs_to_consider_.size());
|
||||
eq_lagrange_mult_.SetAll(1.0);
|
||||
}
|
||||
|
||||
@@ -24,8 +24,8 @@ void MVUDotProdObjective::Init(datanode *module,
|
||||
|
||||
module_=module;
|
||||
auxiliary_mat_=coordinates;
|
||||
pairs_to_consider_.Copy(pairs_to_consider);
|
||||
dot_prod_values_.Copy(dot_prod_values);
|
||||
pairs_to_consider_.InitCopy(pairs_to_consider);
|
||||
dot_prod_values_.InitCopy(dot_prod_values);
|
||||
eq_lagrange_mult_.Init(dot_prod_values.size());
|
||||
eq_lagrange_mult_.SetAll(0.0);
|
||||
num_of_constraints_=dot_prod_values_.size();
|
||||
@@ -37,7 +37,7 @@ void MVUDotProdObjective::ComputeGradient(Matrix &coordinates, Matrix *gradient)
|
||||
la::Scale(-1.0, gradient);
|
||||
index_t dimension=auxiliary_mat_->n_rows();
|
||||
Vector constant;
|
||||
for (index_t i=0; i<num_of_constraints; i++) {
|
||||
for (index_t i=0; i<num_of_constraints_; i++) {
|
||||
index_t ind1=pairs_to_consider_[i].first;
|
||||
index_t ind2=pairs_to_consider_[i].second;
|
||||
double *p1=auxiliary_mat_->GetColumnPtr(ind1);
|
||||
@@ -62,6 +62,7 @@ void MVUDotProdObjective::ComputeObjective(Matrix &coordinates, double *objectiv
|
||||
}
|
||||
|
||||
void MVUDotProdObjective::ComputeFeasibilityError(Matrix &coordinates, double *error) {
|
||||
index_t dimension = coordinates.n_rows();
|
||||
*error=0;
|
||||
for(index_t i=0; i<num_of_constraints_; i++) {
|
||||
index_t ind1=pairs_to_consider_[i].first;
|
||||
@@ -70,14 +71,15 @@ void MVUDotProdObjective::ComputeFeasibilityError(Matrix &coordinates, double *e
|
||||
double *p2=coordinates.GetColumnPtr(ind2);
|
||||
double dot_prod =la::Dot(dimension, p1, p2);
|
||||
double diff=dot_prod-dot_prod_values_[i];
|
||||
error +=diff*diff;
|
||||
*error +=diff*diff;
|
||||
}
|
||||
}
|
||||
|
||||
double MVUDotProdObjective::ComputeLagrangian(Matrix &coordinates) {
|
||||
double lagrangian=0;
|
||||
index_t dimension = coordinates.n_rows();
|
||||
ComputeObjective(coordinates, &lagrangian);
|
||||
for(index_t i=0; i<num_of_constriants_; i++) {
|
||||
for(index_t i=0; i<num_of_constraints_; i++) {
|
||||
index_t ind1=pairs_to_consider_[i].first;
|
||||
index_t ind2=pairs_to_consider_[i].second;
|
||||
double *p1=auxiliary_mat_->GetColumnPtr(ind1);
|
||||
@@ -86,6 +88,7 @@ double MVUDotProdObjective::ComputeLagrangian(Matrix &coordinates) {
|
||||
double diff=dot_prod-dot_prod_values_[i];
|
||||
lagrangian+= -eq_lagrange_mult_[i]*diff + sigma_*diff*diff;
|
||||
}
|
||||
return lagrangian;
|
||||
}
|
||||
|
||||
void MVUDotProdObjective::UpdateLagrangeMult(Matrix &coordinates) {
|
||||
|
||||
Reference in New Issue
Block a user