atomicrex  0.1
An advanced atomistic model building tool
NloptMinimizer.h
1 //
3 // Copyright (C) 2017, Alexander Stukowski and Paul Erhart
4 //
5 // This file is part of atomicrex.
6 //
7 // Atomicrex is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 3 of the License, or
10 // (at your option) any later version.
11 //
12 // Atomicrex is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
19 //
21 
22 #pragma once
23 
24 #include "../Atomicrex.h"
25 #include "Minimizer.h"
26 
27 #include <nlopt.hpp>
28 
29 namespace atomicrex {
30 
31 class NloptMinimizer : public Minimizer
32 {
33 public:
34 
37 
51  virtual void prepare(
52  std::vector<double>&& x0,
53  const std::function<double(const std::vector<double>&)>& func,
54  const std::function<double(const std::vector<double>&, std::vector<double>&)>& gradient = std::function<double(const std::vector<double>&, std::vector<double>&)>()) override;
55 
58  virtual void setConstraints(
59  std::vector<BoundConstraints>&& constraintTypes,
60  std::vector<double>&& lowerBounds,
61  std::vector<double>&& upperBounds) override;
62 
64  virtual MinimizerResult iterate() override;
65 
67  virtual void parse(XML::Element minimizerElement) override;
68 
70  double objective_function(const std::vector<double>&, std::vector<double>&);
71 
73  nlopt::algorithm get_algorithm(FPString);
74 
76  const nlopt::opt* get_optimizer() { return opt.get(); }
77  /* std::shared_ptr<nlopt::opt> get_optimizer() { return opt; } */
78 
79 private:
80 
82  std::vector<BoundConstraints> _constraintTypes;
83  std::vector<double> _lowerBounds;
84  std::vector<double> _upperBounds;
86  std::vector<double> x;
87  std::vector<double> g;
88 
90  std::vector<double> _bestParameters;
91  double _bestResidual;
92 
94  FPString _name;
95  nlopt::algorithm alg;
96  std::unique_ptr<nlopt::opt> opt;
97  nlopt::result result;
98 
100  std::shared_ptr<NloptMinimizer> _submin;
101 
103  int _count = 0;
105  unsigned _n;
106 
108  int nlopt_maxeval = -1;
109  double nlopt_stopval = -1;
110  double nlopt_maxtime = -1;
111  double nlopt_ftol_rel = -1;
112  double nlopt_ftol_abs = -1;
113  double nlopt_xtol_rel = -1;
114  unsigned long seed = 0;
115 };
116 
117 } // End of namespace
virtual void setConstraints(std::vector< BoundConstraints > &&constraintTypes, std::vector< double > &&lowerBounds, std::vector< double > &&upperBounds) override
Definition: NloptMinimizer.cpp:84
FitJob * job() const
Returns the job to which this object belongs.
Definition: Minimizer.h:68
NloptMinimizer(FitJob *job)
Constructor.
Definition: NloptMinimizer.h:36
virtual MinimizerResult iterate() override
Performs one minimization iteration.
Definition: NloptMinimizer.cpp:214
virtual void prepare(std::vector< double > &&x0, const std::function< double(const std::vector< double > &)> &func, const std::function< double(const std::vector< double > &, std::vector< double > &)> &gradient=std::function< double(const std::vector< double > &, std::vector< double > &)>()) override
Definition: NloptMinimizer.cpp:42
const nlopt::opt * get_optimizer()
Returns the optimizer object.
Definition: NloptMinimizer.h:76
This file collects the definition of classes that define various simple crystal structures.
Definition: Atomicrex.h:67
std::string FPString
The default string type used throughout the code:
Definition: Atomicrex.h:70
Definition: XMLUtilities.h:69
double objective_function(const std::vector< double > &, std::vector< double > &)
The Objective function that gets passed to Nlopt.
Definition: NloptMinimizer.cpp:176
Definition: FitJob.h:37
Abstract base class for minimization algorithms.
Definition: Minimizer.h:34
Definition: NloptMinimizer.h:31
nlopt::algorithm get_algorithm(FPString)
Get the Nlopt algorithm from a string.
Definition: NloptMinimizer.cpp:143
virtual void parse(XML::Element minimizerElement) override
Parses the minimizer&#39;s parameters from the XML file.
Definition: NloptMinimizer.cpp:104