1 /*  _______________________________________________________________________
2 
3     PECOS: Parallel Environment for Creation Of Stochastics
4     Copyright (c) 2011, 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 Pecos directory.
7     _______________________________________________________________________ */
8 
9 #error "csopts_toreview.hpp is an inactive header"
10 
11 #ifndef COMPRESSED_SENSING_HPP
12 #define COMPRESSED_SENSING_HPP
13 
14 #include "linear_algebra.hpp"
15 #include "linear_solvers.hpp"
16 //#include "pecos_global_defs.hpp"
17 
18 namespace Pecos {
19 
20 /**
21  * \brief Specify a set of options for using the CompressedSensingTool
22  */
23 class CompressedSensingOptions
24 {
25 public:
26   //solverType solver; //!< Specify which regression solver to use. See solverType
27   short solver; //!< Specify which regression solver to use: see pecos_global_defs
28   Real solverTolerance; //!< Specify the internal tolerance of the solver
29   Real epsilon;         //!< Specify the residual tolerance of the solver
30   Real delta;           //!< Specify the regularization parameter value
31   int maxNumIterations; //!< Specify the maximum number of solver iterations
32   bool standardizeInputs;  //!< Specify if the inputs need to be standardized
33   bool storeHistory;       //<! Specify if the solution history should be stored
34   Real conjugateGradientsTolerance; //<! Specify wether to use conjugate gradients internally to solve newton step in BP and BPDN.  If < 0 cholesky factorization will be used.
35   int verbosity;           //!< The verbosity level. 0: off, 1: warnings on,  2: all print statements on.
36   int numFunctionSamples; //!< The number of function samples used to construct A and B. Used when A contains gradient information. If zero then numFunctionSamples = A.numRows()
37 
38 public:
39 
CompressedSensingOptions()40   CompressedSensingOptions() :
41     solver( DEFAULT_LEAST_SQ_REGRESSION ), solverTolerance( -1. ),
42     epsilon( 0.0 ), delta( 0.0 ),
43     maxNumIterations( std::numeric_limits<int>::max() ),
44     standardizeInputs( false ), storeHistory( false ),
45     conjugateGradientsTolerance( -1 ), verbosity( 0 ), numFunctionSamples( 0 )
46   {};
47 
~CompressedSensingOptions()48   ~CompressedSensingOptions(){};
49 
print()50   void print()
51   {
52     std::cout << "Solver: " << solver << "\n";
53     std::cout << "Solver Tolerance: " << solverTolerance << "\n";
54     std::cout << "Epsilon: " << epsilon << "\n";
55     std::cout << "Delta: " << delta << "\n";
56     std::cout << "MaxNumIterations: " << maxNumIterations << "\n";
57     std::cout << "StandardizeInputs: " << standardizeInputs << "\n";
58     std::cout << "StoreHistory: " << storeHistory << "\n";
59     std::cout << "Verbosity: " << verbosity << "\n";
60   };
61 
operator =(const CompressedSensingOptions & source)62   CompressedSensingOptions& operator=(const CompressedSensingOptions& source )
63   {
64     if(this == &source)
65       return (*this);
66     solver = source.solver;
67     solverTolerance = source.solverTolerance;
68     epsilon = source.epsilon;
69     delta = source.delta;
70     maxNumIterations = source.maxNumIterations;
71     standardizeInputs = source.standardizeInputs;
72     storeHistory = source.storeHistory;
73     conjugateGradientsTolerance = source.conjugateGradientsTolerance;
74     verbosity = source.verbosity;
75     numFunctionSamples = source.numFunctionSamples;
76     return (*this);
77   }
78 
CompressedSensingOptions(const CompressedSensingOptions & source)79   CompressedSensingOptions( const CompressedSensingOptions& source )
80   {
81     solver = source.solver;
82     solverTolerance = source.solverTolerance;
83     epsilon = source.epsilon;
84     delta = source.delta;
85     maxNumIterations = source.maxNumIterations;
86     standardizeInputs = source.standardizeInputs;
87     storeHistory = source.storeHistory;
88     conjugateGradientsTolerance = source.conjugateGradientsTolerance;
89     verbosity = source.verbosity;
90     numFunctionSamples = source.numFunctionSamples;
91   }
92 };
93 
94 typedef std::vector< std::vector<CompressedSensingOptions> > CompressedSensingOptionsList;
95 
96 typedef std::shared_ptr<LinearSolver> LinearSolver_ptr;
97 
98 
99 /**
100  * \class CompressedSensingTool
101  * \brief Tool that implements a number of popular compressed sensing algorithms
102  */
103 class CompressedSensingTool
104 {
105 protected:
106   LinearSolver_ptr linearSolver_;
107 
108 public:
109 
110   /// Default constructor
CompressedSensingTool()111   CompressedSensingTool()
112   {
113     std::cout.precision( std::numeric_limits<Real>::digits10 );
114     std::cout.setf( std::ios::scientific );
115   };
116 
117   // Deconstructor
~CompressedSensingTool()118   ~CompressedSensingTool(){};
119 
120   //! @name Interface tools.
121   //@{
122 
123   /**
124    * \brief Wrapper to call any of the compressed sensing methods
125    *
126    * \param A ( M x N ) matrix of the linear system AX=B
127    *
128    * \param B ( M x num_rhs ) matrix of the linear system AX=B
129    *
130    * \param solutions (output) vector containing multiple solutions
131    * to AX=B. Each entry in solutions is a ( N x num_rhs ) matrix
132    * opts.solver=LS will return only one solution whilst methods such
133    * as OMP, LARS, and LASSO will return a history of solutions
134    *
135    * \param opts specifies the method options
136    *
137    * \param opts_list specifies the method options that can be used to
138    * reproduce all the solutions found.
139    */
140   void solve( RealMatrix &A,
141 	      RealMatrix &B,
142 	      RealMatrixArray &solutions,
143 	      CompressedSensingOptions &opts,
144 	      CompressedSensingOptionsList &opts_list );
145 
146   void set_linear_solver( CompressedSensingOptions &opts );
147 
148   LinearSolver_ptr get_linear_solver();
149 };
150 
151 } // namespace Pecos
152 
153 #endif //COMPRESSED_SENSING_HPP
154