SHOGUN  4.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
NLOPTMinimizer.cpp
Go to the documentation of this file.
1  /*
2  * Copyright (c) The Shogun Machine Learning Toolbox
3  * Written (w) 2015 Wu Lin
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
19  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * The views and conclusions contained in the software and documentation are those
27  * of the authors and should not be interpreted as representing official policies,
28  * either expressed or implied, of the Shogun Development Team.
29  *
30  */
31 #include <shogun/lib/config.h>
32 #include <algorithm>
35 using namespace shogun;
36 
39 {
40  init();
41 }
42 
44 {
45 }
46 
49 {
50  init();
51 }
52 
53 void NLOPTMinimizer::init()
54 {
55 #ifdef HAVE_NLOPT
56  m_max_iterations=1000;
59  m_nlopt_algorithm=NLOPT_LD_LBFGS;
62 #endif
63 }
64 
66 {
67 #ifdef HAVE_NLOPT
69 
70  nlopt_opt opt=nlopt_create(m_nlopt_algorithm, m_target_variable.vlen);
71 
72  //add bound constraints
73  FirstOrderBoundConstraintsCostFunction* bound_constraints_fun
75  if(bound_constraints_fun)
76  {
77  SGVector<float64_t> bound=bound_constraints_fun->get_lower_bound();
78  if(bound.vlen==1)
79  {
80  nlopt_set_lower_bounds1(opt, bound[0]);
81  }
82  else
83  {
85  "The length of target variable (%d) and the length of lower bound (%d) do not match\n",
86  m_target_variable.vlen, bound.vlen);
87  nlopt_set_lower_bounds(opt, bound.vector);
88  }
89 
90  bound=bound_constraints_fun->get_upper_bound();
91  if(bound.vlen==1)
92  {
93  nlopt_set_upper_bounds1(opt, bound[0]);
94  }
95  else
96  {
98  "The length of target variable (%d) and the length of upper bound (%d) do not match\n",
99  m_target_variable.vlen, bound.vlen);
100  nlopt_set_upper_bounds(opt, bound.vector);
101  }
102 
103  }
104  // set maximum number of evaluations
105  nlopt_set_maxeval(opt, m_max_iterations);
106  // set absolute argument tolearance
107  nlopt_set_xtol_abs1(opt, m_variable_tolerance);
108  nlopt_set_ftol_abs(opt, m_function_tolerance);
109 
110  nlopt_set_min_objective(opt, NLOPTMinimizer::nlopt_function, this);
111 
112 #endif
113  // the minimum objective value, upon return
114  double cost=0.0;
115 
116 #ifdef HAVE_NLOPT
117  // optimize our function
118  nlopt_result error_code=nlopt_optimize(opt, m_target_variable.vector, &cost);
119  if(error_code!=1)
120  {
121  SG_SWARNING("Error(s) happened and NLopt failed during minimization (error code:%d)\n",
122  error_code);
123  }
124 
125  // clean up
126  nlopt_destroy(opt);
127 #endif
128 
129  return cost;
130 }
131 
132 #ifdef HAVE_NLOPT
133 void NLOPTMinimizer::set_nlopt_parameters(nlopt_algorithm algorithm,
134  float64_t max_iterations,
135  float64_t variable_tolerance,
136  float64_t function_tolerance)
137 {
138  m_nlopt_algorithm=algorithm;
139  m_max_iterations=max_iterations;
140  m_variable_tolerance=variable_tolerance;
141  m_function_tolerance=function_tolerance;
142 };
143 
144 double NLOPTMinimizer::nlopt_function(unsigned dim, const double* variable, double* gradient,
145  void* func_data)
146 {
147  NLOPTMinimizer* obj_prt=static_cast<NLOPTMinimizer *>(func_data);
148  REQUIRE(obj_prt, "The instance object passed to NLopt optimizer should not be NULL\n");
149 
150  //get the gradient wrt variable_new
151  SGVector<float64_t> grad=obj_prt->m_fun->get_gradient();
152 
153  REQUIRE(grad.vlen==(index_t)dim,
154  "The length of gradient (%d) and the length of variable (%d) do not match\n",
155  grad.vlen,dim);
156 
157  std::copy(grad.vector,grad.vector+dim,gradient);
158 
159  double cost=obj_prt->m_fun->get_cost();
160  return cost;
161 }
162 
164 {
165  REQUIRE(m_fun, "Cost function not set!\n");
167  REQUIRE(m_target_variable.vlen>0,"Target variable from cost function must not empty!\n");
168 }
169 #endif
virtual SGVector< float64_t > get_gradient()=0
SGVector< float64_t > m_target_variable
virtual SGVector< float64_t > get_upper_bound()=0
int32_t index_t
Definition: common.h:62
#define SG_SWARNING(...)
Definition: SGIO.h:178
FirstOrderCostFunction * m_fun
#define REQUIRE(x,...)
Definition: SGIO.h:206
virtual void init_minimization()
nlopt_algorithm m_nlopt_algorithm
The first order cost function base class with bound constrains.
virtual float64_t minimize()
index_t vlen
Definition: SGVector.h:494
double float64_t
Definition: common.h:50
The first order cost function base class.
virtual float64_t get_cost()=0
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
virtual SGVector< float64_t > get_lower_bound()=0
The class wraps the external NLOPT library.
virtual SGVector< float64_t > obtain_variable_reference()=0
virtual void set_nlopt_parameters(nlopt_algorithm algorithm=NLOPT_LD_LBFGS, float64_t max_iterations=1000, float64_t variable_tolerance=1e-6, float64_t function_tolerance=1e-6)
The first order minimizer base class.

SHOGUN Machine Learning Toolbox - Documentation