【问题标题】:Cyclic coordinate Search Optimization, updating new optimum using for loop循环坐标搜索优化,使用for循环更新新的最优值
【发布时间】:2021-04-20 02:13:47
【问题描述】:

在循环坐标搜索中,我们寻找每个坐标轴的最佳内联。 我已将变量 x_new 定义为起点的更新变量。在循环内部,目标函数首先在 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 更新,还是我处理这一切都错了?

【问题讨论】:

  • 嗨,很抱歉,如果这无关紧要,但你有文献参考,我可以看到我们如何以“循环”方式选择坐标?
  • 对不起,我不再有此方法的参考,但我能够找出问题所在。
  • 这种搜索方法的作用是最小化目标函数的一个变量(坐标方向)。然后它使用第二个变量最小化目标函数。重复此循环,直到找到目标函数的全局或局部最小值。

标签: python numpy for-loop optimization scipy-optimize


【解决方案1】:

问题是我没有将 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')

【讨论】:

    猜你喜欢
    • 2021-10-21
    • 2013-09-02
    • 2011-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-22
    • 1970-01-01
    相关资源
    最近更新 更多