Commit 9fd5ea1e authored by Ulrich Kemloh's avatar Ulrich Kemloh

code stub: AgentsSources for generating pedestrians at a certain

frequency in a specific location
parent 2ccc5948
......@@ -32,7 +32,8 @@
#include "math/GCFMModel.h"
#include "math/GompertzModel.h"
//#include "geometry/Goal.h"
#include "pedestrian/AgentsSourcesManager.h"
#include "pedestrian/AgentsQueue.h"
#ifdef _OPENMP
#include <omp.h>
......@@ -99,7 +100,7 @@ bool Simulation::InitArgs(const ArgumentParser& args)
delete Log;
Log = new FileHandler(name);
}
break;
break;
default:
printf("Wrong option for Logfile!\n\n");
return false;
......@@ -133,9 +134,9 @@ bool Simulation::InitArgs(const ArgumentParser& args)
"INFO: \tFormat vtk not yet supported in streaming\n");
return false;
}
default: {
return false;
}
default: {
return false;
}
}
s.append("\tonline streaming enabled \n");
......@@ -184,9 +185,9 @@ bool Simulation::InitArgs(const ArgumentParser& args)
// _iod->AddIO(output);
break;
}
default: {
break;
}
default: {
break;
}
}
}
......@@ -218,6 +219,13 @@ bool Simulation::InitArgs(const ArgumentParser& args)
// IMPORTANT: do not change the order in the following..
_building = std::unique_ptr<Building>(new Building(args.GetProjectFile(), args.GetProjectRootDir(), *_routingEngine, *distributor, args.GetLinkedCellSize()));
// Initialize the agents sources that have been collected in the pedestrians distributor
for (const auto& src: distributor->GetAgentsSources())
{
_agentSrcManager.AddSource(src);
src->Dump();
}
//perform customs initialisation, like computing the phi for the gcfm
//this should be called after the routing engine has been initialised
// because a direction is needed for this initialisation.
......@@ -281,6 +289,7 @@ int Simulation::RunSimulation()
//first initialisation needed by the linked-cells
UpdateRoutesAndLocations();
ProcessAgentsQueue();
//needed to control the execution time PART 1
//in the case you want to run in no faster than realtime
......@@ -292,6 +301,9 @@ int Simulation::RunSimulation()
{
t = 0 + (frameNr - 1) * _deltaT;
//process the queue for incomming pedestrians
ProcessAgentsQueue();
// update the positions
_operationalModel->ComputeNextTimeStep(t, _deltaT, _building.get());
......@@ -322,7 +334,7 @@ int Simulation::RunSimulation()
// double timeToWait=t-difftime(endtime, starttime);
// clock_t goal = timeToWait*1000 + clock();
// while (goal > clock());
++frameNr;
++frameNr;
}
// writing the footer
......@@ -479,7 +491,7 @@ void Simulation::PrintStatistics()
{
auto&& room=it.second;
if(room->GetCaption()!="outside")
Log->Write("%d\t%s\t%.2f",room->GetID(),room->GetCaption().c_str(),room->GetEgressTime());
Log->Write("%d\t%s\t%.2f",room->GetID(),room->GetCaption().c_str(),room->GetEgressTime());
}
Log->Write("\nUsage of Exits");
......@@ -496,3 +508,20 @@ void Simulation::PrintStatistics()
Log->Write("\n");
}
const AgentsSourcesManager& Simulation::GetAgentSrcManager()
{
return _agentSrcManager;
}
void Simulation::ProcessAgentsQueue()
{
vector<Pedestrian*> peds;
AgentsQueue::GetandClear(peds);
for(auto&& ped: peds)
{
_building->AddPedestrian(ped);
}
}
......@@ -51,6 +51,11 @@
#include "pedestrian/PedDistributor.h"
#include "routing/CognitiveMapRouter.h"
#include "events/EventManager.h"
#include "pedestrian/AgentsSourcesManager.h"
//Forward declarations TO DO
//class AgentsSourcesManager;
class EventManager;
class Simulation
{
......@@ -78,13 +83,23 @@ private:
EventManager* _em;
/// argument parser
ArgumentParser _argsParser;
/// Agents sources manager
AgentsSourcesManager _agentSrcManager;
/// profiling flag
bool _profiling;
/// architecture flag
int _hpc;
public:
/**
* Constructor
*/
Simulation(const ArgumentParser& args);
/**
* Destructor
*/
virtual ~Simulation();
/**
......@@ -107,9 +122,6 @@ public:
*/
void UpdateRoutesAndLocations();
//void Update(double &b, double &p, double &t, double &g);
/**
* Set the ProfilingFlag
*/
......@@ -130,6 +142,16 @@ public:
*/
void PrintStatistics();
/**
* @return the agents source manager
*/
const AgentsSourcesManager& GetAgentSrcManager();
/**
* Check if any agents are waiting to enter the simulation
*/
void ProcessAgentsQueue();
};
#endif /*SIMULATION_H_*/
......@@ -73,6 +73,13 @@ xsi:noNamespaceSchemaLocation="http://xsd.jupedsim.org/0.6/jps_ini_core.xsd">
<group group_id="0" room_id="0" subroom_id="0" number="50" router_id="1" agent_parameter_id="1"
risk_tolerance_mean="0.8" risk_tolerance_sigma="0.01"/>
</agents_distribution>
<agents_sources><!-- frequency in persons/minute -->
<source id="1" frequency="1" agents_max="10" group_id="0" caption="source 1">
</source>
</agents_sources>
</agents>
<operational_models>
......
......@@ -29,7 +29,11 @@
#include "geometry/Building.h"
#include "general/ArgumentParser.h"
#include "./Simulation.h"
// #include "logging.h"
#include "pedestrian/AgentsSourcesManager.h"
#include <thread>
#include <functional>
//#include <boost/version.hpp>
int main(int argc, char **argv)
{
......@@ -51,11 +55,24 @@ int main(int argc, char **argv)
if(status&&sim.InitArgs(*args))
{
//Start the threads for managing the sources of agents
//AgentsSourcesManager sManager;
std::thread t1(sim.GetAgentSrcManager(),21);
//Start the thread for managing incoming messages from MatSim
//Start the thread for managing outgoing messages to MatSim
//main thread for the simulation
Log->Write("INFO: \tStart runSimulation()");
int evacTime = sim.RunSimulation();
Log->Write("\nINFO: \tEnd runSimulation()");
time(&endtime);
//the execution is finished at this time
//so join the other threads
t1.join();
// some output
if(args->ShowStatistics())
{
......@@ -85,6 +102,7 @@ int main(int argc, char **argv)
printf("Warnings : %d\n", Log->GetWarnings());
printf("Errors : %d\n", Log->GetErrors());
}
}
else
{
......
/*
* AgentsQueue.cpp
*
* Created on: 14.04.2015
* Author: piccolo
*/
#include "AgentsQueue.h"
#include "Pedestrian.h"
using namespace std;
vector<Pedestrian*> AgentsQueue::_agentsQueue;
mutex AgentsQueue::_queueMutex;
void AgentsQueue::Add(vector<Pedestrian*>& peds)
{
_queueMutex.lock();
_agentsQueue.insert(_agentsQueue.end(),peds.begin(),peds.end());
_queueMutex.unlock();
// while (true)
// {
// // try to lock mutex to modify 'job_shared'
// if (_queueMutex.try_lock())
// {
// _agentsQueue.insert(_agentsQueue.end(),peds.begin(),peds.end());
// _queueMutex.unlock();
// return;
// }
// else
// {
// //maybe the mutex is beeing used
// std::cout << "unable to lock the mutex for adding 1" <<endl;
// //std::this_thread::sleep_for(100);
// }
// }
}
void AgentsQueue::GetandClear(std::vector<Pedestrian*>& peds)
{
//while (true)
//{
// try to lock mutex to modify _agentsQueue
_queueMutex.lock();
if(_agentsQueue.size()!=0)
{
peds.insert(peds.end(),_agentsQueue.begin(), _agentsQueue.end());
//_agentsQueue.pop_back();
_agentsQueue.clear();
}
_queueMutex.unlock();
// }
// else
// {
// //maybe the mutex is beeing used
// std::cout << "unable to lock the mutex for removing 2" <<endl;
// //std::this_thread::sleep_for(100);
// }
//}
}
bool AgentsQueue::IsEmpty()
{
return (_agentsQueue.size()==0);
}
/*
* AgentsQueue.h
*
* Created on: 14.04.2015
* Author: piccolo
*/
#ifndef AGENTSQUEUE_H_
#define AGENTSQUEUE_H_
#include <mutex>
#include <vector>
//forward declaration
class Pedestrian;
class AgentsQueue
{
public:
static void Add(std::vector<Pedestrian*>& ped);
static void GetandClear(std::vector<Pedestrian*>& peds);
static bool IsEmpty();
//static std::vector<Pedestrian*> GetandClear();
private:
AgentsQueue(){};
virtual ~AgentsQueue(){};
static std::vector<Pedestrian*> _agentsQueue;
static std::mutex _queueMutex;
};
#endif /* AGENTSQUEUE_H_ */
/*
* AgentSource.cpp
*
* Created on: 14.04.2015
* Author: piccolo
*/
#include "AgentsSource.h"
#include "Pedestrian.h"
#include "../IO/OutputHandler.h"
#include <iostream>
AgentsSource::AgentsSource(int id,std::string caption,int max_agents,int group_id,int frequency)
{
_id=id;
_caption=caption;
_maxAgents=max_agents;
_groupID=group_id;
_frequency=frequency;
_agents.clear();
}
AgentsSource::~AgentsSource()
{
}
void AgentsSource::GenerateByFrequency(std::vector<Pedestrian*>& ped)
{
if((int)_agents.size()>=_frequency)
{
ped.insert(ped.begin(),_agents.begin(),_agents.begin()+_frequency);
_agents.erase(_agents.begin(),_agents.begin()+_frequency);
}
else
{
ped.insert(ped.begin(),_agents.begin(),_agents.end());
_agents.clear();
}
}
int AgentsSource::GetPoolSize() const
{
return _agents.size();
}
void AgentsSource::Add(Pedestrian* ped)
{
_agents.push_back(ped);
}
int AgentsSource::GetAgentsGenerated() const
{
return _agentsGenerated;
}
void AgentsSource::SetAgentsGenerated(int agentsGenerated)
{
_agentsGenerated = agentsGenerated;
}
const double* AgentsSource::GetBoundaries() const
{
return _boundaries;
}
const std::string& AgentsSource::GetCaption() const
{
return _caption;
}
int AgentsSource::GetFrequency() const
{
return _frequency;
}
int AgentsSource::GetGroupId() const
{
return _groupID;
}
int AgentsSource::GetId() const
{
return _id;
}
int AgentsSource::GetMaxAgents() const
{
return _maxAgents;
}
void AgentsSource::Dump() const
{
Log->Write("\n--------------------------");
Log->Write("Dumping Source");
Log->Write("ID: %d", _id);
Log->Write("Group ID: %d", _groupID);
Log->Write("Frequency: %d", _frequency);
Log->Write("Agents Max: %d", _maxAgents);
}
/*
* AgentSource.h
*
* Created on: 14.04.2015
* Author: piccolo
*/
#ifndef AGENTSOURCE_H_
#define AGENTSOURCE_H_
#include <string>
#include <vector>
class Pedestrian;
class OutputHandler;
// external variables
extern OutputHandler* Log;
class AgentsSource
{
public:
/**
* Constructor
*/
AgentsSource(int id,std::string caption,int max_agents,int group_id,int frequency);
/**
* Destructor
*/
virtual ~AgentsSource();
/**
* Add a new agent to this source
* @param ped
*/
void Add(Pedestrian* ped);
/**
* Generate a number of agents, based on the frequency given in the constructor.
* No agents are generated if the the maximum (_maxAgents) is reached.
* @see _maxAgents
* @param ped
*/
void GenerateByFrequency(std::vector<Pedestrian*>& ped);
/**
* @return the number of agents remaining
*/
int GetPoolSize() const;
/**
* Print relevant information
*/
void Dump() const;
int GetGroupID() const;
int GetAgentsGenerated() const;
void SetAgentsGenerated(int agentsGenerated);
const double* GetBoundaries() const;
const std::string& GetCaption() const;
int GetFrequency() const;
int GetGroupId() const;
int GetId() const;
int GetMaxAgents() const;
private:
int _id=-1;
int _frequency=0;
int _maxAgents=0;
int _agentsGenerated=0;
int _groupID=-1;
double _boundaries [4] = {0,0,0,0};
std::string _caption="no caption";
std::vector<Pedestrian*> _agents;
};
#endif /* AGENTSOURCE_H_ */
/*
* AgentsSourcesManager.cpp
*
* Created on: 14.04.2015
* Author: piccolo
*/
#include "AgentsSourcesManager.h"
#include "Pedestrian.h"
#include "AgentsQueue.h"
#include "AgentsSource.h"
#include <iostream>
#include <thread>
#include <chrono>
using namespace std;
AgentsSourcesManager::AgentsSourcesManager()
{
}
AgentsSourcesManager::~AgentsSourcesManager()
{
}
void AgentsSourcesManager::operator()(int value)
{
//the loop is updated each second.
//it might be better to use a timer
bool finished=false;
do
{
finished=true;
for (const auto& src: _sources)
{
if(src->GetPoolSize())
{
vector<Pedestrian*> peds;
src->GenerateByFrequency(peds);
AgentsQueue::Add(peds);
// compute the optimal position for insertion
for (auto&& ped: peds)
{
ped->SetPos(Point(15,15),true);
}
finished=false;
cout<<"Agents generated: "<<peds.size()<<endl;
}
//src->Dump();
}
//cout<<"Number of sources: "<<_sources.size()<<endl;
//wait one second
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}while (!finished);
}
void AgentsSourcesManager::AddSource(std::shared_ptr<AgentsSource> src)
{
_sources.push_back(src);
}
const std::vector<std::shared_ptr<AgentsSource> >& AgentsSourcesManager::GetSources() const
{
return _sources;
}
/*
* AgentsSourcesManager.h
*
* Created on: 14.04.2015
* Author: piccolo
*/
#ifndef AGENTSSOURCESMANAGER_H_
#define AGENTSSOURCESMANAGER_H_
#include <vector>
#include <memory>
//Forward declarations
class AgentsSource;
class AgentsSourcesManager
{
public:
/**
* Constructor
*/
AgentsSourcesManager();
/**
* Destructor
*/
virtual ~AgentsSourcesManager();
/**
* Make the class "runnable" by overloading the operator
* @param value
*/
void operator()(int value);
/**
* Add a new agent source
*/
void AddSource(std::shared_ptr<AgentsSource> src);
/**
* @return all sources
*/
const std::vector<std::shared_ptr<AgentsSource> >& GetSources() const;
//void operator()();
private:
/// contain the sources
std::vector<std::shared_ptr<AgentsSource>> _sources;
};
#endif /* AGENTSSOURCESMANAGER_H_ */
This diff is collapsed.
......@@ -25,139 +25,75 @@
*
**/
#ifndef _PEDDISTRIBUTOR_H
#define _PEDDISTRIBUTOR_H
#include <vector>
#include <string>
#include <memory>
#include "../routing/Router.h"
#include "../geometry/Building.h"
#include "AgentsParameters.h"
#include "AgentsSource.h"
#include "StartDistribution.h"
#include "../general/ArgumentParser.h"
typedef vector< Point > tPoints;
typedef vector< tPoints > GridPoints;
/************************************************************
StartDistributionRoom
************************************************************/
class StartDistributionRoom {
private:
int _roomID;
int _nPeds;
int _groupID;
int _goalID;
int _routerID;
int _routeID;
int _subroomID;
//demographic parameters
//TODO: should also follow a distribution, see _premovement
std::string _gender;
int _age;
int _height;
double _patience;
//force model parameters
AgentsParameters* _groupParameters;
//string motivation;// low, normal, high
double _startX; //only valid when _nPeds=1
double _startY; //only valid when _nPeds=1
double _startZ; //only valid when _nPeds=1
//bounds for distributing in a square
double _xMin;
double _xMax;
double _yMin;
double _yMax;
//pre movement time distribution
std::normal_distribution<double> _premovementTime;
//typedef vector<Point> tPoints;
//typedef vector<tPoints> GridPoints;
//risk tolerance distribution
std::normal_distribution<double> _riskTolerance;
//random number generator engine
std::default_random_engine _generator;
public:
StartDistributionRoom(int seed);
virtual ~StartDistributionRoom();
int GetAgentsNumber() const;
void SetRoomID(int id);
void SetAgentsNumber(int N);
int GetAge() const;
void SetAge(int age);
const std::string& GetGender() const;
void SetGender(const std::string& gender);
int GetGoalId() const;
void SetGoalId(int goalId);
int GetGroupId() const;
void SetGroupId(int groupId);
int GetHeight() const;
void SetHeight(int height);
int GetRoomId() const;
void SetRoomId(int roomId);
int GetSubroomID() const;
void SetSubroomID(int subroomID);
int GetRouteId() const;
void SetRouteId(int routeId);
int GetRouterId() const;
void SetRouterId(int routerId);
void SetStartPosition(double x, double y, double z);
Point GetStartPosition() const;
double GetPatience() const;
void SetPatience(double patience);
void SetBounds(double xMin, double xMax, double yMin, double yMax);
void Getbounds(double bounds[4]);
void Setbounds(double bounds