steadystaterwp.c 18.3 KB
Newer Older
1
/*******************************************************************************
nikaeinn's avatar
nikaeinn committed
2
3
    OpenAirInterface
    Copyright(c) 1999 - 2014 Eurecom
4

nikaeinn's avatar
nikaeinn committed
5
6
7
8
    OpenAirInterface 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 3 of the License, or
    (at your option) any later version.
9
10


nikaeinn's avatar
nikaeinn committed
11
12
13
14
    OpenAirInterface 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.
15

nikaeinn's avatar
nikaeinn committed
16
17
18
19
    You should have received a copy of the GNU General Public License
    along with OpenAirInterface.The full GNU General Public License is
    included in this distribution in the file called "COPYING". If not,
    see <http://www.gnu.org/licenses/>.
20
21

  Contact Information
nikaeinn's avatar
nikaeinn committed
22
23
  OpenAirInterface Admin: openair_admin@eurecom.fr
  OpenAirInterface Tech : openair_tech@eurecom.fr
24
  OpenAirInterface Dev  : openair4g-devel@lists.eurecom.fr
nikaeinn's avatar
nikaeinn committed
25

ghaddab's avatar
ghaddab committed
26
  Address      : Eurecom, Campus SophiaTech, 450 Route des Chappes, CS 50193 - 06904 Biot Sophia Antipolis cedex, FRANCE
27
28
29
30

*******************************************************************************/

/*! \file steadystaterwp.c
31
* \brief random waypoint mobility generator
32
33
34
35
* \author  S. Gashaw,  J. Harri, N. Nikaein,
* \date 2014
* \version 0.1
* \company Eurecom
36
* \email:
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
* \note
* \warning
*/

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include "omg.h"
#include "steadystaterwp.h"
#include "rwp.h"
#define RESTIRICTED_RWP 1
#define CONNECTED_DOMAIN 2

int
start_steadystaterwp_generator (omg_global_param omg_param_list)
{
  static int n_id = 0;
  int id;
  double cur_time = 0.0, pause_p;
  node_struct *node = NULL;
  mobility_struct *mobility = NULL;
  pair_struct *pair = NULL;

  pause_p = pause_probability (omg_param_list);

  srand (omg_param_list.seed + RWP);

  LOG_I (OMG, "STEADY_RWP mobility model for %d %d nodes\n",
66
         omg_param_list.nodes, omg_param_list.nodes_type);
67

68
  for (id = n_id; id < (omg_param_list.nodes + n_id); id++) {
69

70
71
    node = create_node ();
    mobility = create_mobility ();
72

73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
    node->id = id;
    node->type = omg_param_list.nodes_type;
    node->mob = mobility;
    node->generator = STEADY_RWP;
    node->event_num = 0;
    place_rwp_node (node);  //initial positions

    pair = (pair_struct *) malloc (sizeof (struct pair_struct));
    pair->b = node;
    //pause probability...some of the nodes start at pause & the other on move

    if (randomgen (0, 1) < pause_p)
      sleep_steadystaterwp_node (pair, cur_time); //sleep
    else
      move_steadystaterwp_node (pair, cur_time);

    job_vector_end[STEADY_RWP] = add_job (pair, job_vector_end[STEADY_RWP]);

    if (job_vector[STEADY_RWP] == NULL)
      job_vector[STEADY_RWP] = job_vector_end[STEADY_RWP];

    job_vector_len[STEADY_RWP]++;
  }
96
97
98
99
100
101
102

  n_id += omg_param_list.nodes;



  if (job_vector[STEADY_RWP] == NULL)
    LOG_E (OMG, "[STEADY_RWP] Job Vector is NULL\n");
103

104
105
106
107
108
109
110
111
112
113
114
115
  return (0);
}

/*********************************xxxxxx************************************/

