plan_gen.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #!/usr/bin/env python3
  2. ''' Python generator for MATSim plans. '''
  3. import sys
  4. import time
  5. import numpy as np
  6. import lxml.etree as etree
  7. # constants
  8. MIN_DEPARTURE_TIME = 8 * 3600 # '08:00:00'
  9. MAX_DEPARTURE_TIME = 9 * 3600 # '09:00:00'
  10. WORK_DURATION = '04:00:00'
  11. def read_nodes(input_network):
  12. ''' returns all network nodes as a list '''
  13. tree = etree.parse(input_network)
  14. return [node for node in tree.xpath("/network/nodes/node")]
  15. def rand_time(low, high):
  16. ''' returns a random time between low and high bounds (in seconds) '''
  17. delta = np.random.randint(high - low);
  18. return time.strftime('%H:%M:%S', time.gmtime(low + delta))
  19. def rand_person(nodes):
  20. ''' returns a person as a dictionnary of random parameters '''
  21. len_nodes = len(nodes)
  22. # home coordinates
  23. home_node = nodes[np.random.randint(len_nodes)]
  24. home_xy = (home_node.get('x'), home_node.get('y'))
  25. # work coordinates
  26. work_node = nodes[np.random.randint(len_nodes)]
  27. work_xy = (work_node.get('x'), work_node.get('y'))
  28. # home departure time
  29. home_departure = rand_time(MIN_DEPARTURE_TIME, MAX_DEPARTURE_TIME)
  30. return {'home': home_xy, 'work': work_xy, 'home_departure': home_departure}
  31. def create_child(parent_node, child_name, child_attrs={}):
  32. ''' creates an xml child element and set its attributes '''
  33. child = etree.SubElement(parent_node, child_name)
  34. for attr, value in child_attrs.items():
  35. child.set(attr, value)
  36. return child
  37. def make_plans(persons):
  38. ''' makes xml tree of plans based on persons list '''
  39. plans = etree.Element('plans')
  40. for n, p in enumerate(persons):
  41. person = create_child(plans, 'person', {'id': str(n+1)})
  42. plan = create_child(person, 'plan')
  43. # plan
  44. create_child(plan, 'act', {'type': 'h', 'x': p['home'][0], 'y': p['home'][1], 'end_time': p['home_departure']})
  45. create_child(plan, 'leg', {'mode': 'car'})
  46. create_child(plan, 'act', {'type': 'w', 'x': p['work'][0], 'y': p['work'][1], 'dur': WORK_DURATION})
  47. create_child(plan, 'leg', {'mode': 'car'})
  48. create_child(plan, 'act', {'type': 'h', 'x': p['home'][0], 'y': p['home'][1]})
  49. return plans
  50. if __name__ == '__main__':
  51. # command line arguments
  52. if len(sys.argv) != 3:
  53. print('usage:', sys.argv[0], '<input_network> <nb_persons>')
  54. sys.exit(-1)
  55. INPUT_NETWORK = sys.argv[1]
  56. NB_PERSONS = int(sys.argv[2])
  57. NODES = read_nodes(INPUT_NETWORK)
  58. PERSONS = [rand_person(NODES) for _ in range(NB_PERSONS)]
  59. PLANS = make_plans(PERSONS)
  60. # print XML
  61. print('<?xml version="1.0" ?>')
  62. print('<!DOCTYPE plans SYSTEM "http://www.matsim.org/files/dtd/plans_v4.dtd">')
  63. print(etree.tostring(PLANS, pretty_print=True).decode('utf-8'))