-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFUNC.py
More file actions
118 lines (72 loc) · 3.72 KB
/
FUNC.py
File metadata and controls
118 lines (72 loc) · 3.72 KB
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
import numpy as np
def Production(StockMat, E_CZ, IOV_t, E_VA, OrderMat, IOX_0, NN, mat_key, OverProd):
# Calculate the actual production considering both capacity and orders
# StockMat - amount of available intermediate input
# E_CZ - input coefficients to produce one unit of x
# IOV_t - amount of available production factor input at time step t
# E_VA - input coefficients to produce one unit of x
# OrderMat - order matrix
# IOX_0 - base level of output
# RR - number of countries 189
# NN - number of sectors 26
# UU - number of factors 1
ZX = StockMat / E_CZ #(NN,RRNN)
ZX[np.isnan(ZX)] = 1e20
temp = OverProd * np.tile(IOX_0, (NN, 1)) #(NN,RRNN)
ZX[mat_key] = temp[mat_key]
VX = IOV_t / E_VA #(1,RRNN) production capacity
VX[np.isnan(VX)] = 1e20
OX = np.sum(OrderMat, axis=1).reshape(-1, 1).T #(1,RRNN) total demand sum for each row
result = np.vstack((ZX, VX, OX)) #(NN+1+1,RRNN)
return np.min(result, axis=0)
def Production_max(StockMat, E_CZ, IOV_t, E_VA, IOX_0, NN, mat_key, OverProd):
# Calculate the maximum production capacity with available intermediate stock and production factor input
ZX = StockMat / E_CZ
ZX[np.isnan(ZX)] = 1e20
temp = OverProd * np.tile(IOX_0, (NN, 1)) #(NN,RRNN)
ZX[mat_key] = temp[mat_key]
VX = IOV_t / E_VA #(NN,RRNN)
VX[np.isnan(VX)] = 1e20
result = np.vstack((ZX, VX)) #(NN+1,RRNN)
return np.min(result, axis=0)
def OverProdSignFun(StockMat, E_CZ, IOV_t, E_VA, OrderMat, IOX_0, UU, RR, NN):
# Determine the sign or direction of temporal production expansion
Result = np.zeros((UU, RR*NN))
ZX = StockMat / E_CZ # (RRNN) x (RRNN)
ZX[np.isnan(ZX)] = 1e20
VX = IOV_t / E_VA # UU x (RR*NN) production capacity
OX = np.sum(OrderMat, axis=1).reshape(1, -1) # 1 x (RR*NN)
ZOX = np.vstack((ZX, OX)) #(UU+NN, RRNN)
V1U = VX.repeat(ZOX.shape[0], axis=0) < ZOX # repeat VX along axis 0 to match the shape of ZOX
Result[0, np.where(np.sum(V1U, axis=0) == ZOX.shape[0])] = 1 #sum of True values
V1D = VX.repeat(ZOX.shape[0], axis=0) > ZOX
Result[0, np.where(np.sum(V1D, axis=0) > 0)] = -1
temp = np.abs(OX - VX) / IOX_0 # the gap
OverProdSign = Result * temp # 1 x (RR*NN)
OverProdSign[np.isnan(OverProdSign)] = 0
return OverProdSign # UU x (RR*NN)
def Production_bottleneck(StockMat, E_CZ, IOV_t, E_VA, IOX_0, NN, mat_key, OverProd):
# Calculate the maximum production capacity with available intermediate stock and production factor input
ZX = StockMat / E_CZ
ZX[np.isnan(ZX)] = 1e20
temp = OverProd * np.tile(IOX_0, (NN, 1)) # (NN, RRNN)
ZX[mat_key] = temp[mat_key]
VX = IOV_t / E_VA # (NN, RRNN)
VX[np.isnan(VX)] = 1e20
result = np.vstack((ZX, VX)) # (NN+1, RRNN)
final = np.min(result, axis=0)
final = np.array(final).reshape(1, -1) # (1,RRNN)
# Initialize a list to store row indices for each column
prodchoke_indices_list = []
# Iterate over each column
for col_idx in range(final.shape[1]):
# Find the indices where ZX's column is equal to the final's corresponding column
col_indices = np.where(ZX[:, col_idx] == final[0, col_idx])[0]
# Append the indices to the list
prodchoke_indices_list.append(col_indices)
# Ensure that all elements in the list have the same length
max_len = max(len(lst) for lst in prodchoke_indices_list)
prodchoke_indices_list = [np.concatenate([lst, np.full(max_len - len(lst), np.nan)]) for lst in prodchoke_indices_list]
# Convert the list to a numpy array
prodchoke_indices_array = np.array(prodchoke_indices_list)
return final, prodchoke_indices_array