NeoPZ
TPZStochasticSearch.h
Go to the documentation of this file.
1 /*
2  * File: TPZStochasticSearch.h
3  * Author: quinelato
4  *
5  * Created on 12 de Dezembro de 2017, 15:15
6  */
7 
8 #ifndef TPZSTOCHASTICSEARCH_H
9 #define TPZSTOCHASTICSEARCH_H
10 
11 #include <functional>
12 #include "pzvec.h"
13 #include "tpzautopointer.h"
14 #include "TPZConstrainedRandom.h"
15 #include <vector>
16 
17 template <typename TVar>
19 public:
20  TPZStochasticSearch(const uint64_t n_vars) {
21  fdistributions.resize(n_vars);
22  }
23 
25  }
26 
27  uint64_t NVars() const {
28  return fdistributions.size();
29  }
30 
31  void SetDistribution(const uint64_t var, TPZAutoPointer<TPZConstrainedRandom<TVar>> distribution);
32 
34  return fdistributions[index];
35  }
36 
37  std::vector<TVar> DoSearch(std::function<REAL(std::vector<TVar>)> objective_function, const uint64_t n_max_iterations, REAL min_relative_error);
38 
40 
41  }
42 private:
44 };
45 
46 template <typename TVar>
48 #ifdef PZDEBUG
49  if (var >= fdistributions.size()){
50  DebugStop();
51  }
52 #endif
53  fdistributions[var] = distribution;
54 }
55 
56 template <typename TVar>
57 std::vector<TVar> TPZStochasticSearch<TVar>::DoSearch(std::function<REAL(std::vector<TVar>)> objective_function, const uint64_t n_max_iterations, REAL min_relative_error) {
58  const uint64_t nvars = fdistributions.size();
59  std::vector<TVar> guess(nvars);
60  for (unsigned int i = 0; i < nvars; ++i) {
61 #ifdef PZDEBUG
62  if (!fdistributions[i].operator ->()){
63  DebugStop();
64  }
65 #endif
66  guess[i] = fdistributions[i]->next();
67  }
68 
69  uint64_t n_iterations = 0;
70  std::vector<TVar> best_guess = guess;
71  REAL best_score = objective_function(best_guess);
72  REAL relative_error = std::numeric_limits<REAL>::max();
73  while (n_iterations < n_max_iterations && relative_error > min_relative_error) {
74  for (unsigned int i = 0; i < nvars; ++i) {
75  guess[i] = fdistributions[i]->next();
76  }
77  REAL score = objective_function(guess);
78  if (score < best_score){
79  relative_error = (best_score-score)/best_score;
80  best_score = score;
81  best_guess = guess;
82  }
83  ++n_iterations;
84  }
85  return best_guess;
86 }
87 
88 
89 #endif /* TPZSTOCHASTICSEARCH_H */
90 
TPZStochasticSearch(const TPZStochasticSearch &orig)
virtual void resize(const int64_t newsize)
Definition: pzvec.h:213
Templated vector implementation.
This class implements a simple vector storage scheme for a templated class T. Utility.
Definition: pzgeopoint.h:19
uint64_t NVars() const
int64_t size() const
Returns the number of elements of the vector.
Definition: pzvec.h:196
TPZStochasticSearch(const uint64_t n_vars)
void SetDistribution(const uint64_t var, TPZAutoPointer< TPZConstrainedRandom< TVar >> distribution)
#define DebugStop()
Returns a message to user put a breakpoint in.
Definition: pzerror.h:20
TPZAutoPointer< TPZConstrainedRandom< TVar > > GetDistribution(const int index) const
Contains declaration of the TPZAutoPointer class which has Increment and Decrement actions are mutexe...
std::vector< TVar > DoSearch(std::function< REAL(std::vector< TVar >)> objective_function, const uint64_t n_max_iterations, REAL min_relative_error)
TPZVec< TPZAutoPointer< TPZConstrainedRandom< TVar > > > fdistributions
This class implements a reference counter mechanism to administer a dynamically allocated object...