/
CNLSG.py
213 lines (188 loc) · 9.48 KB
/
CNLSG.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
# import dependencies
import numpy as np
import pandas as pd
from .utils import CNLSG1, CNLSG2, CNLSZG1, CNLSZG2, sweet, tools, interpolation
from .constant import CET_ADDI, CET_MULT, FUN_PROD, FUN_COST, OPT_DEFAULT, RTS_CRS, RTS_VRS, OPT_DEFAULT, OPT_LOCAL
import time
class CNLSG:
"""Convex Nonparametric Least Square (CNLS) with Genetic algorithm
"""
def __init__(self, y, x, z=None, cet=CET_ADDI, fun=FUN_PROD, rts=RTS_VRS):
"""CNLSG model
Args:
y (float): output variable.
x (float): input variables.
z (float, optional): Contextual variable(s). Defaults to None.
cet (String, optional): CET_ADDI (additive composite error term) or CET_MULT (multiplicative composite error term). Defaults to CET_ADDI.
fun (String, optional): FUN_PROD (production frontier) or FUN_COST (cost frontier). Defaults to FUN_PROD.
rts (String, optional): RTS_VRS (variable returns to scale) or RTS_CRS (constant returns to scale). Defaults to RTS_VRS.
"""
# TODO(error/warning handling): Check the configuration of the model exist
self.cutactive = sweet.sweet(x)
self.y, self.x, self.z = tools.assert_valid_basic_data(y, x, z)
self.cet, self.fun, self.rts = cet, fun, rts
# active (added) violated concavity constraint by iterative procedure
self.active = np.zeros((len(x), len(x)))
# violated concavity constraint
self.active2 = np.zeros((len(x), len(x)))
# Optimize model
self.optimization_status, self.problem_status = 0, 0
def optimize(self, email=OPT_LOCAL, solver=OPT_DEFAULT):
"""Optimize the function by requested method"""
# TODO(error/warning handling): Check problem status after optimization
self.t0 = time.time()
if type(self.z) != type(None):
model1 = CNLSZG1.CNLSZG1(
self.y, self.x, self.z, self.cutactive, self.cet, self.fun, self.rts)
else:
model1 = CNLSG1.CNLSG1(
self.y, self.x, self.cutactive, self.cet, self.fun, self.rts)
model1.optimize(email, solver)
self.alpha = model1.get_alpha()
self.beta = model1.get_beta()
self.__model__ = model1.__model__
self.count = 0
while self.__convergence_test(self.alpha, self.beta) > 0.0001:
if type(self.z) != type(None):
model2 = CNLSZG2.CNLSZG2(
self.y, self.x, self.z, self.active, self.cutactive, self.cet, self.fun, self.rts)
else:
model2 = CNLSG2.CNLSG2(
self.y, self.x, self.active, self.cutactive, self.cet, self.fun, self.rts)
model2.optimize(email, solver)
self.alpha = model2.get_alpha()
self.beta = model2.get_beta()
# TODO: Replace print with log system
print("Genetic Algorithm Convergence : %8f" %
(self.__convergence_test(self.alpha, self.beta)))
self.__model__ = model2.__model__
self.count += 1
self.optimization_status = 1
self.tt = time.time() - self.t0
def __convergence_test(self, alpha, beta):
x = np.asarray(self.x)
activetmp1 = 0.0
# go into the loop
for i in range(len(x)):
activetmp = 0.0
# go into the sub-loop and find the violated concavity constraints
for j in range(len(x)):
if self.cet == CET_ADDI:
if self.rts == RTS_VRS:
if self.fun == FUN_PROD:
self.active2[i, j] = alpha[i] + np.sum(beta[i, :] * x[i, :]) - \
alpha[j] - np.sum(beta[j, :] * x[i, :])
elif self.fun == FUN_COST:
self.active2[i, j] = - alpha[i] - np.sum(beta[i, :] * x[i, :]) + \
alpha[j] + np.sum(beta[j, :] * x[i, :])
if self.rts == RTS_CRS:
if self.fun == FUN_PROD:
self.active2[i, j] = np.sum(beta[i, :] * x[i, :]) \
- np.sum(beta[j, :] * x[i, :])
elif self.fun == FUN_COST:
self.active2[i, j] = - np.sum(beta[i, :] * x[i, :]) \
+ np.sum(beta[j, :] * x[i, :])
if self.cet == CET_MULT:
if self.rts == RTS_VRS:
if self.fun == FUN_PROD:
self.active2[i, j] = alpha[i] + np.sum(beta[i, :] * x[i, :]) - \
alpha[j] - np.sum(beta[j, :] * x[i, :])
elif self.fun == FUN_COST:
self.active2[i, j] = - alpha[i] - np.sum(beta[i, :] * x[i, :]) + \
alpha[j] + np.sum(beta[j, :] * x[i, :])
if self.rts == RTS_CRS:
if self.fun == FUN_PROD:
self.active2[i, j] = np.sum(beta[i, :] * x[i, :]) - \
np.sum(beta[j, :] * x[i, :])
elif self.fun == FUN_COST:
self.active2[i, j] = - np.sum(beta[i, :] * x[i, :]) + \
np.sum(beta[j, :] * x[i, :])
if self.active2[i, j] > activetmp:
activetmp = self.active2[i, j]
# find the maximal violated constraint in sub-loop and added into the active matrix
for j in range(len(x)):
if self.active2[i, j] >= activetmp and activetmp > 0:
self.active[i, j] = 1
if activetmp > activetmp1:
activetmp1 = activetmp
return activetmp
def display_status(self):
"""Display the status of problem"""
print(self.optimization_status)
def display_alpha(self):
"""Display alpha value"""
tools.assert_optimized(self.optimization_status)
tools.assert_various_return_to_scale(self.rts)
self.__model__.alpha.display()
def display_beta(self):
"""Display beta value"""
tools.assert_optimized(self.optimization_status)
self.__model__.beta.display()
def display_lamda(self):
"""Display lamda value"""
tools.assert_optimized(self.optimization_status)
tools.assert_contextual_variable(self.z)
self.__model__.lamda.display()
def display_residual(self):
"""Dispaly residual value"""
tools.assert_optimized(self)
self.__model__.epsilon.display()
def get_status(self):
"""Return status"""
return self.optimization_status
def get_alpha(self):
"""Return alpha value by array"""
tools.assert_optimized(self)
tools.assert_various_return_to_scale(self.rts)
alpha = list(self.__model__.alpha[:].value)
return np.asarray(alpha)
def get_beta(self):
"""Return beta value by array"""
tools.assert_optimized(self.optimization_status)
beta = np.asarray([i + tuple([j]) for i, j in zip(list(self.__model__.beta),
list(self.__model__.beta[:, :].value))])
beta = pd.DataFrame(beta, columns=['Name', 'Key', 'Value'])
beta = beta.pivot(index='Name', columns='Key', values='Value')
return beta.to_numpy()
def get_residual(self):
"""Return residual value by array"""
tools.assert_optimized(self.optimization_status)
residual = list(self.__model__.epsilon[:].value)
return np.asarray(residual)
def get_lamda(self):
"""Return beta value by array"""
tools.assert_optimized(self.optimization_status)
tools.assert_contextual_variable(self.z)
lamda = list(self.__model__.lamda[:].value)
return np.asarray(lamda)
def get_frontier(self):
"""Return estimated frontier value by array"""
tools.assert_optimized(self.optimization_status)
if self.cet == CET_MULT:
frontier = np.exp(np.log(np.asarray(self.y)) - self.get_residual())
elif self.cet == CET_ADDI:
frontier = np.asarray(self.y) - self.get_residual()
return np.asarray(frontier)
def get_totalconstr(self):
"""Return the number of total constraints"""
tools.assert_optimized(self.optimization_status)
activeconstr = np.sum(self.active) - np.trace(self.active)
cutactiveconstr = np.sum(self.cutactive) - np.trace(self.cutactive)
totalconstr = activeconstr + cutactiveconstr + 2 * len(self.active) + 1
return totalconstr
def get_runningtime(self):
"""Return the running time"""
tools.assert_optimized(self.optimization_status)
return self.tt
def get_blocks(self):
"""Return the number of blocks"""
tools.assert_optimized(self.optimization_status)
return self.count
def get_predict(self, x_test):
"""Return the estimated function in testing sample"""
tools.assert_optimized(self.optimization_status)
if self.rts == RTS_VRS:
alpha, beta = self.get_alpha(), self.get_beta()
elif self.rts == RTS_CRS:
alpha, beta = np.zeros((self.get_beta()).shape[0]), self.get_beta()
return interpolation.interpolation(alpha, beta, x_test, fun=self.fun)