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