Une MAJ de sécurité est nécessaire sur notre version actuelle. Elle sera effectuée lundi 02/08 entre 12h30 et 13h. L'interruption de service devrait durer quelques minutes (probablement moins de 5 minutes).

Commit 768ec72c authored by LOPEZ GANDIA Axel's avatar LOPEZ GANDIA Axel
Browse files

Meged with xmlparser. Agents should only be spawned by the world.

parent 4c87766f
......@@ -28,8 +28,8 @@ set (CMAKE_CXX_STANDARD 14)
#set(EXECUTABLE_OUTPUT_PATH bin/${CMAKE_BUILD_TYPE})
set(Boost_USE_STATIC_LIBS ON)
find_package(Boost COMPONENTS system filesystem regex REQUIRED)
#set(Boost_USE_STATIC_LIBS ON)
#find_package(Boost COMPONENTS system filesystem regex REQUIRED)
include_directories( ./include )
include_directories(./3rd-party/tinyxml/)
......
/* Crowd Simulator Engine
** Copyright (C) 2018 - Inria Rennes - Rainbow - Julien Pettre
**
** This program is free software; you can redistribute it and/or
** modify it under the terms of the GNU General Public License
** as published by the Free Software Foundation; either version 2
** of the License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**
**
**
** Authors: Axel Lopez Gandia, Javad Amirian, Florian Berton,
** Julien Legros, Lucas Pelerin, Beatriz Cabrero Daniel, Fabien Grzeskowiak
**
** Contact: crowd_group@inria.fr
*/
#ifndef LIB_GOAL_REACHING_H
#define LIB_GOAL_REACHING_H
#include <core/costFunction.h>
/*
* Performs goal reaching at the desired speed of the agent.
* CF = ADS*(GP - AP) · AV
* CF: Cost function, ADS: Agent Desired Speed, AP: Agent Position, GP: Goal Position, AV: Agent current Velocity.
*/
class GoalReaching : public CostFunction
{
private:
public:
GoalReaching();
/**
* Computes the value and gradient of the cost function.
* Return the values to the policy to update the agent.
*/
virtual CostFunctionValues GetCostFunctionGradient(Agent* agent, World * world);
virtual ~GoalReaching();
};
#endif //LIB_GOAL_REACHING_H
......@@ -44,15 +44,20 @@ class Agent {
Vector2D goal_;
float radius_;
float prefered_speed_;
float max_speed_;
float prefered_speed_ = 2;
float max_speed_ = 10;
std::shared_ptr<Policy> policy_;
int id_;
std::string color_;
public:
//Private constructor only the world should create agents
Agent();
friend World;
friend std::unique_ptr<Agent> std::make_unique<Agent>(); //Spawn agents through this function in the world class
void setID(int id); //the world sets the id
public:
/**
* Ask policy for new state and update next_velocity_ accordingly
......@@ -74,7 +79,6 @@ class Agent {
inline float getRadius() const { return radius_; }
void setRadius(float radius) {radius_ = radius;}
void setID(int id);
int getID() { return id_; }
void setPolicy(std::shared_ptr<Policy> &policy);
......
......@@ -36,7 +36,7 @@ class Agent;
class Policy {
private:
std::vector<std::shared_ptr<CostFunction>> cost_functions_;
std::vector<std::shared_ptr<CostFunction> > cost_functions_;
std::vector<double> parameters_;
public:
......@@ -45,7 +45,7 @@ class Policy {
void addCostFunction(const std::shared_ptr<CostFunction>& costFunction, float weight = 1);
std::vector<double> getParameters() const;
//std::vector<CostFunction *> getCostFunctions() const;
std::vector<std::shared_ptr<CostFunction> > & getCostFunctions();
int getCostFunctionCount() const;
};
......
......@@ -93,9 +93,8 @@ class World
float getTime() const;
/**
*
* Creates an Agent. Agents should only be created through this function.
*/
//void AddAgent(Agent * newagent);
Agent* createAgent();
};
......
......@@ -39,21 +39,18 @@
#include <string>
#include <iostream>
#include <vector>
class Agent;
class Obstacle;
#include "core/world.h"
class XMLParser
{
public:
XMLParser();
void load(const std::string &filename);
void save(const std::string &filename);
void load(const std::string &filename, World * world);
void save(const std::string &filename, World * world);
std::vector<Agent*> agents;
std::vector<Obstacle*> obstacles;
//std::vector<Agent*> agents;
//std::vector<Obstacle*> obstacles;
};
......
......@@ -36,8 +36,8 @@ CrowdSimulator::CrowdSimulator()
void CrowdSimulator::loadInitFile(const std::string &filename)
{
XMLParser parser;
parser.load(filename);
//world_->setAgents(parser.agents);
parser.load(filename, world_.get());
//world_->setAgents(parser.agents);
//world_->setObstacles(parser.obstacles);
}
......
......@@ -31,10 +31,10 @@ std::vector<double> Policy::getParameters() const
return parameters_;
}
//std::vector<CostFunction *> Policy::getCostFunctions() const
//{
// return cost_functions_;
//}
std::vector<std::shared_ptr<CostFunction> > & Policy::getCostFunctions()
{
return cost_functions_;
}
int Policy::getCostFunctionCount() const
{
......
......@@ -45,12 +45,8 @@ XMLParser::XMLParser()
{
}
void XMLParser::load(const std::string &filename)
void XMLParser::load(const std::string &filename, World * world)
{
agents.clear();
obstacles.clear();
// Parse the XML into the property tree.
// If the path cannot be resolved, an exception is thrown.
tinyxml2::XMLDocument doc;
......@@ -72,14 +68,14 @@ void XMLParser::load(const std::string &filename)
tinyxml2::XMLElement* agentElement = agentHandle.ToElement();
do
{
Agent *agent_data = new Agent();
Agent *agent_data = world->createAgent();
//id and radius
int id;
float radius;
agentElement->QueryIntAttribute("id", &id);
agentElement->QueryFloatAttribute("rad", &radius);
agent_data->setID(id);
//agent_data->setID(id); //Automatically set
agent_data->setRadius(radius);
//position
......@@ -120,7 +116,7 @@ void XMLParser::load(const std::string &filename)
return;
}
Policy* pl = new Policy;
auto pl = std::make_shared<Policy>();
do
{
......@@ -130,11 +126,11 @@ void XMLParser::load(const std::string &filename)
funcElement->QueryFloatAttribute("coeff", &coeff);
if (costFunctionName == SocialForces().getName())
{
pl->addCostFunction(new SocialForces, coeff);
pl->addCostFunction(std::make_shared<SocialForces>(), coeff);
}
else if (costFunctionName == GoalReaching().getName())
{
pl->addCostFunction(new GoalReaching, coeff);
pl->addCostFunction(std::make_shared<GoalReaching>(), coeff);
}
else {
}
......@@ -143,19 +139,14 @@ void XMLParser::load(const std::string &filename)
agent_data->setPolicy(pl);
agents.push_back(agent_data);
++agentIndex;
} while ((agentElement = agentElement->NextSiblingElement())!=NULL);
}
void XMLParser::save(const std::string &filename)
void XMLParser::save(const std::string &filename, World* world)
{
FILE * xmlFile;
xmlFile = fopen(filename.data(), "w");
......@@ -167,7 +158,7 @@ void XMLParser::save(const std::string &filename)
tinyxml2::XMLPrinter printer(xmlFile);
printer.OpenElement("World");
printer.OpenElement("Agents");
for (Agent* agent : agents)
for (Agent* agent : world->getAgents())
{
//id and radius
printer.OpenElement("Agent");
......
/* Crowd Simulator Engine
** Copyright (C) 2018 - Inria Rennes - Rainbow - Julien Pettre
**
** This program is free software; you can redistribute it and/or
** modify it under the terms of the GNU General Public License
** as published by the Free Software Foundation; either version 2
** of the License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**
**
**
** Authors: Axel Lopez Gandia, Javad Amirian, Florian Berton,
** Julien Legros, Lucas Pelerin, Beatriz Cabrero Daniel, Fabien Grzeskowiak
**
** Contact: crowd_group@inria.fr
*/
#include <vector>
#include <iostream>
#include <core/crowdSimulator.h>
#include <core/agent.h>
#include <core/costFunction.h>
#include <core/world.h>
#include <tools/vector2D.h>
#include <CostFunctions/goalReaching.h>
#include <CostFunctions/socialforces.h>
#include <iostream>
#include <fstream>
using namespace std;
int main( int argc, char * argv[] ){
CrowdSimulator crowdSimulator;
ofstream outfile1;
outfile1.open("TestOut0.csv");
ofstream outfile2;
outfile2.open("TestOut1.csv");
Agent * agent1 = new Agent();
Agent * agent2 = new Agent();
Policy * policy = new Policy();
SocialForces * socialforces = new SocialForces();
Vector2D goalPos(0, 10);
policy->addCostFunction(socialforces, 0.6);
agent1->setPolicy(policy);
agent1->setPosition(Vector2D(0, 0));
agent1->setVelocity(Vector2D(0, 0));
agent1->setPreferdSpeed(2);
agent1->setRadius(1);
agent1->setGoal(goalPos);
agent2->setPolicy(policy);
agent2->setPosition(Vector2D(-1, 0));
agent2->setVelocity(Vector2D(0, 0));
agent2->setPreferdSpeed(2);
agent1->setRadius(1);
agent2->setGoal(goalPos);
crowdSimulator.addAgentToSimulation(agent1);
crowdSimulator.addAgentToSimulation(agent2);
float dt = 0.033;
float time = 0;
int iter = 0;
int MAXITERATIONS = 10000;
while ((agent1->getPosition() - goalPos).magnitude() > 5 && iter < MAXITERATIONS)
{
crowdSimulator.stepWorld(dt);
time += dt;
crowdSimulator.AppendPosInOutput(outfile1, time, agent1);
crowdSimulator.AppendPosInOutput(outfile2, time, agent2);
iter++;
}
outfile1.close();
outfile2.close();
system("pause");
}
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