-
Notifications
You must be signed in to change notification settings - Fork 1
/
chapter6.py
218 lines (159 loc) · 5.2 KB
/
chapter6.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# Decentralised water storage model: master file - Neal Hughes
# Chapter 6 model runs
from __future__ import division
import numpy as np
from para import Para
from model import Model
from results import chapter6
from results.chartbuilder import *
import multiprocessing
from multiprocessing.queues import Queue
import sys
home = '/home/nealbob'
folder = '/Dropbox/Model/results/chapter6/'
NCI = '/short/fr3/ndh401/chapter6/'
out = '/Dropbox/Thesis/IMG/chapter6/'
para = Para()
scenarios = ['CS'] #['RS-HL-O', 'RS-HL', 'RS-O', 'RS', 'CS', 'CS-O', 'CS-HL', 'CS-HL-O', 'CS-U']
results = {scen: 0 for scen in scenarios}
Lambda = {scen: 0 for scen in scenarios}
LambdaK = {scen: 0 for scen in scenarios}
#==========================================
# Central case (with trade)
#==========================================
for scen in scenarios:
para.set_property_rights(scenario=scen)
para.aproximate_shares(nonoise=True)
mod = Model(para)
results[scen], Lambda[scen], LambdaK[scen] = mod.chapter6(sens=True)
del mod
#chapter6.tables(results, scenarios, Lambda, LambdaK, label='central')
#with open(home + folder + 'central_result.pkl', 'wb') as f:
# pickle.dump(results, f)
# f.close()
"""
#==========================================
# No trade case
#==========================================
para.t_cost = 10000000000
for scen in scenarios:
para.set_property_rights(scenario=scen)
mod = Model(para)
results[scen], Lambda[scen], LambdaK[scen] = mod.chapter6()
del mod
chapter6.tables(results, scenarios, Lambda, LambdaK, label='notrade')
with open(home + folder + 'notrade_result.pkl', 'wb') as f:
pickle.dump(results, f)
f.close()
"""
"""
#==========================================
# Risk aversion
#==========================================
para.central_case(utility=True, risk=3)
for scen in scenarios:
para.set_property_rights(scenario=scen)
mod = Model(para)
results[scen], Lambda[scen], LambdaK[scen] = mod.chapter6()
del mod
chapter6.tables(results, scenarios, Lambda, LambdaK, label='risk1', risk=True)
with open(home + folder + 'risk1_result.pkl', 'wb') as f:
pickle.dump(results, f)
f.close()
"""
"""
#==========================================
# Inflow share search
#==========================================
try:
arg1 = sys.argv[1]
arg2 = sys.argv[2]
except IndexError:
print "Provide arguments <runnum> <numofjobs>"
para.central_case(utility=True)
N = int(arg2)
paralist = []
resultlist = []
def solve_model(para, scen, que):
para.set_property_rights(scenario=scen)
para.aproximate_shares()
mod = Model(para)
stats, _, _ = mod.chapter6(sens=True)
Lambda = [mod.RSLambda, mod.CSLambda]
del mod
que.put([stats, Lambda])
def retry_on_eintr(function, *args, **kw):
while True:
try:
return function(*args, **kw)
except IOError, e:
if e.errno == errno.EINTR:
continue
else:
raise
class RetryQueue(Queue):
def get(self, block=True, timeout=None):
return retry_on_eintr(Queue.get, self, block, timeout)
for i in range(N):
#try:
res = []
para.randomize()
para.CPU_CORES = 2
paralist.append(para.para_list)
ques = [RetryQueue(), RetryQueue()]
args = [(para, 'CS', ques[0]), (para, 'CS-HL', ques[1])]
jobs = [multiprocessing.Process(target=solve_model, args=(a)) for a in args]
for j in jobs: j.start()
for q in ques: res.append(q.get())
for j in jobs: j.join()
resultlist.append(res)
#with open(NCI + 'sens_para_' + arg1 + '.pkl', 'wb') as f:
# pickle.dump(paralist, f)
# f.close()
#with open(NCI + 'sens_result_' + arg1 +'.pkl', 'wb') as f:
# pickle.dump(resultlist, f)
# f.close()
#except KeyboardInterrupt:
# raise
#except:
# pass
"""
"""
#==========================================
# Risk aversion plot
#==========================================
Y = np.zeros([3, 200])
X = np.zeros([3, 200])
risk = [0.25, 1.5, 3]
names = ['0', '1.5', '3.0']
for i in range(3):
para.central_case(N=100, printp=False, utility=True, risk=risk[i])
mod = model.Model(para)
X[i,:], Y[i,:] = mod.users.SW_f.plot(['x', 1], returndata=True)
Y[i,:] = Y[i,:] / Y[i , 199]
df = dataframe(200, 3, names, X[0,:], Y.T)
chart = {'OUTFILE': home + out + 'Risk.pdf',
'XLABEL': 'Total water use, $Q_t$',
'YLABEL': 'Social welfare (utility)',
'YMIN' : 0.2,
'YMAX' : 1.1}
build_chart(chart, df, chart_type='date', ylim=True)
para.central_case(N=100, printp=False)
para.set_property_rights(scenario='CS')
Y = np.zeros([3, 200])
X = np.linspace(0, 1, 200)
risk = [0.001, 1.5, 3]
names = ['0', '1.5', '3.0']
for i in range(3):
Y[i,:] = 1 - np.exp(-risk[i]* X)
Y[i,:] = Y[i,:] / Y[i , 199]
df = dataframe(200, 3, names, X, Y.T)
chart = {'OUTFILE': home + out + 'Risk0.pdf',
'XLABEL': 'Scaled user profit, $u_{it}/{\\bar \\pi_h}$',
'YLABEL': 'Scaled utility',
'YMIN' : 0,
'YMAX' : 1.1}
build_chart(chart, df, chart_type='date', ylim=True)
para.central_case(N=100, printp=False)
para.set_property_rights(scenario='CS')
"""