main.cc

00001 /* MLPACK 0.2
00002  *
00003  * Copyright (c) 2008, 2009 Alexander Gray,
00004  *                          Garry Boyer,
00005  *                          Ryan Riegel,
00006  *                          Nikolaos Vasiloglou,
00007  *                          Dongryeol Lee,
00008  *                          Chip Mappus, 
00009  *                          Nishant Mehta,
00010  *                          Hua Ouyang,
00011  *                          Parikshit Ram,
00012  *                          Long Tran,
00013  *                          Wee Chin Wong
00014  *
00015  * Copyright (c) 2008, 2009 Georgia Institute of Technology
00016  *
00017  * This program is free software; you can redistribute it and/or
00018  * modify it under the terms of the GNU General Public License as
00019  * published by the Free Software Foundation; either version 2 of the
00020  * License, or (at your option) any later version.
00021  *
00022  * This program is distributed in the hope that it will be useful, but
00023  * WITHOUT ANY WARRANTY; without even the implied warranty of
00024  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00025  * General Public License for more details.
00026  *
00027  * You should have received a copy of the GNU General Public License
00028  * along with this program; if not, write to the Free Software
00029  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00030  * 02110-1301, USA.
00031  */
00032 /*
00033  * =====================================================================================
00034  *
00035  *       Filename:  main.cc
00036  *
00037  *    Description:  
00038  *
00039  *        Version:  1.0
00040  *        Created:  10/27/2008 11:52:43 PM EDT
00041  *       Revision:  none
00042  *       Compiler:  gcc
00043  *
00044  *         Author:  Nikolaos Vasiloglou (NV), nvasil@ieee.org
00045  *        Company:  Georgia Tech Fastlab-ESP Lab
00046  *
00047  * =====================================================================================
00048  */
00049 
00050 #include <cerrno>
00051 #include <string>
00052 #include "fastlib/fastlib.h"
00053 #include "allkfn.h"
00054 
00055 
00056 int main(int argc, char *argv[]) {
00057   fx_module *module = fx_init(argc, argv, NULL);
00058   std::string result_file = fx_param_str(module, "result_file", "result.txt");
00059   std::string reference_file = fx_param_str_req(module, "reference_file");
00060   Matrix reference_data;
00061   ArrayList<index_t> neighbors;
00062   ArrayList<double> distances;
00063   if (data::Load(reference_file.c_str(), &reference_data)==SUCCESS_FAIL) {
00064     FATAL("Reference file %s not found", reference_file.c_str());
00065   }
00066   NOTIFY("Loaded reference data from file %s", reference_file.c_str());
00067  
00068   AllkFN allkfn; 
00069   if (fx_param_exists(module, "query_file")) {
00070     std::string query_file=fx_param_str_req(module, "query_file");
00071     Matrix query_data;
00072     if (data::Load(query_file.c_str(), &query_data)==SUCCESS_FAIL) {
00073       FATAL("Query file %s not found", query_file.c_str());
00074     }
00075     NOTIFY("Query data loaded from %s", query_file.c_str());
00076     NOTIFY("Building query and reference tree"); 
00077     allkfn.Init(query_data, reference_data, module);
00078   } else {
00079     NOTIFY("Building reference tree");
00080     allkfn.Init(reference_data, module);
00081   }
00082   NOTIFY("Tree(s) built");
00083   index_t kfns=fx_param_int_req(module, "kfns");
00084   NOTIFY("Computing %"LI"d furthest neighbors", kfns);
00085   allkfn.ComputeNeighbors(&neighbors, &distances);
00086   NOTIFY("Neighbors computed");
00087   NOTIFY("Exporting results");
00088   FILE *fp=fopen(result_file.c_str(), "w");
00089   if (fp==NULL) {
00090     FATAL("Error while opening %s...%s", result_file.c_str(),
00091         strerror(errno));
00092   }
00093   for(index_t i=0 ; i < neighbors.size()/kfns ; i++) {
00094     for(index_t j=0; j<kfns; j++) {
00095       fprintf(fp, "%"LI"d %"LI"d %lg\n", i, neighbors[i*kfns+j], distances[i*kfns+j]);
00096     }
00097   }
00098   fclose(fp);
00099   fx_done(module);
00100 }
Generated on Mon Jan 24 12:04:37 2011 for FASTlib by  doxygen 1.6.3