1 /*  _______________________________________________________________________
2 
3     Surfpack: A Software Library of Multidimensional Surface Fitting Methods
4     Copyright (c) 2006, Sandia National Laboratories.
5     This software is distributed under the GNU Lesser General Public License.
6     For more information, see the README file in the top Surfpack directory.
7     _______________________________________________________________________ */
8 
9 #include "surfpack_system_headers.h"
10 #include "SurfpackInterface.h"
11 #include "surfpack.h"
12 #include "AxesBounds.h"
13 #include "SurfpackParserArgs.h"
14 #include "SurfData.h"
15 #include "ModelFactory.h"
16 
17 using std::cerr;
18 using std::cout;
19 using std::endl;
20 using std::ostream;
21 using std::runtime_error;
22 using std::string;
23 using std::vector;
24 using std::ostringstream;
25 
26 #include "SurfpackModel.h"
27 #include "ModelFitness.h"
28 using SurfpackInterface::CreateAxes;
29 using SurfpackInterface::CreateSurface;
30 using SurfpackInterface::LoadData;
31 using SurfpackInterface::LoadModel;
32 using SurfpackInterface::Evaluate;
33 
34 ///////////////////////////////////////////////////////////////////////////////
35 /////		SurfpackInterface namespace functions			  /////
36 ///////////////////////////////////////////////////////////////////////////////
37 
38 
LoadData(const std::string & filename)39 SurfData* SurfpackInterface::LoadData(const std::string& filename)
40 {
41   SurfData* sd = new SurfData(filename);
42   assert(sd);
43   return sd;
44 }
45 
LoadData(const std::string & filename,unsigned n_predictors,unsigned n_responses,unsigned n_cols_to_skip)46 SurfData* SurfpackInterface::LoadData(const std::string& filename, unsigned n_predictors, unsigned n_responses, unsigned n_cols_to_skip)
47 {
48   SurfData* sd = new SurfData(filename,n_predictors,n_responses,n_cols_to_skip);
49   assert(sd);
50   return sd;
51 }
52 
LoadModel(const std::string & filename)53 SurfpackModel* SurfpackInterface::LoadModel(const std::string& filename)
54 {
55   SurfpackModel* model = NULL;
56 
57   // TODO: clean up where files get opened / closed
58 #ifdef SURFPACK_HAVE_BOOST_SERIALIZATION
59   bool binary = surfpack::isBinaryModelFilename(filename);
60 
61   std::ifstream model_ifstream(filename.c_str(), (binary ? std::ios::in|std::ios::binary : std::ios::in));
62   if (!model_ifstream.good())
63     throw std::string("Failure opening model file for load.");
64 
65   if (binary) {
66     boost::archive::binary_iarchive input_archive(model_ifstream);
67     input_archive >> model;
68     std::cout << "Model loaded from binary file '" << filename << "'."
69 	      << std::endl;
70   }
71   else {
72     boost::archive::text_iarchive input_archive(model_ifstream);
73     input_archive >> model;
74     std::cout << "Model loaded from text file '" << filename << "'."
75 	      << std::endl;
76   }
77 
78 #else
79   throw string("surface load requires compilation with Boost serialization.");
80 #endif
81 
82   return model;
83 }
84 
Save(const SurfpackModel * model,const std::string & filename)85 void SurfpackInterface::Save(const SurfpackModel* model, const std::string& filename)
86 {
87   // TODO: consider where files are opened/managed
88 #ifdef SURFPACK_HAVE_BOOST_SERIALIZATION
89   bool binary = surfpack::isBinaryModelFilename(filename);
90 
91   std::ofstream model_ofstream(filename.c_str(), (binary ? std::ios::out|std::ios::binary : std::ios::out));
92   if (!model_ofstream.good())
93     throw std::string("Failure opening model file for save.");
94 
95   if (binary) {
96     boost::archive::binary_oarchive output_archive(model_ofstream);
97     output_archive << model;
98     std::cout << "Model saved to binary file '" << filename << "'."
99 	      << std::endl;
100   }
101   else {
102     boost::archive::text_oarchive output_archive(model_ofstream);
103     output_archive << model;
104     std::cout << "Model saved to text file '" << filename << "'." << std::endl;
105   }
106 #else
107   throw
108     string("surface save requires compilation with Boost serialization.");
109 #endif
110 }
111 
112 
113 
114 
Save(const SurfData * data,const std::string & filename)115 void SurfpackInterface::Save(const SurfData* data, const std::string& filename)
116 {
117   data->write(filename);
118 }
119 
CreateSurface(const SurfData * sd,ParamMap & args)120 SurfpackModel* SurfpackInterface::CreateSurface(const SurfData* sd, ParamMap& args)
121 {
122   assert(sd);
123   SurfpackModel* model = 0;
124   // Surface* model = ModelFactory::Create(ParamMap)
125   return model;
126 }
127 
Evaluate(const SurfpackModel * model,SurfData * sd,const std::string & response_name)128 void SurfpackInterface::Evaluate(const SurfpackModel* model, SurfData* sd,
129   const std::string& response_name)
130 {
131   assert(model);
132   assert(sd);
133   VecDbl responses = (*model)(*sd);
134   sd->addResponse(responses, response_name);
135 }
136 
Evaluate(SurfData * sd,const VecStr test_functions)137 void SurfpackInterface::Evaluate(SurfData* sd, const VecStr test_functions)
138 {
139   assert(sd);
140   for (VecStr::const_iterator itr = test_functions.begin();
141     itr != test_functions.end(); ++itr) {
142     VecDbl results(sd->size());
143     for (unsigned i = 0; i < results.size(); i++) {
144       results[i] = surfpack::testFunction(*itr,(*sd)(i));
145     }
146     sd->addResponse(results,*itr);
147   }
148 }
149 
CreateAxes(const std::string axes)150 AxesBounds* SurfpackInterface::CreateAxes(const std::string axes)
151 {
152   return new AxesBounds(axes);
153 }
154 
CreateSample(const AxesBounds * axes,const VecUns grid_points)155 SurfData* SurfpackInterface::CreateSample(const AxesBounds* axes, const VecUns grid_points)
156 {
157   return axes->sampleGrid(grid_points);
158 }
159 
CreateSample(const AxesBounds * axes,unsigned n_samples)160 SurfData* SurfpackInterface::CreateSample(const AxesBounds* axes, unsigned n_samples)
161 {
162   return axes->sampleMonteCarlo(n_samples);
163 }
164 
Fitness(const SurfpackModel * model,SurfData * sd,const std::string & metric,unsigned response,unsigned n)165 double SurfpackInterface::Fitness(const SurfpackModel* model, SurfData* sd,
166 const std::string& metric, unsigned response, unsigned n)
167 {
168   assert(model);
169   assert(sd);
170   sd->setDefaultIndex(response);
171   ModelFitness* mf = ModelFitness::Create(metric,n);
172   double result = (*mf)(*model,*sd);
173   delete mf;
174   return result;
175 }
176 
177 /// Doxygen comment
Fitness(const SurfpackModel *,const std::string & metric,unsigned response,unsigned n)178 double SurfpackInterface::Fitness(const SurfpackModel*, const std::string& metric,
179 unsigned response, unsigned n)
180 {
181   throw string("Must pass data set to compute metric");
182   return 0.0;
183 }
184 
HasFeature(const std::string & feature)185 bool SurfpackInterface::HasFeature(const std::string& feature)
186 {
187   if (feature == "model_save" || feature == "model_load") {
188 #ifdef SURFPACK_HAVE_BOOST_SERIALIZATION
189     return true;
190 #else
191     return false;
192 #endif
193   }
194 
195   return false;
196 }
197 
198