Skip to content

Commit

Permalink
Add argument to specify data path
Browse files Browse the repository at this point in the history
  • Loading branch information
buensons committed Dec 12, 2020
1 parent 1a8107a commit 51c5df4
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 24 deletions.
61 changes: 37 additions & 24 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@

#include "include/TimeSeriesPredictor.cuh"

auto readDataToMemory() -> std::vector<float>;
auto readDataToMemory(std::string path) -> std::vector<float>;
auto normalizeData(std::vector<float> &data) -> void;
auto usage(char * arg) -> void;

int main(int argc, char ** argv) {
int nodes, populationSize, windowSize;
std::string dataPath;

if(argc != 4) {
if(argc != 5) {
usage(argv[0]);
return 1;
}
Expand All @@ -26,13 +27,14 @@ int main(int argc, char ** argv) {
nodes = std::stoi(argv[1]);
populationSize = std::stoi(argv[2]);
windowSize = std::stoi(argv[3]);
dataPath = std::string(argv[4]);
} catch(std::exception const & e) {
usage(argv[0]);
return 1;
}

std::cout << "Reading input data...\n";
auto timeSeries = readDataToMemory();
std::cout << "Reading input data from " << dataPath << std::endl;
auto timeSeries = readDataToMemory(dataPath);
normalizeData(timeSeries);

TimeSeriesPredictor predictor(timeSeries, nodes, populationSize, windowSize);
Expand All @@ -45,7 +47,7 @@ int main(int argc, char ** argv) {
}

auto usage(char * arg) -> void {
std::cerr << "Usage: " << arg << " <num_of_nodes> <population_size> <window_size>" << std::endl;
std::cerr << "Usage: " << arg << " <num_of_nodes> <population_size> <window_size> <path_to_data>" << std::endl;
}

auto normalizeData(std::vector<float> &data) -> void {
Expand All @@ -58,31 +60,42 @@ auto normalizeData(std::vector<float> &data) -> void {
}
}

auto readDataToMemory() -> std::vector<float> {
std::string dataFolder = "./data/";
auto extractDataFromCsv(std::string path, std::vector<float> &dataVector) {
std::ifstream infile(path);
std::string line;

while (std::getline(infile, line)) {
std::istringstream iss(line);

while(iss.good()){
std::string substr;
getline(iss, substr, ',');
dataVector.push_back(std::stof(substr));
}
}
}

auto readDataToMemory(std::string path) -> std::vector<float> {
std::vector<float> dataVector;

size_t pos = path.rfind('.');

namespace fs = std::filesystem;
if (pos != std::string::npos) {
std::string ext = path.substr(pos+1);

for(auto itEntry = fs::recursive_directory_iterator(fs::current_path());
itEntry != fs::recursive_directory_iterator();
++itEntry) {
if (ext == "csv") {
extractDataFromCsv(path, dataVector);
return dataVector;
}
}

const auto extension = itEntry->path().extension();
if(extension == ".csv") {
std::ifstream infile(itEntry->path());
std::string line;
for(auto& p: std::filesystem::directory_iterator(path)) {
const auto extension = p.path().extension();

while (std::getline(infile, line)) {
std::istringstream iss(line);

while(iss.good()){
std::string substr;
getline(iss, substr, ',');
dataVector.push_back(std::stof(substr));
}
}
if(extension == ".csv") {
extractDataFromCsv(p.path().string(), dataVector);
}
}

return dataVector;
}
3 changes: 3 additions & 0 deletions src/FitnessFunctionKernel.cu
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ __global__ void calculate_fitness(

float y = 1.0 / (1.0 + expf(-5 * x));
float prediction_error = abs(y - data[window_size * node_number + i + j]);

// TODO: experiment with percentage error
// TODO: experiment with max percentage error
cumulative_error += prediction_error;
}
i += node_number;
Expand Down
1 change: 1 addition & 0 deletions src/TimeSeriesPredictor.cu
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ auto TimeSeriesPredictor::printPopulation() -> void {

}
}

auto TimeSeriesPredictor::maxFitness(std::vector<Chromosome> population) -> Chromosome {
float maxFitness = -1.0;
Chromosome max;
Expand Down

0 comments on commit 51c5df4

Please sign in to comment.