1 #ifndef GenSetBase_h
2 #define GenSetBase_h
3 //------------------------------------------------------------------------
4 // Generating Set Class - for use with OptGSS
5 //------------------------------------------------------------------------
6 
7 /*------------------------------------------------------------------------
8  Copyright (c) 2003,
9  Ricardo Oliva (raoliva@lbl.gov)
10  Lawrence Berkeley National Laboratory
11  ------------------------------------------------------------------------*/
12 
13 #ifdef HAVE_CONFIG_H
14 #include "OPT++_config.h"
15 #endif
16 
17 #define WANT_MATH
18 
19 #include <string>
20 #include <iostream>
21 #ifdef HAVE_STD
22 #include <cfloat>
23 #include <cstdlib>
24 #else
25 #include <float.h>
26 #include <stdlib.h>
27 #endif
28 
29 #include "newmatap.h"
30 
31 using std::cerr;
32 using std::string;
33 
34 namespace OPTPP {
35 
36 class GenSetBase {  // Generating Set Base Class
37 
38  protected:
39   int  Vdim;
40 
41   int  Size;
42   int  nAct;
43 
44   NEWMAT::ColumnVector ActiveIDs;
45   NEWMAT::ColumnVector InactiveIDs;
46 
47  public:
classnm()48   virtual string classnm() { return "GenSetBase";};
49 
50   // default constructor;
GenSetBase()51   GenSetBase() : Vdim(0),  Size(0), nAct(0) {};
52 
53   // Constructor with specific vector-size
GenSetBase(int n)54   GenSetBase(int n) : Vdim(n), Size(0), nAct(0) {};
55 
56   /// Destructor
~GenSetBase()57   virtual ~GenSetBase() {;}
58 
59   // Basic init method -- call after default constr.
init(int vd)60   void init(int vd) { Vdim = vd; }
61 
62   // Basic set/get methods
setSize(int s)63   void setSize(int s) { Size = s; }
setVdim(int n)64   void setVdim(int n) { Vdim = n; }
size()65   int size() { return Size; }
vdim()66   int vdim() { return Vdim; }
67 
68   //--
69   // Generating Methods
70   //--
71 
72   // -- wrt ALL Directions --
73 
74   //  virtual NEWMAT::ColumnVector generate(int i);
75   ///< Returns  d_i = ith element of D
76 
77   //  virtual void generate(int i, NEWMAT::ColumnVector& y);
78   ///< Stores d_i in y
79 
80   //  virtual NEWMAT::ColumnVector generate(int i, double a, NEWMAT::ColumnVector& x);
81   ///< Returns the vector y_i =  x + a*d_i
82 
83   virtual void generate(int i, double a, NEWMAT::ColumnVector &x,
84     NEWMAT::ColumnVector &y) = 0;
85   ///< Stores in y the vector  x + a*d_i
86 
87   // -- wrt ACTIVE Directions --
88   /*
89   virtual
90     NEWMAT::ColumnVector generateActive(int i)
91     { return generate(activeID(i)); }
92 
93   virtual
94     void generateActive(int i, NEWMAT::ColumnVector& y)
95     { generate(activeID(i), y); }
96 
97   virtual
98     NEWMAT::ColumnVector generateActive(int i, double s, NEWMAT::ColumnVector& x)
99     { return generate(activeID(i), s, x); }
100   */
generateActive(int i,double s,NEWMAT::ColumnVector & x,NEWMAT::ColumnVector & y)101   virtual void generateActive(int i, double s, NEWMAT::ColumnVector &x,
102     NEWMAT::ColumnVector &y)
103     { generate(activeID(i), s, x, y); }
104 
105   // -- wrt INACTIVE Directions --
106   /*
107   virtual
108     NEWMAT::ColumnVector generateInactive(int i)
109     ///< Returns b_i, the ith element of the INACTIVE subset of D
110     {
111       NEWMAT::ColumnVector v;
112       v = generate(inactiveID(i));
113       return v;
114     }
115 
116   virtual
117     void generateInactive(int i, NEWMAT::ColumnVector& y)
118     ///< Stores b_i in y
119     { generate(inactiveID(i), y); }
120 
121   virtual
122     NEWMAT::ColumnVector generateInactive(int i, double s, NEWMAT::ColumnVector& x)
123     ///< Returns the vector x + s*b_i,
124     { return generate(inactiveID(i), s, x); }
125   */
126   virtual
generateInactive(int i,double s,NEWMAT::ColumnVector & x,NEWMAT::ColumnVector & y)127     void generateInactive(int i, double s, NEWMAT::ColumnVector &x, NEWMAT::ColumnVector &y)
128     ///< Stores in y the vector x + s*b_i,
129     { generate(inactiveID(i), s, x, y); }
130 
131   //--
132   // Pruning methods
133   //--
initActive()134   virtual void initActive() {
135     // call this in constructor of derived class
136     // after size of derived class has been set
137     if (Size==0) {
138       cerr << "!!! ERROR: GenSetBase::initActive() called when size==0\n";
139       return;
140     }
141     nAct = Size;
142     ActiveIDs.ReSize(Size);
143     for (int i=1; i<=Size; i++) ActiveIDs(i) = i;
144     InactiveIDs.ReSize(Size);
145     InactiveIDs = 0;
146   }
147 
nActive()148   virtual int nActive() { return nAct; }
nInactive()149   virtual int nInactive() { return (Size - nAct); }
activeID(int j)150   virtual int activeID(int j) { return static_cast<int>(ActiveIDs(j)); }
inactiveID(int j)151   virtual int inactiveID(int j) { return static_cast<int>(InactiveIDs(j)); }
152 
init()153   virtual int init(){ return 0;}    ///< Computes initial generating set D
init(NEWMAT::ColumnVector & pV)154   virtual int init(NEWMAT::ColumnVector& pV){ return 0;}
155 
update()156   virtual int update(){ return 0;}    ///< Updates D on each iteration
update(NEWMAT::ColumnVector & pV)157   virtual int update(NEWMAT::ColumnVector& pV){return 0;}
158 
prunes()159   virtual bool prunes(){return false;}
160   ///< switch to true if implementing pruning in derived class
161 
162   bool   generateAll(NEWMAT::Matrix& M, NEWMAT::ColumnVector& X, double Delta=1.0);
163   NEWMAT::Matrix generateAll(NEWMAT::ColumnVector& X, double D=1.0) {
164     NEWMAT::Matrix M(Vdim,Size);
165     generateAll(M,X,D);
166     return M;
167   }
168   NEWMAT::Matrix generateAll(double Delta=1.0) {
169     NEWMAT::ColumnVector X(Vdim);
170     X = 0;
171     return generateAll(X,Delta);
172   }
173 
174   bool   generateAllActive(NEWMAT::Matrix& M, NEWMAT::ColumnVector& X, double Delta=1.0);
175   NEWMAT::Matrix generateAllActive(NEWMAT::ColumnVector& X, double D=1.0) {
176     int n = nActive();
177     int m = Vdim;
178     NEWMAT::Matrix M(m,n);
179     generateAllActive(M,X,D);
180     return M;
181   }
182   NEWMAT::Matrix generateAllActive(double Delta=1.0) {
183     NEWMAT::ColumnVector X(Vdim);
184     X = 0;
185     return generateAllActive(X,Delta);
186   }
187 
188   NEWMAT::Matrix pllMesh(int P, NEWMAT::ColumnVector& xc, NEWMAT::ColumnVector& xn, double d=0.0);
189 
190 }; // end of GenSetBase class
191 
192 } // namespace OPTPP
193 
194 #endif
195