G+Smo  24.08.0
Geometry + Simulation Modules
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
gsOptProblem.h
Go to the documentation of this file.
1 
14 #include <gsCore/gsLinearAlgebra.h>
15 
16 #pragma once
17 
18 namespace gismo
19 {
23 template <typename T>
25 {
26 
27 public:
28 
29  // /** default constructor */
30  // gsOptProblem();
31 
32  // /** default destructor */
33  // virtual ~gsOptProblem();
34 
35 public:
36 
39  virtual T evalObj( const gsAsConstVector<T> &) const
41 
45  virtual void gradObj_into ( const gsAsConstVector<T> & u, gsAsVector<T> & result) const
46  {
47  const index_t n = u.rows();
48  //GISMO_ASSERT((index_t)m_numDesignVars == n*m, "Wrong design.");
49 
50  gsMatrix<T> uu = u;//copy
51  gsAsVector<T> tmp(uu.data(), n);
52  gsAsConstVector<T> ctmp(uu.data(), n);
53  index_t c = 0;
54 
55  // for all partial derivatives (column-wise)
56  for ( index_t i = 0; i!=n; i++ )
57  {
58  // to do: add m_desLowerBounds m_desUpperBounds check
59  tmp[i] += T(0.00001);
60  const T e1 = this->evalObj(ctmp);
61  tmp[i] = u[i] + T(0.00002);
62  const T e3 = this->evalObj(ctmp);
63  tmp[i] = u[i] - T(0.00001);
64  const T e2 = this->evalObj(ctmp);
65  tmp[i] = u[i] - T(0.00002);
66  const T e4 = this->evalObj(ctmp);
67  tmp[i] = u[i];
68  result[c++]= ( 8 * (e1 - e2) + e4 - e3 ) / T(0.00012);
69  }
70  }
71 
72 
74  virtual void evalCon_into ( const gsAsConstVector<T> &, gsAsVector<T> &) const
76 
80  virtual void jacobCon_into( const gsAsConstVector<T> &, gsAsVector<T> &) const
82 
84  virtual void hessLagr_into( const gsAsConstVector<T> &, gsAsVector<T> &) const
86 
92  virtual void computeJacStructure()
93  {
94  // full Jacobian
96  m_conJacCols.resize(m_numConstraints*m_numDesignVars);
97  index_t c = 0;
98  for ( index_t j = 0; j < m_numDesignVars; ++j)
99  for ( index_t i = 0; i < m_numConstraints; ++i)
100  {
101  m_conJacRows[c ] = i;
102  m_conJacCols[c++] = j;
103  }
104 
106  }
107 
108  //void evalSingleCon_into(int k, gsMatrix<T> & result);
109  //void hassObj_into( gsMatrix<T> & result);
110 
111 
115  // virtual bool intermediateCallback() { return true;}
116 
117 public:
118 
119  int numDesignVars () const { return m_curDesign.size(); }
120 
121  int numConstraints() const { return m_numConstraints; }
122 
123  int numConJacNonZero() const { return m_numConJacNonZero; }
124 
125  int numConDerivs () const { return m_conJacRows.size(); }
126 
127  T lowerCon(int k) const { return m_conLowerBounds[k]; }
128 
129  T upperCon(int k) const { return m_conUpperBounds[k]; }
130 
131  T lowerDesignVar(int k) const { return m_desLowerBounds[k]; }
132 
133  T upperDesignVar(int k) const { return m_desUpperBounds[k]; }
134 
135  const gsVector<T> & desLowerBounds() const {return m_desLowerBounds;}
136  const gsVector<T> & desUpperBounds() const {return m_desUpperBounds;}
137 
138  const gsVector<T> & conLowerBounds() const {return m_conLowerBounds;}
139  const gsVector<T> & conUpperBounds() const {return m_conUpperBounds;}
140 
141  const std::vector<index_t> & conJacRows() const { return m_conJacRows; }
142 
143  const std::vector<index_t> & conJacCols() const { return m_conJacCols; }
144 
145 public:
146 
147  // void solve ();
148 
149  // std::ostream &print(std::ostream &os) const
150  // {
151  // os << "Design variables: " << m_numDesignVars
152  // << "\nNumber of constraints: " << m_numConstraints
153  // //<< "\ndesign lower:" << m_desLowerBounds.transpose()
154  // //<< "\ndesign upper:" << m_desUpperBounds.transpose()
155  // //<< "\nconstr lower:" << m_conLowerBounds.transpose()
156  // //<< "\nconstr upper:" << m_conUpperBounds.transpose()
157  // << "\nNumber of active sensitivities: " << m_numConJacNonZero
158  // << "\nSparsity of sensitivities: " <<std::fixed<<std::setprecision(2)
159  // << (100.0*m_numConJacNonZero)/(m_numDesignVars*m_numConstraints) << " %"
160  // //<< "\nm_conJacRows:" << m_conJacRows.transpose()
161  // //<< "\nm_conJacCols:" << m_conJacCols.transpose()
162  // //<< "\nm_curDesign:" << m_curDesign.transpose()
163  // ;
164  // return os;
165  // }
166 
167 
168 protected:
169 
172 
175 
178 
181 
184 
187 
190 
192  std::vector<index_t> m_conJacRows;
193 
195  std::vector<index_t> m_conJacCols;
196 
199 
202 };
203 
204 
205 } // end namespace gismo
206 
207 // note: must be statically compiled in header-only mode
208 #ifndef GISMO_BUILD_LIB
209 #include GISMO_HPP_HEADER(gsOptProblem.hpp)
210 #endif
virtual void gradObj_into(const gsAsConstVector< T > &u, gsAsVector< T > &result) const
Returns the gradient of the objective function at design value u By default it uses finite difference...
Definition: gsOptProblem.h:45
Class defining an optimization problem.
Definition: gsOptProblem.h:24
int m_numConstraints
Number of constraints.
Definition: gsOptProblem.h:174
virtual void computeJacStructure()
Computes the sparsity pattern of the constraint Jacobian matrix.
Definition: gsOptProblem.h:92
#define GISMO_NO_IMPLEMENTATION
Definition: gsDebug.h:129
gsMatrix< T > m_lambda
Lagrange multipliers (set in the finalize_solution method)
Definition: gsOptProblem.h:201
gsVector< T > m_conUpperBounds
Upper bounds for the constraints.
Definition: gsOptProblem.h:189
#define index_t
Definition: gsConfig.h:32
virtual void jacobCon_into(const gsAsConstVector< T > &, gsAsVector< T > &) const
Returns Jacobian of the constraints at design value u. Format of result is sparse, complying to m_conJacRows and m_conJacCols.
Definition: gsOptProblem.h:80
gsVector< T > m_conLowerBounds
Lower bounds for the constraints.
Definition: gsOptProblem.h:186
gsVector< T > m_desUpperBounds
Upper bounds for the design variables.
Definition: gsOptProblem.h:183
Creates a mapped object or data pointer to a vector without copying data.
Definition: gsLinearAlgebra.h:129
int m_numDesignVars
Number of design variables.
Definition: gsOptProblem.h:171
int numDesignVars() const
Callback function is executed after every iteration. Returning false causes premature termination of ...
Definition: gsOptProblem.h:119
std::vector< index_t > m_conJacRows
Constraint Jacobian non-zero entries rows.
Definition: gsOptProblem.h:192
virtual T evalObj(const gsAsConstVector< T > &) const
Returns the gradient value of the objective function at design value u.
Definition: gsOptProblem.h:39
Creates a mapped object or data pointer to a const vector without copying data.
Definition: gsLinearAlgebra.h:130
This is the main header file that collects wrappers of Eigen for linear algebra.
virtual void evalCon_into(const gsAsConstVector< T > &, gsAsVector< T > &) const
Returns values of the constraints at design value u.
Definition: gsOptProblem.h:74
gsVector< T > m_desLowerBounds
Lower bounds for the design variables.
Definition: gsOptProblem.h:180
virtual void hessLagr_into(const gsAsConstVector< T > &, gsAsVector< T > &) const
Returns Hessian Lagrangian of the constraints at design value.
Definition: gsOptProblem.h:84
int m_numConJacNonZero
Number of nonzero entries in the Constraint Jacobian.
Definition: gsOptProblem.h:177
gsMatrix< T > m_curDesign
Current design variables (and starting point )
Definition: gsOptProblem.h:198
std::vector< index_t > m_conJacCols
Constraint Jacobian non-zero entries columns.
Definition: gsOptProblem.h:195