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