1 // *******************************************************************
2 // *******************************************************************
3 // Definition for the class Mars
4 // Date         : November, 1998
5 //
6 // Modified by Kevin Long, April 21 1999.
7 // *******************************************************************
8 // *******************************************************************
9 
10 #ifndef MARS_H
11 #define MARS_H
12 
13 #if defined(HAVE_CONFIG_H) && !defined(DISABLE_DAKOTA_CONFIG_H)
14   #include "ddace_config.h"
15   #define MARS_F77 F77_FUNC(mars,MARS)
16   #define FMODM_F77 F77_FUNC(fmodm,FMODM)
17 #else
18   #include "ddace_fortran.h"
19   #define MARS_F77 DDACE_FORTRAN_GLOBAL(mars,MARS)
20   #define FMODM_F77 DDACE_FORTRAN_GLOBAL(fmodm,FMODM)
21 #endif /* HAVE_CONFIG_H */
22 
23 #include "FuncApproxBase.h"
24 #include <vector>
25 #include <iostream>
26 #include <fstream>
27 
28 /**
29  * Mars is a handle class to FuncApproxBase.
30  * This class is an interface to the MARS package for generating
31  * response surfaces.
32  * <p>
33  * EXAMPLE:  Given some (x,y,z) values, MARS can compute
34  * the surface in 3D space.  i.e. MARS will divide the surface
35  * into equal-area rectangles.  MARS will return the (x,y,z)
36  * value of each and every rectangle.
37  *
38  * Author       : Charles Tong
39  * Modified by Kevin Long, April 21 1999.
40  */
41 
42 #ifdef __cplusplus
43 extern "C"  /* prevent C++ name mangling */
44 #endif
45 void MARS_F77(int&, int&, float*, float*, float*, int&,
46 	      int&, int*, float*, int*, float*, double*, int*);
47 
48 #ifdef __cplusplus
49 extern "C"  /* prevent C++ name mangling */
50 #endif
51 void FMODM_F77(int&, int&, float*, float*, int*, float*, float*);
52 
53 class Mars : public FuncApproxBase
54 {
55  public:
56 
57   /**
58    * Constructor
59    */
60   Mars();
61 
62   /**
63    * Constructor
64    * @param nInputs The number of input, or independent, variables.
65    * For example, for 3D space, the number of input variables is 2
66    * (the x and y values).
67    * @param nSamples The number input data points.
68    * For example, for 3D space, the number of (x,y,z) tuples
69    * that are given to MARS.
70    *
71    */
72   Mars(int nInputs, int nSamples);
73 
74 
75   Mars(int nInputs, int nSamples, int nBasis,
76 			 int maxVarPerBasis, const std::vector<int>& varFlags);
77 
~Mars()78   virtual ~Mars(){;}
79 
80 
81 
82   virtual void generateGridData(const std::vector<DDaceSamplePoint>& samplePts,
83 				const std::vector<double>& sampleResults,
84 				std::vector<std::vector<double> >& gridPts,
85 				std::vector<double>& gridResults) ;
86 
87   /**
88    * Given some (x,y,z) data values, compute the
89    * surface of the 3D space.  MARS will divide the surface
90    * into equal-area rectangle.  MARS will return the (x,y,z)
91    * value of each and every rectangle.
92    * <P>
93    * SIDE EFFECT:  The parameter, gridpts, is populated
94    * with the (x,y) coordiantes of the computer surface.
95    * SIDE EFFECT:  the parameter, gridResults, is populated
96    * with the (z) coordiantes of the computer surface.
97    * @param filename The name of the output text file.
98    * @param samplePts Contains (x1, x2, ...) tuples of data
99    * values.  The input variable var1 will select one of
100    * these variables to represent the x axis.  the input
101    * variable var2 will select one of these variables to
102    * represent the y axis.
103    * @param sampleResults The z coordinates of the data that
104    * MARS will process.
105    * @param var1 The index value of the x-axis variable.
106    * If the index value is set to 0, then the first axis
107    * in the input parameter, samplePts, will become the x axis.
108    * @param var2 The index value of the y-axis variable.
109    * If the index value is set to 1, then the second axis
110    * in the input parameter, samplePts, will become the y axis.
111    * @settings The first element contains the number of dimensions
112    * of the response surface (which should be set to 2); the second
113    * element contains the initial z value of the response surface.
114    * @param gridPts Will contain the (x,y) pairs of every
115    * rectangle on the computed surface. <br>
116    * Example, if the x-y space is divided into 9 rectangles, then <br>
117    * &nbsp;&nbsp;girdPts[0] contains (y[0],x[0]) <br>
118    * &nbsp;&nbsp;girdPts[1] contains (y[0],x[1]) <br>
119    * &nbsp;&nbsp;girdPts[2] contains (y[0],x[2]) <br>
120    * &nbsp;&nbsp;girdPts[3] contains (y[1],x[0]) <br>
121    * &nbsp;&nbsp;girdPts[4] contains (y[1],x[1]) <br>
122    * &nbsp;&nbsp;girdPts[5] contains (y[1],x[2]) <br>
123    * &nbsp;&nbsp;girdPts[6] contains (y[2],x[0]) <br>
124    * &nbsp;&nbsp;girdPts[7] contains (y[2],x[1]) <br>
125    * &nbsp;&nbsp;girdPts[8] contains (y[2],x[2]) <br>
126    *
127    * @param gridResults Will contain the (z) coordinate of every
128    * rectangle on the computer surface. <br>
129    * Example, if the x-y space is divided into 9 rectangles, then <br>
130    * &nbsp;&nbsp;girdResults[0] contains z value at (y[0],x[0]) <br>
131    * &nbsp;&nbsp;girdResults[1] contains z value at (y[0],x[1]) <br>
132    * &nbsp;&nbsp;girdResults[2] contains z value at (y[0],x[2]) <br>
133    * &nbsp;&nbsp;girdResults[3] contains z value at (y[1],x[0]) <br>
134    * &nbsp;&nbsp;girdResults[4] contains z value at (y[1],x[1]) <br>
135    * &nbsp;&nbsp;girdResults[5] contains z value at (y[1],x[2]) <br>
136    * &nbsp;&nbsp;girdResults[6] contains z value at (y[2],x[0]) <br>
137    * &nbsp;&nbsp;girdResults[7] contains z value at (y[2],x[1]) <br>
138    * &nbsp;&nbsp;girdResults[8] contains z value at (y[2],x[2]) <br>
139    */
140   virtual void generate2DGridData(const std::vector<DDaceSamplePoint>& samplePts,
141 				  const std::vector<double>& sampleResults,
142 				  int var1,
143 				  int var2,
144 				  const std::vector<double>& settings,
145 				  std::vector<std::vector<double> >& gridPts,
146 				  std::vector<double>& gridResults) ;
147 
148   /**
149    * Given some (x,y,z) data values, compute the
150    * surface of the 3D space.  MARS will divide the surface
151    * into equal-area rectangle.  MARS will return the (x,y,z)
152    * value of each and every rectangle.  The (x,y,z) values
153    * are written to a text file; the format of the file is: <br>
154    * &nbsp;&nbsp;n, number of coordinates per axis <br>
155    * &nbsp;&nbsp;x[0], value of 1st coordinate along x axis <br>
156    * &nbsp;&nbsp;x[1], value of 2nd coordinate along x axis <br>
157    * &nbsp;&nbsp;:
158    * &nbsp;&nbsp;x[n], value of last coordinate along x axis <br>
159    * &nbsp;&nbsp;y[0], value of 1st coordinate along x axis <br>
160    * &nbsp;&nbsp;y[1], value of 2nd coordinate along x axis <br>
161    * &nbsp;&nbsp;:
162    * &nbsp;&nbsp;y[n], value of last coordinate along y axis <br>
163    * &nbsp;&nbsp;z[0][0], value of z at y[0],x[0] <br>
164    * &nbsp;&nbsp;z[0][1], value of z at y[0],x[1] <br>
165    * &nbsp;&nbsp;:
166    * &nbsp;&nbsp;z[0][n], value of z at y[0],x[n] <br>
167    * &nbsp;&nbsp;z[1][0], value of z at y[1],x[0] <br>
168    * &nbsp;&nbsp;z[1][1], value of z at y[1],x[1] <br>
169    * &nbsp;&nbsp;:
170    * &nbsp;&nbsp;z[1][n], value of z at y[1],x[n] <br>
171    * &nbsp;&nbsp;:
172    * &nbsp;&nbsp;:
173    * &nbsp;&nbsp;:
174    * &nbsp;&nbsp;z[n][0], value of z at y[n],x[0] <br>
175    * &nbsp;&nbsp;z[n][1], value of z at y[n],x[1] <br>
176    * &nbsp;&nbsp;:
177    * &nbsp;&nbsp;z[n][n], value of z at y[n],x[n] <br>
178    *
179    * @param filename The name of the output text file.
180    * @param samplePts Contains (x1, x2, ...) tuples of data
181    * values.  The input variable var1 will select one of
182    * these variables to represent the x axis.  the input
183    * variable var2 will select one of these variables to
184    * represent the y axis.
185    * @param sampleResults The z coordinates of the data that
186    * MARS will process.
187    * @param var1 The index value of the x-axis variable.
188    * If the index value is set to 0, then the first axis
189    * in the input parameter, samplePts, will become the x axis.
190    * @param var2 The index value of the y-axis variable.
191    * If the index value is set to 1, then the second axis
192    * in the input parameter, samplePts, will become the y axis.
193    * @settings The first element contains the number of dimensions
194    * of the response surface (which should be set to 2); the second
195    * element contains the initial z value of the response surface.
196    */
197   virtual void write2DGridData(const std::string& filename,
198 			       const std::vector<DDaceSamplePoint>& samplePts,
199 			       const std::vector<double>& sampleResults,
200 			       int var1,
201 			       int var2,
202 			       const std::vector<double>& settings);
203 
204   virtual double evaluatePoint(const std::vector<double> & x) const ;
205   virtual void evaluatePoints(const std::vector<std::vector<double> >& pts,
206 			      std::vector<double>& y) const ;
207 
208   // preprocess: call fortran mars() to get the regression coeffs.
209   void preProcess(const std::vector<DDaceSamplePoint>& samplePts,
210 		  const std::vector<double>& sampleResults);
211 
212   virtual FuncApproxBase* clone() const ;
213  private:
214 
215 
216 
217   std::vector<float> weights_;
218   int nBasis_;
219   int maxVarPerBasis_;
220   std::vector<int> varFlags_;
221   std::vector<float> fm_;
222   std::vector<int> im_;
223 
224   // set default values using static variables, so that they're
225   // available during constructor calls.
226   static int defaultNBasis_;
227   static int defaultMaxVarPerBasis_;
228 };
229 
230 #endif // MARS_H
231 
232 
233 
234 
235 
236 
237