Ejemplo n.º 1
0
			sigmod = exp(-(newl - oldl)/T)
			if tmp < sigmod:
				x = newx
				xx.append(x)    # xx[n,:] = x
				di.append(newl) # di[n]= newl				
				k += 1
		m += 1            # 内循环次数加1
	# 内循环
	iteration += 1      # 外循环次数加1
	T = T*Lambda        # 降温

# 计算最优值
bestd = min(di) 
indx = argmin(di)
bestx = xx[indx]
print "循环迭代",k,"次"
print "最优解:",bestd
print "最佳路线:",bestx	

# 优化前城市图,路径图
Untils.drawScatter(cityPosition,flag=False)
Untils.drawPath(range(m-1),cityPosition)

# 显示优化后城市图,路径图
Untils.drawScatter(cityPosition,flag=False)
Untils.drawPath(bestx,cityPosition,color='r')

# 绘制误差趋势线
x0 = range(len(di));
Untils.TrendLine(x0,di)
Ejemplo n.º 2
0
# -*- coding:utf-8 -*-
# Filename : testBoltzmann01.py

import operator
import copy
import Untils
import Boltzmann
from numpy import *
import matplotlib.pyplot as plt

dataSet = Untils.loadDataSet("dataSet25.txt")
cityPosition = mat(dataSet)
m, n = shape(cityPosition)
bestx, di = Boltzmann.boltzmann(cityPosition, MAX_ITER=1000, T0=100)

# 优化前城市图,路径图
Untils.drawScatter(cityPosition, flag=False)
Untils.drawPath(list(range(m)), cityPosition)

# 显示优化后城市图,路径图
Untils.drawScatter(cityPosition, flag=False)
Untils.drawPath(bestx, cityPosition, color='b')

# 绘制误差趋势线
x0 = list(range(len(di)))
Untils.TrendLine(x0, di)
Ejemplo n.º 3
0
            sigmod = exp(-(newl - oldl) / T)
            if tmp < sigmod:
                x = newx
                xx.append(x)  # xx[n,:] = x
                di.append(newl)  # di[n]= newl
                k += 1
        m += 1  # 内循环次数加1
    # 内循环
    iteration += 1  # 外循环次数加1
    T = T * Lambda  # 降温

# 计算最优值
bestd = min(di)
indx = argmin(di)
bestx = xx[indx]
print "循环迭代", k, "次"
print "最优解:", bestd
print "最佳路线:", bestx

# 优化前城市图,路径图
Untils.drawScatter(cityPosition, flag=False)
Untils.drawPath(range(m - 1), cityPosition)

# 显示优化后城市图,路径图
Untils.drawScatter(cityPosition, flag=False)
Untils.drawPath(bestx, cityPosition, color='r')

# 绘制误差趋势线
x0 = range(len(di))
Untils.TrendLine(x0, di)