|
@@ -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}
|
|
|
|