Skip to content

Commit

Permalink
Optimized output file opening for training
Browse files Browse the repository at this point in the history
  • Loading branch information
AleCarminati committed May 12, 2022
1 parent c249808 commit 19f511f
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 71 deletions.
109 changes: 41 additions & 68 deletions controllers/fault_detection/fault_detection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,16 +348,21 @@ void CEPuckFaultDetection::Init(TConfigurationNode& t_node) {
int random_seed = CSimulator::GetInstance().GetRandomSeed();
bool newFile = false;
std::string filename = "";

filename = "./data/binary_training/" + m_behavior_str + "/" + m_behavior_str +
"_" + m_faultType + "_" + std::to_string(random_seed) + ".csv";
std::string testing_or_training = "testing";
std::string binary_or_numerical = "numerical";
std::string behav_fault_folder = m_behavior_str + "_" + m_faultType + "/";
if (m_training) {
testing_or_training = "training";
behav_fault_folder = "";
}
if (CConfiguration::BOOLEAN_OBSERVATIONS) {
filename = "./data/binary_testing/" + m_behavior_str + "/" +
m_behavior_str + "_" + m_faultType + "/" + m_behavior_str + "_" +
m_faultType + "_" + std::to_string(random_seed) + ".csv";
} else {
filename = "./data/numerical_testing/" + m_behavior_str + "/" +
m_behavior_str + "_" + m_faultType + "/" + m_behavior_str + "_" +
m_faultType + "_" + std::to_string(random_seed) + ".csv";
binary_or_numerical = "binary";
}
filename = "./data/" + binary_or_numerical + "_" + testing_or_training + "/" +
m_behavior_str + "/" + behav_fault_folder + m_behavior_str + "_" +
m_faultType + "_" + std::to_string(random_seed) + ".csv";

std::ifstream fileExistence(filename);
if (!fileExistence) {
Expand All @@ -373,9 +378,30 @@ void CEPuckFaultDetection::Init(TConfigurationNode& t_node) {

// If the file has just been created, write its header.
if (newFile) {
output_file
<< "control_step;observed_robot;number_votes;coalition_response;";
output_file << "real_faulty" << std::endl;
if (m_training) {
output_file << "id_experiment;control_step;observed_robot;";
if (CConfiguration::BOOLEAN_OBSERVATIONS) {
for (int i = N_OBSERVATIONS - 1; i >= 0; --i) {
for (int j = 1; j < 7; ++j) {
output_file << "F" + std::to_string(j) + "_t-" + std::to_string(i) +
";";
}
}
} else {
for (int i = N_OBSERVATIONS - 1; i >= 0; --i) {
output_file << "vel_left_wheel_t-" + std::to_string(i) + ";";
output_file << "vel_right_wheel_t-" + std::to_string(i) + ";";
output_file << "distance_traveled_t-" + std::to_string(i) + ";";
output_file << "min_neighbor_distance_t-" + std::to_string(i) + ";";
output_file << "avg_neighbor_distance_t-" + std::to_string(i) + ";";
}
}
output_file << "fault_probability" << std::endl;
} else {
output_file
<< "control_step;observed_robot;number_votes;coalition_response;";
output_file << "real_faulty" << std::endl;
}
}
}

Expand Down Expand Up @@ -1606,45 +1632,7 @@ unsetenv("PYTHONPATH");*/
}

void CEPuckFaultDetection::WriteToCSVTraining() {
int random_seed = CSimulator::GetInstance().GetRandomSeed();
bool newFile = false;

std::string filename = "";

if (CConfiguration::BOOLEAN_OBSERVATIONS) {
filename = "./data/binary_training/" + m_behavior_str + "/" +
m_behavior_str + "_" + m_faultType + "_" +
std::to_string(random_seed) + ".csv";
} else {
filename = "./data/numerical_training/" + m_behavior_str + "/" +
m_behavior_str + "_" + m_faultType + "_" +
std::to_string(random_seed) + ".csv";
}

std::ifstream fileExistence(filename);
if (!fileExistence) {
newFile = true;
}
fileExistence.close();

std::ofstream output(filename, std::ofstream::app);

if (!output.is_open()) {
THROW_ARGOSEXCEPTION("Error in opening the output CSV file.");
}

if (CConfiguration::BOOLEAN_OBSERVATIONS) {
// If the file has just been created, write its header.
if (newFile) {
output << "id_experiment;control_step;observed_robot;";
for (int i = N_OBSERVATIONS - 1; i >= 0; --i) {
for (int j = 1; j < 7; ++j) {
output << "F" + std::to_string(j) + "_t-" + std::to_string(i) + ";";
}
}
output << "fault_probability\n";
}

for (int i = 0; i < N_ROBOTS; ++i) {
bool write = true;

Expand All @@ -1661,28 +1649,15 @@ void CEPuckFaultDetection::WriteToCSVTraining() {
write = false;
}
}
datapoint += std::to_string(m_faultProbabilities[i]) + "\n";
datapoint += std::to_string(m_faultProbabilities[i]);

stringReplace(datapoint, ',', '.');

if (write) {
output << datapoint;
output_file << datapoint << std::endl;
}
}
} else {
// If the file has just been created, write its header.
if (newFile) {
output << "id_experiment;control_step;observed_robot;";
for (int i = N_OBSERVATIONS - 1; i >= 0; --i) {
output << "vel_left_wheel_t-" + std::to_string(i) + ";";
output << "vel_right_wheel_t-" + std::to_string(i) + ";";
output << "distance_traveled_t-" + std::to_string(i) + ";";
output << "min_neighbor_distance_t-" + std::to_string(i) + ";";
output << "avg_neighbor_distance_t-" + std::to_string(i) + ";";
}
output << "fault_probability\n";
}

for (int i = 0; i < N_ROBOTS; ++i) {
bool write = true;

Expand All @@ -1700,17 +1675,15 @@ void CEPuckFaultDetection::WriteToCSVTraining() {
write = false;
}
}
datapoint += std::to_string(m_faultProbabilities[i]) + "\n";
datapoint += std::to_string(m_faultProbabilities[i]);

stringReplace(datapoint, ',', '.');

if (write) {
output << datapoint;
output_file << datapoint << std::endl;
}
}
}

