In [1]:
%load_ext autoreload
%autoreload 2
In [33]:
import os
import sys
import numpy as np
import matplotlib.pyplot as plt
import hashlib
import time
import shutil
In [3]:
MNM_nb_folder = os.path.join('..', '..', '..', 'side_project', 'network_builder')
sys.path.append(MNM_nb_folder)
In [4]:
from MNM_nb import *
import MNMAPI
In [5]:
data_folder = os.path.join('..', '..', '..', 'data', 'input_files_2link_fix')
new_folder = os.path.join('MNM_cache', 'input_files_2link_fix')
In [6]:
nb = MNM_network_builder()
In [7]:
nb.load_from_folder(data_folder)
In [8]:
nb.config
Out[8]:
In [9]:
observed_links = [3]
estimated_paths = np.arange(0, 2)
In [10]:
from scipy.sparse import coo_matrix
def massage_raw_dar(raw_dar, f, num_link, ass_freq, observed_links, estimated_paths, num_interval):
num_e_path = len(estimated_paths)
num_e_link = len(observed_links)
link_seq = (np.array(map(lambda x: observed_links.index(x), raw_dar[:, 2].astype(np.int)))
+ raw_dar[:, 3] * num_e_link / ass_freq).astype((np.int))
path_seq = (raw_dar[:, 0] + raw_dar[:, 1] * num_e_path).astype(np.int)
p = raw_dar[:, 4] / f[path_seq]
mat = coo_matrix((p, (link_seq, path_seq)),
shape=(num_interval * num_e_link, num_interval * num_e_path)).toarray()
return mat
In [11]:
def get_x(f, nb, observed_link_list, estimated_paths, new_folder):
num_interval = nb.config.config_dict['DTA']['max_interval']
ass_freq = nb.config.config_dict['DTA']['assign_frq']
num_link = nb.config.config_dict['DTA']['num_of_link']
total_interval = num_interval * ass_freq
nb.config.config_dict['DTA']['total_interval'] = total_interval * 2
num_path = nb.config.config_dict['FIXED']['num_path']
nb.update_demand_path(f)
nb.dump_to_folder(new_folder)
a = MNMAPI.dta_api()
a.initialize(new_folder)
a.register_links(observed_link_list)
a.register_paths(estimated_paths)
a.install_cc()
a.install_cc_tree()
a.run_whole()
x = a.get_link_inflow(np.arange(0, total_interval, ass_freq), np.arange(0, total_interval, ass_freq) + ass_freq).reshape((len(observed_link_list), total_interval/ass_freq), order = 'F').flatten()
raw_dar = a.get_dar_matrix(np.arange(0, total_interval, ass_freq), np.arange(0, total_interval, ass_freq) + ass_freq)
dar = massage_raw_dar(raw_dar, f, num_link, ass_freq, observed_link_list, estimated_paths, num_interval)
return x, dar
In [12]:
total_interval = 100
ass_freq = 10
num_path = nb.config.config_dict['FIXED']['num_path']
num_link = nb.config.config_dict['DTA']['num_of_link']
num_interval = nb.config.config_dict['DTA']['max_interval']
In [13]:
f_real = np.random.rand(num_path * num_interval) * 10
x_real, dar = get_x(f_real, nb, observed_links, estimated_paths, new_folder)
In [14]:
x_real
Out[14]:
In [15]:
# nb.update_demand_path(f_real)
In [16]:
# nb.dump_to_folder(new_folder)
In [29]:
In [35]:
hash = hashlib.sha1()
hash.update(str(time.time()))
new_folder = str(hash.hexdigest())
nb.update_demand_path(f_real)
nb.dump_to_folder(new_folder)
a = MNMAPI.dta_api()
a.initialize(new_folder)
shutil.rmtree(new_folder)
a.register_links([3])
a.register_paths(np.arange(0, 2))
a.install_cc()
a.install_cc_tree()
a.run_whole()
Out[35]:
In [32]:
In [27]:
In [18]:
a.get_dar_matrix(np.arange(0, total_interval, ass_freq), np.arange(0, total_interval, ass_freq) + ass_freq)
Out[18]:
In [19]:
# a.get_cur_loading_interval()
In [20]:
f_e = np.random.rand(num_path * num_interval)*5
x_e, dar_e = get_x(f_e, nb, observed_links, estimated_paths, new_folder)
In [21]:
step_size = 0.1
In [22]:
for i in range(50):
x_e, dar = get_x(f_e, nb, observed_links, estimated_paths, new_folder)
grad = - dar.T.dot(x_real - x_e)
print np.linalg.norm(x_e - x_real), np.linalg.norm(f_e - f_real)
f_e -= grad * step_size / np.sqrt(i + 1)
In [23]:
f_e
Out[23]:
In [24]:
f_real
Out[24]:
In [26]:
plt.plot(f_e, f_real, '*')
plt.show()