python自带包遗传算法GA参数详解和例子

def demo1_func(p):
    '''This function has plenty of local minimum, with strong shocks
    global minimum at (0,0) with value 0'''
    x1, x2 = p
    print(x1, x2)
    x = x1 ** 2 + x2 ** 2
    return x


# 等式约束
def cons1(x):
    return [x[0] + x[1] - 10]
    # return [x[0] + x[1] - 10, x[0] - 2]


cons = cons1


# 遗传算法
from sko.GA import GA
# 参数详解
# func:自己定义的最小目标函数
# n_dim:目标函数中变量的个数
# size_pop:每代取50个
# max_iter:进行800次迭代
# lb:各个变量的最小值
# ub:各个变量的最大值
# precision:精度的大小
# constraint_eq:约束条件(也可以说是惩罚项)
ga = GA(func=demo1_func, n_dim=2, size_pop=50, max_iter=800, lb=[-1, -1], ub=[8, 10], precision=1e-7, constraint_eq=[cons])
# ga = GA(func=demo1_func, n_dim=2, size_pop=50, max_iter=800, lb=[-1, -1], ub=[80, 100], precision=[1, 0.1], constraint_eq=[cons])
best_x, best_y = ga.run()
print('best_x:', best_x, '\n', 'best_y:', best_y)

结果

best_x: [5.35140186 4.64859812] 
best_y: [50.24696633]
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值