循环坐标搜索优化,使用 for 循环更新新的最优值

Cyclic coordinate Search Optimization, updating new optimum using for loop

在循环坐标搜索中,我们寻找与每个坐标轴的最佳内联。 我已将变量 x_new 定义为起点的更新变量。在循环内部,objective函数首先在x1方向最小化,然后在x2方向最小化。

我在更新 x_new 时遇到了问题。它不会更新为当前编码,打印时输出为 [0, 0]。

import scipy.optimize as opt                                            
import numpy as np                                                      
# Objective Function                                                    
# f(x1,x2) = (x2-x1**2)**4 + (3-x1)**2                                  
# x = [0, 0]                                                            
                                                                        
# Initialize                                                            
x0 = np.array([0, 0])                                                   
iter = 0                                                                
epsilon = 0.0001                                                        
x_new = x0                                                              
                                                                        
for i in range(len(x0)):                                                
    if i == 0:                                                          
        def obj_func(x):                                                
            return (x_new[1] - x ** 2) ** 4 + (3 - x) ** 2              
        x_new[0] = opt.minimize_scalar(obj_func).x                                                                                    
    else:                                                               
        def obj_func(x):                                                
            return (x - x_new[0]**2) ** 4 + (3 - x_new[0]) ** 2         
        x_new[1] = opt.minimize_scalar(obj_func).x                                                                                   
    iter += 1                                                           
                                                                        
                                                                        
print(x_new)                                                            
print(iter)                                                             

这是这段代码的输出

我怎样才能 x_new 进行更新,还是我的处理方式全错了?

问题是我没有将 numpy 数组定义为浮点数,所以它不会更新 x_new

的原始分配
import scipy.optimize as opt
import numpy as np

# Objective Function
# f(x1,x2) = (x2-x1**2)**4 + (3-x1)**2
# x = [0, 0]

x_new = np.array([0.0, 0.0])
x0 = np.array([0.0, 0.0])
x_hist = np.array([0.0, 0.0])
f = np.array([9])
iter = 0
while iter < 12:
    for i in range(len(x0)):
        if i == 0:
            def obj_func(x):
                return (x_new[1] - x ** 2) ** 4 + (3 - x) ** 2
            x_new[0] = opt.minimize_scalar(obj_func).x
            f = np.append(f, obj_func(x_new[0]))
            x_hist = np.append(x_hist, x_new[0])
            iter += 1
        else:
            def obj_func(x):
                return (x - x_new[0] ** 2) ** 4 + (3 - x_new[0]) ** 2
            x_new[1] = opt.minimize_scalar(obj_func).x
            x_hist = np.append(x_hist, x_new[1])
            f = np.append(f, obj_func(x_new[1]))
            iter += 1

print('The optimum value found after', iter, 'iterations is f* = ',
      round((x_new[1] - x_new[0] ** 2) ** 4 + (3 - x_new[0]) ** 2, 5),
      '\nThe optimizers were x* = ', x_new, '\nThe minimum was found using',
      'Cyclic Coordinate Search w/o acceleration')