output.close();
}

void CEPuckFaultDetection::WriteToCSVTesting() {
Expand Down
1 change: 1 addition & 0 deletions data/binary_training/aggr/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.csv
1 change: 1 addition & 0 deletions data/binary_training/disp/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.csv
1 change: 1 addition & 0 deletions data/binary_training/floc/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.csv
1 change: 1 addition & 0 deletions data/binary_training/homi/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.csv
1 change: 1 addition & 0 deletions data/numerical_training/aggr/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.csv
1 change: 1 addition & 0 deletions data/numerical_training/disp/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.csv
1 change: 1 addition & 0 deletions data/numerical_training/floc/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.csv
1 change: 1 addition & 0 deletions data/numerical_training/homi/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.csv
6 changes: 3 additions & 3 deletions experiments/fault_detection.argos
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
All the algorithms have been tested on single-threaded simulation. Multi-threading could
generate unexpected behaviors due to concurrency. -->
<system threads="0" />
<experiment length="120"
<experiment length="600"
ticks_per_second="10"
random_seed="210018" />
random_seed="0" />
</framework>

<!-- *************** -->
Expand All @@ -31,7 +31,7 @@
<differential_steering implementation="default" vel_noise_range="-0.1:0.1" dist_noise_range="-1.0:1.0" />
<positioning implementation="default" />
</sensors>
<params behavior="homi" velocity="5" fault_type="lact" training="true"/>
<params behavior="aggr" velocity="5" fault_type="bact" training="false"/>
</epuck_fault_detection>

</controllers>
Expand Down

0 comments on commit 19f511f

Please sign in to comment.