scikit-opt学习笔记

1.差分约束算法

'''
min f(x1, x2, x3) = x1^2 + x2^2 + x3^2
s.t.
    x1*x2 >= 1
    x1*x2 <= 5
    x2 + x3 = 1
    0 <= x1, x2, x3 <= 5
'''


def obj_func(p):
    x1, x2, x3 = p
    return x1 ** 2 + x2 ** 2 + x3 ** 2


constraint_eq = [
    lambda x: 1 - x[1] - x[2]
]
# 等于0
constraint_ueq = [
    lambda x: 1 - x[0] * x[1],
    lambda x: x[0] * x[1] - 5
]
# 小于0
# %% Do DifferentialEvolution
from sko.DE import DE

de = DE(func=obj_func, n_dim=3, size_pop=50, max_iter=800, lb=[0, 0, 0], ub=[5, 5, 5],
        constraint_eq=constraint_eq, constraint_ueq=constraint_ueq)
'''
func: 目标函数,即要优化的函数。
n_dim: 变量的维度,这里是3,表示有3个自变量。
size_pop: 种群大小,即每一代的个体数量。
max_iter: 最大迭代次数,表示算法将进行多少代的进化。
lb: 每个自变量的下界,即允许的最小值。
ub: 每个自变量的上界,即允许的最大值。
constraint_eq: 等式约束函数,用于处理等式约束条件。
constraint_ueq: 不等式约束函数,用于处理不等式约束条件。
'''


best_x, best_y = de.run()
print('best_x:', best_x, '\n', 'best_y:', best_y)

 

posted @ 2024-03-12 13:43  liujunxi  阅读(54)  评论(0编辑  收藏  举报