Skip to content

Commit ff12ac6

Browse files
Merge pull request #35 from ci-group/revolve_bot
RevolveBot
2 parents a3403e6 + 5b80907 commit ff12ac6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

95 files changed

+4177
-3128
lines changed

Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ RUN apt-get install -y build-essential \
1515
python \
1616
python3-pip \
1717
libyaml-cpp-dev \
18-
xsltproc
18+
xsltproc \
19+
libcairo2-dev
1920
RUN apt-get install -y libgazebo9-dev gazebo9
2021
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
2122

cpprevolve/revolve/gazebo/brains/NeuralNetwork.cpp

Lines changed: 62 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -86,18 +86,22 @@ NeuralNetwork::NeuralNetwork(
8686
// motors, creating the adequate neurons in place as we do so.
8787

8888
// Map of ID to neuron element
89+
// neuron.id ---> sdf_element
8990
std::map< std::string, sdf::ElementPtr > neuronMap;
91+
std::map< std::string, std::vector<sdf::ElementPtr> > neuronPartIdMap;
9092

91-
// List of all hidden neurons for convenience
93+
// List of all hidden neurons (ids) for convenience
9294
std::vector< std::string > hiddenNeurons;
9395

94-
// Set for tracking all collected input/output neurons
96+
// Set for tracking all collected input/output neurons (ids)
9597
std::set< std::string > toProcess;
9698

99+
auto controller_settings = _settings->GetElement("rv:controller");
100+
97101
// Fetch the first neuron; note the HasElement call is necessary to
98102
// prevent SDF from complaining if no neurons are present.
99-
auto neuron = _settings->HasElement("rv:neuron")
100-
? _settings->GetElement("rv:neuron")
103+
auto neuron = controller_settings->HasElement("rv:neuron")
104+
? controller_settings->GetElement("rv:neuron")
101105
: sdf::ElementPtr();
102106
while (neuron)
103107
{
@@ -109,8 +113,9 @@ NeuralNetwork::NeuralNetwork(
109113
}
110114
auto layer = neuron->GetAttribute("layer")->GetAsString();
111115
auto neuronId = neuron->GetAttribute("id")->GetAsString();
116+
auto neuronPartId = neuron->GetAttribute("part_id")->GetAsString();
112117

113-
if (this->layerMap_.count(neuronId))
118+
if (this->layerMap_.count(neuronId) == 1)
114119
{
115120
std::cerr << "Duplicate neuron ID '" << neuronId << "'" << std::endl;
116121
throw std::runtime_error("Robot brain error");
@@ -119,6 +124,12 @@ NeuralNetwork::NeuralNetwork(
119124
this->layerMap_[neuronId] = layer;
120125
neuronMap[neuronId] = neuron;
121126

127+
if (neuronPartIdMap.find(neuronPartId) == neuronPartIdMap.end()) {
128+
neuronPartIdMap[neuronPartId] = std::vector<sdf::ElementPtr>();
129+
}
130+
neuronPartIdMap[neuronPartId].push_back(neuron);
131+
132+
// INPUT LAYER
122133
if ("input" == layer)
123134
{
124135
if (this->nInputs_ >= MAX_INPUT_NEURONS)
@@ -132,6 +143,7 @@ NeuralNetwork::NeuralNetwork(
132143
toProcess.insert(neuronId);
133144
++(this->nInputs_);
134145
}
146+
// OUTPUT LAYER
135147
else if ("output" == layer)
136148
{
137149
if (this->nOutputs_ >= MAX_OUTPUT_NEURONS)
@@ -145,6 +157,7 @@ NeuralNetwork::NeuralNetwork(
145157
toProcess.insert(neuronId);
146158
++(this->nOutputs_);
147159
}
160+
// HIDDEN LAYER
148161
else if ("hidden" == layer)
149162
{
150163
if (hiddenNeurons.size() >= MAX_HIDDEN_NEURONS)
@@ -157,7 +170,7 @@ NeuralNetwork::NeuralNetwork(
157170
}
158171

159172
hiddenNeurons.push_back(neuronId);
160-
++(this->nHidden_);
173+
++(this->nHidden_);
161174
}
162175
else
163176
{
@@ -171,70 +184,80 @@ NeuralNetwork::NeuralNetwork(
171184
// Create motor output neurons at the correct position
172185
// We iterate a part's motors and just assign every
173186
// neuron we find in order.
174-
std::map< std::string, unsigned int > outputCountMap;
175187
unsigned int outputsIndex = 0;
176188
for (const auto &motor : _motors)
177189
{
178-
auto partId = motor->PartId();
179-
if (not outputCountMap.count(partId))
190+
std::string partId = motor->PartId();
191+
auto details = neuronPartIdMap.find(partId);
192+
if (details == neuronPartIdMap.end())
180193
{
181-
outputCountMap[partId] = 0;
194+
std::cerr << "Required output neuron " << partId
195+
<< " for motor could not be located" << std::endl;
196+
throw std::runtime_error("Robot brain error");
182197
}
183198

199+
const auto &neuron_list = details->second;
200+
auto neuron_iter = neuron_list.cbegin();
201+
184202
for (unsigned int i = 0, l = motor->Outputs(); i < l; ++i)
185203
{
186-
std::stringstream neuronId;
187-
neuronId << partId << "-out-" << outputCountMap[partId];
188-
++outputCountMap[partId];
189-
190-
auto details = neuronMap.find(neuronId.str());
191-
if (details == neuronMap.end())
192-
{
193-
std::cerr << "Required output neuron " << neuronId.str()
194-
<< " for motor could not be located" << std::endl;
195-
throw std::runtime_error("Robot brain error");
196-
}
204+
while (not ((*neuron_iter)->GetAttribute("layer")->GetAsString() == "output"))
205+
{
206+
++neuron_iter;
207+
if (neuron_iter == neuron_list.cend())
208+
{
209+
std::cerr << "Required input neuron " << partId
210+
<< " for sensor could not be located" << std::endl;
211+
throw std::runtime_error("Robot brain error");
212+
}
213+
}
214+
std::string neuronId = (*neuron_iter)->GetAttribute("id")->GetAsString();
197215

198216
neuronHelper(&this->params_[outputsIndex * MAX_NEURON_PARAMS],
199217
&this->types_[outputsIndex],
200-
details->second);
201-
this->positionMap_[neuronId.str()] = outputsIndex;
202-
toProcess.erase(neuronId.str());
218+
*neuron_iter);
219+
this->positionMap_[neuronId] = outputsIndex;
220+
toProcess.erase(neuronId);
203221
++outputsIndex;
222+
++neuron_iter;
204223
}
205224
}
206225

207226
// Create sensor input neurons
208-
std::map< std::string, unsigned int > inputCountMap;
209227
unsigned int inputsIndex = 0;
210228
for (const auto &sensor : _sensors)
211229
{
212230
auto partId = sensor->PartId();
213-
214-
if (not inputCountMap.count(partId))
231+
auto details = neuronPartIdMap.find(partId);
232+
if (details == neuronPartIdMap.end())
215233
{
216-
inputCountMap[partId] = 0;
234+
std::cerr << "Required input neuron list " << partId
235+
<< " for sensor could not be located" << std::endl;
236+
throw std::runtime_error("Robot brain error");
217237
}
238+
const auto &neuron_list = details->second;
239+
auto neuron_iter = neuron_list.cbegin();
218240

219241
for (unsigned int i = 0, l = sensor->Inputs(); i < l; ++i)
220242
{
221-
std::stringstream neuronId;
222-
neuronId << partId << "-in-" << inputCountMap[partId];
223-
++inputCountMap[partId];
224-
225-
auto details = neuronMap.find(neuronId.str());
226-
if (details == neuronMap.end())
243+
while (not ((*neuron_iter)->GetAttribute("layer")->GetAsString() == "input"))
227244
{
228-
std::cerr << "Required input neuron " << neuronId.str()
229-
<< " for sensor could not be located" << std::endl;
230-
throw std::runtime_error("Robot brain error");
245+
++neuron_iter;
246+
if (neuron_iter == neuron_list.cend())
247+
{
248+
std::cerr << "Required input neuron " << partId
249+
<< " for sensor could not be located" << std::endl;
250+
throw std::runtime_error("Robot brain error");
251+
}
231252
}
253+
std::string neuronId = (*neuron_iter)->GetAttribute("id")->GetAsString();
232254

233255
// Input neurons can currently not have a type, so
234256
// there is no need to process it.
235-
this->positionMap_[neuronId.str()] = inputsIndex;
236-
toProcess.erase(neuronId.str());
257+
this->positionMap_[neuronId] = inputsIndex;
258+
toProcess.erase(neuronId);
237259
++inputsIndex;
260+
++neuron_iter;
238261
}
239262
}
240263

cpprevolve/revolve/gazebo/brains/RLPower.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <random>
2525
#include <string>
2626
#include <vector>
27+
#include <algorithm>
2728

2829
#include <gsl/gsl_spline.h>
2930

@@ -64,7 +65,10 @@ RLPower::RLPower(
6465
this->tau_ = 0.2;
6566
this->sourceYSize_ = 3;
6667

67-
this->stepRate_ = this->numInterpolationPoints_ / this->sourceYSize_;
68+
this->stepRate_ = std::max(
69+
static_cast<size_t>(1),
70+
this->numInterpolationPoints_ / this->sourceYSize_);
71+
6872

6973
// Generate first random policy
7074
auto numMotors = _motors.size();
@@ -404,7 +408,9 @@ void RLPower::IncreaseSplinePoints(const size_t _numSplines)
404408
this->sourceYSize_++;
405409

406410
// LOG code
407-
this->stepRate_ = this->numInterpolationPoints_ / this->sourceYSize_;
411+
this->stepRate_ = std::max(
412+
static_cast<size_t>(1),
413+
this->numInterpolationPoints_ / this->sourceYSize_);
408414

409415
// Copy current policy for resizing
410416
Policy policy_copy(this->currentPolicy_->size());

cpprevolve/revolve/gazebo/motors/MotorFactory.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ namespace gz = gazebo;
2727
using namespace revolve::gazebo;
2828

2929
/////////////////////////////////////////////////
30-
MotorFactory::MotorFactory(::gazebo::physics::ModelPtr _model)
31-
: model_(_model)
30+
MotorFactory::MotorFactory(::gazebo::physics::ModelPtr model)
31+
: model_(std::move(model))
3232
{
3333
}
3434

@@ -39,17 +39,17 @@ MotorFactory::~MotorFactory() = default;
3939
MotorPtr MotorFactory::Motor(
4040
sdf::ElementPtr _motorSdf,
4141
const std::string &_type,
42-
const std::string &_motorId,
43-
const std::string &_partId)
42+
const std::string &_partId,
43+
const std::string &_motorId)
4444
{
4545
MotorPtr motor;
4646
if ("position" == _type)
4747
{
48-
motor.reset(new PositionMotor(model_, _motorId, _partId, _motorSdf));
48+
motor.reset(new PositionMotor(this->model_, _partId, _motorId, _motorSdf));
4949
}
5050
else if ("velocity" == _type)
5151
{
52-
motor.reset(new VelocityMotor(model_, _motorId, _partId, _motorSdf));
52+
motor.reset(new VelocityMotor(this->model_, _partId, _motorId, _motorSdf));
5353
}
5454

5555
return motor;
@@ -60,6 +60,7 @@ MotorPtr MotorFactory::Create(sdf::ElementPtr _motorSdf)
6060
{
6161
auto typeParam = _motorSdf->GetAttribute("type");
6262
auto partIdParam = _motorSdf->GetAttribute("part_id");
63+
// auto partNameParam = _motorSdf->GetAttribute("part_name");
6364
auto idParam = _motorSdf->GetAttribute("id");
6465

6566
if (not typeParam or not partIdParam or not idParam)
@@ -69,6 +70,7 @@ MotorPtr MotorFactory::Create(sdf::ElementPtr _motorSdf)
6970
throw std::runtime_error("Motor error");
7071
}
7172

73+
// auto partName = partNameParam->GetAsString();
7274
auto partId = partIdParam->GetAsString();
7375
auto type = typeParam->GetAsString();
7476
auto id = idParam->GetAsString();

cpprevolve/revolve/gazebo/motors/MotorFactory.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ namespace revolve
3838
/// \brief[in] _partId Module identifier
3939
/// \brief[in] _motorId Motor identifier
4040
/// \brief[in] _outputs Number of motor outputs
41-
public: explicit MotorFactory(::gazebo::physics::ModelPtr _model);
41+
public: explicit MotorFactory(::gazebo::physics::ModelPtr model);
4242

4343
/// \brief Destructor
4444
public: virtual ~MotorFactory();
@@ -48,10 +48,10 @@ namespace revolve
4848
/// required attributes already checked, usually you should override
4949
/// this when adding new motor types.
5050
public: virtual MotorPtr Motor(
51-
sdf::ElementPtr _motorSdf,
52-
const std::string &_type,
53-
const std::string &_motorId,
54-
const std::string &_partId);
51+
sdf::ElementPtr _motorSdf,
52+
const std::string &_type,
53+
const std::string &_partId,
54+
const std::string &_motorId);
5555

5656
/// \brief Creates a motor for the given model for the given SDF element.
5757
public: virtual MotorPtr Create(sdf::ElementPtr _motorSdf);

cpprevolve/revolve/gazebo/motors/PositionMotor.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ PositionMotor::PositionMotor(
3030
gz::physics::ModelPtr _model,
3131
const std::string &_partId,
3232
const std::string &_motorId,
33-
sdf::ElementPtr _motor)
34-
: JointMotor(_model, _partId, _motorId, _motor, 1)
33+
const sdf::ElementPtr _motor)
34+
: JointMotor(std::move(_model), _partId, _motorId, _motor, 1)
3535
, positionTarget_(0)
3636
, noise_(0)
3737
{

cpprevolve/revolve/gazebo/motors/PositionMotor.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,15 @@ namespace revolve
4444
::gazebo::physics::ModelPtr _model,
4545
const std::string &_partId,
4646
const std::string &_motorId,
47-
sdf::ElementPtr _motor);
47+
const sdf::ElementPtr _motor);
4848

4949
/// \brief Destructor
50-
public: virtual ~PositionMotor();
50+
public: virtual ~PositionMotor() override;
5151

5252
/// \brief
5353
public: virtual void Update(
5454
double *_outputs,
55-
double _step);
55+
double _step) override;
5656

5757
/// \brief World update event function
5858
// protected: void OnUpdate(const ::gazebo::common::UpdateInfo info);

0 commit comments

Comments
 (0)