-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathallocate.py
159 lines (128 loc) · 4.73 KB
/
allocate.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# #############################################
# Runs the PSRC allocation mode of urbansim.
# #############################################
# Set the name of the input file as well as various other allocation-related settings
# in configs/settings_allocation.yaml
# Set generic (non-allocation related) settings in configs/settings.yaml
# Set the base year in psrc_urbansim/datasources.py
# #############################################
import os
import time
import psrc_urbansim.models
import psrc_urbansim.workplace_models
import psrc_urbansim.developer_models
import orca
import yaml
import logging
import pandas as pd
import psrc_urbansim.vars.variables_interactions
import psrc_urbansim.vars.variables_generic
from urbansim.utils import yamlio
from urbansim.utils import misc
from psrc_urbansim.utils import deep_merge
debug = False # switch this to True for detailed debug messages
log_into_file = True # should log messages go into a file (True) or be printed into the console (False)
FORMAT = '%(asctime)s %(name)s %(levelname)s %(message)s'
timestr = time.strftime("%Y%m%d")
log_file = None
if debug:
if log_into_file:
log_file = "log_allocation_debug_" + timestr + ".txt"
loglevel = logging.DEBUG
else:
if log_into_file:
log_file = "log_allocation_" + timestr + ".txt"
loglevel = logging.INFO
logging.basicConfig(level = loglevel, filename = log_file, format = FORMAT, datefmt = '%H:%M:%S', filemode = 'w')
@orca.injectable('simfile')
def simfile():
return "results_alloc_" + timestr + ".h5"
@orca.injectable('settings', cache=True)
def settings():
# merges settings.yaml with settings_allocation.yaml
with open(os.path.join(misc.configs_dir(), "settings.yaml")) as f:
settings = yaml.load(f, Loader=yaml.FullLoader)
with open(os.path.join(misc.configs_dir(), "settings_allocation.yaml")) as af:
deep_merge(yaml.load(af, Loader=yaml.FullLoader), settings)
# monkey patch on the settings object since it's pretty global
# but will also be available as injectable
orca.settings = settings
return settings
@orca.injectable('control_years', cache=True)
def control_years():
return list(range(2020, 2041, 5)) + [2044, 2050]
@orca.injectable('isCY', cache=False)
def isCY(year, control_years):
return year in control_years
# remove results file if exists
outfile = simfile()
if os.path.exists(outfile):
os.remove(outfile)
settings = settings()
# get tables from input file
def tables_in_base_year():
h5store = pd.HDFStore(os.path.join(misc.data_dir(), settings['store']), mode="r")
store_table_names = orca.get_injectable('store_table_names_dict')
return [t for t in orca.list_tables() if t in h5store or store_table_names.get(t, "UnknownTable") in h5store]
# models for control years
orca.run([
'start_year',
# Misc
#######
"update_household_previous_building_id",
"update_buildings_lag1",
# REPM
#######
"repmres_simulate", # residential REPM
"repmnr_simulate", # non-residential REPM
# Transition
#######
# (Must run hh/job transition models first in order to
# determine the demand for the developer model.)
"households_transition_alloc", #
"jobs_transition_alloc", #
# Developer
#######
"process_mpds",
"cap_residential_development",
"cap_nonresidential_development",
"proforma_feasibility_alloc",
"developer_picker_alloc",
# Density boosts
#######
"boost_residential_density",
"boost_nonresidential_density",
# Events models
#######
'households_events_model',
'households_zone_control_hct_events_model',
# Transition again, in case events model changed the counts
#######
"households_transition_alloc", #
"jobs_transition_alloc", #
# Relocate and place households
#######
"households_relocation_alloc",
"hlcm_simulate_alloc",
# Relocate and place jobs
#######
"jobs_relocation_alloc",
"elcm_simulate_alloc", # ELCM
"governmental_jobs_scaling_alloc",
"update_persons_jobs",
# Scaling of unplaced HHs and jobs in control years
#######
'scaling_unplaced_households',
'scaling_unplaced_jobs',
# Workplace models
#######
"wahcm_simulate_alloc",
"wplcm_simulate",
# Cleanup subreg_id
#######
"delete_subreg_geo_from_households",
"delete_subreg_geo_from_jobs",
'end_year'
], iter_vars=range(2024,2051), data_out=outfile, out_base_tables=tables_in_base_year(),
compress=True, out_run_local=True)
logging.info('Allocation finished')