Statistics
| Branch: | Revision:

mobicen / util / ConfPars.py @ e1cf8bea

History | View | Annotate | Download (3.74 KB)

1
import configparser  # https://docs.python.org/2.7/library/configparser.html
2
from pymobility.models.mobility import gauss_markov, reference_point_group, \
3
    tvc, truncated_levy_walk, random_direction, random_waypoint, random_walk
4

    
5
class ConfSettings(object):
6

    
7
    def __init__(self, settingsFile, sectionExp):
8
        self.SF = settingsFile
9
        self.SEC = sectionExp
10
        # return self.getSimulationParameters(settingsFile, sectionExp)
11

    
12
    def getSimulationParameters(self):
13
        settingsFile, sectionExp = self.SF, self.SEC
14
        retParameters = {}
15
        settings = configparser.ConfigParser()
16
        settings._interpolation = configparser.ExtendedInterpolation()
17
        settings.read(settingsFile)
18
        mand = set([
19
            'mob_model', 'max_x', 'max_y', 'nodes_number', 'max_velocity',
20
            'radius', 'mobility_timer', 'min_velocity', 'duration', 'max_wait_time'
21
        ])
22
        opts = dict(settings.items(sectionExp))
23
        if (not mand.issubset(opts.keys())):
24
            sys.exit('You are missing a mandatory parameter!\n'
25
                     'These are required:\n' + ', '.join(mand))
26

    
27
        for option in settings.options(sectionExp):
28
            arg = settings.get(sectionExp, option)
29
            #code.interact(local=dict(globals(), **locals()))
30
            if option == 'duration':
31
                retParameters['duration'] = int(arg.strip('s'))
32
            elif option == 'mob_model':
33
                mobset = set(['RandoWalk', 'RandomWayPoint', 'RandomDirection',
34
                              'TruncatedLevyWalk', 'GaussMarkov', 'ReferencePointGroup', 'TimeVariantCommunity'])
35
                if arg not in mobset:
36
                    sys.exit(arg+' is not a mobility model supported!\n'
37
                             'These are supported:\n' + ', '.join(mobset))
38
                retParameters['mob_model'] = arg
39
            elif option == 'max_x':
40
                retParameters['max_x'] = int(arg)
41
            elif option == 'max_y':
42
                retParameters['max_y'] = int(arg)
43
            elif option == 'nodes_number':
44
                retParameters['nodes_number'] = int(arg)
45
            elif option == 'max_velocity':
46
                retParameters['max_velocity'] = float(arg)
47
            elif option == 'min_velocity':
48
                retParameters['min_velocity'] = float(arg)
49
            elif option == 'max_wait_time':
50
                retParameters['max_wait_time'] = float(arg)
51
            elif option == 'radius':
52
                retParameters['radius'] = float(arg)
53
            elif option == 'mobility_timer':
54
                retParameters['mobility_timer'] = float(arg.strip('s'))
55
            else:
56
                print 'Unparsed ', option, '=', settings.get(sectionExp, option)
57
        return retParameters
58

    
59
    def configMobility(self, params):
60
        '''# Time-variant Community Mobility Model
61
        groups = [4 for _ in range(10)]
62
        nr_nodes = sum(groups)
63
        tvcm = tvc(groups, dimensions=(100, 100),
64
                   aggregation=[0.5, 0.], epoch=[100, 100])
65
        #return tvcm'''
66
        if params['mob_model'] != "RandomWayPoint":
67
            print "Only RandomWayPoint supported now!"
68
            sys.exit(1)
69
        else:
70
            if not set(['max_x', 'max_y', 'nodes_number', 'max_velocity']).issubset(params.keys()):
71
                print "You miss some params to configure RandomWayPoint"
72
                sys.exit(1)
73
            else:
74
                N = params['nodes_number']
75
                max_x, max_y = params['max_x'], params['max_y']
76
                min_v, max_v = params['min_velocity'], params['max_velocity']
77
                max_wt = params['max_wait_time']
78
                rw = random_waypoint(N, dimensions=(
79
                    max_x, max_y), velocity=(min_v, max_v), wt_max=max_wt)
80
                return rw