void
place_steadystaterwp_node (node_struct * node)
{
  int loc_num;
  double block_xmin, block_ymin;


116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
  if (grid == RESTIRICTED_RWP) {
    loc_num = (int) randomgen (0, omg_param_list[node->type].max_vertices);
    node->x_pos =
      (double) vertice_xpos (loc_num, omg_param_list[node->type]);
    node->y_pos =
      (double) vertice_ypos (loc_num, omg_param_list[node->type]);
    //LOG_D(OMG,"location number %d x pos %.2f y pos %.2f \n\n",loc_num,node->x_pos,node->y_pos);
  } else if (grid == CONNECTED_DOMAIN) {
    node->block_num =
      (int) randomgen (0, omg_param_list[node->type].max_block_num);
    block_xmin = area_minx (node->block_num, omg_param_list[node->type]);
    block_ymin = area_miny (node->block_num, omg_param_list[node->type]);

    node->x_pos =
      (double) ((int) (randomgen (block_xmin, xloc_div + block_xmin) * 100))
      / 100;

    node->y_pos =
      (double) ((int) (randomgen (block_ymin, yloc_div + block_ymin) * 100))
      / 100;

  } else {
    node->x_pos =
      (double) ((int)
                (randomgen
                 (omg_param_list[node->type].min_x,
                  omg_param_list[node->type].max_x) * 100)) / 100;

    node->y_pos =
      (double) ((int)
                (randomgen
                 (omg_param_list[node->type].min_y,
                  omg_param_list[node->type].max_y) * 100)) / 100;
  }
150
151
152
153
154
155
156

  node->mob->x_from = node->x_pos;
  node->mob->x_to = node->x_pos;
  node->mob->y_from = node->y_pos;
  node->mob->speed = 0.0;
  node->mob->journey_time = 0.0;
  LOG_I (OMG,
157
158
         "#[STEADY_RWP] Initial position of node ID: %d type: %d X = %.2f, Y = %.2f\n ",
         node->id, node->type, node->x_pos, node->y_pos);
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175

  node_vector_end[node->type] =
    (node_list *) add_entry (node, node_vector_end[node->type]);

  if (node_vector[node->type] == NULL)
    node_vector[node->type] = node_vector_end[node->type];

  node_vector_len[node->type]++;
  //Initial_Node_Vector_len[RWP]++;
}


/*********************************xxxxxx************************************/

void
sleep_steadystaterwp_node (pair_struct * pair, double cur_time)
{
176
  //static int initial = 1;
177
178
179
180
181
182
183
184
  node_struct *node;
  node = pair->b;
  node->mobile = 0;
  node->mob->speed = 0.0;
  node->mob->x_from = node->mob->x_to;
  node->mob->y_from = node->mob->y_to;
  node->x_pos = node->mob->x_to;
  node->y_pos = node->mob->y_to;
185
186
187
188
189
190
191
192
193
194
195
196

  if (node->event_num == 0) {
    node->mob->sleep_duration = initial_pause (omg_param_list[node->type]);
    node->event_num++;
  } else {
    node->mob->sleep_duration =
      (double) ((int)
                (randomgen
                 (omg_param_list[node->type].min_sleep,
                  omg_param_list[node->type].max_sleep) * 100)) / 100;
  }

197
  // LOG_D (OMG, "[STEADY_RWP] node: %d \tsleep duration : %.2f\n", node->ID,
198
  //       node->mob->sleep_duration);
199
200

  node->mob->start_journey = cur_time;
201
  pair->next_event_t = cur_time + node->mob->sleep_duration;  //when to wake up
202
  // LOG_D (OMG, "[STEADY_RWP] wake up at time: cur_time + sleep_duration : %.2f\n",
203
  //       pair->a);
204
205
206
207
208
209
210
211


}


