-
Notifications
You must be signed in to change notification settings - Fork 0
/
min_cost_d_norm.py
66 lines (57 loc) · 2.13 KB
/
min_cost_d_norm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Nov 27 18:28:29 2018
@author: ellereyireland1
"""
import system as sys
import run_gravity as g
import numpy as np
import coarse_graining as cg
import numpy as np
import matplotlib.pyplot as plt
from tqdm import tqdm
import save
for i in tqdm(range(1,101)):
#Make the system
s = g.Gravity()
system = sys.System()
# set normal to False to use zipf distribution for city size
system.random_system(1000, normal=True)
#set the lists that will contain the data points
cell_areas = []
min_cost_values =[]
#set up the original system
s.set_system(system)
s.tuning_function()
s.set_flows()
#Coarse grain the system
for level in tqdm(range(2,22)):
distances = []
cost_values = []
coarse_grainer = cg.Coarse_graining(system, level)
cell_area = coarse_grainer.get_cell_area()
cell_areas.append(cell_area)
grained_system = coarse_grainer.generate_new_system()
original_flows = grained_system.flow_matrix
#get the mean value from the distance matrix and set the bounds
mean_dist = np.mean(grained_system.distance_matrix)
bound = np.sqrt(mean_dist)
distances = list(np.linspace(0.01, mean_dist + 3*bound, 100))
# for each value of distance chosen, rerun the tuning on grained system
for d in distances:
s.set_system(grained_system, distance =d)
s.tuning_function()
s.set_flows()
grained_flows = grained_system.flow_matrix
s.cost_function(original_flows, grained_flows)
cost_values.append(s.cost)
min_cost_values.append(distances[np.argmin(cost_values)])
save.save_object(min_cost_values, "cost_value_array_norm_{}".format(i), skip_dialogue = True)
save.save_object(cell_areas, "cell_area_array_norm_{}".format(i), skip_dialogue = True)
#fig = plt.figure()
#ax = fig.add_subplot(111)
#ax.plot(cell_areas, min_cost_values, 'ro')
#ax.set_xlabel("Cell area")
#ax.set_ylabel("Value of minimum cost function")
#plt.show()