ROL
ROL_UserInputGenerator.hpp
Go to the documentation of this file.
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_USERINPUTGENERATOR_HPP
45 #define ROL_USERINPUTGENERATOR_HPP
46 
47 #include "ROL_SampleGenerator.hpp"
48 #include "ROL_BatchManager.hpp"
49 #include <fstream>
50 #include <iostream>
51 #include <string>
52 
53 namespace ROL {
54 
55 template<class Real>
56 class UserInputGenerator : public SampleGenerator<Real> {
57 private:
58  int nSamp_;
59 
60  void sample(const std::string &file_pt,
61  const std::string &file_wt,
62  const int n,
63  const int dim,
64  const ROL::Ptr<BatchManager<Real> > &bman) {
65  nSamp_ = n;
66  // Read in full point data and weight data
67  std::fstream input_pt;
68  input_pt.open(file_pt.c_str(),std::ios::in);
69  std::fstream input_wt;
70  input_wt.open(file_wt.c_str(),std::ios::in);
71  if ( !input_pt.is_open() || !input_wt.is_open() ) {
72  if ( !input_pt.is_open() ) {
73  if ( bman->batchID() == 0 ) {
74  std::cout << "CANNOT OPEN " << file_pt.c_str() << "\n";
75  }
76  }
77  if ( !input_wt.is_open() ) {
78  if ( bman->batchID() == 0 ) {
79  std::cout << "CANNOT OPEN " << file_wt.c_str() << "\n";
80  }
81  }
82  }
83  else {
84  std::vector<std::vector<Real> > pt(n);
85  std::vector<Real> wt(n,0.0);
86  std::vector<Real> point(dim,0.0);;
87  for (int i = 0; i < n; i++) {
88  for (int j = 0; j < dim; j++) {
89  input_pt >> point[j];
90  }
91  pt[i] = point;
92  input_wt >> wt[i];
93  }
94  // Get process rankd and number of processes
95  int rank = bman->batchID();
96  int nProc = bman->numBatches();
97  // Separate samples across processes
98  int frac = n/nProc;
99  int rem = n%nProc;
100  int N = frac;
101  if ( rank < rem ) {
102  N++;
103  }
104  std::vector<std::vector<Real> > my_pt(N);
105  std::vector<Real> my_wt(N,0.0);
106  int index = 0;
107  for (int i = 0; i < N; i++) {
108  index = i*nProc + rank;
109  my_pt[i] = pt[index];
110  my_wt[i] = wt[index];
111  }
114  }
115  input_pt.close();
116  input_wt.close();
117  }
118 
119 public:
120  UserInputGenerator(ROL::ParameterList &parlist,
121  const ROL::Ptr<BatchManager<Real> > &bman)
122  : SampleGenerator<Real>(bman) {
123  ROL::ParameterList &list
124  = parlist.sublist("SOL").sublist("Sample Generator").sublist("User Input");
125  if ( list.isParameter("Points File") &&
126  list.isParameter("Weights File") &&
127  list.isParameter("Number of Samples") &&
128  list.isParameter("Dimension") ) {
129  std::string file_pt = list.get("Points File Name","points.txt");
130  std::string file_wt = list.get("Weights File Name","weights.txt");
131  int n = list.get("Number of Samples",100);
132  int dim = list.get("Dimension",4);
133  sample(file_pt,file_wt,n,dim,bman);
134  }
135  else {
136  ROL_TEST_FOR_EXCEPTION(true,std::invalid_argument,
137  ">>> (ROL::UserInputGenerator): ParameterList does not contain sufficient information.");
138  }
139  }
140 
141  UserInputGenerator(const std::string file_pt,
142  const std::string file_wt,
143  const int n,
144  const int dim,
145  const ROL::Ptr<BatchManager<Real> > &bman)
146  : SampleGenerator<Real>(bman) {
147  sample(file_pt,file_wt,n,dim,bman);
148  }
149 
150  void refine(void) {}
151 
152  int numGlobalSamples(void) const {
153  return nSamp_;
154  }
155 };
156 
157 }
158 
159 #endif
void sample(const std::string &file_pt, const std::string &file_wt, const int n, const int dim, const ROL::Ptr< BatchManager< Real > > &bman)
UserInputGenerator(ROL::ParameterList &parlist, const ROL::Ptr< BatchManager< Real > > &bman)
UserInputGenerator(const std::string file_pt, const std::string file_wt, const int n, const int dim, const ROL::Ptr< BatchManager< Real > > &bman)
void setPoints(std::vector< std::vector< Real > > &p)
constexpr auto dim
void setWeights(std::vector< Real > &w)