void
move_steadystaterwp_node (pair_struct * pair, double cur_time)
{
212
  //static int initial = 1;
213
214
  double distance, journeytime_next, max_distance;
  double temp_x, temp_y, u1, u2;
215
  //int loc_num;
216
217
218
219
220
221
222
223
224
225
226
  double pr, block_xmin, block_ymin;

  //LOG_D (OMG, "[STEADY_RWP] move node: %d\n", node->ID);
  node_struct *node;
  node = pair->b;
  node->mob->x_from = node->mob->x_to;
  node->mob->y_from = node->mob->y_to;
  node->x_pos = node->mob->x_to;
  node->y_pos = node->mob->y_to;
  node->mobile = 1;

227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
  //initial move
  if (node->event_num == 0) {


    if (grid == CONNECTED_DOMAIN) {
      max_distance = 2 * yloc_div;

      /* sqrtf (pow
         (omg_param_list[node->type].max_x -
         omg_param_list[node->type].min_x,
         2) + pow (omg_param_list[node->type].max_y -
         omg_param_list[node->type].min_y, 2)); */
      while (true) {
        pr = randomgen (0, 1);

        if (pr <= 0.50)
          /*node->block_num =
             (int) randomgen (0,
             omg_param_list[node->type].max_block_num); */
          node->block_num =
            (int) next_block (node->block_num,
                              omg_param_list[node->type]);

        block_xmin =
          area_minx (node->block_num, omg_param_list[node->type]);
        block_ymin =
          area_miny (node->block_num, omg_param_list[node->type]);

        node->mob->x_to =
          (double) ((int)
                    (randomgen (block_xmin, xloc_div + block_xmin) *
                     100)) / 100;

        node->mob->y_to =
          (double) ((int)
                    (randomgen (block_ymin, yloc_div + block_ymin) *
                     100)) / 100;
        pr = randomgen (0, 1);

        if (pr <= 0.50)
          /*node->block_num =
             (int) randomgen (0,
             omg_param_list[node->type].max_block_num); */
          node->block_num =
            (int) next_block (node->block_num,
                              omg_param_list[node->type]);

        block_xmin =
          area_minx (node->block_num, omg_param_list[node->type]);
        block_ymin =
          area_miny (node->block_num, omg_param_list[node->type]);

        temp_x =
          (double) ((int)
                    (randomgen (block_xmin, xloc_div + block_xmin) *
                     100)) / 100;

        temp_y =
          (double) ((int)
                    (randomgen (block_ymin, yloc_div + block_ymin) *
                     100)) / 100;


        u1 = randomgen (0, 1);

        if (u1 <
            (sqrtf
             (pow (node->mob->x_to - temp_x, 2) +
              pow (node->mob->y_to - temp_y, 2)) / max_distance)) {
          u2 = randomgen (0, 1);
          distance =
            (double) ((int)
                      (sqrtf (pow (temp_x - node->mob->x_to, 2) +
                              pow (temp_y - node->mob->y_to,
                                   2)) * 100)) / 100;
          node->mob->azimuth = atan2 (node->mob->y_to - node->mob->y_from, node->mob->x_to - node->mob->x_from);  //radian
          node->mob->x_from =
            temp_x + u2 * distance * cos (node->mob->azimuth);
          node->mob->y_from =
            temp_y + u2 * distance * sin (node->mob->azimuth);
          break;
        }
      }
    } else {
      max_distance =
        sqrtf (pow
               (omg_param_list[node->type].max_x -
                omg_param_list[node->type].min_x,
                2) + pow (omg_param_list[node->type].max_y -
                          omg_param_list[node->type].min_y, 2));

      while (true) {
        node->mob->x_to =
          (double) ((int)
                    (randomgen
                     (omg_param_list[node->type].min_x,
                      omg_param_list[node->type].max_x) * 100)) / 100;

        node->mob->y_to =
          (double) ((int)
                    (randomgen
                     (omg_param_list[node->type].min_y,
                      omg_param_list[node->type].max_y) * 100)) / 100;

        temp_x = (double) ((int)
                           (randomgen
                            (omg_param_list[node->type].min_x,
                             omg_param_list[node->type].max_x) * 100)) /
                 100;

        temp_y = (double) ((int)
                           (randomgen
                            (omg_param_list[node->type].min_y,
                             omg_param_list[node->type].max_y) * 100)) /
                 100;

        u1 = randomgen (0, 1);

        if (u1 <
            (sqrtf
             (pow (node->mob->x_to - temp_x, 2) +
              pow (node->mob->y_to - temp_y, 2)) / max_distance)) {
          u2 = randomgen (0, 1);
          node->mob->x_from =
            u2 * temp_x + (1 - u2) * node->mob->x_to;
          node->mob->y_from =
            u2 * temp_y + (1 - u2) * node->mob->y_to;
          break;
        }
      }
357
    }
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423


    node->mob->speed = initial_speed (omg_param_list[node->type]);
    node->event_num++;
    distance =
      (double) ((int)
                (sqrtf (pow (node->mob->x_from - node->mob->x_to, 2) +
                        pow (node->mob->y_from - node->mob->y_to,
                             2)) * 100)) / 100;

  }
  //for the next move
  else {

    if (grid == CONNECTED_DOMAIN) {
      pr = randomgen (0, 1);

      if (pr <= 0.50)
        /* node->block_num =
           (int) randomgen (0, omg_param_list[node->type].max_block_num); */
        node->block_num =
          (int) next_block (node->block_num, omg_param_list[node->type]);

      block_xmin =
        area_minx (node->block_num, omg_param_list[node->type]);
      block_ymin =
        area_miny (node->block_num, omg_param_list[node->type]);

      node->mob->x_to =
        (double) ((int)
                  (randomgen (block_xmin, xloc_div + block_xmin) * 100)) /
        100;

      node->mob->y_to =
        (double) ((int)
                  (randomgen (block_ymin, yloc_div + block_ymin) * 100)) /
        100;

      distance =
        (double) ((int) (sqrtf
                         (pow (node->mob->x_from - node->mob->x_to, 2) +
                          pow (node->mob->y_from - node->mob->y_to,
                               2)) * 100)) / 100;

      node->mob->azimuth =
        atan2 (node->mob->y_to - node->mob->y_from,
               node->mob->x_to - node->mob->x_from);
    } else {
      node->mob->x_to =
        (double) ((int)
                  (randomgen
                   (omg_param_list[node->type].min_x,
                    omg_param_list[node->type].max_x) * 100)) / 100;

      node->mob->y_to =
        (double) ((int)
                  (randomgen
                   (omg_param_list[node->type].min_y,
                    omg_param_list[node->type].max_y) * 100)) / 100;

      distance =
        (double) ((int)
                  (sqrtf (pow (node->mob->x_from - node->mob->x_to, 2) +
                          pow (node->mob->y_from - node->mob->y_to,
                               2)) * 100)) / 100;

424
425
426

    }

427
428
429
430
431
432
433
434
435
436
    node->mob->speed =
      (double) ((int)
                (randomgen
                 (omg_param_list[node->type].min_speed,
                  omg_param_list[node->type].max_speed) * 100)) / 100;
    node->event_num++;

  }

  journeytime_next = (double) ((int) (distance / node->mob->speed * 100)) / 100;  //duration to get to dest
437
438
439
440
441

  //LOG_D (OMG, "[STEADY_RWP] mob->journey_time_next %.2f\n", journeyTime_next);

  node->mob->start_journey = cur_time;
  //LOG_D (OMG, "[STEADY_RWP] start_journey %.2f\n", node->mob->start_journey);
442
  pair->next_event_t = cur_time + journeytime_next; //when to reach the destination
443
444
445
446
  // LOG_D (OMG,
  // "[STEADY_RWP] reaching the destination at time : start journey + journey_time next =%.2f\n",
  // pair->a);

447
448
449
450
451
  if (node->event_num < 100) {
    event_sum[node->event_num] += node->mob->speed;
    events[node->event_num]++;
    node->event_num++;
  }
452
453
454
455
456
457
458
459
460
461

}

