Statistics
| Branch: | Revision:

mobicen / util / ConfPars.py @ 23c7ab1e

History | View | Annotate | Download (8.2 KB)

1 1eca333b LoreBz
from inherit_config_parser import InheritConfigParser
2 e1cf8bea LoreBz
import configparser  # https://docs.python.org/2.7/library/configparser.html
3
from pymobility.models.mobility import gauss_markov, reference_point_group, \
4
    tvc, truncated_levy_walk, random_direction, random_waypoint, random_walk
5 38d1e922 LoreBz
import sys
6 2dbcade9 LoreBz
from milanoMob import milano_mob
7 1eca333b LoreBz
import code  # code.interact(local=dict(globals(), **locals()))
8 e1cf8bea LoreBz
9 23c7ab1e LoreBz
10 e1cf8bea LoreBz
class ConfSettings(object):
11
12
    def __init__(self, settingsFile, sectionExp):
13
        self.SF = settingsFile
14
        self.SEC = sectionExp
15
        # return self.getSimulationParameters(settingsFile, sectionExp)
16
17 23c7ab1e LoreBz
    def getVelocity(self, kind):
18
        velMap = {'slow': (0.139, 0.556), 'pedestrian': (0.556, 1.667), 'jogging': (
19
            1.667, 3.332), 'bike': (3.332, 8.332), 'citycar': (8.332, 22.223)}
20
        if kind not in velMap:
21
            return velMap['pedestrian']
22
        else:
23
            return velMap[kind]
24
25 e1cf8bea LoreBz
    def getSimulationParameters(self):
26
        settingsFile, sectionExp = self.SF, self.SEC
27
        retParameters = {}
28 1eca333b LoreBz
        #settings = configparser.ConfigParser()
29
        #settings._interpolation = configparser.ExtendedInterpolation()
30
        settings = InheritConfigParser()
31
        settings.optionxform = str
32 e1cf8bea LoreBz
        settings.read(settingsFile)
33
        mand = set([
34 2dbcade9 LoreBz
            'mob_model', 'max_x', 'max_y', 'radius',
35
            'mobility_timer', 'duration'])
36 e1cf8bea LoreBz
        opts = dict(settings.items(sectionExp))
37
        if (not mand.issubset(opts.keys())):
38
            sys.exit('You are missing a mandatory parameter!\n'
39
                     'These are required:\n' + ', '.join(mand))
40
41 1eca333b LoreBz
        for option in opts:
42 e1cf8bea LoreBz
            arg = settings.get(sectionExp, option)
43
            if option == 'duration':
44
                retParameters['duration'] = int(arg.strip('s'))
45
            elif option == 'mob_model':
46 2dbcade9 LoreBz
                mobset = set(['RandoWalk', 'RandomWayPoint', 'RandomDirection', 'Milan',
47 e1cf8bea LoreBz
                              'TruncatedLevyWalk', 'GaussMarkov', 'ReferencePointGroup', 'TimeVariantCommunity'])
48
                if arg not in mobset:
49
                    sys.exit(arg+' is not a mobility model supported!\n'
50
                             'These are supported:\n' + ', '.join(mobset))
51
                retParameters['mob_model'] = arg
52
            elif option == 'max_x':
53
                retParameters['max_x'] = int(arg)
54
            elif option == 'max_y':
55
                retParameters['max_y'] = int(arg)
56 23c7ab1e LoreBz
            elif option == 'velocity':
57
                retParameters['velocity'] = arg
58
                min_v, max_v = self.getVelocity(arg)
59
                retParameters['min_velocity'] = min_v
60
                retParameters['max_velocity'] = max_v
61 e1cf8bea LoreBz
            elif option == 'nodes_number':
62
                retParameters['nodes_number'] = int(arg)
63
            elif option == 'max_velocity':
64
                retParameters['max_velocity'] = float(arg)
65
            elif option == 'min_velocity':
66
                retParameters['min_velocity'] = float(arg)
67
            elif option == 'max_wait_time':
68
                retParameters['max_wait_time'] = float(arg)
69
            elif option == 'radius':
70
                retParameters['radius'] = float(arg)
71
            elif option == 'mobility_timer':
72
                retParameters['mobility_timer'] = float(arg.strip('s'))
73 38d1e922 LoreBz
            elif option == 'nodes_x_group':
74
                retParameters['nodes_x_group'] = int(arg)
75
            elif option == 'groups_number':
76
                retParameters['groups_number'] = int(arg)
77 23c7ab1e LoreBz
            elif option == 'aggregation':
78
                retParameters['aggregation'] = float(arg)
79 e1cf8bea LoreBz
            else:
80
                print 'Unparsed ', option, '=', settings.get(sectionExp, option)
81
        return retParameters
82
83
    def configMobility(self, params):
84 38d1e922 LoreBz
        userMobi = params['mob_model']
85 23c7ab1e LoreBz
        supportedModels = ["RandomWayPoint",
86
                           "TimeVariantCommunity", "Milan", "ReferencePointGroup"]
87 38d1e922 LoreBz
        if userMobi not in supportedModels:
88
            print userMobi + " is not supported now!\n" \
89
                "This moility models are currently supported:"
90
            print ", ".join(supportedModels)
91 e1cf8bea LoreBz
            sys.exit(1)
92
        else:
93 38d1e922 LoreBz
            if userMobi == "RandomWayPoint":
94
                if not set(['max_x', 'max_y', 'nodes_number', 'max_velocity']).issubset(params.keys()):
95
                    print "You miss some params to configure RandomWayPoint"
96
                    sys.exit(1)
97
                else:
98
                    N = params['nodes_number']
99
                    max_x, max_y = params['max_x'], params['max_y']
100
                    min_v, max_v = params['min_velocity'], params['max_velocity']
101
                    max_wt = params['max_wait_time']
102
                    rw = random_waypoint(N, dimensions=(
103
                        max_x, max_y), velocity=(min_v, max_v), wt_max=max_wt)
104
                    return rw
105 2dbcade9 LoreBz
            elif userMobi == "Milan":
106
                return milano_mob()
107
108 23c7ab1e LoreBz
            elif userMobi == "ReferencePointGroup":
109
                # Reference Point Group model
110
                NxG = params['nodes_x_group']
111
                NG = params['groups_number']
112
                aggreg = params['aggregation']
113
                max_x, max_y = params['max_x'], params['max_y']
114
                min_v, max_v = params['min_velocity'], params['max_velocity']
115
                groups = [NxG for _ in range(NG)]
116
                nr_nodes = sum(groups)
117
                params['nodes_number'] = nr_nodes
118
                rpg = reference_point_group(
119
                    groups, velocity=(min_v, max_v), dimensions=(max_x, max_y), aggregation=aggreg)
120
121
                return rpg
122 2dbcade9 LoreBz
123 38d1e922 LoreBz
            elif userMobi == "TimeVariantCommunity":
124
                '''
125
    Time-variant Community Mobility Model, discussed in the paper
126

127
        Wei-jen Hsu, Thrasyvoulos Spyropoulos, Konstantinos Psounis, and Ahmed Helmy, 
128
        "Modeling Time-variant User Mobility in Wireless Mobile Networks," INFOCOM 2007, May 2007.
129

130
    This is a variant of the original definition, in the following way:
131
    - Communities don't have a specific area, but a reference point where the 
132
       community members aggregate around.
133
    - The community reference points are not static, but follow a random direction model.
134
    - You can define a list of epoch stages, each value is the duration of the stage.
135
       For each stage a different aggregation value is used (from the aggregation parameter).
136
    - Aggregation values should be doubles between 0 and 1.
137
       For aggregation 0, there's no attraction point and the nodes move in a random walk model.
138
       For aggregation near 1, the nodes move closer to the community reference point.
139

140
    Required arguments:
141

142
      *nr_nodes*:
143
        list of integers, the number of nodes in each group.
144

145
      *dimensions*:
146
        Tuple of Integers, the x and y dimensions of the simulation area.
147

148
    keyword arguments:
149

150
      *velocity*:
151
        Tuple of Doubles, the minimum and maximum values for community velocities.
152

153
      *aggregation*:
154
        List of Doubles, parameters (between 0 and 1) used to aggregate the nodes around the community center.
155
        Usually between 0 and 1, the more this value approximates to 1,
156
        the nodes will be more aggregated and closer to the group center.
157
        With aggregation 0, the nodes are randomly distributed in the simulation area.
158
        With aggregation near 1, the nodes are closer to the group center.
159

160
      *epoch*:
161
        List of Integers, the number of steps each epoch stage lasts.
162
    '''
163
                if not set(['max_x', 'max_y', 'nodes_x_group', 'min_velocity',
164
                            'max_velocity', 'groups_number']).issubset(params.keys()):
165
                    print "You miss some params to configure TimeVariantCommunity"
166
                    sys.exit(1)
167
                else:
168
                    NxG = params['nodes_x_group']
169
                    NG = params['groups_number']
170
                    max_x, max_y = params['max_x'], params['max_y']
171
                    min_v, max_v = params['min_velocity'], params['max_velocity']
172
                    groups = [NxG for _ in range(NG)]
173
                    nr_nodes = sum(groups)
174
175
                    tvcm = tvc(groups, dimensions=(100, 100), velocity=(min_v, max_v),
176
                               aggregation=[0.5, 0.], epoch=[100, 100])
177
                    return tvcm