Commit f61f7ef0 authored by Ulrich Kemloh's avatar Ulrich Kemloh

Performance improvement and cleanign

When an agent cannot be send to matsim, it is rescheduled after 100
timesteps

Cleaning old not longer used functions.
parent 19afcb74
...@@ -32,8 +32,8 @@ ...@@ -32,8 +32,8 @@
#include "math/GCFMModel.h" #include "math/GCFMModel.h"
#include "math/GompertzModel.h" #include "math/GompertzModel.h"
#include "pedestrian/AgentsSourcesManager.h"
#include "pedestrian/AgentsQueue.h" #include "pedestrian/AgentsQueue.h"
#include "pedestrian/AgentsSourcesManager.h"
#ifdef _USE_PROTOCOL_BUFFER #ifdef _USE_PROTOCOL_BUFFER
#include "matsim/HybridSimulationManager.h" #include "matsim/HybridSimulationManager.h"
...@@ -292,7 +292,7 @@ void Simulation::UpdateRoutesAndLocations() ...@@ -292,7 +292,7 @@ void Simulation::UpdateRoutesAndLocations()
//pedestrians to be deleted //pedestrians to be deleted
//you should better create this in the constructor and allocate it once. //you should better create this in the constructor and allocate it once.
vector<Pedestrian*> pedsToRemove; vector<Pedestrian*> pedsToRemove;
pedsToRemove.reserve(100); //just reserve some space pedsToRemove.reserve(500); //just reserve some space
// collect all pedestrians in the simulation. // collect all pedestrians in the simulation.
const vector<Pedestrian*>& allPeds = _building->GetAllPedestrians(); const vector<Pedestrian*>& allPeds = _building->GetAllPedestrians();
...@@ -549,7 +549,7 @@ void Simulation::ProcessAgentsQueue() ...@@ -549,7 +549,7 @@ void Simulation::ProcessAgentsQueue()
//incoming pedestrians //incoming pedestrians
vector<Pedestrian*> peds; vector<Pedestrian*> peds;
AgentsQueue::GetandClear(peds); AgentsQueueIn::GetandClear(peds);
for(auto&& ped: peds) for(auto&& ped: peds)
{ {
_building->AddPedestrian(ped); _building->AddPedestrian(ped);
......
...@@ -49,7 +49,7 @@ ...@@ -49,7 +49,7 @@
// should be true only when using this file in the simulation core // should be true only when using this file in the simulation core
#define _SIMULATOR 1 #define _SIMULATOR 1
//#define _USE_PROTOCOL_BUFFER 1 #define _USE_PROTOCOL_BUFFER 1
......
...@@ -9,7 +9,6 @@ ...@@ -9,7 +9,6 @@
#include "MATSimInterface.pb.h" #include "MATSimInterface.pb.h"
#include "../pedestrian/Pedestrian.h" #include "../pedestrian/Pedestrian.h"
#include "../geometry/Building.h" #include "../geometry/Building.h"
#include "../pedestrian/AgentsQueue.h"
#include "../pedestrian/AgentsSourcesManager.h" #include "../pedestrian/AgentsSourcesManager.h"
#include "../Simulation.h" #include "../Simulation.h"
#include "JPSserver.h" #include "JPSserver.h"
...@@ -33,6 +32,7 @@ ...@@ -33,6 +32,7 @@
#include <grpc++/client_context.h> #include <grpc++/client_context.h>
#include <grpc++/create_channel.h> #include <grpc++/create_channel.h>
#include <grpc++/credentials.h> #include <grpc++/credentials.h>
#include "../pedestrian/AgentsQueue.h"
using grpc::Server; using grpc::Server;
......
...@@ -7,15 +7,14 @@ ...@@ -7,15 +7,14 @@
#include "JPSclient.h" #include "JPSclient.h"
#include "../pedestrian/Pedestrian.h" #include "../pedestrian/Pedestrian.h"
#include "../pedestrian/AgentsQueue.h"
#include "../geometry/Building.h" #include "../geometry/Building.h"
#include "../pedestrian/AgentsQueueIn.h"
#include "MATSimInterface.pb.h" #include "MATSimInterface.pb.h"
using namespace std; using namespace std;
JPSclient::JPSclient(std::shared_ptr<ChannelInterface> channel) JPSclient::JPSclient(std::shared_ptr<ChannelInterface> channel)
//:_jupedsimChannel(ExternInterfaceService::NewStub(channel))
{ {
//communication channel to matsim //communication channel to matsim
_matsimChannel = MATSimInterfaceService::NewStub(channel); _matsimChannel = MATSimInterfaceService::NewStub(channel);
...@@ -32,33 +31,29 @@ JPSclient::~JPSclient() ...@@ -32,33 +31,29 @@ JPSclient::~JPSclient()
void JPSclient::ProcessAgentQueue(Building* building) void JPSclient::ProcessAgentQueue(Building* building)
{ {
std::vector<Pedestrian*> peds; std::vector<Pedestrian*> peds;
//cout<<"size:"<<AgentsQueueOut::Size()<<endl;
AgentsQueueOut::GetandClear(peds); AgentsQueueOut::GetandClear(peds);
for (auto && ped:peds) for (auto && ped:peds)
{ {
//if the agent has waited enough
/* for testing only, the agent is send back to jupedsim if (_counter[ped->GetID()]<=0)
if(HasSpaceOnJuPedSim(ped->GetFinalDestination())==true)
{ {
if(SendAgentToJuPedSim(ped)==false) //remove the pedestrian only if successfully sent
if(SendAgentToMatsim(ped)==true)
{ {
Log->Write("ERROR:\t RPC:JPSclient request failed (send agent to jupedsim)"); building->DeletePedestrian(ped);
}
else
{
AgentsQueueOut::Add(ped);
//reschedule after 100 timesteps
_counter[ped->GetID()]=100;
} }
}
else
{
Log->Write("ERROR:\t RPC:JPSclient request failed (space on jupedsim)");
}
*/
//remove the pedestrian only if successfully sent
if(SendAgentToMatsim(ped)==true)
{
building->DeletePedestrian(ped);
} }
else else
{ {
AgentsQueueOut::Add(ped); AgentsQueueOut::Add(ped);
_counter[ped->GetID()]--;
} }
} }
} }
...@@ -114,7 +109,6 @@ bool JPSclient::NotifyExternalService(const std::string& host, int port) ...@@ -114,7 +109,6 @@ bool JPSclient::NotifyExternalService(const std::string& host, int port)
request.set_host(host); request.set_host(host);
request.set_port(port); request.set_port(port);
Status status =_matsimChannel->reqExternalConnect(&context, request, &reply); Status status =_matsimChannel->reqExternalConnect(&context, request, &reply);
//std::cout<<"Details: "<<status.details()<<endl;
return status.IsOk(); return status.IsOk();
} }
...@@ -145,7 +139,6 @@ bool JPSclient::NotifyEndOfSimulation() ...@@ -145,7 +139,6 @@ bool JPSclient::NotifyEndOfSimulation()
ClientContext context; ClientContext context;
ExternSimStepFinished request; ExternSimStepFinished request;
ExternSimStepFinishedReceived reply; ExternSimStepFinishedReceived reply;
Status status =_matsimChannel->reqExternSimStepFinished(&context, request, &reply); Status status =_matsimChannel->reqExternSimStepFinished(&context, request, &reply);
//Log->Write("INFO:\tRPC::JPSserver simulation step finished"); //Log->Write("INFO:\tRPC::JPSserver simulation step finished");
//std::cout<<"Details: "<<status.details()<<endl; //std::cout<<"Details: "<<status.details()<<endl;
......
...@@ -8,21 +8,6 @@ ...@@ -8,21 +8,6 @@
#ifndef MATSIM_JPSCLIENT_H_ #ifndef MATSIM_JPSCLIENT_H_
#define MATSIM_JPSCLIENT_H_ #define MATSIM_JPSCLIENT_H_
//#include <grpc++/status.h>
//client stuff
//#include <grpc++/channel_arguments.h>
//#include <grpc++/create_channel.h>
//#include <grpc++/credentials.h>
//using grpc::Server;
//using grpc::ServerBuilder;
//using grpc::ServerContext;
//using grpc::Status;
//using grpc::ChannelArguments;
//using grpc::ClientContext;
//using grpc::Status;
#include <grpc/grpc.h> #include <grpc/grpc.h>
#include <grpc++/server.h> #include <grpc++/server.h>
#include <grpc++/server_builder.h> #include <grpc++/server_builder.h>
...@@ -109,6 +94,9 @@ private: ...@@ -109,6 +94,9 @@ private:
std::unique_ptr<MATSimInterfaceService::Stub> _matsimChannel; std::unique_ptr<MATSimInterfaceService::Stub> _matsimChannel;
//map the matsim agent id to the jupedsim agent id //map the matsim agent id to the jupedsim agent id
std::map<int,std::string> _mapMatsimID2JPSID; std::map<int,std::string> _mapMatsimID2JPSID;
//map the pedestrian id, to a time which is set
// if the pedestrian could not be transfered to matsim
std::map <int,int>_counter;
}; };
#endif /* MATSIM_JPSCLIENT_H_ */ #endif /* MATSIM_JPSCLIENT_H_ */
...@@ -10,7 +10,6 @@ ...@@ -10,7 +10,6 @@
#include "../IO/OutputHandler.h" #include "../IO/OutputHandler.h"
#include "../pedestrian/AgentsSourcesManager.h" #include "../pedestrian/AgentsSourcesManager.h"
#include "../pedestrian/AgentsSource.h" #include "../pedestrian/AgentsSource.h"
#include "../pedestrian/AgentsQueue.h"
#include "../pedestrian/Pedestrian.h" #include "../pedestrian/Pedestrian.h"
#include "../Simulation.h" #include "../Simulation.h"
...@@ -19,10 +18,10 @@ ...@@ -19,10 +18,10 @@
//client stuff //client stuff
#include <grpc++/channel_arguments.h> #include <grpc++/channel_arguments.h>
//#include <grpc++/channel_interface.h>
//#include <grpc++/client_context.h>
#include <grpc++/create_channel.h> #include <grpc++/create_channel.h>
//#include <grpc++/credentials.h>
#include "../pedestrian/AgentsQueue.h"
// external variables // external variables
extern OutputHandler* Log; extern OutputHandler* Log;
...@@ -46,11 +45,9 @@ void JPSserver::RunSimulation() ...@@ -46,11 +45,9 @@ void JPSserver::RunSimulation()
{ {
if(_doSimulation) if(_doSimulation)
{ {
//Log->Write("INFO:\tRPC::JPSserver starting a new simulation");
_SimManager.RunBody(_maxSimTime); _SimManager.RunBody(_maxSimTime);
_doSimulation=false; _doSimulation=false;
_jpsClient->NotifyEndOfSimulation(); _jpsClient->NotifyEndOfSimulation();
//exit(0);
} }
//Log->Write("INFO:\tRPC::JPSserver idle for 3 seconds"); //Log->Write("INFO:\tRPC::JPSserver idle for 3 seconds");
...@@ -66,9 +63,7 @@ Status JPSserver::reqMATSim2ExternHasSpace(ServerContext* context __attribute__( ...@@ -66,9 +63,7 @@ Status JPSserver::reqMATSim2ExternHasSpace(ServerContext* context __attribute__(
{ {
string nodeID=request->nodeid(); string nodeID=request->nodeid();
//Log->Write("INFO:\tRPC::JPSserver I have space on node " + nodeID); //Log->Write("INFO:\tRPC::JPSserver I have space on node " + nodeID);
response->set_hasspace(true); response->set_hasspace(true);
return Status::OK; return Status::OK;
} }
...@@ -84,11 +79,9 @@ Status JPSserver::reqMATSim2ExternPutAgent(ServerContext* context __attribute__ ...@@ -84,11 +79,9 @@ Status JPSserver::reqMATSim2ExternPutAgent(ServerContext* context __attribute__
auto& agentSrcMng=_SimManager.GetAgentSrcManager(); auto& agentSrcMng=_SimManager.GetAgentSrcManager();
auto srcs=agentSrcMng.GetSources(); auto srcs=agentSrcMng.GetSources();
//cout<<"There are: "<<srcs.size()<<" options"<<endl;
for(auto&& src:srcs) for(auto&& src:srcs)
{ {
//cout<<"size: "<<src->GetPoolSize()<<endl;
if(src->GetId()==std::stoi(enter_node)) if(src->GetId()==std::stoi(enter_node))
{ {
std::vector<Pedestrian*> peds; std::vector<Pedestrian*> peds;
...@@ -104,7 +97,6 @@ Status JPSserver::reqMATSim2ExternPutAgent(ServerContext* context __attribute__ ...@@ -104,7 +97,6 @@ Status JPSserver::reqMATSim2ExternPutAgent(ServerContext* context __attribute__
src->AddToPool(ped); src->AddToPool(ped);
} }
agentSrcMng.ProcessAllSources(); agentSrcMng.ProcessAllSources();
//AgentsQueue::Add(peds);
} }
} }
......
...@@ -44,6 +44,7 @@ public: ...@@ -44,6 +44,7 @@ public:
*/ */
virtual ~JPSserver(); virtual ~JPSserver();
/// The stub for these functions are automatically generated from the grpc module
virtual Status reqMATSim2ExternHasSpace(ServerContext* context, const MATSim2ExternHasSpace* request, MATSim2ExternHasSpaceConfirmed* response); virtual Status reqMATSim2ExternHasSpace(ServerContext* context, const MATSim2ExternHasSpace* request, MATSim2ExternHasSpaceConfirmed* response);
virtual Status reqMATSim2ExternPutAgent(ServerContext* context, const MATSim2ExternPutAgent* request, MATSim2ExternPutAgentConfirmed* response); virtual Status reqMATSim2ExternPutAgent(ServerContext* context, const MATSim2ExternPutAgent* request, MATSim2ExternPutAgentConfirmed* response);
virtual Status reqExternDoSimStep(ServerContext* context, const ExternDoSimStep* request, ExternDoSimStepReceived* response); virtual Status reqExternDoSimStep(ServerContext* context, const ExternDoSimStep* request, ExternDoSimStepReceived* response);
......
...@@ -6,25 +6,26 @@ ...@@ -6,25 +6,26 @@
*/ */
#include "AgentsQueue.h" #include "AgentsQueue.h"
#include "Pedestrian.h" #include "Pedestrian.h"
using namespace std; using namespace std;
vector<Pedestrian*> AgentsQueue::_agentsQueue; vector<Pedestrian*> AgentsQueueIn::_agentsQueue;
vector<Pedestrian*> AgentsQueueOut::_agentsQueue; vector<Pedestrian*> AgentsQueueOut::_agentsQueue;
mutex AgentsQueue::_queueMutex; mutex AgentsQueueIn::_queueMutex;
mutex AgentsQueueOut::_queueMutex; mutex AgentsQueueOut::_queueMutex;
void AgentsQueue::Add(vector<Pedestrian*>& peds) void AgentsQueueIn::Add(vector<Pedestrian*>& peds)
{ {
_queueMutex.lock(); _queueMutex.lock();
_agentsQueue.insert(_agentsQueue.end(),peds.begin(),peds.end()); _agentsQueue.insert(_agentsQueue.end(),peds.begin(),peds.end());
_queueMutex.unlock(); _queueMutex.unlock();
} }
void AgentsQueue::GetandClear(std::vector<Pedestrian*>& peds) void AgentsQueueIn::GetandClear(std::vector<Pedestrian*>& peds)
{ {
_queueMutex.lock(); _queueMutex.lock();
...@@ -37,11 +38,15 @@ void AgentsQueue::GetandClear(std::vector<Pedestrian*>& peds) ...@@ -37,11 +38,15 @@ void AgentsQueue::GetandClear(std::vector<Pedestrian*>& peds)
_queueMutex.unlock(); _queueMutex.unlock();
} }
bool AgentsQueue::IsEmpty() bool AgentsQueueIn::IsEmpty()
{ {
return (_agentsQueue.size()==0); return (_agentsQueue.size()==0);
} }
int AgentsQueueIn::Size()
{
return _agentsQueue.size();
}
///////////////////////////////////////////////////// /////////////////////////////////////////////////////
//////////////////////////////////////////////////// ////////////////////////////////////////////////////
...@@ -51,11 +56,10 @@ void AgentsQueueOut::Add(vector<Pedestrian*>& peds) ...@@ -51,11 +56,10 @@ void AgentsQueueOut::Add(vector<Pedestrian*>& peds)
_queueMutex.lock(); _queueMutex.lock();
_agentsQueue.insert(_agentsQueue.end(),peds.begin(),peds.end()); _agentsQueue.insert(_agentsQueue.end(),peds.begin(),peds.end());
//todo: avoid this by using a map //todo: Can save time using a map
std::sort( _agentsQueue.begin(), _agentsQueue.end() ); std::sort( _agentsQueue.begin(), _agentsQueue.end() );
_agentsQueue.erase( unique( _agentsQueue.begin(), _agentsQueue.end() ), _agentsQueue.end() ); _agentsQueue.erase( std::unique( _agentsQueue.begin(), _agentsQueue.end() ), _agentsQueue.end() );
//std::cout<<"queue size:"<<_agentsQueue.size()<<endl;
///
_queueMutex.unlock(); _queueMutex.unlock();
} }
...@@ -83,3 +87,8 @@ bool AgentsQueueOut::IsEmpty() ...@@ -83,3 +87,8 @@ bool AgentsQueueOut::IsEmpty()
{ {
return (_agentsQueue.size()==0); return (_agentsQueue.size()==0);
} }
int AgentsQueueOut::Size()
{
return _agentsQueue.size();
}
...@@ -18,17 +18,18 @@ class Pedestrian; ...@@ -18,17 +18,18 @@ class Pedestrian;
/** /**
* Queue for incoming agents * Queue for incoming agents
*/ */
class AgentsQueue class AgentsQueueIn
{ {
public: public:
static void Add(std::vector<Pedestrian*>& ped); static void Add(std::vector<Pedestrian*>& ped);
static void GetandClear(std::vector<Pedestrian*>& peds); static void GetandClear(std::vector<Pedestrian*>& peds);
static bool IsEmpty(); static bool IsEmpty();
static int Size();
//static std::vector<Pedestrian*> GetandClear(); //static std::vector<Pedestrian*> GetandClear();
private: private:
AgentsQueue(){}; AgentsQueueIn(){};
virtual ~AgentsQueue(){}; virtual ~AgentsQueueIn(){};
static std::vector<Pedestrian*> _agentsQueue; static std::vector<Pedestrian*> _agentsQueue;
static std::mutex _queueMutex; static std::mutex _queueMutex;
}; };
...@@ -43,6 +44,7 @@ public: ...@@ -43,6 +44,7 @@ public:
static void Add(Pedestrian* ped); static void Add(Pedestrian* ped);
static void GetandClear(std::vector<Pedestrian*>& peds); static void GetandClear(std::vector<Pedestrian*>& peds);
static bool IsEmpty(); static bool IsEmpty();
static int Size();
private: private:
AgentsQueueOut(){}; AgentsQueueOut(){};
...@@ -50,4 +52,5 @@ private: ...@@ -50,4 +52,5 @@ private:
static std::vector<Pedestrian*> _agentsQueue; static std::vector<Pedestrian*> _agentsQueue;
static std::mutex _queueMutex; static std::mutex _queueMutex;
}; };
#endif /* AGENTSQUEUE_H_ */ #endif /* AGENTSQUEUE_H_ */
...@@ -7,7 +7,6 @@ ...@@ -7,7 +7,6 @@
#include "AgentsSourcesManager.h" #include "AgentsSourcesManager.h"
#include "Pedestrian.h" #include "Pedestrian.h"
#include "AgentsQueue.h"
#include "StartDistribution.h" #include "StartDistribution.h"
#include "PedDistributor.h" #include "PedDistributor.h"
#include "AgentsSource.h" #include "AgentsSource.h"
...@@ -20,6 +19,7 @@ ...@@ -20,6 +19,7 @@
#include <iostream> #include <iostream>
#include <thread> #include <thread>
#include <chrono> #include <chrono>
#include "AgentsQueue.h"
using namespace std; using namespace std;
...@@ -104,7 +104,7 @@ bool AgentsSourcesManager::ProcessAllSources() const ...@@ -104,7 +104,7 @@ bool AgentsSourcesManager::ProcessAllSources() const
//ComputeBestPositionVoronoi(src.get(), ped); //ComputeBestPositionVoronoi(src.get(), ped);
//ped->Dump(ped->GetID()); //ped->Dump(ped->GetID());
//} //}
AgentsQueue::Add(peds); AgentsQueueIn::Add(peds);
empty = false; empty = false;
} }
//src->Dump();//exit(0); //src->Dump();//exit(0);
......
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