|
@@ -1,28 +1,49 @@
|
|
|
#!/usr/bin/env python3
|
|
|
-''' Python generator for MATSim plans. '''
|
|
|
+''' plan_gen main functions '''
|
|
|
|
|
|
-import sys
|
|
|
import time
|
|
|
import numpy as np
|
|
|
import lxml.etree as etree
|
|
|
|
|
|
# constants
|
|
|
+# ------------------
|
|
|
MIN_DEPARTURE_TIME = '08:00:00'
|
|
|
MAX_DEPARTURE_TIME = '09:00:00'
|
|
|
WORK_DURATION = '04:00:00'
|
|
|
|
|
|
-def read_nodes(input_network):
|
|
|
- ''' returns all network nodes as a list '''
|
|
|
- if not input_network:
|
|
|
- return None
|
|
|
- tree = etree.parse(input_network)
|
|
|
- return [node for node in tree.xpath("/network/nodes/node")]
|
|
|
+# utils
|
|
|
+# ------------------
|
|
|
+
|
|
|
+def parse_value(string):
|
|
|
+ ''' convert string to int, float or string '''
|
|
|
+ try:
|
|
|
+ return int(string)
|
|
|
+ except ValueError:
|
|
|
+ try:
|
|
|
+ return float(string)
|
|
|
+ except ValueError:
|
|
|
+ return string
|
|
|
+
|
|
|
+def parse_params(param_str):
|
|
|
+ ''' parse a param string to a dict '''
|
|
|
+ dict_params = {}
|
|
|
+ if param_str:
|
|
|
+ for key_value_str in param_str.split(','):
|
|
|
+ key, value = key_value_str.split('=')
|
|
|
+ if key in ['xmin', 'xmax', 'c', 'w', 'alpha']:
|
|
|
+ dict_params[key] = np.fromstring(str(value), dtype=float, sep=':')
|
|
|
+ else:
|
|
|
+ dict_params[key] = parse_value(value)
|
|
|
+ return dict_params
|
|
|
|
|
|
def get_seconds(time_str):
|
|
|
''' returns seconds from a time string '''
|
|
|
h, m, s = time_str.split(':')
|
|
|
return int(h) * 3600 + int(m) * 60 + int(s)
|
|
|
|
|
|
+# main functions
|
|
|
+# ------------------
|
|
|
+
|
|
|
def rand_time(low, high):
|
|
|
''' returns a random time between low and high bounds '''
|
|
|
low_s = get_seconds(low)
|
|
@@ -69,28 +90,9 @@ def make_plans(persons):
|
|
|
create_child(plan, 'act', {'type': 'h', 'x': p['home'][0], 'y': p['home'][1]})
|
|
|
return plans
|
|
|
|
|
|
-if __name__ == '__main__':
|
|
|
-
|
|
|
- # command line arguments
|
|
|
- if len(sys.argv) != 5:
|
|
|
- print('usage:', sys.argv[0], '<nb_persons> <input_network> <input_home_clusters> <input_work_clusters>')
|
|
|
- print('type "" if you don\'t want to use specific clusters')
|
|
|
- sys.exit(-1)
|
|
|
- NB_PERSONS = int(sys.argv[1])
|
|
|
- INPUT_NETWORK = sys.argv[2]
|
|
|
- INPUT_HOME_CLUSTERS = sys.argv[3].split(',')
|
|
|
- INPUT_WORK_CLUSTERS = sys.argv[4].split(',')
|
|
|
-
|
|
|
- # get data
|
|
|
- NODES = read_nodes(INPUT_NETWORK)
|
|
|
- HOME_CLUSTERS = [read_nodes(cluster) for cluster in INPUT_HOME_CLUSTERS]
|
|
|
- WORK_CLUSTERS = [read_nodes(cluster) for cluster in INPUT_WORK_CLUSTERS]
|
|
|
-
|
|
|
- # make xml
|
|
|
- PERSONS = [rand_person(NODES, HOME_CLUSTERS, WORK_CLUSTERS) for _ in range(NB_PERSONS)]
|
|
|
- PLANS = make_plans(PERSONS)
|
|
|
-
|
|
|
- # print XML
|
|
|
- print('<?xml version="1.0" ?>')
|
|
|
- print('<!DOCTYPE plans SYSTEM "http://www.matsim.org/files/dtd/plans_v4.dtd">')
|
|
|
- print(etree.tostring(PLANS, pretty_print=True).decode('utf-8'))
|
|
|
+def read_nodes(input_network):
|
|
|
+ ''' returns all network nodes as a list '''
|
|
|
+ if not input_network:
|
|
|
+ return None
|
|
|
+ tree = etree.parse(input_network)
|
|
|
+ return [node for node in tree.xpath("/network/nodes/node")]
|