scds_pso.cpp
Go to the documentation of this file.
1 
23 #include <boost/function.hpp>
24 #include "micros_swarm/scds_pso.h"
25 
26 namespace micros_swarm {
27  Agent::Agent():name_(""), run_(false), dim_(0), fitness_(0)
28  {
29  //srand(time(NULL));
30  w_ = 1;
31  c1_ = 1.49445;
32  c2_ = 1.49445;
35  robot_id_ = rth_->getRobotID();
36  cur_gen_ = 0;
37  gen_limit_ = 0;
38  if(robot_id_ == 0) {
39  file.open("~/catkin_ws/scds_pso.data");
40  }
41  }
42 
43  Agent::Agent(std::string name):name_(name), run_(false), dim_(0), fitness_(0)
44  {
45  //srand(time(NULL));
46  w_ = 1;
47  c1_ = 1.49445;
48  c2_ = 1.49445;
51  robot_id_ = rth_->getRobotID();
52  cur_gen_ = 0;
53  gen_limit_ = 0;
54  if(robot_id_ == 0) {
55  file.open("~/catkin_ws/scds_pso.data");
56  }
57  }
58 
60  {
61  rth_.reset();
62  }
63 
64  void Agent::set_param(float w, float c1, float c2)
65  {
66  w_ = w;
67  c1_ = c1;
68  c2_ = c2;
69  }
70 
71  void Agent::set_dim(int dim)
72  {
73  dim_ = dim;
74  min_pos_.resize(dim_);
75  for(int i = 0; i < dim_; i++) {
76  min_pos_[i] = 0;
77  }
78  max_pos_.resize(dim_);
79  for(int i = 0; i < dim_; i++) {
80  max_pos_[i] = 0;
81  }
82  min_vel_.resize(dim_);
83  for(int i = 0; i < dim_; i++) {
84  min_vel_[i] = 0;
85  }
86  max_vel_.resize(dim_);
87  for(int i = 0; i < dim_; i++) {
88  max_vel_[i] = 0;
89  }
90  cur_pos_.resize(dim_);
91  for(int i = 0; i < dim_; i++) {
92  cur_pos_[i] = 0;
93  }
94  cur_vel_.resize(dim_);
95  for(int i = 0; i < dim_; i++) {
96  cur_vel_[i] = 0;
97  }
98  }
99 
100  void Agent::set_fitness(const boost::function<float(const std::vector<float>& )>& fitness)
101  {
102  fitness_ = fitness;
103  }
104 
105  void Agent::set_min_pos(const std::vector<float>& pos)
106  {
107  for(int i = 0; i < pos.size(); i++) {
108  min_pos_[i] = pos[i];
109  }
110  }
111 
112  void Agent::set_max_pos(const std::vector<float>& pos)
113  {
114  for(int i = 0; i < pos.size(); i++) {
115  max_pos_[i] = pos[i];
116  }
117  }
118 
119  void Agent::set_min_vel(const std::vector<float>& vel)
120  {
121  for(int i = 0; i < vel.size(); i++) {
122  min_vel_[i] = vel[i];
123  }
124  }
125 
126  void Agent::set_max_vel(const std::vector<float>& vel)
127  {
128  for(int i = 0; i < vel.size(); i++) {
129  max_vel_[i] = vel[i];
130  }
131  }
132 
133  void Agent::init_pos(const std::vector<float>& pos)
134  {
135  for(int i = 0; i < pos.size(); i++) {
136  cur_pos_[i] = pos[i];
137  }
138 
139  pbest_.pos = cur_pos_;
142  pbest_.gen = cur_gen_;
143  pbest_.timestamp = time(NULL);
144 
145  gbest_.pos = cur_pos_;
148  gbest_.gen = cur_gen_;
149  gbest_.timestamp = time(NULL);
150 
151  best_tuple_.put("scds_pso", gbest_);
152  }
153 
154  void Agent::init_vel(const std::vector<float>& vel)
155  {
156  for(int i = 0; i < vel.size(); i++) {
157  cur_vel_[i] = vel[i];
158  }
159  }
160 
161  bool Agent::has_pos_limit(int index)
162  {
163  if((min_pos_[index] == 0) && (max_pos_[index] == 0)) {
164  return false;
165  }
166  return true;
167  }
168 
169  bool Agent::has_vel_limit(int index)
170  {
171  if((min_vel_[index] == 0) && (max_vel_[index] == 0)) {
172  return false;
173  }
174  return true;
175  }
176 
178  {
179  for(int i = 0; i < dim_; i++) {
180  if(has_pos_limit(i)) {
181  cur_pos_[i] = random_float(min_pos_[i], max_pos_[i]);
182  }
183  else {
184  cur_pos_[i] = random_float(-5.12, 5.12);
185  }
186 
187  pbest_.pos = cur_pos_;
190  pbest_.gen = cur_gen_;
191  pbest_.timestamp = time(NULL);
192  gbest_.pos = cur_pos_;
195  gbest_.gen = cur_gen_;
196  gbest_.timestamp = time(NULL);
197 
198  best_tuple_.put("scds_pso", gbest_);
199 
200  if(has_vel_limit(i)) {
201  cur_vel_[i] = random_float(min_vel_[i], max_vel_[i]);
202  }
203  else {
204  cur_vel_[i] = random_float(-5.12, 5.12);
205  }
206  }
207  }
208 
210  {
211  if(!run_) {
212  return;
213  }
214 
215  cur_gen_++;
216  float r1 = (float)rand()/RAND_MAX;
217  float r2 = (float)rand()/RAND_MAX;
218  gbest_ = best_tuple_.get("scds_pso");
219 
220  for(int i = 0; i < dim_; i++) {
221  float s1 = c1_*r1*(pbest_.pos[i] - cur_pos_[i]);
222  float s2 = c2_*r2*(gbest_.pos[i] - cur_pos_[i]);
223  cur_vel_[i] = w_*cur_vel_[i] + s1 + s2;
224 
225  if(has_vel_limit(i)) {
226  if(cur_vel_[i] > max_vel_[i]) {
227  cur_vel_[i] = max_vel_[i];
228  }
229  if(cur_vel_[i] < min_vel_[i]) {
230  cur_vel_[i] = min_vel_[i];
231  }
232  }
233 
234  cur_pos_[i] = cur_pos_[i] + cur_vel_[i];
235  if(has_pos_limit(i)) {
236  if(cur_pos_[i] > max_pos_[i]) {
237  cur_pos_[i] = max_pos_[i];
238  }
239  if(cur_pos_[i] < min_pos_[i]) {
240  cur_pos_[i] = min_pos_[i];
241  }
242  }
243  }
244 
245  float cur_val = fitness_(cur_pos_);
246  if(cur_val > pbest_.val) {
247  pbest_.pos = cur_pos_;
248  pbest_.val = cur_val;
250  pbest_.gen = cur_gen_;
251  pbest_.timestamp = time(NULL);
252  }
253 
254  if(cur_val > gbest_.val) {
255  gbest_.pos = cur_pos_;
256  gbest_.val = cur_val;
258  gbest_.gen = cur_gen_;
259  gbest_.timestamp = time(NULL);
260 
261  best_tuple_.put("scds_pso", gbest_);
262  }
263 
264  //if(robot_id_ == 0) {
265  // std::cout << "robot_id: " << robot_id_ << " cur_gen: " << cur_gen_ << ", cur_val: " << cur_val
266  // << ", gbest: " << gbest_.val << ", gen: " << gbest_.gen << std::endl;
267  //}
268 
269  if(robot_id_ == 0) {
270  std::cout<<cur_gen_<<" "<<gbest_.val<< std::endl;
271  file<<cur_gen_<<" "<<gbest_.val<< std::endl;
272  }
273 
274  if(gen_limit_) {
275  if(cur_gen_ > gen_limit_) {
276  timer_.stop();
277  }
278  }
279  }
280 
282  {
283  if(!dim_) {
284  std::cout<<"dimension is not set!"<<std::endl;
285  return;
286  }
287 
288  if(fitness_ == 0) {
289  std::cout<<"fitness is not set!"<<std::endl;
290  return;
291  }
292 
293  run_ = true;
295  }
296 
297  void Agent::start(int loop_gen)
298  {
299  gen_limit_ = loop_gen;
300  start();
301  }
302 
303  void Agent::stop()
304  {
305  run_ = false;
306  dim_ = 0;
307  name_ = "";
308  cur_gen_ = 0;
309  gen_limit_ = 0;
310  }
311 };
SCDSPSODataTuple get(const std::string &key)
std::vector< float > pos
Definition: data_type.h:170
SCDSPSOTuple best_tuple_
Definition: scds_pso.h:78
void set_max_vel(const std::vector< float > &pos)
Definition: scds_pso.cpp:126
void init_vel(const std::vector< float > &vel)
Definition: scds_pso.cpp:154
ros::Timer timer_
Definition: scds_pso.h:84
std::vector< float > cur_pos_
Definition: scds_pso.h:72
void stop()
void set_dim(int dim)
Definition: scds_pso.cpp:71
void set_max_pos(const std::vector< float > &pos)
Definition: scds_pso.cpp:112
bool has_pos_limit(int index)
Definition: scds_pso.cpp:161
boost::function< float(const std::vector< float > &)> fitness_
Definition: scds_pso.h:67
ros::NodeHandle nh_
Definition: scds_pso.h:83
std::vector< float > min_pos_
Definition: scds_pso.h:68
Timer createTimer(Rate r, Handler h, Obj o, bool oneshot=false, bool autostart=true) const
std::ofstream file
Definition: scds_pso.h:86
bool has_vel_limit(int index)
Definition: scds_pso.cpp:169
std::string name_
Definition: scds_pso.h:61
static boost::shared_ptr< T > getSingleton()
Definition: singleton.h:70
void set_min_pos(const std::vector< float > &pos)
Definition: scds_pso.cpp:105
void put(const std::string &key, const SCDSPSODataTuple &data)
std::vector< float > min_vel_
Definition: scds_pso.h:70
void loop(const ros::TimerEvent &)
Definition: scds_pso.cpp:209
void init_pos(const std::vector< float > &pos)
Definition: scds_pso.cpp:133
void set_param(float w, float c1, float c2)
Definition: scds_pso.cpp:64
SCDSPSODataTuple gbest_
Definition: scds_pso.h:77
std::vector< float > max_vel_
Definition: scds_pso.h:71
std::vector< float > max_pos_
Definition: scds_pso.h:69
boost::shared_ptr< RuntimeHandle > rth_
Definition: scds_pso.h:81
void set_fitness(const boost::function< float(const std::vector< float > &)> &fitness)
Definition: scds_pso.cpp:100
void set_min_vel(const std::vector< float > &pos)
Definition: scds_pso.cpp:119
std::vector< float > cur_vel_
Definition: scds_pso.h:73
SCDSPSODataTuple pbest_
Definition: scds_pso.h:76
float random_float(float min, float max)
Definition: random.cpp:32


micros_swarm
Author(s):
autogenerated on Mon Jun 10 2019 14:02:06