模拟退火法三维Python代码实现

单起点,有时还是会落在局部最小值点

import random
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
'''
没有拜托局限性,要在随机时开始循环,每次结果进行比较找到全局最优解
'''


def inputfun(x1, x2):
    return (x1 - 2) * (x1 + 3) * (x1 + 8) * (x1 - 9)*(x2 - 2) * (x2 + 3) * (x2 + 8) * (x2 - 9)


initT = 1000    # 初始温度
minT = 1        # 温度下限
iterL = 500     # 每个T值的迭代次数
delta = 0.95    # 温度衰减系数
k = 30         # 随机起点个数
x1, x2, y1, y2 = [], [], [], []
minx = []
n = 0

# 随机选取起始点,以求寻去全局最小值

initx1 = 20*(random.random()-0.5)       # 确保有正有负,且要够分散
x1.append(initx1)
initx2 = 20*(random.random()-0.5)
x2.append(initx2)
# print("初始解x1:", initx1)
# print("初始解x2:", initx2)

nowT = initT
# 模拟退火算法寻找局部最小值过程
while nowT > minT:
    for i in np.arange(1, iterL):
        y = inputfun(initx1, initx2)
        newx1 = initx1 + 20*(random.random()-0.5)
        newx2 = initx2 + 20*(random.random()-0.5)
        if newx1 >= -10 and newx1 <= 10:
            if newx2  >= -10 and newx2  <= 10:
                newy = inputfun(newx1, newx2)
                dy = newy - y
                if dy < 0:
                    initx1 = newx1
 
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值