chiark / gitweb /
put source code into src subdirectory
[nlopt.git] / src / algs / stogo / tools.h
1
2 //   Various datastructures and functions used by the global optimizer
3
4 #ifndef TOOLS_H
5 #define TOOLS_H
6
7 #include <float.h>
8 #include <iostream>
9
10 #include <algorithm>
11 #include <iterator>
12 #include <list>
13
14 #include "linalg.h"
15
16 #ifndef FALSE
17 const int FALSE=(1==0); // boolean FALSE
18 #endif
19 #ifndef FALSE
20 const int TRUE=(1==1); // boolean TRUE
21 #endif
22
23 typedef const class Trial CTrial;
24 typedef CTrial& RCTrial;
25 typedef CTrial* PCTrial;
26
27 class Trial{
28 public:
29   RVector xvals;
30   double objval;
31
32   Trial();
33   Trial(int);
34   Trial(RCTrial); // Copy constructor
35   RCTrial operator=(RCTrial) ; // assignment operator
36   friend ostream & operator << (ostream &, RCTrial) ;
37 };
38
39 class TrialGT : public unary_function<Trial, bool>
40 // Predicate for Trial (needed for remove_if)
41 {
42 public:
43   explicit TrialGT(double val) : _val(val) {}
44   bool operator()(Trial& foo) { 
45     return foo.objval > _val;
46   }
47 private:
48   double _val;
49 };
50
51 typedef class VBox& RVBox;
52 typedef const class VBox CVBox;
53 typedef CVBox* PCVBox;
54 typedef CVBox& RCVBox;
55
56 class VBox{
57 public:
58   RVector lb, ub;
59
60   VBox();        // Construct a box
61   VBox(int);
62   VBox(RCVBox);  // Copy constructor
63   RCVBox operator=(RCVBox);      // assignment operator
64
65   int GetDim();                  // Returns the dimension of the box
66   double Width(int) ;            // Returns the width of the i-th interval
67   void Midpoint(RCRVector);      // Returns the midpoint
68
69   friend ostream & operator << (ostream &, const VBox &);
70 };
71
72 typedef class TBox& RTBox;
73 typedef const class TBox CTBox;
74 typedef CTBox* PCTBox;
75 typedef CTBox& RCTBox;
76
77 class TBox: public VBox {
78 public:
79   double minf;   // Smallest function value found so far
80   list<Trial> TList; // List of trials
81
82   TBox();        // Construct a box
83   TBox(int);
84   TBox(RCTBox);  // Copy constructor
85
86   RCTBox operator=(RCTBox);      // assignment operator
87
88   double GetMin();               // Returns 'minf'
89   bool EmptyBox();               // Returns TRUE if Box contains no trials
90   void AddTrial(RCTrial);        // Add a trial to the (back of) box
91   void RemoveTrial(Trial &);     // Remove a trial from the (back of) box
92   void GetLastTrial(Trial &);    // Return a trial from the back of the box
93
94   list<Trial>::const_iterator FirstTrial();
95   list<Trial>::const_iterator LastTrial();
96
97   void GetTrial(list<Trial>::const_iterator, Trial&);
98   void ClearBox();            
99   bool CloseToMin(RVector&, double*, double);
100
101   unsigned int NStationary();      // Returns the number of stationary points
102
103   void split(RTBox, RTBox);     // Split a box
104   void dispTrials();
105
106   bool InsideBox(RCRVector);
107   int  OutsideBox(RCRVector, RCTBox);
108   double ShortestSide(int*);    // Returns the shortest side
109   double LongestSide(int*);     // Returns the longest side
110   double ClosestSide(RCRVector x);
111   double FarthestSide(RCRVector x);
112   bool Intersection(RCRVector, RCRVector, RCRVector);
113   double LowerBound(double);
114
115   bool operator<(const TBox & x) const {return (minf>x.minf);}
116   friend ostream & operator << (ostream &, const TBox &);
117 };
118
119 #endif