Une MAJ de sécurité est nécessaire sur notre version actuelle. Elle sera effectuée lundi 02/08 entre 12h30 et 13h. L'interruption de service devrait durer quelques minutes (probablement moins de 5 minutes).

Commit 0d70c958 authored by Javad AMIRIAN's avatar Javad AMIRIAN Committed by Fabien Grzeskowiak
Browse files

Resolve "CSV-Writer"

parent b9c28dc0
......@@ -24,11 +24,10 @@
cmake_minimum_required( VERSION 2.8 )
project( CrowdSimulator )
set (CMAKE_CXX_STANDARD 11)
#set(EXECUTABLE_OUTPUT_PATH bin/${CMAKE_BUILD_TYPE})
find_package(Boost COMPONENTS system filesystem regex REQUIRED)
include_directories( ./include )
link_directories( ./lib/${CMAKE_BUILD_TYPE} )
......@@ -39,6 +38,11 @@ endif (WIN32)
file( GLOB_RECURSE source_files src/* include/*)
add_executable( my_app main.cpp ${source_files} )
add_executable(my_app main.cpp ${source_files} )
add_executable(test_xmlparser ./test/test_xmlparser.cpp ${source_files})
add_executable(test_socialforces ./test/SocialForcesExample.cpp ${source_files})
target_link_libraries(my_app ${Boost_LIBRARIES})
target_link_libraries(test_xmlparser ${Boost_LIBRARIES})
target_link_libraries(test_socialforces ${Boost_LIBRARIES})
......@@ -30,10 +30,12 @@
#include <core/world.h>
#include <core/agent.h>
#include <fstream>
class CSVWriter;
class CrowdSimulator {
private:
World* world_;
CSVWriter* writer;
public:
......@@ -47,11 +49,13 @@ public:
/**
* Writes the result of the simulation to disk
*/
void writeSimulation();
void setOutputDir(const std::string& dirname);
/**
* Runs one simulation step
* @param dt
* @todo: keep all the results inside this class, to be written later
* or write thme instantly (super time-consuming)
*/
void stepWorld(float dt);
......
......@@ -43,6 +43,7 @@ class World
private:
std::vector<Agent*> agents_;
std::vector<Obstacle*> obstacles_;
float time_;
lqInternalDB2D *lqDB;
std::vector<lqClientProxy2D*> lqProxies;
......@@ -72,6 +73,9 @@ class World
void setAgents(const std::vector<Agent *> &agents);
void setObstacles(const std::vector<Obstacle *> &obstacles);
std::vector<Agent *> getAgents() const;
std::vector<Obstacle *> getObstacles() const;
float getTime() const;
/**
*
......
/* Crowd Simulator Engine
** Copyright (C) 2018 - Inria Rennes - Rainbow - Julien Pettre
**
** This program is free software; you can redistribute it and/or
** modify it under the terms of the GNU General Public License
** as published by the Free Software Foundation; either version 2
** of the License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**
**
**
** Authors: Axel Lopez Gandia, Javad Amirian, Florian Berton,
** Julien Legros, Lucas Pelerin, Beatriz Cabrero Daniel, Fabien Grzeskowiak
**
** Contact: crowd_group@inria.fr
*/
//========================================================================
/*!
@file csvwriter.h
@class CSVWriter
@date 24/8/2018
@brief
@author Javad Amirian, (C) 2018
*/
//========================================================================
#ifndef _CSV_WRITER_H
#define _CSV_WRITER_H
#include <string>
#include <vector>
#include <mutex>
#include <map>
#include <iostream>
#include "tools/vector2D.h"
class CSVWriter
{
struct TimeAndPos
{
float t_, x_, y_;
TimeAndPos(float t, float x, float y) {
t_ = t; x_ = x; y_ = y;
}
};
std::mutex mtx_;
std::string dirname_;
std::map<int, std::vector<TimeAndPos>> pos_log_;
public:
CSVWriter() {}
/*!
* @warning: dirname should end with slash *
*/
void setDirectory(const std::string &dirname);
/*!
* @todo implement a policy for calling flush
*/
void flush();
void appendPedPosition(int pid, const Vector2D& pos, float t);
void appendPedPositions(std::map<int, Vector2D> &poss, float t);
void appendPedTrajectort(int pid, const std::vector<Vector2D>& traj);
void appendTrajectories(const std::vector<std::vector<Vector2D> >& trajs);
};
#endif // _CSV_WRITER_H
......@@ -25,10 +25,12 @@
#include <core/crowdSimulator.h>
#include "tools/xmlparser.h"
#include "tools/csvwriter.h"
CrowdSimulator::CrowdSimulator()
{
createNewWorld();
writer = new CSVWriter();
}
void CrowdSimulator::loadInitFile(const std::string &filename)
......@@ -39,6 +41,11 @@ void CrowdSimulator::loadInitFile(const std::string &filename)
world_->setObstacles(parser.obstacles);
}
void CrowdSimulator::setOutputDir(const std::string &dirname)
{
writer->setDirectory(dirname);
}
void CrowdSimulator::AppendPosInOutput(std::ofstream &outfile, double time, Agent* agent)
{
if(outfile.is_open())
......@@ -56,8 +63,16 @@ void CrowdSimulator::addAgentToSimulation(Agent * agent) {
* Runs one simulation step
* @param dt
*/
void CrowdSimulator::stepWorld(float dt) {
void CrowdSimulator::stepWorld(float dt)
{
world_->doStep(dt);
float t = world_->getTime();
std::vector<Agent*> agents = world_->getAgents();
std::map<int, Vector2D> poss;
for(int i=0; i<agents.size(); i++)
poss[agents[i]->getID()] = agents[i]->getPosition();
writer->appendPedPositions(poss, t);
writer->flush(); //! @todo: should be removed after test
}
/**
......
......@@ -38,6 +38,7 @@ World::World()//:
lqProxies.push_back(proxy);
lqUpdateForNewLocation(lqDB, proxy, agents_[i]->getPosition().x(), agents_[i]->getPosition().y());
}
time_ = 0;
}
std::vector<Agent *> World::getNeighboursOfAgent(int agent_id, float search_radius)
......@@ -61,7 +62,7 @@ void World::perNeighborCallBackFunction(void *clientObject, float, void *clientQ
void World::doStep(double dt)
{
time_ += dt;
for (size_t i = 0; i < agents_.size(); ++i)
{
agents_[i]->runPolicy(this);
......@@ -115,6 +116,21 @@ void World::setObstacles(const std::vector<Obstacle *> &obstacles)
obstacles_ = obstacles;
}
std::vector<Agent *> World::getAgents() const
{
return agents_;
}
std::vector<Obstacle *> World::getObstacles() const
{
return obstacles_;
}
float World::getTime() const
{
return time_;
}
void World::AddAgent(Agent * newagent) {
agents_.push_back(newagent);
newagent->setID(agents_.size() - 1);
......
/* Crowd Simulator Engine
** Copyright (C) 2018 - Inria Rennes - Rainbow - Julien Pettre
**
** This program is free software; you can redistribute it and/or
** modify it under the terms of the GNU General Public License
** as published by the Free Software Foundation; either version 2
** of the License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**
**
**
** Authors: Axel Lopez Gandia, Javad Amirian, Florian Berton,
** Julien Legros, Lucas Pelerin, Beatriz Cabrero Daniel, Fabien Grzeskowiak
**
** Contact: crowd_group@inria.fr
*/
//========================================================================
/*!
@file csvwriter.cpp
@class CSVWriter
@date 24/8/2018
@brief
@author Javad Amirian, (C) 2018
*/
//========================================================================
#include "../include/tools/csvwriter.h"
#include <boost/filesystem.hpp>
#include <fstream>
void CSVWriter::setDirectory(const std::string &dirname)
{
this->dirname_ = dirname;
if (!boost::filesystem::exists(dirname))
boost::filesystem::create_directory(dirname);
}
void CSVWriter::flush()
{
mtx_.lock();
std::map<int, std::vector<TimeAndPos>> pos_log_copy = pos_log_;
pos_log_.clear();
mtx_.unlock();
for(auto itr=pos_log_copy.begin(); itr != pos_log_copy.end(); itr++) {
std::fstream file_i;
int id = itr->first;
std::string file_name = dirname_ + "output_" + std::to_string(id) + ".csv";
file_i.open(file_name, std::ios::app);
for(int i=0; i<itr->second.size(); i++)
file_i << itr->second[i].t_ << "," << itr->second[i].x_ << "," << itr->second[i].y_ << "\n";
file_i.close();
itr->second.clear();
}
}
void CSVWriter::appendPedPosition(int pid, const Vector2D &pos, float t)
{
mtx_.lock();
pos_log_[pid].push_back(TimeAndPos(t, pos.x(), pos.y()));
mtx_.unlock();
}
void CSVWriter::appendPedPositions(std::map<int, Vector2D> &poss, float t)
{
mtx_.lock();
for(auto itr=poss.begin(); itr != poss.end(); itr++)
pos_log_[itr->first].push_back(TimeAndPos(t, itr->second.x(), itr->second.y()));
mtx_.unlock();
}
......@@ -37,13 +37,9 @@
using namespace std;
int main( int argc, char * argv[] ){
CrowdSimulator crowdSimulator;
ofstream outfile1;
outfile1.open("TestOut0.csv");
ofstream outfile2;
outfile2.open("TestOut1.csv");
int main( int argc, char * argv[] )
{
CrowdSimulator crowdSimulator;
Agent * agent1 = new Agent();
Agent * agent2 = new Agent();
......@@ -68,19 +64,13 @@ int main( int argc, char * argv[] ){
crowdSimulator.addAgentToSimulation(agent1);
crowdSimulator.addAgentToSimulation(agent2);
crowdSimulator.setOutputDir("./output3/");
float dt = 0.033;
float time = 0;
int iter = 0;
int MAXITERATIONS = 10000;
while ((agent1->getPosition() - goalPos).magnitude() > 5 && iter < MAXITERATIONS)
while ((agent1->getPosition() - goalPos).magnitude() > 5 && iter++ < MAXITERATIONS)
{
crowdSimulator.stepWorld(dt);
time += dt;
crowdSimulator.AppendPosInOutput(outfile1, time, agent1);
crowdSimulator.AppendPosInOutput(outfile2, time, agent2);
iter++;
}
outfile1.close();
outfile2.close();
system("pause");
system("pause");
}
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