MOOCHO (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MoochoPack_active_set_change.cpp
Go to the documentation of this file.
1 #if 0
2 
3 // @HEADER
4 // ***********************************************************************
5 //
6 // Moocho: Multi-functional Object-Oriented arCHitecture for Optimization
7 // Copyright (2003) Sandia Corporation
8 //
9 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
10 // license for use of this work by or on behalf of the U.S. Government.
11 //
12 // Redistribution and use in source and binary forms, with or without
13 // modification, are permitted provided that the following conditions are
14 // met:
15 //
16 // 1. Redistributions of source code must retain the above copyright
17 // notice, this list of conditions and the following disclaimer.
18 //
19 // 2. Redistributions in binary form must reproduce the above copyright
20 // notice, this list of conditions and the following disclaimer in the
21 // documentation and/or other materials provided with the distribution.
22 //
23 // 3. Neither the name of the Corporation nor the names of the
24 // contributors may be used to endorse or promote products derived from
25 // this software without specific prior written permission.
26 //
27 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
28 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
31 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
32 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
33 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
34 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
35 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
36 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
37 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 //
39 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov)
40 //
41 // ***********************************************************************
42 // @HEADER
43 
44 #include <assert.h>
45 
46 #include <iomanip>
47 #include <ostream>
48 
50 #include "AbstractLinAlgPack/src/AbstractLinAlgPack_SpVectorClass.hpp"
51 
53  const SpVectorSlice& nu_k, const SpVectorSlice& nu_km1, Range1D indep
54  ,EJournalOutputLevel olevel, std::ostream* out
55  ,size_type* num_adds, size_type* num_drops
56  ,size_type* num_active_indep, size_type* num_adds_indep, size_type* num_drops_indep
57  )
58 {
59  using std::setw;
60  using std::endl;
61 
62  const int w = 12;
63 
64  *num_adds = *num_drops = *num_adds_indep = *num_drops_indep = 0;
65  *num_active_indep = nu_k(indep).nz();
66 
67  if( !nu_k.nz() && !nu_km1.nz() )
68  return;
69 
70  TEUCHOS_TEST_FOR_EXCEPT( !( nu_k.is_sorted() && nu_km1.is_sorted() ) );
71 
72  bool dump_change = (int)olevel >= (int)PRINT_ACTIVE_SET;
73 
74  if( dump_change ) {
75  *out
76  << "\n*** Changes in active set\n\n"
77  << setw(w) << "i"
78  << setw(w) << "uplo"
79  << setw(w) << "dep/indep"
80  << setw(w) << "change\n"
81  << setw(w) << "----------"
82  << setw(w) << "----------"
83  << setw(w) << "----------"
84  << setw(w) << "----------\n";
85  }
86 
87  SpVectorSlice::const_iterator
88  nu_k_itr = nu_k.begin(),
89  nu_k_end = nu_k.end(),
90  nu_km1_itr = nu_km1.begin(),
91  nu_km1_end = nu_km1.end();
92 
93  while( nu_k_itr != nu_k_end || nu_km1_itr != nu_km1_end ) {
94  if( nu_k_itr != nu_k_end && ( nu_km1_itr == nu_km1_end || ( nu_k_itr != nu_k_end
95  && nu_k_itr->indice()+nu_k.offset() < nu_km1_itr->indice()+nu_km1.offset() ) ) )
96  {
97  // *nu_k_itr was added to active set.
98  const size_type i = nu_k_itr->indice() + nu_k.offset();
99  const bool is_indep = indep.in_range(i);
100  if(is_indep)
101  (*num_adds_indep)++;
102  (*num_adds)++;
103  if(dump_change)
104  *out
105  << setw(w) << i
106  << setw(w) << ( nu_k_itr->value() >= 0.0 ? "upper" : "lower" )
107  << setw(w) << ( is_indep ? "indep" : "dep" )
108  << setw(w) << "added" << endl;
109  nu_k_itr++;
110  }
111  else if( nu_km1_itr != nu_km1_end && ( nu_k_itr == nu_k_end || ( nu_km1_itr != nu_km1_end
112  && nu_k_itr->indice()+nu_k.offset() > nu_km1_itr->indice()+nu_km1.offset() ) ) )
113  {
114  // *nu_km1_itr was removed from the active set.
115  const size_type i = nu_km1_itr->indice() + nu_km1.offset();
116  const bool is_indep = indep.in_range(i);
117  if(is_indep)
118  (*num_drops_indep)++;
119  (*num_drops)++;
120  if(dump_change)
121  *out
122  << setw(w) << i
123  << setw(w) << ( nu_km1_itr->value() >= 0.0 ? "upper" : "lower" )
124  << setw(w) << ( is_indep ? "indep" : "dep" )
125  << setw(w) << "dropped" << endl;
126  nu_km1_itr++;
127  }
128  else {
129  // same variable (but the bound may have changed)
130  const size_type i = nu_k_itr->indice() + nu_k.offset();
131  const bool is_indep = indep.in_range(i);
132  if( nu_k_itr->value() * nu_km1_itr->value() < 0.0 ) {
133  // Switched bounds.
134  if(is_indep) {
135  (*num_adds_indep)++;
136  (*num_drops_indep)++;
137  }
138  (*num_adds)++;
139  (*num_drops)++;
140  if(dump_change)
141  *out
142  << setw(w) << i
143  << setw(w) << ( nu_k_itr->value() >= 0.0 ? "upper" : "lower" )
144  << setw(w) << ( is_indep ? "indep" : "dep" )
145  << setw(w) << "switch bnd" << endl;
146  }
147  nu_k_itr++;
148  nu_km1_itr++;
149  }
150  }
151 
152  // Output summary
153  if( static_cast<int>(olevel) >= static_cast<int>(PRINT_ALGORITHM_STEPS) ) {
154  *out
155  << "\n*** Active set change summary\n"
156  << "nact_old = " << nu_km1.nz() << endl
157  << "nact_new = " << nu_k.nz() << endl
158  << "num_adds = " << *num_adds << endl
159  << "num_drops = " << *num_drops << endl
160  << "nact_indep_old = " << nu_km1(indep).nz() << endl
161  << "nact_indep_new = " << *num_active_indep << endl
162  << "num_indep_adds = " << *num_adds_indep << endl
163  << "num_indep_drops = " << *num_drops_indep << endl;
164  }
165 }
166 
167 #endif // 0
AbstractLinAlgPack::size_type size_type
const f_int f_dbl_prec const f_int f_int const f_int f_int const f_dbl_prec f_int f_int f_dbl_prec w[]
EJournalOutputLevel
enum for journal output.
std::ostream * out
void active_set_change(const SpVectorSlice &nu_k, const SpVectorSlice &nu_km1, Range1D indep, EJournalOutputLevel olevel, std::ostream *out, size_type *num_adds, size_type *num_drops, size_type *num_active_indep, size_type *num_adds_indep, size_type *num_drops_indep)
Calculate the change in the active set and output change if asked to.
SparseVectorSlice< SparseElement< index_type, value_type > > SpVectorSlice
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)