MachineIntelligenceCore:ReinforcementLearning
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator
MazeMatrixImporter.cpp
Go to the documentation of this file.
1 
24 
25 #include <fstream>
26 
27 #include <string>
28 #include <iostream>
29 #include <sstream>
30 
31 namespace mic {
32 namespace importers {
33 
34 MazeMatrixImporter::MazeMatrixImporter(std::string node_name_) : Importer (node_name_),
35  data_filename("data_filename","mazes.csv")
36 {
37  // Register properties - so their values can be overridden (read from the configuration file).
38  registerProperty(data_filename);
39 }
40 
42  LOG(LSTATUS) << "Importing mazes from file: " << data_filename;
43 
44  std::ifstream data_file(data_filename);
45 
46  std::string line;
47 
48  // If managed to open the file properly.
49  if (data_file.is_open()) {
50 
51  // Read first line - header.
52  line.clear();
53  std::getline(data_file, line);
54 
55  // Try to read the maze dimensions.
56  line.clear();
57  std::getline(data_file, line);
58 
59  std::stringstream ss(line);
60 
61  ss >> maze_width;
62  ss.ignore();// ignore coma (,)
63  ss >> maze_height;
64 
65 
66  LOG(LDEBUG) << "maze_width=" << maze_width << " maze_height=" << maze_height ;
67 
68  // Read third line - second header.
69  line.clear();
70  std::getline(data_file, line);
71  LOG(LDEBUG) << "2nd header : " << line ;
72 
73  // Read mazes.
74  while (std::getline(data_file, line)) {
75  LOG(LDEBUG) << line << '\n';
76 
77  // Create new matrix of MNIST image size.
78  mic::types::MatrixXiPtr mat (new mic::types::MatrixXi(maze_height, maze_width));
79 
80  int value;
81  size_t i = 0;
82  std::stringstream ss(line);
83  // Parse line and get consecutire values.
84  while (ss >> value) {
85  // Compute matrix index.
86  unsigned col = i / maze_width;
87  unsigned row = i - ( col * maze_width);
88  // Set value
89  (*mat)(col, row) = value;
90  LOG(LDEBUG) << " " << i <<"("<< col<<","<< row <<")|" << value;
91  // Increment index.
92  i++;
93 
94  // Skip comas.
95  if (ss.peek() == ',')
96  ss.ignore();
97  }//: while i in line
98 
99  LOG(LDEBUG) << *mat;
100  // Add matrix do vector.
101  sample_data.push_back(mat);
102  }//: while line
103 
104 
105  LOG(LINFO) << "Imported " << sample_data.size() << " mazes of size (h x w) = " << maze_height << " x " << maze_width;
106 
107  // Fill the labels and indices tables.
108  for (size_t i=0; i < sample_data.size(); i++ ){
109  sample_labels.push_back( std::make_shared <size_t> (i) );
110  sample_indices.push_back(i);
111  }
112 
113  LOG(LINFO) << "Data import finished";
114  data_file.close();
115 
116  return true;
117  } else {
118  LOG(LFATAL) << "Oops! Couldn't find file: " << data_filename;
119  return false;
120  }//: else
121 }
122 
123 } /* namespace importers */
124 } /* namespace mic */
mic::configuration::Property< std::string > data_filename
size_t maze_height
Height of a maze.
MazeMatrixImporter(std::string node_name_="maze_importer")