Commit 485f392a authored by p7k's avatar p7k

Implements text file data logging for particle swarm optimization

parent 494c8e74
......@@ -11,7 +11,7 @@ void limit_angular_thickness(CoordinateSpace &d, Particle &p) {
for (size_t iter = 0; iter != 3; ++iter) {
double_t scale{0.93 / sum_at};
sum_at = 0;
for (size_t i = 0; i != 10; ++i) {
for (size_t i = 0; i != 6; ++i) {
std::string key = "at" + std::to_string(i);
if (iter == 0) {
......
......@@ -2,7 +2,7 @@
#include "globalobjective.h"
#include "model.h"
#define SAVE_DIR "./"
#define SAVE_DIR "./output/"
int main(int argc, char** argv) {
MPI_Init(NULL,NULL);
......@@ -15,7 +15,7 @@ int main(int argc, char** argv) {
// Arguments
size_t swarm_size{31};
double_t objective_tolerance{1e-2 * 22.0};
size_t maximum_iterations{3};
size_t maximum_iterations{20};
CoordinateSpace ds = design_space();
......@@ -27,6 +27,10 @@ int main(int argc, char** argv) {
Swarm swarm{ds,go,swarm_size,objective_tolerance,maximum_iterations,std::greater<double_t>()};
if (rank == 0) {
swarm.set_output_file(SAVE_DIR,"sws");
}
for (auto &p : swarm.Particles) {
p.local_objective() = [](ObjectiveMap m) {
if (m["power"] < 55.0) {
......
#include "Swarm.h"
#include <experimental/filesystem>
void Swarm::run() {
std::uniform_real_distribution<> dist(0.0,1.0);
......@@ -14,6 +15,10 @@ void Swarm::run() {
}
}
for (size_t i = 0; i != Particles.size(); ++i) {
append_particle_to_output_file(iter,i,Particles[i]);
}
all_converged = true;
for (auto &p : Particles) {
all_converged &= p.is_converged(Space,CoordinateTolerance,ObjectiveTolerance);
......@@ -41,6 +46,40 @@ void Swarm::run() {
}
}
void Swarm::set_output_file(std::string path, std::string file_name) {
if (!std::experimental::filesystem::exists(path)) {
std::experimental::filesystem::create_directories(path);
}
OutputFile = path + file_name + ".txt";
std::fstream fs;
fs.open(OutputFile, std::fstream::out);
fs << "iteration,particle,pbest,gbest";
for (auto key_ind : Particles[0].state().map()) {
fs << "," << key_ind.first;
}
fs << "\n";
fs.close();
};
void Swarm::append_particle_to_output_file(double_t iter, size_t pnum, Particle &p) {
if (OutputFile.size() > 4) {
std::fstream fs;
fs.open(OutputFile, std::fstream::out | std::fstream::app);
fs << iter << "," << pnum << "," << p.personal_best() << "," << p.global_best();
for (auto state : p.state().vector()) {
fs << "," << state.Position;
}
fs << "\n";
fs.close();
}
}
std::ostream& operator<<(std::ostream& os, Swarm& s) {
for (auto &p : s.Particles) {
os << p;
......@@ -115,6 +154,8 @@ void Swarm::mpi_master_receive(std::deque<int> &process_queue, std::deque<int> &
// Update personal best
Particles[particle].update_personal_best(objective[particle],Comparator);
append_particle_to_output_file(iter,(size_t)particle,Particles[particle]);
// Perturb
if (!Particles[particle].has_improved()) {
Space.perturb(Particles[particle],RNG);
......
#ifndef OERSTED_SWARM_H
#define OERSTED_SWARM_H
#include <fstream>
#include "Particle.h"
#include "CoordinateSpace.h"
......@@ -41,6 +43,10 @@ public:
void mpi_run();
void set_output_file(std::string path, std::string file_name);
void append_particle_to_output_file(double_t iter, size_t pnum, Particle &p);
friend std::ostream& operator<<(std::ostream& os, Swarm& s);
std::vector<Particle> Particles;
......@@ -62,7 +68,9 @@ protected:
std::mt19937 RNG;
protected: // MPI Inforomation
std::string OutputFile;
protected: // MPI Information
void mpi_master();
void mpi_master_send(std::deque<int> &processes, std::deque<int> &particles);
void mpi_master_receive(std::deque<int> &process_queue, std::deque<int> &particle_queue, double_t &iter);
......@@ -83,7 +91,6 @@ protected: // MPI Inforomation
MPI_Datatype MPI_CoordinateState;
MPI_Datatype MPI_ParticleState;
};
#endif //OERSTED_SWARM_H
......@@ -22,6 +22,10 @@ TEST(Particle_Swarm_Optimization, mpi_swarm) {
Swarm swarm{cs,go,21,1e-2,50};
if (rank == 0) {
swarm.set_output_file(SAVE_DIR, "mpi_sos");
}
for (size_t i = 0; i != swarm.size(); ++i) {
swarm.Particles[i].local_objective() = [](ObjectiveMap go) {
return go["z0"];
......
......@@ -4,4 +4,6 @@
#include "Optimization.hpp"
#include "gtest.h"
#define SAVE_DIR "./test/output/Optimization/"
#endif //OERSTED_TEST_OPTIMIZATION_HPP
......@@ -27,7 +27,7 @@ TEST(Particle_Swarm_Optimization, particle_initialization) {
std::random_device rd;
std::mt19937 rng(rd());
Particle p = cs.new_particle(rng);
Particle p = cs.new_particle(rng,DBL_MAX);
for (std::string key : {"x","y"}) {
EXPECT_GE(p.position(key), cs[key].lower_bound());
......@@ -78,6 +78,8 @@ TEST(Particle_Swarm_Optimization, single_objective_swarm) {
Swarm swarm{cs,go,10,1e-2,20};
swarm.set_output_file(SAVE_DIR,"sos");
for (size_t i = 0; i != swarm.size(); ++i) {
swarm.Particles[i].local_objective() = [](ObjectiveMap go) {
return go["z0"];
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment