Statistics
| Branch: | Revision:

mobicen / util / ConfPars.py @ 23c7ab1e

History | View | Annotate | Download (8.2 KB)

1
from inherit_config_parser import InheritConfigParser
2
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
import sys
6
from milanoMob import milano_mob
7
import code  # code.interact(local=dict(globals(), **locals()))
8

    
9

    
10
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
    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
    def getSimulationParameters(self):
26
        settingsFile, sectionExp = self.SF, self.SEC
27
        retParameters = {}
28
        #settings = configparser.ConfigParser()
29
        #settings._interpolation = configparser.ExtendedInterpolation()
30
        settings = InheritConfigParser()
31
        settings.optionxform = str
32
        settings.read(settingsFile)
33
        mand = set([
34
            'mob_model', 'max_x', 'max_y', 'radius',
35
            'mobility_timer', 'duration'])
36
        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
        for option in opts:
42
            arg = settings.get(sectionExp, option)
43
            if option == 'duration':
44
                retParameters['duration'] = int(arg.strip('s'))
45
            elif option == 'mob_model':
46
                mobset = set(['RandoWalk', 'RandomWayPoint', 'RandomDirection', 'Milan',
47
                              '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
            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
            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
            elif option == 'nodes_x_group':
74
                retParameters['nodes_x_group'] = int(arg)
75
            elif option == 'groups_number':
76
                retParameters['groups_number'] = int(arg)
77
            elif option == 'aggregation':
78
                retParameters['aggregation'] = float(arg)
79
            else:
80
                print 'Unparsed ', option, '=', settings.get(sectionExp, option)
81
        return retParameters
82

    
83
    def configMobility(self, params):
84
        userMobi = params['mob_model']
85
        supportedModels = ["RandomWayPoint",
86
                           "TimeVariantCommunity", "Milan", "ReferencePointGroup"]
87
        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
            sys.exit(1)
92
        else:
93
            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
            elif userMobi == "Milan":
106
                return milano_mob()
107

    
108
            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

    
123
            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