Commit 3c9328a6 authored by NICAS MIQUEL Alicia's avatar NICAS MIQUEL Alicia

Complete XML with agent speeds and simulation parameters

parent 7e2f77e0
......@@ -44,7 +44,7 @@ class Agent {
Vector2D goal_;
float radius_;
float prefered_speed_ = 2;
float preferred_speed_ = 2;
float max_speed_ = 10;
std::shared_ptr<Policy> policy_;
......@@ -69,8 +69,10 @@ class Agent {
void setPosition(float px, float py);
Vector2D getVelocity() const;
void setVelocity(const Vector2D &velocity);
float getPreferdSpeed() { return prefered_speed_; };
void setPreferdSpeed(float speed);
float getPreferdSpeed() { return preferred_speed_; };
void setPreferredSpeed(float speed);
float getMaximumSpeed() { return max_speed_; };
void setMaximumSpeed(float speed);
Vector2D getGoal() const;
void setGoal(const Vector2D &goal);
void setGoal(float gx, float gy);
......
......@@ -47,6 +47,13 @@ public:
*/
void loadInitFile(const std::string& filename);
/**
* Loads the world from a configuration file
* and runs the simulation
*/
void runConfigFile(const std::string &filename);
/**
* Writes the result of the simulation to disk
*/
......
......@@ -48,6 +48,10 @@ class World
std::vector<std::unique_ptr<Agent> > agents_;
std::vector<std::unique_ptr<Obstacle> > obstacles_;
int iterations_;
float delta_time_;
float time_;
/*lqInternalDB2D *lqDB;
......@@ -92,6 +96,11 @@ class World
std::vector<Obstacle *> getObstacles() const;
float getTime() const;
int getIterations() const;
float getDeltaTime() const;
void setDeltaTime(float delta_time);
void setIterations(int iterations);
/**
* Creates an Agent. Agents should only be created through this function.
*/
......
......@@ -47,8 +47,6 @@ public:
XMLParser();
void load(const std::string &filename, World * world);
void save(const std::string &filename, World * world);
//std::vector<Agent*> agents;
//std::vector<Obstacle*> obstacles;
......
......@@ -36,10 +36,10 @@ using namespace std;
int main( int argc, char * argv[] )
{
CrowdSimulator cs;
cs.loadInitFile("test2.xml");
cs.setOutputDir("./output3/");
cs.runWorld(1000, 0.0333);
cs.setOutputDir("./output3/");
cs.runConfigFile("config.xml");
//cs.runWorld(1000, 0.0333);
std::cout << "---------------" << std::endl;
......
......@@ -88,10 +88,16 @@ void Agent::setNextVelocity(const Vector2D &nextVelocity)
next_velocity_ = nextVelocity;
}
void Agent::setPreferdSpeed(float speed) {
prefered_speed_ = speed;
void Agent::setPreferredSpeed(float speed) {
preferred_speed_ = speed;
}
void Agent::setMaximumSpeed(float speed) {
max_speed_ = speed;
}
void Agent::setID(int id) {
id_ = id;
}
......
......@@ -41,6 +41,18 @@ void CrowdSimulator::loadInitFile(const std::string &filename)
//world_->setObstacles(parser.obstacles);
}
void CrowdSimulator::runConfigFile(const std::string &filename)
{
XMLParser parser;
parser.load(filename, world_.get());
//world_->setAgents(parser.agents);
//world_->setObstacles(parser.obstacles);
runWorld(world_->getIterations(), world_->getDeltaTime());
}
void CrowdSimulator::setOutputDir(const std::string &dirname)
{
writer->setDirectory(dirname);
......
......@@ -167,6 +167,30 @@ float World::getTime() const
return time_;
}
float World::getDeltaTime() const
{
return delta_time_;
}
void World::setDeltaTime(float delta_time)
{
delta_time_ = delta_time;
}
void World::setIterations(int iterations)
{
iterations_ = iterations;
}
int World::getIterations() const
{
return iterations_;
}
Agent* World::createAgent() {
agents_.push_back(std::make_unique<Agent>());
Agent* agent = agents_.back().get();
......
......@@ -61,6 +61,24 @@ void XMLParser::load(const std::string &filename, World * world)
tinyxml2::XMLHandle docHandle(&doc);
//read the simulation parameters
tinyxml2::XMLHandle simulationHandle = docHandle.FirstChildElement("Simulation");
if (tinyxml2::XMLElement* simulationElement = simulationHandle.ToElement()) {
int iterations;
simulationElement->QueryIntAttribute("iterations", &iterations);
world->setIterations(iterations);
float delta_time;
simulationElement->QueryFloatAttribute("delta_time", &delta_time);
world->setDeltaTime(delta_time);
}
else {
std::cerr << "Warning: No simulation parameters in the XML file" << std::endl;
}
//start by parsing the policies, because the agents reference them
tinyxml2::XMLHandle policyHandle = docHandle.FirstChildElement("World").FirstChildElement("Policies").FirstChildElement("Policy");
if (!policyHandle.ToElement()) {
......@@ -132,15 +150,25 @@ void XMLParser::load(const std::string &filename, World * world)
tinyxml2::XMLElement* agentElement = agentHandle.ToElement();
do
{
Agent *agent_data = world->createAgent();
//id and radius
int id;
float radius;
float value;
agentElement->QueryIntAttribute("id", &id);
agentElement->QueryFloatAttribute("rad", &radius);
//agent_data->setID(id); //Automatically set
agent_data->setRadius(radius);
//agent_data->setID(id); //Automatically set
agentElement->QueryFloatAttribute("rad", &value);
agent_data->setRadius(value);
agentElement->QueryFloatAttribute("pref_speed", &value);
agent_data->setPreferredSpeed(value);
agentElement->QueryFloatAttribute("max_speed", &value);
agent_data->setMaximumSpeed(value);
//position
tinyxml2::XMLElement* positionElement = agentElement->FirstChildElement("pos");
......
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