Statistics
| Branch: | Revision:

mobicen / util / ConfPars.py @ b6158841

History | View | Annotate | Download (7.06 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 b6158841 LoreBz
            elif option == 'alpha':
80
                retParameters['alpha'] = float(arg)
81 e1cf8bea LoreBz
            else:
82
                print 'Unparsed ', option, '=', settings.get(sectionExp, option)
83
        return retParameters
84
85
    def configMobility(self, params):
86 38d1e922 LoreBz
        userMobi = params['mob_model']
87 23c7ab1e LoreBz
        supportedModels = ["RandomWayPoint",
88 b6158841 LoreBz
                           "TimeVariantCommunity", "Milan", "ReferencePointGroup", "TruncatedLevyWalk", "GaussMarkov"]
89 38d1e922 LoreBz
        if userMobi not in supportedModels:
90
            print userMobi + " is not supported now!\n" \
91 b6158841 LoreBz
                "This mobility models are currently supported:"
92 38d1e922 LoreBz
            print ", ".join(supportedModels)
93 e1cf8bea LoreBz
            sys.exit(1)
94
        else:
95 38d1e922 LoreBz
            if userMobi == "RandomWayPoint":
96
                if not set(['max_x', 'max_y', 'nodes_number', 'max_velocity']).issubset(params.keys()):
97
                    print "You miss some params to configure RandomWayPoint"
98
                    sys.exit(1)
99
                else:
100
                    N = params['nodes_number']
101
                    max_x, max_y = params['max_x'], params['max_y']
102
                    min_v, max_v = params['min_velocity'], params['max_velocity']
103
                    max_wt = params['max_wait_time']
104
                    rw = random_waypoint(N, dimensions=(
105
                        max_x, max_y), velocity=(min_v, max_v), wt_max=max_wt)
106
                    return rw
107 b6158841 LoreBz
            
108 2dbcade9 LoreBz
            elif userMobi == "Milan":
109
                return milano_mob()
110
111 23c7ab1e LoreBz
            elif userMobi == "ReferencePointGroup":
112
                # Reference Point Group model
113
                NxG = params['nodes_x_group']
114
                NG = params['groups_number']
115
                aggreg = params['aggregation']
116
                max_x, max_y = params['max_x'], params['max_y']
117
                min_v, max_v = params['min_velocity'], params['max_velocity']
118
                groups = [NxG for _ in range(NG)]
119
                nr_nodes = sum(groups)
120
                params['nodes_number'] = nr_nodes
121
                rpg = reference_point_group(
122
                    groups, velocity=(min_v, max_v), dimensions=(max_x, max_y), aggregation=aggreg)
123
124
                return rpg
125 2dbcade9 LoreBz
126 b6158841 LoreBz
            elif userMobi == "TruncatedLevyWalk":
127
                N = params['nodes_number']
128
                max_x, max_y = params['max_x'], params['max_y']
129
                tlw = truncated_levy_walk(N, dimensions=(max_x, max_y))
130
                return tlw
131 38d1e922 LoreBz
132 b6158841 LoreBz
            elif userMobi == "GaussMarkov":
133
                N = params['nodes_number']
134
                max_x, max_y = params['max_x'], params['max_y']
135
                al = params['alpha']
136
                gm = gauss_markov(N, dimensions=(max_x, max_y), alpha=al)
137
                return gm
138 38d1e922 LoreBz
139 b6158841 LoreBz
            elif userMobi == "TimeVariantCommunity":
140 38d1e922 LoreBz
                if not set(['max_x', 'max_y', 'nodes_x_group', 'min_velocity',
141
                            'max_velocity', 'groups_number']).issubset(params.keys()):
142
                    print "You miss some params to configure TimeVariantCommunity"
143
                    sys.exit(1)
144
                else:
145
                    NxG = params['nodes_x_group']
146
                    NG = params['groups_number']
147
                    max_x, max_y = params['max_x'], params['max_y']
148
                    min_v, max_v = params['min_velocity'], params['max_velocity']
149
                    groups = [NxG for _ in range(NG)]
150
                    nr_nodes = sum(groups)
151
152
                    tvcm = tvc(groups, dimensions=(100, 100), velocity=(min_v, max_v),
153
                               aggregation=[0.5, 0.], epoch=[100, 100])
154
                    return tvcm