EPEC solve
Solving Equilibrium Problems with Equilibrium Constraints (EPECs)
Utils.cpp
Go to the documentation of this file.
1 #include "utils.h"
2 #include <armadillo>
3 #include <boost/log/trivial.hpp>
4 #include <boost/program_options.hpp>
5 #include <fstream>
6 
7 using namespace std;
8 using namespace arma;
9 
10 arma::sp_mat Utils::resize_patch(const arma::sp_mat &Mat, const unsigned int nR,
11  const unsigned int nC) {
18  arma::sp_mat MMat(nR, nC);
19  MMat.zeros();
20  if (nR >= Mat.n_rows && nC >= Mat.n_cols) {
21  if (Mat.n_rows >= 1 && Mat.n_cols >= 1)
22  MMat.submat(0, 0, Mat.n_rows - 1, Mat.n_cols - 1) = Mat;
23  } else {
24  if (nR <= Mat.n_rows && nC <= Mat.n_cols)
25  MMat = Mat.submat(0, 0, nR, nC);
26  else
27  throw string(
28  "Error in resize() - the patch for arma::resize. Either both "
29  "dimension should be larger or both should be smaller!");
30  }
31  return MMat;
32 }
33 
34 // For arma::mat
35 arma::mat Utils::resize_patch(const arma::mat &Mat, const unsigned int nR,
36  const unsigned int nC) {
43  arma::mat MMat(nR, nC);
44  MMat.zeros();
45  if (nR >= Mat.n_rows && nC >= Mat.n_cols) {
46  if (Mat.n_rows >= 1 && Mat.n_cols >= 1)
47  MMat.submat(0, 0, Mat.n_rows - 1, Mat.n_cols - 1) = Mat;
48  } else {
49  if (nR <= Mat.n_rows && nC <= Mat.n_cols)
50  MMat = Mat.submat(0, 0, nR, nC);
51  else
52  throw string(
53  "Error in resize() - the patch for arma::resize. Either both "
54  "dimension should be larger or both should be smaller!");
55  }
56  return MMat;
57 }
58 
59 // For arma::vec
60 arma::vec Utils::resize_patch(const arma::vec &Mat, const unsigned int nR) {
67  arma::vec MMat(nR);
68  MMat.zeros();
69  if (nR > Mat.n_rows)
70  MMat.subvec(0, Mat.n_rows - 1) = Mat;
71  else
72  MMat = Mat.subvec(0, nR);
73  return MMat;
74 }
75 
76 void Utils::appendSave(const sp_mat &matrix,
77  const string out,
78  const string header,
79  bool erase
81  )
86 {
87  // Using C++ file operations to copy the data into the target given by @out
88  unsigned int nR{0}, nC{0}, nnz{0};
89 
90  ofstream outfile(out, erase ? ios::out : ios::app);
91 
92  nR = matrix.n_rows;
93  nC = matrix.n_cols;
94  nnz = matrix.n_nonzero;
95 
96  outfile << header << "\n";
97  outfile << nR << "\t" << nC << "\t" << nnz << "\n";
98  for (auto it = matrix.begin(); it != matrix.end(); ++it)
99  outfile << it.row() << "\t" << it.col() << "\t" << (*it)
100  << "\n"; // Write the required information of sp_mat
101  outfile << "\n";
102  outfile.close(); // and close it
103 }
104 
105 long int Utils::appendRead(
106  sp_mat &matrix,
107  const string in,
108  long int pos,
110  const string header
111  )
116 {
117  unsigned int nR, nC, nnz;
118 
119  ifstream infile(in, ios::in);
120  infile.seekg(pos);
121 
122  string header_checkwith;
123  infile >> header_checkwith;
124 
125  if (header != "" && header != header_checkwith)
126  throw string(
127  "Error in Utils::appendRead<sp_mat>. Wrong header. Expected: " +
128  header + " Found: " + header_checkwith);
129 
130  infile >> nR >> nC >> nnz;
131  if (nR == 0 || nC == 0)
132  matrix.set_size(nR, nC);
133  else {
134  arma::umat locations(2, nnz);
135  arma::vec values(nnz);
136 
137  unsigned int r, c;
138  double val;
139 
140  for (unsigned int i = 0; i < nnz; ++i) {
141  infile >> r >> c >> val;
142  locations(0, i) = r;
143  locations(1, i) = c;
144  values(i) = val;
145  }
146  matrix = arma::sp_mat(locations, values, nR, nC);
147  }
148 
149  pos = infile.tellg();
150  infile.close();
151 
152  return pos;
153 }
154 
155 void appendSave(const vector<double> v, const string out, const string header,
156  bool erase) {
160  ofstream outfile(out, erase ? ios::out : ios::app);
161  outfile << header << "\n" << v.size() << "\n";
162  for (const double x : v)
163  outfile << x << "\n";
164  outfile.close();
165 }
166 
167 long int appendRead(vector<double> &v, const string in, long int pos,
168  const string header) {
169  unsigned long int size;
170  ifstream infile(in, ios::in);
171  infile.seekg(pos);
177  string header_checkwith;
178  infile >> header_checkwith;
179 
180  if (header != "" && header != header_checkwith)
181  throw string(
182  "Error in Utils::appendRead<sp_mat>. Wrong header. Expected: " +
183  header + " Found: " + header_checkwith);
184 
185  infile >> size;
186 
187  v.resize(size);
188  for (unsigned int i = 0; i < size; ++i)
189  infile >> v[i];
190  pos = infile.tellg();
191  infile.close();
192  return pos;
193 }
194 
195 void Utils::appendSave(const vec &matrix,
196  const string out,
197  const string header,
198  bool erase
200 ) {
205  // Using C++ file operations to copy the data into the target given by @out
206  unsigned int nR{0};
207 
208  ofstream outfile(out, erase ? ios::out : ios::app);
209 
210  nR = matrix.n_rows;
211 
212  outfile << header << "\n";
213 
214  outfile << nR << "\n";
215  for (auto it = matrix.begin(); it != matrix.end(); ++it)
216  outfile << (*it) << "\n"; // Write the required information of sp_mat
217  outfile << "\n";
218  outfile.close(); // and close it
219 }
220 
221 long int Utils::appendRead(
222  vec &matrix,
223  const string in,
224  long int pos,
226  const string header
227 ) {
232  unsigned int nR;
233  string buffers;
234  string checkwith;
235  ifstream in_file(in, ios::in);
236  in_file.seekg(pos);
237 
238  in_file >> checkwith;
239  if (header != "" && checkwith != header)
240  throw string("Error in Utils::appendRead<vec>. Wrong header. Expected: " +
241  header + " Found: " + checkwith);
242  in_file >> nR;
243  matrix.zeros(nR);
244  for (unsigned int i = 0; i < nR; ++i) {
245  double val;
246  in_file >> val;
247  matrix.at(i) = val;
248  }
249 
250  pos = in_file.tellg();
251  in_file.close();
252 
253  return pos;
254 }
255 
256 void Utils::appendSave(const long int v, const string out, const string header,
257  bool erase)
261 {
262  ofstream outfile(out, erase ? ios::out : ios::app);
263  outfile << header << "\n";
264  outfile << v << "\n";
265  outfile.close();
266 }
267 
268 long int Utils::appendRead(long int &v, const string in, long int pos,
269  const string header) {
274  ifstream infile(in, ios::in);
275  infile.seekg(pos);
276 
277  string header_checkwith;
278  infile >> header_checkwith;
279 
280  if (header != "" && header != header_checkwith)
281  throw string(
282  "Error in Utils::appendRead<long int>. Wrong header. Expected: " +
283  header + " Found: " + header_checkwith);
284 
285  long int val;
286  infile >> val;
287  v = val;
288 
289  pos = infile.tellg();
290  infile.close();
291 
292  return pos;
293 }
294 
295 void Utils::appendSave(const unsigned int v, const string out,
296  const string header, bool erase)
300 {
301  ofstream outfile(out, erase ? ios::out : ios::app);
302  outfile << header << "\n";
303  outfile << v << "\n";
304  outfile.close();
305 }
306 
307 long int Utils::appendRead(unsigned int &v, const string in, long int pos,
308  const string header) {
309  ifstream infile(in, ios::in);
310  infile.seekg(pos);
311 
312  string header_checkwith;
313  infile >> header_checkwith;
314 
315  if (header != "" && header != header_checkwith)
316  throw string(
317  "Error in Utils::appendRead<unsigned int>. Wrong header. Expected: " +
318  header + " Found: " + header_checkwith);
319 
320  unsigned int val;
321  infile >> val;
322  v = val;
323 
324  pos = infile.tellg();
325  infile.close();
326 
327  return pos;
328 }
329 
330 void Utils::appendSave(const string v, const string out, bool erase)
334 {
335  ofstream outfile(out, erase ? ios::out : ios::app);
336  outfile << v << "\n";
337  outfile.close();
338 }
339 
340 long int Utils::appendRead(string &v, const string in, long int pos) {
345  ifstream infile(in, ios::in);
346  infile.seekg(pos);
347 
348  string val;
349  infile >> val;
350  v = val;
351 
352  pos = infile.tellg();
353  infile.close();
354 
355  return pos;
356 }
357 unsigned long int Utils::vec_to_num(std::vector<short int> binary) {
358  unsigned long int number = 0;
359  unsigned int posn = 1;
360  while (!binary.empty()) {
361  short int bit = (binary.back() + 1) / 2; // The least significant bit
362  number += (bit * posn);
363  posn *= 2; // Update place value
364  binary.pop_back(); // Remove that bit
365  }
366  return number;
367 }
368 
369 std::vector<short int> Utils::num_to_vec(unsigned long int number,
370  const unsigned int &nCompl) {
371  std::vector<short int> binary{};
372  for (unsigned int vv = 0; vv < nCompl; vv++) {
373  binary.push_back(number % 2);
374  number /= 2;
375  }
376  std::for_each(binary.begin(), binary.end(),
377  [](short int &vv) { vv = (vv == 0 ? -1 : 1); });
378  std::reverse(binary.begin(), binary.end());
379  return binary;
380 }
long int appendRead(vec &matrix, const std::string in, long int pos, const std::string header="")
unsigned long int vec_to_num(std::vector< short int > binary)
Definition: Utils.cpp:357
void appendSave(const vec &matrix, const std::string out, const std::string header="", bool erase=false)
Definition: games.h:702
void appendSave(const vector< double > v, const string out, const string header, bool erase)
Definition: Utils.cpp:155
arma::sp_mat resize_patch(const arma::sp_mat &Mat, const unsigned int nR, const unsigned int nC)
Definition: Utils.cpp:10
long int appendRead(vector< double > &v, const string in, long int pos, const string header)
Definition: Utils.cpp:167
std::vector< short int > num_to_vec(unsigned long int number, const unsigned int &nCompl)
Definition: Utils.cpp:369