1 // @HEADER
2 // ************************************************************************
3 //
4 //               Rapid Optimization Library (ROL) Package
5 //                 Copyright (2014) 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 lead developers:
38 //              Drew Kouri   (dpkouri@sandia.gov) and
39 //              Denis Ridzal (dridzal@sandia.gov)
40 //
41 // ************************************************************************
42 // @HEADER
43 
44 #ifndef ROL_OBJSUM_H
45 #define ROL_OBJSUM_H
46 
47 #include "misfit_robj.hpp"
48 
49 template <class Real>
50 class Sum_Objective : public ROL::Objective<Real> {
51 private:
52   const ROL::Ptr<ROL::Objective<Real>> misfit_, penalty_, binary_;
53 
54   ROL::Ptr<ROL::Vector<Real>> xdual_;
55   bool initialized_;
56 
57   const ROL::Ptr<std::ostream> stream_;
58   const bool printToStream_;
59 
60   Real misCost_, penCost_, binCost_;
61 
62 public:
Sum_Objective(const ROL::Ptr<FEMdata<Real>> & fem_,const ROL::Ptr<ROL::Objective<Real>> & pen,const ROL::Ptr<ROL::Objective<Real>> & bin,ROL::ParameterList & list,ROL::Ptr<std::ostream> & stream=ROL::nullPtr,bool printToStream=false)63   Sum_Objective(const ROL::Ptr<FEMdata<Real>> &fem_,
64                 const ROL::Ptr<ROL::Objective<Real>> &pen,
65                 const ROL::Ptr<ROL::Objective<Real>> &bin,
66                 ROL::ParameterList &list,
67                 ROL::Ptr<std::ostream> &stream = ROL::nullPtr,
68                 bool printToStream = false)
69     : misfit_(ROL::makePtr<Misfit_Objective<Real>>(fem_,list)),
70       penalty_(pen), binary_(bin),
71       xdual_(ROL::nullPtr), initialized_(false),
72       stream_(stream), printToStream_(printToStream) {
73     misCost_ = list.sublist("Problem").get("State Cost",       1.0);
74     penCost_ = list.sublist("Problem").get("Control Cost",     1.0);
75     binCost_ = list.sublist("Problem").get("Integrality Cost", 1.0);
76   }
77 
update(const ROL::Vector<Real> & x,bool flag=true,int iter=-1)78   void update(const ROL::Vector<Real> &x, bool flag = true, int iter = -1) {
79     misfit_->update(x,flag,iter);
80     penalty_->update(x,flag,iter);
81     binary_->update(x,flag,iter);
82   }
83 
value(const ROL::Vector<Real> & x,Real & tol)84   Real value( const ROL::Vector<Real> &x, Real &tol ) {
85     Real misfit  = misfit_->value(x,tol);
86     Real penalty = penalty_->value(x,tol);
87     Real binary  = binary_->value(x,tol);
88     if (printToStream_) {
89       *stream_ << "Unscaled: "
90                << "Misfit Value = "  << misfit  << "  "
91                << "Penalty Value = " << penalty << "  "
92                << "Binary Value = "  << binary  << std::endl;
93       *stream_ << "Scaled:   "
94                << "Misfit Value = "  << misCost_*misfit  << "  "
95                << "Penalty Value = " << penCost_*penalty << "  "
96                << "Binary Value = "  << binCost_*binary  << std::endl;
97     }
98     return misCost_*misfit + penCost_*penalty + binCost_*binary;
99   }
100 
gradient(ROL::Vector<Real> & g,const ROL::Vector<Real> & x,Real & tol)101   void gradient( ROL::Vector<Real> &g, const ROL::Vector<Real> &x, Real &tol ) {
102     if (!initialized_) {
103       xdual_ = g.clone();
104       initialized_ = true;
105     }
106     g.zero();
107     misfit_->gradient(g,x,tol);
108     g.scale(misCost_);
109     penalty_->gradient(*xdual_,x,tol);
110     g.axpy(penCost_,*xdual_);
111     binary_->gradient(*xdual_,x,tol);
112     g.axpy(binCost_,*xdual_);
113   }
114 
hessVec(ROL::Vector<Real> & hv,const ROL::Vector<Real> & v,const ROL::Vector<Real> & x,Real & tol)115   void hessVec( ROL::Vector<Real> &hv, const ROL::Vector<Real> &v, const ROL::Vector<Real> &x, Real &tol ) {
116     if (!initialized_) {
117       xdual_ = hv.clone();
118       initialized_ = true;
119     }
120     hv.zero();
121     misfit_->hessVec(hv,v,x,tol);
122     hv.scale(misCost_);
123     penalty_->hessVec(*xdual_,v,x,tol);
124     hv.axpy(penCost_,*xdual_);
125     binary_->hessVec(*xdual_,v,x,tol);
126     hv.axpy(binCost_,*xdual_);
127   }
128 
129 }; // class Sum_Objective
130 
131 #endif
132