plan_gen_cli.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #!/usr/bin/env python3
  2. ''' Python generator for MATSim plans. '''
  3. import sys
  4. import lxml.etree as etree
  5. import numpy as np
  6. import joblib as jl
  7. import plan_gen.plan_gen as pg
  8. NB_CORES = 4
  9. def run_rand_person(i):
  10. ''' parallelisation '''
  11. np.random.seed(SEEDS[i])
  12. return [pg.rand_person(NODES, CLUSTERS, H_DENSITIES, W_DENSITIES)
  13. for _ in range(int(NB_PERSONS/NB_CORES))]
  14. if __name__ == '__main__':
  15. # command line arguments
  16. if len(sys.argv) != 2:
  17. print('usage:', sys.argv[0], '<params>')
  18. sys.exit(-1)
  19. DICT_PARAMS = pg.parse_params(sys.argv[1])
  20. NB_CLUSTERS = DICT_PARAMS['nc']
  21. NB_PERSONS = DICT_PARAMS['np']
  22. INPUT_NETWORK = DICT_PARAMS['nw']
  23. H_CENTERS = DICT_PARAMS['hc'] if 'hc' in DICT_PARAMS else None
  24. W_CENTERS = DICT_PARAMS['wc'] if 'wc' in DICT_PARAMS else None
  25. H_RADIUS = DICT_PARAMS['hr'] if 'hr' in DICT_PARAMS else None
  26. W_RADIUS = DICT_PARAMS['wr'] if 'wr' in DICT_PARAMS else None
  27. # prepare data
  28. NODES = pg.get_nodes(INPUT_NETWORK)
  29. CLUSTERS = pg.make_clusters(NB_CLUSTERS, NODES)
  30. H_DENSITIES = pg.make_densities(NB_CLUSTERS, H_CENTERS, H_RADIUS)
  31. W_DENSITIES = pg.make_densities(NB_CLUSTERS, W_CENTERS, W_RADIUS)
  32. # make xml
  33. SEEDS = np.random.random_integers(0, 1e9, NB_CORES)
  34. PERSONS = jl.Parallel(n_jobs=NB_CORES)(jl.delayed(run_rand_person)(i) for i in range(NB_CORES))
  35. PERSONS = sum(PERSONS,[])
  36. PLANS = pg.make_plans(PERSONS)
  37. # print XML
  38. print('<?xml version="1.0" ?>')
  39. print('<!DOCTYPE plans SYSTEM "http://www.matsim.org/files/dtd/plans_v4.dtd">')
  40. print(etree.tostring(PLANS, pretty_print=True).decode('utf-8'))