您的当前位置:首页正文

python实现最优化之——进退法

2024-11-05 来源:个人技术集锦

废话不多说,上代码!!!!

def func(x):
    return (x - 60.39636407237782) * (x - 60.39636407237782)


def for_back(function):
    x = 30  # 初始值
    step = 1  # 初始步长
    magnification = 2  # 放大倍数
    print(x, step, magnification)
    if function(x) > function(x+step):
        while function(x) > function(x + step):
            print(x + step)
            x = x + step
            step = step*magnification
        if function(x) <= function(x + step):
            print(x + step)
            return int(x-step/magnification), int(x + step)
    elif function(x) > function(x - step):
        while function(x) > function(x - step):
            print(x - step)
            x = x - step
            step = step * magnification
        if function(x) <= function(x - step):
            print(x - step)
            return int(x - step), int(x + step/magnification)
    else:
        print("测试点为最低点,请更改测试值。")


if __name__ == "__main__":
    print(for_back(func))

补充说明:这是一个寻找凹函数最小值区间的功能模块。

Top