-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcrisis_simulator.py
executable file
·159 lines (130 loc) · 7.16 KB
/
crisis_simulator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/python
import sha
import json
import time
import names
import random
import argparse
import Connecter
provider_role = 'provider'
consumer_role = 'consumer'
item_list = [ 'food', 'shelter', 'water', 'clothing', 'medical', 'manual labor', 'transport']
locationdb= [ "Adambakkam", "Adyar", "Alandur", "Alwarpet", "Alwarthirunagar",
"Ambattur", "Aminjikarai", "Anakaputhur", "Anna Nagar", "Annanur", "Arumbakkam",
"Ashok Nagar", "Avadi", "Ayanavaram", "Besant Nagar", "Basin Bridge", "Chepauk",
"Chetput", "Chintadripet", "Chitlapakkam", "Choolai", "Choolaimedu", "Chrompet",
"Egmore", "Ekkaduthangal", "Ennore", "Foreshore Estate", "Fort St. George",
"George Town", "Gopalapuram", "Government Estate", "Guindy", "IIT Madras",
"Injambakkam", "ICF", "Iyyapanthangal", "Jafferkhanpet", "Karapakkam",
"Kattivakkam", "Kazhipattur", "K.K. Nagar", "Keelkattalai", "Kelambakkam",
"Kilpauk", "Kodambakkam", "Kodungaiyur", "Kolathur", "Korattur", "Korukkupet",
"Kottivakkam", "Kotturpuram", "Kottur", "Kovalam", "Kovilambakkam", "Koyambedu",
"Kundrathur", "Madhavaram", "Madhavaram Milk Colony", "Madipakkam",
"Madambakkam", "Maduravoyal", "Manali", "Manali New Town", "Manapakkam",
"Mandaveli", "Mangadu", "Mannadi", "Mathur", "Medavakkam", "Meenambakkam",
"MGR Nagar", "Minjur", "Mogappair", "MKB Nagar", "Mount Road", "Moolakadai",
"Moulivakkam", "Mugalivakkam", "Mudichur", "Mylapore", "Nandanam",
"Nanganallur", "Navalur", "Neelankarai", "Nemilichery", "Nesapakkam",
"Nolambur", "Noombal", "Nungambakkam", "Otteri", "Padi", "Pakkam", "Palavakkam",
"Pallavaram", "Pallikaranai", "Pammal", "Park Town", "Parry's Corner",
"Pattabiram", "Pattaravakkam", "Pazhavanthangal", "Peerkankaranai", "Perambur",
"Peravallur", "Perumbakkam", "Perungalathur", "Perungudi", "Pozhichalur",
"Poonamallee", "Porur", "Pudupet", "Purasaiwalkam", "Puthagaram", "Puzhal",
"Puzhuthivakkam", "Raj Bhavan", "Ramavaram", "Red Hills", "Royapettah",
"Royapuram", "Saidapet", "Saligramam", "Santhome", "Sembakkam", "Selaiyur",
"Shenoy Nagar", "Sholavaram", "Sholinganallur", "Sithalapakkam", "Sowcarpet",
"St.Thomas Mount", "Tambaram", "Teynampet", "Tharamani", "T. Nagar",
"Thirumangalam", "Thirumullaivoyal", "Thiruneermalai", "Thiruninravur",
"Thiruvanmiyur", "Tiruverkadu", "Thiruvotriyur", "Tirusulam", "Tiruvallikeni",
"Tondiarpet", "United India Colony", "Vandalur", "Vadapalani", "Valasaravakkam",
"Vallalar Nagar", "Vanagaram", "Velachery", "Villivakkam", "Virugambakkam",
"Vyasarpadi", "Washermanpet", "West Mambalam" ]
available_locations = []
def parseArgs():
parser = argparse.ArgumentParser()
parser.add_argument('-u', '--number-of-users', dest='number_users', action='store',
default=100, type=int, help='Total number of users for the simulation (default: 10)')
parser.add_argument('-a', '--max-num-of-areas', dest='num_areas', action='store',
default=5, type=int, help='Total number of users for the simulation (default: 10)')
parser.add_argument('-s', '--seed', dest='seed', action='store',
help='Seed for the randomizer.')
parser.add_argument('-f', '--json-file', dest='inventory_file', action='store',
required=True, help='File Name for the inventory.')
parser.add_argument('-i', '--iter-timeout', dest='iter_timeout',
action='store', type=float, default=1.0,
help='Timeout for populating db.')
return parser.parse_args()
def generate_subscriber_subscribers( number_users ):
total_subscribers = random.randint(1, number_users)
print "Provisioning %d users in the system ..." % options.number_users
subscribers = []
for i in range(1, total_subscribers):
subscribers.append(
{'name': names.get_full_name(),
'phone': generate_phone_number(),
'offered_services': generate_offer()
})
return subscribers
def generate_phone_number():
cc = "+91"
ndc = random.randint(7000,9000)
sn = random.randint(0,999999)
return "%s%d%06d" % (cc, ndc, sn)
def generate_offer():
sample_size = random.randint(1, len(item_list))
return random.sample(item_list, sample_size)
def generate_sha1_id(role, phone, item):
input_seed = "%s:%s:%s" % (role, phone, item)
digest_gen = sha.new(input_seed)
return digest_gen.hexdigest()
def generate_initial_inventory(subscribers):
inventory = {'entries': []}
for subscriber in subscribers:
is_subscriber_in_need = random.random() < 0.70
is_subscriber_offering = random.random() < 0.50
subscriber_current_location = random.choice(available_locations)
if is_subscriber_offering:
for item in subscriber['offered_services']:
digest = generate_sha1_id(provider_role,
subscriber['phone'],
item)
entry = Connecter.Entry(digest,
provider_role,
subscriber['phone'],
item,
random.randint(1,10),
subscriber_current_location)
inventory['entries'].append( entry.dict() )
if is_subscriber_in_need:
item = random.choice(item_list)
digest = generate_sha1_id(consumer_role,
subscriber['phone'],
item)
entry = Connecter.Entry(digest,
consumer_role,
subscriber['phone'],
item,
random.randint(1,10),
subscriber_current_location)
inventory['entries'].append( entry.dict() )
return inventory
def simulate(iteration, subscribers, inventory):
print "Iteration:%d, Simulating" % iteration
if __name__ == "__main__":
# Parse input arguments
options = parseArgs()
# Set the seed to the one passed as an argument
random.seed( options.seed )
# Define the available areas for the simulation
available_locations = random.sample(locationdb, options.num_areas)
# Subscriber profile databse
subscribers = generate_subscriber_subscribers( options.number_users )
# The initialize the inventory
inventory = generate_initial_inventory(subscribers)
iteration = 0
while iteration<=1:
iteration = iteration + 1
with open(options.inventory_file, 'w') as output_file:
json.dump(inventory, output_file, indent=4, sort_keys=True)
simulate(iteration, subscribers, inventory)
time.sleep(float(options.iter_timeout))