ソースを参照

working "grid" clusters

Florian 7 年 前
コミット
c49c1d62da
2 ファイル変更26 行追加23 行削除
  1. 16 14
      plan_gen/plan_gen.py
  2. 10 9
      plan_gen/plan_gen_cli.py

+ 16 - 14
plan_gen/plan_gen.py

@@ -38,7 +38,7 @@ def get_seconds(time_str):
     h, m, s = time_str.split(':')
     return int(h) * 3600 + int(m) * 60 + int(s)
 
-def make_gaussian(size, radius=10, center=None):
+def make_gaussian(size, center=None, radius=10):
     ''' make a square gaussian kernel '''
     x = np.arange(0, size, 1, float)
     y = x[:, np.newaxis]
@@ -70,13 +70,13 @@ def make_clusters(nb_clusters, nodes):
         clusters[i][j] += [node]
     return clusters
 
-def make_densities(nb_clusters, radius, centers):
+def make_densities(nb_clusters, centers=None, radius=None):
     ''' make a list of gaussian probability densities '''
     densities = np.zeros((nb_clusters, nb_clusters))
     if centers is None:
-        return densities + 1
+        return make_gaussian(nb_clusters, radius=nb_clusters/2)
     for n, c in enumerate(centers):
-        densities += make_gaussian(nb_clusters, radius=radius[n], center=c)
+        densities += make_gaussian(nb_clusters, center=c, radius=radius[n])
     return densities
 
 # random generators
@@ -89,19 +89,21 @@ def rand_time(low, high):
     delta = np.random.randint(high_s - low_s)
     return time.strftime('%H:%M:%S', time.gmtime(low_s + delta))
 
-def rand_node_xy(nodes, clusters):
-    ''' returns a random node coordinates from a list of nodes '''
-    used_nodes = nodes
-    if any(clusters):
-        cluster = np.random.randint(len(clusters))
-        used_nodes = clusters[cluster]
-    node = used_nodes[np.random.randint(len(used_nodes))]
+def rand_node_xy(nodes, clusters, densities):
+    ''' returns a random node coordinates from a random cluster '''
+    clusters = clusters.flatten()
+    densities = densities.flatten()
+    cluster = np.random.choice(clusters, p=densities/sum(densities))
+    if cluster is not None:
+        node = cluster[np.random.randint(len(cluster))]
+    else:
+        node = nodes[np.random.randint(len(nodes))]
     return (node.get('x'), node.get('y'))
 
-def rand_person(nodes, home_clusters, work_clusters):
+def rand_person(nodes, clusters, h_dens, w_dens):
     ''' returns a person as a dictionnary of random parameters '''
-    home_xy = rand_node_xy(nodes, home_clusters)
-    work_xy = rand_node_xy(nodes, work_clusters)
+    home_xy = rand_node_xy(nodes, clusters, h_dens)
+    work_xy = rand_node_xy(nodes, clusters, w_dens)
     home_departure = rand_time(MIN_DEPARTURE_TIME, MAX_DEPARTURE_TIME)
     return {'home': home_xy, 'work': work_xy, 'home_departure': home_departure}
 

+ 10 - 9
plan_gen/plan_gen_cli.py

@@ -8,7 +8,7 @@ import plan_gen.plan_gen as pg
 
 # TODO: make these constants as user params
 D_RADIUS = [10, 5]
-D_CENTERS = [(20, 20), (5,5)]
+D_CENTERS = [(10, 5), (5,5)]
 
 if __name__ == '__main__':
 
@@ -25,13 +25,14 @@ if __name__ == '__main__':
     # prepare data
     NODES = pg.get_nodes(INPUT_NETWORK)
     CLUSTERS = pg.make_clusters(NB_CLUSTERS, NODES)
-    D_CLUSTERS = pg.make_densities(NB_CLUSTERS, D_RADIUS, D_CENTERS)
+    H_DENSITIES = pg.make_densities(NB_CLUSTERS, D_CENTERS, D_RADIUS)
+    W_DENSITIES = pg.make_densities(NB_CLUSTERS)
 
-    # # make xml
-    # PERSONS = [pg.rand_person(NODES, HOME_CLUSTERS, WORK_CLUSTERS) for _ in range(NB_PERSONS)]
-    # PLANS = pg.make_plans(PERSONS)
+    # make xml
+    PERSONS = [pg.rand_person(NODES, CLUSTERS, H_DENSITIES, W_DENSITIES) for _ in range(NB_PERSONS)]
+    PLANS = pg.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'))
+    # 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'))