Commit 35cd4dbf authored by Ulrich Kemloh's avatar Ulrich Kemloh

JuPedSim now keeps agents for a while if there is no place on the matsim

side. Proper shutdown of server at the end.
parent e310e9e2
......@@ -303,6 +303,11 @@ int Simulation::RunSimulation(double maxSimTime)
//time_t starttime, endtime;
//time(&starttime);
cout<<"nPeds: "<<_nPeds<<endl;
cout<<"manager: "<<_agentSrcManager.IsCompleted()<<endl;
cout<<"tmax: "<<maxSimTime<<endl;
cout<<"t: "<<t<<endl;
// main program loop
while ( (_nPeds || !_agentSrcManager.IsCompleted() ) && t < maxSimTime)
{
......@@ -311,6 +316,9 @@ int Simulation::RunSimulation(double maxSimTime)
//process the queue for incoming pedestrians
ProcessAgentsQueue();
//cout<<"nPeds: "<<_nPeds<<endl;
//cout<<"manager: "<<_agentSrcManager.IsCompleted()<<endl;
// update the positions
_operationalModel->ComputeNextTimeStep(t, _deltaT, _building.get());
......@@ -648,7 +656,7 @@ void Simulation::ProcessAgentsQueue()
}
}
const AgentsSourcesManager& Simulation::GetAgentSrcManager()
AgentsSourcesManager& Simulation::GetAgentSrcManager()
{
return _agentSrcManager;
}
......@@ -155,7 +155,7 @@ public:
/**
* @return the agents source manager
*/
const AgentsSourcesManager& GetAgentSrcManager();
AgentsSourcesManager& GetAgentSrcManager();
/**
* Check if any agents are waiting to enter the simulation
......
......@@ -69,7 +69,9 @@ int main(int argc, char **argv)
else
{
//Start the threads for managing the sources of agents if any
std::thread t1(sim.GetAgentSrcManager());
//std::thread t1(sim.GetAgentSrcManager());
std::thread t1(&AgentsSourcesManager::Run, &sim.GetAgentSrcManager());
//main thread for the simulation
Log->Write("INFO: \tStart runSimulation()");
......
......@@ -128,12 +128,6 @@ bool HybridSimulationManager::Run(Simulation& sim)
Log->Write("ERROR:\tNotification failed");
}
// Pedestrian ped;
// ped.SetID(14);
// ped.SetFinalDestination(4);
// _rpcClient->SendAgentToMatsim(&ped);
// _rpcClient->NotifyExternalService();
//starting the simulation thread and waiting
std::thread t2(&JPSserver::RunSimulation, &jupedsimService);
......@@ -280,9 +274,9 @@ std::string HybridSimulationManager::ToString()
+ std::to_string(_externalServerPort) + "]\n";
}
void HybridSimulationManager::AttachSourceManager(const AgentsSourcesManager& src)
{
//TODO create a deep copy of the manager
// and copy the sources without copying the generated pedestrian
_agentSrcMng=src;
}
//void HybridSimulationManager::AttachSourceManager(const AgentsSourcesManager& src)
//{
// //TODO create a deep copy of the manager
// // and copy the sources without copying the generated pedestrian
// _agentSrcMng=src;
//}
......@@ -51,26 +51,14 @@ void JPSclient::ProcessAgentQueue(Building* building)
}
*/
if(HasSpaceOnMatsim(ped->GetFinalDestination())==true)
//remove the pedestrian only if successfully sent
if(SendAgentToMatsim(ped)==true)
{
SendAgentToMatsim(ped);
building->DeletePedestrian(ped);
}
//std::cout<<"deleting from the client:"<<std::endl;
building->DeletePedestrian(ped);
}
}
bool JPSclient::HasSpaceOnMatsim(int nodeID)
{
//Status status =_matsimChannel->reqExtern2MATSim(&context, request, &reply);
//if(status.IsOk())
//{
//
//}
return (nodeID>0);
}
bool JPSclient::SendAgentToMatsim(Pedestrian* ped)
{
ClientContext context;
......@@ -86,7 +74,11 @@ bool JPSclient::SendAgentToMatsim(Pedestrian* ped)
Status status =_matsimChannel->reqExtern2MATSim(&context, request, &reply);
return status.IsOk();
if(status.IsOk())
{
return reply.accepted();
}
return false;
}
bool JPSclient::HasSpaceOnJuPedSim(int nodeID)
......
......@@ -100,7 +100,6 @@ public:
}
private:
bool HasSpaceOnMatsim(int nodeID);
bool HasSpaceOnJuPedSim(int nodeID);
bool SendAgentToJuPedSim(Pedestrian* ped);
......
......@@ -17,14 +17,12 @@
#include <iostream>
#include <thread>
//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>
// external variables
extern OutputHandler* Log;
......@@ -32,14 +30,12 @@ using namespace std;
JPSserver::JPSserver(Simulation& src): _SimManager(src)
{
//_rpcClient = std::unique_ptr<JPSclient>(new JPSclient( grpc::CreateChannel(connection,
// grpc::InsecureCredentials(), grpc::ChannelArguments())));
_doSimulation=false;
}
JPSserver::~JPSserver()
{
}
void JPSserver::RunSimulation()
......@@ -50,7 +46,7 @@ void JPSserver::RunSimulation()
{
if(_doSimulation)
{
Log->Write("INFO:\tRPC::JPSserver starting a new simulation");
//Log->Write("INFO:\tRPC::JPSserver starting a new simulation");
_SimManager.RunBody(_maxSimTime);
_doSimulation=false;
_jpsClient->NotifyEndOfSimulation();
......@@ -69,7 +65,7 @@ Status JPSserver::reqMATSim2ExternHasSpace(ServerContext* context __attribute__(
MATSim2ExternHasSpaceConfirmed* response)
{
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);
......@@ -84,9 +80,9 @@ Status JPSserver::reqMATSim2ExternPutAgent(ServerContext* context __attribute__
string agent_id=request->agent().id();
string leave_node=request->agent().nodes(0);//just the first node
string enter_node=request->agent().enternode();
Log->Write("INFO:\tRPC::JPSserver I am taking agent %s going to node %s ",agent_id.c_str(),leave_node.c_str());
//Log->Write("INFO:\tRPC::JPSserver I am taking agent %s going to node %s ",agent_id.c_str(),leave_node.c_str());
auto agentSrcMng=_SimManager.GetAgentSrcManager();
auto& agentSrcMng=_SimManager.GetAgentSrcManager();
auto srcs=agentSrcMng.GetSources();
//cout<<"There are: "<<srcs.size()<<" options"<<endl;
......@@ -100,8 +96,6 @@ Status JPSserver::reqMATSim2ExternPutAgent(ServerContext* context __attribute__
//there should be only one agent in this vector
for(auto&& ped:peds)
{
//ped->SetID(std::stoi(agent_id));
//_mapMatsimID2JPSID[agent_id]=ped->GetID();
//TODO: there might be a race condition here if the client is sending agents out
_jpsClient->MapMatsimAgentToJPSagent(ped->GetID(),agent_id);
ped->SetFinalDestination(std::stoi(leave_node));
......@@ -121,7 +115,7 @@ Status JPSserver::reqExternDoSimStep(ServerContext* context __attribute__((unuse
{
double from =request->fromtime();
double to=request->totime();
Log->Write("INFO:\tRPC::JPSserver I will perform a simulation step from %f to %f seconds",from,to);
Log->Write("INFO:\tRPC::JPSserver Simulation step from %.2f to %.2f seconds",from,to);
_doSimulation=true;
_maxSimTime=to;
return Status::OK;
......@@ -141,7 +135,7 @@ Status JPSserver::reqExternAfterSim(
const ExternAfterSim* request __attribute__((unused)),
ExternAfterSimConfirmed* response __attribute__((unused)))
{
Log->Write("INFO:\tRPC::JPSserver I received shutdown order. But can I do that ?");
Log->Write("INFO:\tRPC::JPSserver I received shutdown order. Good bye");
_shutdown=true;
return Status::OK;
}
......
......@@ -79,7 +79,7 @@ LCGrid::LCGrid(double boundaries[4], double cellsize, int nPeds)
LCGrid::~LCGrid()
{
for(int i=0; i<_nPeds; i++) {
if(!_localPedsCopy[i])
if(_localPedsCopy[i])
delete _localPedsCopy[i];
}
delete [] _list;
......
......@@ -20,9 +20,10 @@
using namespace std;
bool AgentsSourcesManager::_isCompleted=false;
AgentsSourcesManager::AgentsSourcesManager()
{
}
AgentsSourcesManager::~AgentsSourcesManager()
......@@ -32,7 +33,19 @@ AgentsSourcesManager::~AgentsSourcesManager()
void AgentsSourcesManager::operator()()
{
Log->Write("INFO:\tStarting agent manager thread");
//the loop is updated each second.
//Generate all agents required for the complete simulation
//It might be more efficient to generate at each frequency step
for (const auto& src : _sources)
{
src->GenerateAgentsAndAddToPool(src->GetMaxAgents(), _building);
cout<<"generation: "<<src->GetPoolSize()<<endl;
}
//first call ignoring the return value
ProcessAllSources();
//the loop is updated each x second.
//it might be better to use a timer
_isCompleted = false;
bool finished = false;
......@@ -41,6 +54,12 @@ void AgentsSourcesManager::operator()()
{
int current_time = Pedestrian::GetGlobalTime();
//first step
//if(current_time==0){
//finished=ProcessAllSources();
// ProcessAllSources();
// //cout<<"here:"<<endl; exit(0);
//}
if ((current_time != _lastUpdateTime)
&& ((current_time % updateFrequency) == 0))
{
......@@ -54,18 +73,68 @@ void AgentsSourcesManager::operator()()
//std::this_thread::sleep_for(std::chrono::milliseconds(1));
} while (!finished);
Log->Write("INFO:\tTerminating agent manager thread");
_isCompleted = true;
_isCompleted = true;exit(0);
}
bool AgentsSourcesManager::ProcessAllSources()
void AgentsSourcesManager::Run()
{
Log->Write("INFO:\tStarting agent manager thread");
//Generate all agents required for the complete simulation
//It might be more efficient to generate at each frequency step
for (const auto& src : _sources)
{
src->GenerateAgentsAndAddToPool(src->GetMaxAgents(), _building);
cout<<"generation: "<<src->GetPoolSize()<<endl;
}
//first call ignoring the return value
ProcessAllSources();
//the loop is updated each x second.
//it might be better to use a timer
_isCompleted = false;
bool finished = false;
long updateFrequency = 5; // 1 second
do
{
int current_time = Pedestrian::GetGlobalTime();
//first step
//if(current_time==0){
//finished=ProcessAllSources();
// ProcessAllSources();
// //cout<<"here:"<<endl; exit(0);
//}
if ((current_time != _lastUpdateTime)
&& ((current_time % updateFrequency) == 0))
{
//cout<<"TIME:"<<current_time<<endl;
finished=ProcessAllSources();
_lastUpdateTime = current_time;
cout << "source size: " << _sources.size() << endl;
}
//wait some time
//cout<<"sleepinp..."<<endl;
//std::this_thread::sleep_for(std::chrono::milliseconds(1));
} while (!finished);
Log->Write("INFO:\tTerminating agent manager thread");
_isCompleted = true;exit(0);
}
bool AgentsSourcesManager::ProcessAllSources() const
{
bool empty=true;
//cout<<"src size: "<<_sources.size()<<endl;
for (const auto& src : _sources)
{
//cout<<"size: "<<src->GetPoolSize()<<endl;//exit(0);
if (src->GetPoolSize())
{
vector<Pedestrian*> peds;
src->RemoveAgentsFromPool(peds,src->GetFrequency());
//cout<<"removing: "<<peds.size()<<" agents from the sources"<<endl;//getc(stdin);exit(0);
ComputeBestPositionRandom(src.get(), peds);
// compute the optimal position for insertion
for (auto&& ped : peds)
......@@ -201,7 +270,7 @@ void AgentsSourcesManager::ComputeBestPositionVoronoi(AgentsSource* src,
}
void AgentsSourcesManager::ComputeBestPositionRandom(AgentsSource* src,
std::vector<Pedestrian*>& peds)
std::vector<Pedestrian*>& peds) const
{
//generate the agents with default positions
......@@ -268,6 +337,17 @@ void AgentsSourcesManager::ComputeBestPositionRandom(AgentsSource* src,
}
}
void AgentsSourcesManager::GenerateAgents()
{
for (const auto& src : _sources)
{
src->GenerateAgentsAndAddToPool(src->GetMaxAgents(), _building);
}
}
void AgentsSourcesManager::AddSource(std::shared_ptr<AgentsSource> src)
{
_sources.push_back(src);
......
......@@ -10,6 +10,7 @@
#include <vector>
#include <memory>
#include <atomic>
//Forward declarations
class AgentsSource;
......@@ -24,6 +25,9 @@ public:
*/
AgentsSourcesManager();
///disable copying
AgentsSourcesManager(const AgentsSourcesManager& ) = delete;
/**
* Destructor
*/
......@@ -35,6 +39,8 @@ public:
*/
void operator()();
void Run();
/**
* Add a new agent source
*/
......@@ -62,10 +68,16 @@ public:
Building* GetBuilding() const;
/**
*
*Schedule the pedestrians for the simulation
* @return true if all source are empty
*/
bool ProcessAllSources();
bool ProcessAllSources() const;
/**
* Trigger the sources to generate the specified
* number of agents for this frequency
*/
void GenerateAgents();
private:
/// contain the sources
......@@ -75,11 +87,12 @@ private:
/// building object
Building* _building=nullptr;
/// whether all agents have been dispatched
bool _isCompleted=true;
static bool _isCompleted;
//std::atomic<bool>_isCompleted=false;
private:
void ComputeBestPositionVoronoi(AgentsSource* src, Pedestrian* agent);
void ComputeBestPositionRandom(AgentsSource* src, std::vector<Pedestrian*>& peds);
void ComputeBestPositionRandom(AgentsSource* src, std::vector<Pedestrian*>& peds) const;
};
#endif /* AGENTSSOURCESMANAGER_H_ */
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