00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050 #include <string>
00051 #include "fastlib/fastlib.h"
00052 #include "mvu_objectives.h"
00053
00054 #include "fastlib/optimization/lbfgs/lbfgs.h"
00079 const fx_entry_doc main_entries[] = {
00080 {"optimized_function", FX_PARAM, FX_STR, NULL,
00081 " choose the method MVU or MFNU .\n"},
00082 {"new_dimension", FX_PARAM, FX_INT, NULL,
00083 " The dimension of the nonlinear projection (MVU or MFNU).\n"},
00084 {"data_file", FX_REQUIRED, FX_STR, NULL,
00085 " the csv file with the data.\n"},
00086 {"result_file", FX_PARAM, FX_STR, NULL,
00087 " the results of the method are exported to a csv file.\n"},
00088 {"pca_pre", FX_PARAM, FX_BOOL, NULL,
00089 " sometimes it is good to do pca preprocessing and then MVU/MFNU.\n"},
00090 {"pca_dim", FX_PARAM, FX_INT, NULL,
00091 " the projection dimension with PCA if chosen.\n"},
00092 {"pca_init", FX_PARAM, FX_BOOL, NULL,
00093 " if this flag is true then the optimization of MVU/MFNU is initialized .\n"},
00094 FX_ENTRY_DOC_DONE
00095 };
00096
00097 const fx_submodule_doc main_submodules[] = {
00098 {"optfun", &mvu_doc,
00099 " Responsible for intializing MVU/MFNU.\n"},
00100 {"lbfgs", &lbfgs_doc,
00101 " Responsible for the Limited BFGS module.\n"},
00102 FX_SUBMODULE_DOC_DONE
00103 };
00104
00105 const fx_module_doc main_doc = {
00106 main_entries, main_submodules,
00107 " This program computes Maximum Variance Unfolding \n"
00108 " and Maximum Furthest Neighbor Unfolding\n"
00109 };
00110
00111 int main(int argc, char *argv[]){
00112 fx_module *fx_root=fx_init(argc, argv, &main_doc);
00113 std::string optimized_function=fx_param_str(fx_root, "/optimized_function", "mvfu");
00114 fx_module *optfun_node;
00115 fx_module *l_bfgs_node;
00116 l_bfgs_node=fx_submodule(fx_root, "/lbfgs");
00117 optfun_node=fx_submodule(fx_root, "/optfun");
00118
00119 index_t new_dimension=fx_param_int(l_bfgs_node, "/new_dimension", 2);
00120 fx_set_param_int(optfun_node, "new_dimension", new_dimension);
00121
00122 if (!fx_param_exists(fx_root, "/optfun/nearest_neighbor_file")) {
00123 Matrix data_mat;
00124 std::string data_file=fx_param_str_req(fx_root, "/data_file");
00125 if (data::Load(data_file.c_str(), &data_mat)==SUCCESS_FAIL) {
00126 FATAL("Didn't manage to load %s", data_file.c_str());
00127 }
00128 NOTIFY("Removing the mean., centering data...");
00129 OptUtils::RemoveMean(&data_mat);
00130
00131
00132 bool pca_preprocess=fx_param_bool(fx_root, "/pca_pre", false);
00133 index_t pca_dimension=fx_param_int(fx_root, "/pca_dim", 5);
00134 bool pca_init=fx_param_bool(fx_root, "/pca_init", false);
00135 Matrix *initial_data=NULL;
00136 if (pca_preprocess==true) {
00137 NOTIFY("Preprocessing with pca");
00138 Matrix temp;
00139 OptUtils::SVDTransform(data_mat, &temp, pca_dimension);
00140 data_mat.Destruct();
00141 data_mat.Own(&temp);
00142 }
00143 if (pca_init==true) {
00144 NOTIFY("Preprocessing with pca");
00145 initial_data = new Matrix();
00146 index_t new_dimension=fx_param_int(l_bfgs_node, "new_dimension", 2);
00147 OptUtils::SVDTransform(data_mat, initial_data, new_dimension);
00148 }
00149
00150
00151 fx_set_param_int(l_bfgs_node, "num_of_points", data_mat.n_cols());
00152 std::string result_file=fx_param_str(fx_root, "/result_file", "result.csv");
00153 bool done=false;
00154
00155 if (optimized_function == "mvu") {
00156 MaxVariance opt_function;
00157 opt_function.Init(optfun_node, data_mat);
00158 Lbfgs<MaxVariance> engine;
00159 engine.Init(&opt_function, l_bfgs_node);
00160 if (pca_init==true) {
00161 engine.set_coordinates(*initial_data);
00162 }
00163 engine.ComputeLocalOptimumBFGS();
00164 if (data::Save(result_file.c_str(), *engine.coordinates())==SUCCESS_FAIL) {
00165 FATAL("Didn't manage to save %s", result_file.c_str());
00166 }
00167 done=true;
00168 }
00169 if (optimized_function == "mvfu"){
00170 MaxFurthestNeighbors opt_function;
00171 opt_function.Init(optfun_node, data_mat);
00172
00173 Lbfgs<MaxFurthestNeighbors> engine;
00174 fx_set_param_bool(l_bfgs_node, "use_default_termination", false);
00175 engine.Init(&opt_function, l_bfgs_node);
00176 if (pca_init==true) {
00177 la::Scale(1e-1, initial_data);
00178 engine.set_coordinates(*initial_data);
00179 }
00180 engine.ComputeLocalOptimumBFGS();
00181 if (data::Save(result_file.c_str(), *engine.coordinates())==SUCCESS_FAIL) {
00182 FATAL("Didn't manage to save %s", result_file.c_str());
00183 }
00184 done=true;
00185 }
00186 if (done==false) {
00187 FATAL("The method you provided %s is not supported",
00188 optimized_function.c_str());
00189 }
00190 if (pca_init==true) {
00191 delete initial_data;
00192 }
00193 fx_done(fx_root);
00194 } else {
00195
00196
00197 std::string result_file=fx_param_str(NULL, "/result_file", "result.csv");
00198 bool done=false;
00199
00200 if (optimized_function == "mvu") {
00201 MaxVariance opt_function;
00202 opt_function.Init(optfun_node);
00203 Matrix init_mat;
00204
00205 fx_set_param_int(l_bfgs_node, "num_of_points", opt_function.num_of_points());
00206
00207 Lbfgs<MaxVariance> engine;
00208 engine.Init(&opt_function, l_bfgs_node);
00209 engine.ComputeLocalOptimumBFGS();
00210 if (data::Save(result_file.c_str(), *engine.coordinates())==SUCCESS_FAIL) {
00211 FATAL("Didn't manage to save %s", result_file.c_str());
00212 }
00213 done=true;
00214 }
00215 if (optimized_function == "mvfu"){
00216 MaxFurthestNeighbors opt_function;
00217 opt_function.Init(optfun_node);
00218
00219 fx_set_param_int(l_bfgs_node, "num_of_points", opt_function.num_of_points());
00220 fx_set_param_bool(l_bfgs_node, "use_default_termination", false);
00221
00222 Lbfgs<MaxFurthestNeighbors> engine;
00223 engine.Init(&opt_function, l_bfgs_node);
00224 engine.ComputeLocalOptimumBFGS();
00225 if (data::Save(result_file.c_str(), *engine.coordinates())==SUCCESS_FAIL) {
00226 FATAL("Didn't manage to save %s", result_file.c_str());
00227 }
00228 done=true;
00229 }
00230 if (done==false) {
00231 FATAL("The method you provided %s is not supported",
00232 optimized_function.c_str());
00233 }
00234 fx_done(fx_root);
00235 }
00236 }