plan_gen.py 919 B

123456789101112131415161718192021222324252627282930
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. ''' Python generator for MATSim plans. '''
  4. import numpy as np
  5. import lxml.etree as etree
  6. # constants
  7. INPUT_NETWORK = 'input/network.xml'
  8. OUTPUT_PLANS = 'out_plans.xml'
  9. NB_PERSONS = 10
  10. def get_nodes():
  11. ''' returns all network nodes as a list '''
  12. tree = etree.parse(INPUT_NETWORK)
  13. return [node for node in tree.xpath("/network/nodes/node")]
  14. def rand_person(nodes):
  15. ''' returns a person as a dict of home and work random locations '''
  16. len_nodes = len(nodes)
  17. home_node = nodes[np.random.randint(len_nodes)]
  18. work_node = nodes[np.random.randint(len_nodes)]
  19. home_xy = (home_node.get('x'), home_node.get('y'))
  20. work_xy = (work_node.get('x'), work_node.get('y'))
  21. return {'home': home_xy, 'work': work_xy}
  22. if __name__ == '__main__':
  23. NODES = get_nodes()
  24. PERSONS = [rand_person(NODES) for _ in range(NB_PERSONS)]
  25. print(PERSONS)