【问题标题】:COBYLA optimization algorithm pythonCOBYLA优化算法python
【发布时间】:2018-04-02 20:36:38
【问题描述】:

我想使用 COBYLA 算法最大化两个对象之间的距离。对于每个对象,我都有一个包含 x 和 y 值的二维数组。

可能发生变化的变量是a1、a2、a3、b1、b2、b3。我希望 COBYLA 计算这些变量的哪些值使两个对象之间的距离最大化。

有两个约束:

a1,a2,a3 >= 1.5

b1,b2,b3 <= 3.0

我写了大部分代码,但我不明白如何调用这个函数。我希望我的代码进行 2000 次迭代,以确保变量的最佳值。到目前为止,这是我所拥有的:

import sys

import numpy as np 

from scipy import optimize

from scipy.optimize import _cobyla

obj1_data = np.loadtxt('obj1_data.txt')

obj2_data = np.loadtxt('obj2_data.txt')

x_obj1 = obj1_data[:,0] #array of x-values for obj1

y_obj1 = obj1_data[:,1] #array of y-values for obj1

y_obj2 = obj2_data[:,0] #array of x-values for obj2

x_obj2 = obj2_data[:,1] #array of y-values for obj2

眼镜蛇最小化

def cobyla(a1,a2,a3,b1,b2,b3):

    #~~~ OBJECT 1 ~~~#

    #OBJECT 1 RANGE (a1 to b1)

    #np.where returns elements, either x or y, depending on condition
    #find index of elements where (from a to b)
    xindex1 = np.where(np.logical_and(x_obj1 >= a1, x_obj1 <= b1))

    #make an x array that corresponds to x index
    obj1_xarray1 = x_obj1[xindex1]

    #find y values corresponding to x range
    obj1_yarray1 = y_obj1[xindex1]

    #integrate values in yarray to give summed value of x-values in array
    obj1_int1 = np.trapz(obj1_yarray1,x=obj1_xarray1)



    #OBJECT 1 RANGE (a2 to b2)
    xindex2 = np.where(np.logical_and(x_obj1 >= a2, x_obj1 <= b2))

    obj1_xarray2 = x_obj1[xindex2]

    obj1_yarray2 = y_obj1[xindex2]

    obj1_int2 = np.trapz(obj1_yarray2,x=obj1_xarray2)



    #OBJECT 1 RANGE (a3 to b3)
    xindex3 = np.where(np.logical_and(x_obj1 >= a2, x_obj1 <= b2))

    obj1_xarray3 = x_obj1[xindex3]

    obj1_yarray3 = y_obj1[xindex3]

    obj1_int3 = np.trapz(obj1_yarray3,x=obj1_xarray3)


    #~~~ OBJECT 2 ~~~#


    #OBJECT 1 RANGE (a1 to b1)
    #np.where returns elements, either x or y, depending on condition
    #find index of elements where (from a to b)
    xindex4 = np.where(np.logical_and(x_obj2 >= a1, x_obj2 <= b1))

    #make an earth x array that corresponds to x index
    obj2_xarray1 = x_obj2[xindex4]

    #find y values corresponding to x range
    obj2_yarray1 = y_obj2[xindex4]

    #integrate values in yarray to give summed value of x-values in array
    obj2_int1 = np.trapz(obj2_yarray1,x=obj2_xarray1)


    ##OBJECT 1 RANGE (a2 to b2)
    xindex5 = np.where(np.logical_and(x_obj2 >= a2, x_obj2 <= b2))

    obj2_xarray2 = x_obj2[xindex5]

    obj2_yarray2 = y_obj2[xindex5]

    obj2_int2 = np.trapz(obj2_yarray2,x=obj2_xarray2)


    #OBJECT 1 RANGE (a3 to b3)
    xindex6 = np.where(np.logical_and(x_obj2 >= a2, x_obj2 <= b2))

    obj2_xarray3 = x_obj2[xindex6]

    obj2_yarray3 = y_obj2[xindex6]

    obj2_int3 = np.trapz(obj2_yarray3,x=obj2_xarray3)



    #~~~~EQUATION THAT SHOULD BE OPTMIZED~~~#
    max = (((obj1_int1/obj1_int2) - (obj2_int1/obj2_int2))**2 +
           ((obj1_int3/obj1_int2) - (obj2_int3/obj2_int2))**2)**0.5
    return max

约束

def constr1(a1,a2,a3):

    a1,a2,a3 >= 1.5

    return a1, a2, a3

def constr2(b1,b2,b3):

    b1,b2,b3 <= 3.0

    return b1,b2,b3



p = optimize.fmin_cobyla(cobyla, cons=[constr1,constr2])

【问题讨论】:

    标签: python algorithm optimization


    【解决方案1】:

    我使用这种方法为 SGD 篮子交易创建策略。 虽然我不是该主题的专家,但我有一个公平的想法。 尝试将您的目标和约束转换为函数。 然后在你的

    中调用这些函数
    x = fmin_cobyla(objtv_func,x0,constr_funcs,rhoend = 1e-7)
    

    这应该可以解决您调用 cobyla 优化器的问题。 如果您遇到错误,您还可以看到确切的错误点。

    希望这对你有用!祝你好运!

    【讨论】:

      猜你喜欢
      • 2019-06-25
      • 2015-10-25
      • 2021-06-29
      • 2012-02-21
      • 1970-01-01
      • 2021-04-29
      • 2020-07-08
      • 1970-01-01
      • 2016-07-30
      相关资源
      最近更新 更多