MOOCHO  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
NLPInterfacePack_NLPWBCounterExample.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 
44 #include "NLPInterfacePack_NLPWBCounterExample.hpp"
45 #include "DenseLinAlgPack_PermVecMat.hpp"
46 #include "DenseLinAlgPack_LinAlgOpPack.hpp"
47 #include "Teuchos_Assert.hpp"
48 
49 namespace NLPInterfacePack {
50 
51 // Constructors / initializers
52 
53 NLPWBCounterExample::NLPWBCounterExample(
54  value_type xinit[3], value_type a, value_type b, bool nlp_selects_basis, bool linear_obj
55  )
56  :is_initialized_(false),nlp_selects_basis_(nlp_selects_basis),basis_selection_was_given_(false)
57  ,linear_obj_(linear_obj),n_orig_(3),m_orig_(2),Gc_orig_nz_(4),a_(a),b_(b)
58 {
59 #ifdef TEUCHOS_DEBUG
60  const char msg_err_head[] = "NLPWBCounterExample::NLPWBCounterExample(...) : Error";
61  TEUCHOS_TEST_FOR_EXCEPTION( !(b >= 0), std::invalid_argument, msg_err_head<<"!" );
62  TEUCHOS_TEST_FOR_EXCEPTION( !(a + b*b != 0), std::invalid_argument, msg_err_head<<"!" );
63  TEUCHOS_TEST_FOR_EXCEPTION( !(xinit[1] >= 0), std::invalid_argument, msg_err_head<<"!" );
64  TEUCHOS_TEST_FOR_EXCEPTION( !(xinit[2] >= 0), std::invalid_argument, msg_err_head<<"!" );
65 #endif
66  // Set the inital guess and the variable bounds
67  xinit_orig_.resize(n_orig_); xl_orig_.resize(n_orig_); xu_orig_.resize(n_orig_);
68  const value_type inf = NLP::infinite_bound();
69  xinit_orig_(1) = xinit[0]; xl_orig_(1) = -inf; xu_orig_(1) = +inf;
70  xinit_orig_(2) = xinit[1]; xl_orig_(2) = 0.0; xu_orig_(2) = +inf;
71  xinit_orig_(3) = xinit[2]; xl_orig_(3) = 0.0; xu_orig_(3) = +inf;
72 }
73 
74 // Overridden public members from NLP
75 
76 void NLPWBCounterExample::initialize(bool test_setup)
77 {
78  // Nothing to initialize locally so just pass it on
80  is_initialized_ = true;
81 }
82 
84 {
85  return is_initialized_;
86 }
87 
89 {
90  return +1e+20; // Functions defined everywhere!
91 }
92 
93 // Overridden protected methods from NLPSerialPreprocess
94 
96 {
97  return !is_initialized_;
98 }
99 
101 {
102  return n_orig_;
103 }
104 
106 {
107  return m_orig_;
108 }
109 
111 {
112  return 0;
113 }
114 
115 const DVectorSlice NLPWBCounterExample::imp_xinit_orig() const
116 {
117  return xinit_orig_();
118 }
119 
121 {
122  return true;
123 }
124 
125 const DVectorSlice NLPWBCounterExample::imp_xl_orig() const
126 {
127  return xl_orig_();
128 }
129 
130 const DVectorSlice NLPWBCounterExample::imp_xu_orig() const
131 {
132  return xu_orig_();
133 }
134 
135 const DVectorSlice NLPWBCounterExample::imp_hl_orig() const
136 {
137  TEUCHOS_TEST_FOR_EXCEPT(true); // Should never be called
138  return xinit_orig_();
139 }
140 
141 const DVectorSlice NLPWBCounterExample::imp_hu_orig() const
142 {
143  TEUCHOS_TEST_FOR_EXCEPT(true); // Should never be called
144  return xinit_orig_();
145 }
146 
148  const DVectorSlice &x_full, bool newx, const ZeroOrderInfoSerial &zero_order_info ) const
149 {
150  DVectorSlice x = x_full(1,n_orig_);
151  *zero_order_info.f = ( linear_obj_ ? x(1) : 0.5*x(1)*x(1) );
152 }
153 
155  const DVectorSlice &x_full, bool newx, const ZeroOrderInfoSerial &zero_order_info ) const
156 {
157  DVectorSlice x = x_full(1,n_orig_);
158  DVector &c = *zero_order_info.c;
159  c(1) = x(1)*x(1) - x(2) + a_;
160  c(2) = x(1) - x(3) - b_;
161 }
162 
164  const DVectorSlice &x_full, bool newx, const ZeroOrderInfoSerial &zero_order_info ) const
165 {
166  TEUCHOS_TEST_FOR_EXCEPT(true); // Should never be called
167 }
168 
170  const DVectorSlice &x_full, bool newx, const ObjGradInfoSerial &obj_grad_info ) const
171 {
172  DVectorSlice x = x_full(1,n_orig_);
173  DVector &Gf = *obj_grad_info.Gf;
174  Gf(1) = (linear_obj_ ? 1.0 : x(1) ); Gf(2) = 0.0; Gf(3) = 0.0;
175 }
176 
178  IVector *var_perm_full
179  ,IVector *equ_perm_full
180  ,size_type *rank_full
181  ,size_type *rank
182  )
183 {
184 #ifdef TEUCHOS_DEBUG
185  TEUCHOS_TEST_FOR_EXCEPT( !( var_perm_full) );
186  TEUCHOS_TEST_FOR_EXCEPT( !( equ_perm_full ) );
187  TEUCHOS_TEST_FOR_EXCEPT( !( rank_full ) );
188  TEUCHOS_TEST_FOR_EXCEPT( !( rank ) );
189 #endif
190  if(basis_selection_was_given_) return false; // Already gave this basis selection
191  // Select x(2) ans x(3) as the basic variables (sorted!)
192  var_perm_full->resize(n_orig_);
193  (*var_perm_full)(1) = 2; // The basis variables
194  (*var_perm_full)(2) = 3; // ""
195  (*var_perm_full)(3) = 1; // The nonbasis variable
196  equ_perm_full->resize(m_orig_);
197  DenseLinAlgPack::identity_perm( equ_perm_full ); // Gc_orig is full rank
198  *rank_full = m_orig_;
199  *rank = m_orig_;
200  basis_selection_was_given_ = true;
201  return true;
202 }
203 
205  const DVectorSlice &x_orig
206  ,const DVectorSlice *lambda_orig
207  ,const DVectorSlice *lambdaI_orig
208  ,const DVectorSlice *nu_orig
209  ,bool is_optimal
210  )
211 {
212  // ToDo: Do something with the final soltuion?
213 }
214 
216 {
217  return nlp_selects_basis_;
218 }
219 
220 // Overridden protected methods from NLPSerialPreprocessExplJac
221 
223 {
224  return Gc_orig_nz_;
225 }
226 
228 {
229  return 0;
230 }
231 
233  const DVectorSlice& x_full, bool newx, const FirstOrderExplInfo& first_order_expl_info ) const
234 {
235  DVectorSlice x = x_full(1,n_orig_);
236  // Get references/pointers to data for Gc to be computed/updated.
237  index_type &Gc_nz = *first_order_expl_info.Gc_nz;
238  value_type *Gc_v = &(*first_order_expl_info.Gc_val)[0];
239  index_type *Gc_i = ( first_order_expl_info.Gc_ivect ? &(*first_order_expl_info.Gc_ivect)[0] : NULL );
240  index_type *Gc_j = ( first_order_expl_info.Gc_jvect ? &(*first_order_expl_info.Gc_jvect)[0] : NULL );
241  // Set up the nonzero structure of Gc_orig (sorted by constraint and then by variable)
242  if( Gc_i ) {
243  Gc_j[0] = 1; Gc_i[0] = 1; // d(c(1))/d(x(1))
244  Gc_j[1] = 1; Gc_i[1] = 2; // d(c(1))/d(x(2))
245  Gc_j[2] = 2; Gc_i[2] = 1; // d(c(2))/d(x(1))
246  Gc_j[3] = 2; Gc_i[3] = 3; // d(c(2))/d(x(3))
247  }
248  // Fill in the nonzero values of Gc_orig (must have the same order as structure!)
249  Gc_v[0] = 2*x(1); // d(c(1))/d(x(1))
250  Gc_v[1] = -1.0; // d(c(1))/d(x(2))
251  Gc_v[2] = +1.0; // d(c(2))/d(x(1))
252  Gc_v[3] = -1.0; // d(c(2))/d(x(3))
253  // Set the actual number of nonzeros
254  Gc_nz = Gc_orig_nz_;
255 }
256 
258  const DVectorSlice& x_full, bool newx, const FirstOrderExplInfo& first_order_expl_info ) const
259 {
260  TEUCHOS_TEST_FOR_EXCEPT(true); // Should never be called
261 }
262 
263 } // end namespace NLPInterfacePack
bool imp_get_next_basis(IVector *var_perm_full, IVector *equ_perm_full, size_type *rank_full, size_type *rank)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
void imp_calc_c_orig(const DVectorSlice &x_full, bool newx, const ZeroOrderInfoSerial &zero_order_info) const
void imp_calc_Gf_orig(const DVectorSlice &x_full, bool newx, const ObjGradInfoSerial &obj_grad_info) const
void imp_calc_Gh_orig(const DVectorSlice &x_full, bool newx, const FirstOrderExplInfo &first_order_expl_info) const
size_t size_type
void imp_calc_f_orig(const DVectorSlice &x_full, bool newx, const ZeroOrderInfoSerial &zero_order_info) const
void imp_calc_Gc_orig(const DVectorSlice &x_full, bool newx, const FirstOrderExplInfo &first_order_expl_info) const
void imp_report_orig_final_solution(const DVectorSlice &x_orig, const DVectorSlice *lambda_orig, const DVectorSlice *lambdaI_orig, const DVectorSlice *nu_orig, bool is_optimal)
void imp_calc_h_orig(const DVectorSlice &x_full, bool newx, const ZeroOrderInfoSerial &zero_order_info) const
static value_type infinite_bound()
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)
virtual VectorMutable & c()