double
pause_probability (omg_global_param omg_param)
{

  double alpha, delta;
  alpha =
    (omg_param.max_sleep + omg_param.min_sleep) * (omg_param.max_speed -
462
463
464
465
466
467
468
        omg_param.min_speed) / (2 *
                                log
                                (omg_param.
                                 max_speed
                                 /
                                 omg_param.
                                 min_speed));
469
470
471

  delta =
    sqrtf (pow (omg_param.max_x - omg_param.min_x, 2) +
472
           pow (omg_param.max_y - omg_param.min_y, 2));
473
474
475
476
477
478
479
480
481
482

  return alpha / (alpha + delta);

}

double
initial_pause (omg_global_param omg_param)
{
  double u;
  u = randomgen (0, 1);
483

484
485
486
487
488
489
  if (u <
      (2 * omg_param.min_sleep / (omg_param.max_sleep + omg_param.min_sleep)))
    return u * (omg_param.max_sleep + omg_param.min_sleep) / 2;

  else
    return omg_param.max_sleep -
490
491
           sqrtf ((1 - u) * (pow (omg_param.max_sleep, 2) -
                             pow (omg_param.min_sleep, 2)));
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
}

double
initial_speed (omg_global_param omg_param)
{
  double u;
  u = randomgen (0, 1);

  return pow (omg_param.max_speed, u) / pow (omg_param.min_speed, u - 1);

}

/*update RWP nodes position*/

