NLPInterfacePack: C++ Interfaces and Implementation for Non-Linear Programs  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
NLPInterfacePack_NLPBarrier.cpp
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Moocho: Multi-functional Object-Oriented arCHitecture for Optimization
5 // Copyright (2003) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 #include <math.h>
43 #include <iostream>
44 #include <limits>
45 
46 #include "NLPInterfacePack_NLPBarrier.hpp"
47 #include "AbstractLinAlgPack_VectorSpace.hpp"
48 #include "AbstractLinAlgPack_VectorAuxiliaryOps.hpp"
49 #include "AbstractLinAlgPack_VectorOut.hpp"
50 #include "Teuchos_Assert.hpp"
51 
52 namespace NLPInterfacePack {
53 
55  :
56  barrier_term_(0.0),
57  objective_term_(0.0),
58  nlp_(Teuchos::null)
59  {
60  }
61 
62 
64  Teuchos::RCP<NLP> original_nlp
65  )
66  {
68  !original_nlp.get(),
69  std::logic_error,
70  "null nlp passed to NLPBarrier decorator"
71  );
72 
73  nlp_ = Teuchos::rcp_dynamic_cast<NLPObjGrad>(original_nlp);
74 
76  !nlp_.get(),
77  std::logic_error,
78  "non NLPObjGrad NLP passed to NLPBarrier decorator"
79  );
80  }
81 
82 void NLPBarrier::mu(const value_type mu)
83  {
84  mu_ = mu;
85  }
86 
87 value_type NLPBarrier::barrier_term() const
88  {
89  return barrier_term_;
90  }
91 
92 value_type NLPBarrier::objective_term() const
93  {
94  return objective_term_;
95  }
96 
98  {
99  return grad_barrier_term_;
100  }
101 
103  {
104  return grad_objective_term_;
105  }
106 
107 
108 void NLPBarrier::calc_f(const Vector& x, bool newx) const
109  {
110  nlp_->calc_f(x, newx);
111  value_type* f_val = nlp_->get_f();
112 
113  objective_term_ = *f_val;
114  barrier_term_ = CalculateBarrierTerm(x);
115 
116  (*f_val) += barrier_term_;
117  }
118 
119 void NLPBarrier::calc_Gf(const Vector& x, bool newx) const
120  {
122 
123  nlp_->calc_Gf(x, newx);
124  grad_objective_term_ = nlp_->get_Gf()->clone();
125 
126  //std::cout << "grad_objective_term=\n";
127  //grad_objective_term_->output(std::cout);
128 
129  if (!grad_barrier_term_temp_.get())
130  { grad_barrier_term_temp_ = grad_objective_term_->space().create_member(); }
131 
132  if (!grad_barrier_term_.get())
133  { grad_barrier_term_ = grad_objective_term_->space().create_member(); }
134 
135  *grad_barrier_term_temp_ = 0.0;
136  *grad_barrier_term_ = 0.0;
137 
138  inv_of_difference(mu_, nlp_->xu(), x, grad_barrier_term_.get());
139  //std::cout << "mu*invXU=\n";
140  //grad_barrier_term_->output(std::cout);
141 
142  inv_of_difference(mu_, x, nlp_->xl(), grad_barrier_term_temp_.get());
143  //std::cout << "mu*invXL=\n";
144  //grad_barrier_term_temp_->output(std::cout);
145 
146  grad_barrier_term_->axpy(-1.0, *grad_barrier_term_temp_);
147 
148  nlp_->get_Gf()->axpy(1.0, *grad_barrier_term_);
149 
150  //std::cout << "grad_objective_term with barrier=\n";
151  //nlp_->get_Gf()->output(std::cout);
152  }
153 
155  const Vector& x,
156  bool newx,
157  const ZeroOrderInfo& zero_order_info
158  ) const
159  {
160  TEUCHOS_TEST_FOR_EXCEPT( !( false && !"This should never get called." ) );
161  }
162 
164  const Vector& x,
165  bool newx,
166  const ZeroOrderInfo& zero_order_info
167  ) const
168  {
169  TEUCHOS_TEST_FOR_EXCEPT( !( false && !"This should never get called." ) );
170  }
171 
173  const Vector& x,
174  bool newx,
175  const ZeroOrderInfo& zero_order_info_breve
176  ) const
177  {
178  TEUCHOS_TEST_FOR_EXCEPT( !( false && !"This should never get called." ) );
179  }
180 
182  const Vector& x,
183  bool newx,
184  const ZeroOrderInfo& zero_order_info_breve
185  ) const
186  {
187  TEUCHOS_TEST_FOR_EXCEPT( !( false && !"This should never get called." ) );
188  }
189 
191  const Vector& x,
192  bool newx,
193  const ObjGradInfo& obj_grad_info
194  ) const
195  {
196  TEUCHOS_TEST_FOR_EXCEPT( !( false && !"This should never get called." ) );
197  }
198 
199 
200 value_type NLPBarrier::CalculateBarrierTerm(const Vector& x) const
201  {
203  barrier_term_ = log_bound_barrier(x, xl(), xu());
204 // std::cerr << "NLPBarrier::CalculateBarrierTerm(x) : (1) barrier_term_ = " << barrier_term_ << std::endl;
205  barrier_term_ *= -mu_;
206 // std::cerr << "NLPBarrier::CalculateBarrierTerm(x) : (2) barrier_term_ = " << barrier_term_ << std::endl;
207  return barrier_term_;
208  }
209 
210 } // end namespace NLPInterfacePack
NLP interface class that adds gradient information for the objective function {abstract}.
value_type barrier_term() const
Get the barrier term. Must be called after calc_f().
Struct for gradient (objective), objective and constriants (pointers)
virtual const VectorSpace & space() const =0
void imp_calc_c(const Vector &x, bool newx, const ZeroOrderInfo &zero_order_info) const
void calc_f(const Vector &x, bool newx=true) const
Overloaded to include barrier term.
void inv_of_difference(const value_type alpha, const Vector &v0, const Vector &v1, VectorMutable *z)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
void InitializeFromNLP(Teuchos::RCP< NLP > original_nlp)
T * get() const
const Teuchos::RCP< Vector > grad_objective_term() const
Get the value of the gradient of the true objective term. Must be called after calc_Gf().
void imp_calc_c_breve(const Vector &x, bool newx, const ZeroOrderInfo &zero_order_info_breve) const
void mu(const value_type mu)
Set the barrier parameter.
virtual void axpy(value_type alpha, const Vector &x)
Struct for objective and constriants (pointer).
void imp_calc_h_breve(const Vector &x, bool newx, const ZeroOrderInfo &zero_order_info_breve) const
value_type objective_term() const
Get the true objective term value. Must be called after calc_f().
void imp_calc_Gf(const Vector &x, bool newx, const ObjGradInfo &obj_grad_info) const
value_type log_bound_barrier(const Vector &x, const Vector &xl, const Vector &xu)
const Teuchos::RCP< Vector > grad_barrier_term() const
Get the value of the gradient of the barrier term. Must be called after calc_Gf() ...
void imp_calc_f(const Vector &x, bool newx, const ZeroOrderInfo &zero_order_info) const
virtual vec_mut_ptr_t create_member() const =0
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)
void calc_Gf(const Vector &x, bool newx=true) const
Overloaded to include barrier term.