Spaces:
Runtime error
Runtime error
Plotting hdmr with the original function is added.
Browse files- .gitignore +2 -1
- src/functions.py +10 -0
- src/main.py +48 -5
.gitignore
CHANGED
|
@@ -1,3 +1,4 @@
|
|
| 1 |
src/__pycache__/
|
| 2 |
deneme.py
|
| 3 |
-
results
|
|
|
|
|
|
| 1 |
src/__pycache__/
|
| 2 |
deneme.py
|
| 3 |
+
results/*.png
|
| 4 |
+
results/*.txt
|
src/functions.py
CHANGED
|
@@ -3,6 +3,16 @@ import numpy as np
|
|
| 3 |
# ------- BEGIN FUNCTIONS -------
|
| 4 |
|
| 5 |
# 2d Camel3
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
def camel3_2d(X):
|
| 7 |
try:
|
| 8 |
X.shape[1]
|
|
|
|
| 3 |
# ------- BEGIN FUNCTIONS -------
|
| 4 |
|
| 5 |
# 2d Camel3
|
| 6 |
+
def testfunc_2d(x):
|
| 7 |
+
if len(x.shape) == 2:
|
| 8 |
+
N, n = x.shape
|
| 9 |
+
axis = 1
|
| 10 |
+
else:
|
| 11 |
+
n = len(x)
|
| 12 |
+
axis = 0
|
| 13 |
+
y = np.sum(x**2, axis=axis, keepdims=True)
|
| 14 |
+
return y
|
| 15 |
+
|
| 16 |
def camel3_2d(X):
|
| 17 |
try:
|
| 18 |
X.shape[1]
|
src/main.py
CHANGED
|
@@ -104,6 +104,8 @@ def hdmr_opt(fun, x0, args=(), jac=None, callback=None,
|
|
| 104 |
return f
|
| 105 |
|
| 106 |
def plot_results():
|
|
|
|
|
|
|
| 107 |
f = np.zeros((N,n))
|
| 108 |
y = fun(xs)
|
| 109 |
for idx in range(n):
|
|
@@ -138,6 +140,43 @@ def hdmr_opt(fun, x0, args=(), jac=None, callback=None,
|
|
| 138 |
axs[jj-1, 1].legend()
|
| 139 |
plt.subplots_adjust(hspace=0.6, wspace=0.3)
|
| 140 |
plt.savefig(file_name + '.png')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
|
| 142 |
def calculate_distances(x0, arr):
|
| 143 |
return np.sqrt(np.sum((x0 - np.array(arr)) ** 2, axis=1))
|
|
@@ -219,9 +258,13 @@ def hdmr_opt(fun, x0, args=(), jac=None, callback=None,
|
|
| 219 |
new_x0 = np.array(result.x)
|
| 220 |
old_a = new_a
|
| 221 |
old_b = new_b
|
| 222 |
-
|
|
|
|
|
|
|
|
|
|
| 223 |
else:
|
| 224 |
-
|
|
|
|
| 225 |
xs = (b-a)*np.random.random((N,n))+a # Generate sampling data
|
| 226 |
print('XS: ', xs.shape)
|
| 227 |
alpha = calculate_alpha_coeff(xs)
|
|
@@ -233,6 +276,7 @@ def hdmr_opt(fun, x0, args=(), jac=None, callback=None,
|
|
| 233 |
temp_status.append(status.x[0])
|
| 234 |
result = OptimizeResult(x=temp_status, fun=fun(x0, *args), success=True, message=" ", nfev=1, njev=0, nhev=0)
|
| 235 |
result.nfev = N
|
|
|
|
| 236 |
|
| 237 |
return result
|
| 238 |
|
|
@@ -243,8 +287,8 @@ parser = argparse.ArgumentParser(
|
|
| 243 |
parser.add_argument('--numSamples', type=int, help='Number of samples to calculate alpha coefficients.', required=True)
|
| 244 |
parser.add_argument('--numVariables', type=int, help='Number of variable of the test function.', required=True)
|
| 245 |
parser.add_argument('--function', help='Test function name.', required=True)
|
| 246 |
-
parser.add_argument('--min', type=
|
| 247 |
-
parser.add_argument('--max', type=
|
| 248 |
parser.add_argument('--randomInit', action='store_true', help='Initializes x0 as random numbers in the range of xs. Default is initializing as 0.')
|
| 249 |
parser.add_argument('--legendreDegree', type=int, default=7, help='Number of legendre polynomial. Default is 7.')
|
| 250 |
parser.add_argument('--adaptive', action='store_true', help='Uses iterative method when set.')
|
|
@@ -294,7 +338,6 @@ if __name__ == "__main__":
|
|
| 294 |
|
| 295 |
# status_bfgs = minimize(test_function, x0, method="BFGS") # Applying direct optimization method to the function
|
| 296 |
# print(f"BFGS status: {status_bfgs}")
|
| 297 |
-
|
| 298 |
status_hdmr = minimize(test_function, x0, args=(), method=hdmr_opt) # Applying hdmr-opt method to the function
|
| 299 |
print(f"hdmr_opt status: {status_hdmr}")
|
| 300 |
|
|
|
|
| 104 |
return f
|
| 105 |
|
| 106 |
def plot_results():
|
| 107 |
+
a = a_
|
| 108 |
+
b = b_
|
| 109 |
f = np.zeros((N,n))
|
| 110 |
y = fun(xs)
|
| 111 |
for idx in range(n):
|
|
|
|
| 140 |
axs[jj-1, 1].legend()
|
| 141 |
plt.subplots_adjust(hspace=0.6, wspace=0.3)
|
| 142 |
plt.savefig(file_name + '.png')
|
| 143 |
+
|
| 144 |
+
def plot_with_function():
|
| 145 |
+
a = a_
|
| 146 |
+
b = b_
|
| 147 |
+
Y = fun(xs)
|
| 148 |
+
yhat = evalute_hdmr(xs, np.mean(Y, axis=0), alpha)
|
| 149 |
+
|
| 150 |
+
X1, X2 = zip(*xs)
|
| 151 |
+
fig = plt.figure(figsize=(8, n*4))
|
| 152 |
+
ax = fig.add_subplot(111, projection='3d')
|
| 153 |
+
|
| 154 |
+
# Scatter plot
|
| 155 |
+
|
| 156 |
+
# ax.scatter(X1, X2, Y, c='r', marker='o', alpha=0.6)
|
| 157 |
+
ax.scatter(X1, X2, yhat, c='b', marker='o', alpha=0.6)
|
| 158 |
+
x1_min, x1_max = np.array((np.min(X1), np.max(X1)))
|
| 159 |
+
x2_min, x2_max = np.array((np.min(X2), np.max(X2)))
|
| 160 |
+
|
| 161 |
+
print("x1_min: ", x1_min)
|
| 162 |
+
|
| 163 |
+
x1 = np.linspace(x1_min, x1_max, N)
|
| 164 |
+
x2 = np.linspace(x2_min, x2_max, N)
|
| 165 |
+
|
| 166 |
+
x1, x2 = np.meshgrid(x1, x2)
|
| 167 |
+
y = fun(np.column_stack((x1.ravel(), x2.ravel()))).reshape(x1.shape)
|
| 168 |
+
|
| 169 |
+
ax.plot_surface(x1, x2, y, cmap='jet', alpha=0.5)
|
| 170 |
+
|
| 171 |
+
# Limit the Y axis so scatter is easier to see.
|
| 172 |
+
# ax.set_zlim(np.min(yhat), np.max(yhat))
|
| 173 |
+
|
| 174 |
+
# Set labels for each axis
|
| 175 |
+
ax.set_xlabel('X1')
|
| 176 |
+
ax.set_ylabel('X2')
|
| 177 |
+
ax.set_zlabel('y')
|
| 178 |
+
plt.show()
|
| 179 |
+
|
| 180 |
|
| 181 |
def calculate_distances(x0, arr):
|
| 182 |
return np.sqrt(np.sum((x0 - np.array(arr)) ** 2, axis=1))
|
|
|
|
| 258 |
new_x0 = np.array(result.x)
|
| 259 |
old_a = new_a
|
| 260 |
old_b = new_b
|
| 261 |
+
a_ = new_a
|
| 262 |
+
b_ = new_b
|
| 263 |
+
plot_results()
|
| 264 |
+
plot_with_function()
|
| 265 |
else:
|
| 266 |
+
a_ = a
|
| 267 |
+
b_ = b
|
| 268 |
xs = (b-a)*np.random.random((N,n))+a # Generate sampling data
|
| 269 |
print('XS: ', xs.shape)
|
| 270 |
alpha = calculate_alpha_coeff(xs)
|
|
|
|
| 276 |
temp_status.append(status.x[0])
|
| 277 |
result = OptimizeResult(x=temp_status, fun=fun(x0, *args), success=True, message=" ", nfev=1, njev=0, nhev=0)
|
| 278 |
result.nfev = N
|
| 279 |
+
plot_with_function()
|
| 280 |
|
| 281 |
return result
|
| 282 |
|
|
|
|
| 287 |
parser.add_argument('--numSamples', type=int, help='Number of samples to calculate alpha coefficients.', required=True)
|
| 288 |
parser.add_argument('--numVariables', type=int, help='Number of variable of the test function.', required=True)
|
| 289 |
parser.add_argument('--function', help='Test function name.', required=True)
|
| 290 |
+
parser.add_argument('--min', type=float, help='Lower range of the test function.', required=True)
|
| 291 |
+
parser.add_argument('--max', type=float, help='Upper range of the test function.', required=True)
|
| 292 |
parser.add_argument('--randomInit', action='store_true', help='Initializes x0 as random numbers in the range of xs. Default is initializing as 0.')
|
| 293 |
parser.add_argument('--legendreDegree', type=int, default=7, help='Number of legendre polynomial. Default is 7.')
|
| 294 |
parser.add_argument('--adaptive', action='store_true', help='Uses iterative method when set.')
|
|
|
|
| 338 |
|
| 339 |
# status_bfgs = minimize(test_function, x0, method="BFGS") # Applying direct optimization method to the function
|
| 340 |
# print(f"BFGS status: {status_bfgs}")
|
|
|
|
| 341 |
status_hdmr = minimize(test_function, x0, args=(), method=hdmr_opt) # Applying hdmr-opt method to the function
|
| 342 |
print(f"hdmr_opt status: {status_hdmr}")
|
| 343 |
|