void
update_steadystaterwp_nodes (double cur_time)
{

  int done = 0;
  job_list *tmp = job_vector[STEADY_RWP];
  node_struct *my_node;
513
514
515
516
517
518
519
520
521
522

  while (tmp != NULL && done == 0) {
    //case1:time to next event equals to current time
    if (tmp->pair != NULL
        && ((double) tmp->pair->next_event_t >= cur_time - omg_eps)
        && ((double) tmp->pair->next_event_t <= cur_time + omg_eps)) {
      my_node = tmp->pair->b;

      if (my_node->mobile == 1)
        sleep_rwp_node (tmp->pair, cur_time);
523
      else
524
525
526
527
528
529
530
531
532
        move_rwp_node (tmp->pair, cur_time);

    }
    //case2: current time is greater than the time to next event

    else if (tmp->pair != NULL
             && (cur_time - omg_eps) > tmp->pair->next_event_t) {

      my_node = tmp->pair->b;
533

534
535
536
537
538
      while (cur_time >= tmp->pair->next_event_t) {
        if (my_node->mobile == 1)
          sleep_rwp_node (tmp->pair, cur_time);
        else
          move_rwp_node (tmp->pair, cur_time);
539

540
      }
541
    }
542
543
544
545
546
547
548
549
    //case3: current time less than the time to next event
    else {
      done = 1;   //quit the loop
    }


    tmp = tmp->next;
  }
550
551
552
553

  //sorting the new entries
  //LOG_D (OMG, "--------DISPLAY JOB LIST--------\n");   //LOG_T
  // display_job_list (job_vector);
554
  job_vector[STEADY_RWP] = quick_sort (job_vector[STEADY_RWP]); ///////////
555
556
557
558
559
560
561
562
563
564
565
566
567
  // LOG_D (OMG, "--------DISPLAY JOB LIST AFTER SORTING--------\n");
  // display_job_list (job_vector[STEADY_RWP]);
}


void
get_steadystaterwp_positions_updated (double cur_time)
{

  double x_now = 0.0, y_now = 0.0;
  double len, dx, dy;
  job_list *tmp = job_vector[STEADY_RWP];

568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
  while (tmp != NULL) {

    if (tmp->pair->b->mobile == 1 && tmp->pair->next_event_t >= cur_time) {


      len =
        sqrtf (pow
               (tmp->pair->b->mob->x_from -
                tmp->pair->b->mob->x_to,
                2) + pow (tmp->pair->b->mob->y_from -
                          tmp->pair->b->mob->y_to, 2));

      if (len != 0) {
        dx =
          fabs (tmp->pair->b->mob->x_from -
                tmp->pair->b->mob->x_to) / len;
        dy =
          fabs (tmp->pair->b->mob->y_from -
                tmp->pair->b->mob->y_to) / len;

        //x coordinate
        if (tmp->pair->b->mob->x_from < tmp->pair->b->mob->x_to) {
          x_now =
            tmp->pair->b->mob->x_from +
            (dx *
             (tmp->pair->b->mob->speed *
              (cur_time - tmp->pair->b->mob->start_journey)));
        } else {
          x_now =
            tmp->pair->b->mob->x_from -
            (dx *
             (tmp->pair->b->mob->speed *
              (cur_time - tmp->pair->b->mob->start_journey)));
        }

        //y coordinate
        if (tmp->pair->b->mob->y_from < tmp->pair->b->mob->y_to) {
          y_now =
            tmp->pair->b->mob->y_from +
            (dy *
             (tmp->pair->b->mob->speed *
              (cur_time - tmp->pair->b->mob->start_journey)));
        } else {
          y_now =
            tmp->pair->b->mob->y_from -
            (dy *
             (tmp->pair->b->mob->speed *
              (cur_time - tmp->pair->b->mob->start_journey)));
        }

        tmp->pair->b->x_pos = (double) ((int) (x_now * 100)) / 100;
        tmp->pair->b->y_pos = (double) ((int) (y_now * 100)) / 100;
        //len

      }


    } else {
      tmp->pair->b->x_pos = tmp->pair->b->mob->x_to;
      tmp->pair->b->y_pos = tmp->pair->b->mob->y_to;
    }
629
630


631
    tmp = tmp->next;
632

633
  }
634
635

}