dReal4
nlopt_optimizer.h
1 #pragma once
2 
3 #include <memory>
4 #include <ostream>
5 #include <unordered_map>
6 #include <vector>
7 
8 #pragma GCC diagnostic push
9 #pragma GCC diagnostic ignored "-Wold-style-cast"
10 #include <nlopt.hpp>
11 #pragma GCC diagnostic pop
12 
13 #include "dreal/solver/config.h"
15 #include "dreal/util/box.h"
16 #include "dreal/util/nnfizer.h"
17 
18 namespace dreal {
19 
20 /// Cached expression class.
22  public:
23  CachedExpression() = default;
24  CachedExpression(Expression e, const Box& box);
25  const Box& box() const;
26  Environment& mutable_environment();
27  const Environment& environment() const;
28  double Evaluate(const Environment& env) const;
29  const Expression& Differentiate(const Variable& x);
30 
31  private:
32  Expression expression_;
33  Environment environment_;
34  const Box* box_{nullptr};
35  std::unordered_map<Variable, Expression, hash_value<Variable>> gradient_;
36 
37  friend std::ostream& operator<<(std::ostream& os,
38  const CachedExpression& expression);
39 };
40 
41 std::ostream& operator<<(std::ostream& os, const CachedExpression& expression);
42 
43 /// Wrapper class for nlopt.
45  public:
46  /// Constructs an NloptOptimizer instance given @p algorithm and the
47  /// bound @p box.
48  ///
49  /// @see http://nlopt.readthedocs.io/en/latest/NLopt_Algorithms, for
50  /// possible values of NLopt Algorithms.
51  NloptOptimizer(nlopt::algorithm algorithm, Box bound, const Config& config);
52 
53  /// Deleted copy-constructor.
54  NloptOptimizer(const NloptOptimizer&) = delete;
55 
56  /// Deleted move-constructor.
57  NloptOptimizer(NloptOptimizer&&) = default;
58 
59  /// Deleted copy-assignment operator.
60  NloptOptimizer& operator=(const NloptOptimizer&) = delete;
61 
62  /// Deleted move-assignment operator.
63  NloptOptimizer& operator=(NloptOptimizer&&) = delete;
64 
65  /// Destructor.
66  ~NloptOptimizer() = default;
67 
68  /// Specifies the objective function.
69  void SetMinObjective(const Expression& objective);
70 
71  /// Specifies a constraint.
72  ///
73  /// @note @p formula should be one of the following kinds:
74  /// 1) A relational formula (i.e. x >= y)
75  /// 2) A negation of a relational formula (i.e. ¬(x > y))
76  /// 3) A conjunction of 1) or 2).
77  /// @throw std::runtime_error if the above condition does not meet.
78  void AddConstraint(const Formula& formula);
79 
80  /// Specifies a relational constraint.
81  ///
82  /// @pre @p formula is a relational constraint.
83  void AddRelationalConstraint(const Formula& formula);
84 
85  /// Specifies constraints.
86  void AddConstraints(const std::vector<Formula>& formulas);
87 
88  /// Runs optimization. Uses @p x as an initial value for the
89  /// optimization and updates it with a solution. @p opt_f will be
90  /// updated with the found optimal value.
91  nlopt::result Optimize(std::vector<double>* x, double* opt_f);
92 
93  /// Runs optimization.
94  ///
95  /// @note Constraint and objective functions possibly include
96  /// non-decision variables. If this is the case, @p env should be
97  /// provided so that we can have full information to evaluate those
98  /// functions.
99  nlopt::result Optimize(std::vector<double>* x, double* opt_f,
100  const Environment& env);
101 
102  private:
103  nlopt::opt opt_;
104  const Box box_;
105  const double delta_{0.0};
106  CachedExpression objective_;
107  std::vector<std::unique_ptr<CachedExpression>> constraints_;
108  const Nnfizer nnfizer_{};
109 };
110 } // namespace dreal
Wrapper class for nlopt.
Definition: nlopt_optimizer.h:44
Sum type of symbolic::Expression and symbolic::Formula.
Definition: api.cc:9
Represents a symbolic variable.
Definition: symbolic_variable.h:16
Cached expression class.
Definition: nlopt_optimizer.h:21
Represents a symbolic environment (mapping from a variable to a value).
Definition: symbolic_environment.h:52
Represents a n-dimensional interval vector.
Definition: box.h:17
Definition: config.h:12
A class implementing NNF (Negation Normal Form) conversion.
Definition: nnfizer.h:12
This is the header file that we consolidate Drake&#39;s symbolic classes and expose them inside of dreal ...
Represents a symbolic form of an expression.
Definition: symbolic_expression.h:164
Represents a symbolic form of a first-order logic formula.
Definition: symbolic_formula.h:101