Beispiel #1
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)
Beispiel #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("cities.txt")
cityPosition = mat(dataSet)
m,n = shape(cityPosition)
pn = m
# 将城市的坐标矩阵转换为邻接矩阵(城市间距离矩阵)
dist = Boltzmann.distM(cityPosition,cityPosition.transpose())

# 初始化
MAX_ITER = 2000 # 1000-2000
MAX_M = m;
Lambda = 0.97;
T0 = 1000; # 100-1000
# 构造一个初始可行解
x0 = arange(m)
random.shuffle(x0)
# 
T = T0;
iteration = 0;
x = x0;                   # 路径变量
xx = x0.tolist();         # 每个路径
di = []
Beispiel #3
0
tmp = random.rand()
print tmp

# 不重复的随机数
path = mat(zeros((m, m)))
tmp = arange(m)
for i in range(m):
    random.shuffle(tmp)
    path[i, :] = tmp
# print path

# 绘制数据散点图
# Untils.drawScatter(dataSet)

# 将城市的坐标矩阵转换为邻接矩阵(城市间距离矩阵)
dist = Boltzmann.distM(cityPosition, cityPosition.transpose())
path = arange(m)
# print Boltzmann.pathLen(dist,path)

# sort
a = [1, 3, 5, 7, 9, 2, 4, 6, 8, 0]
a.sort()
# print a
x1 = 3
x2 = 5
# print a[x1:x2]
# print x2-x1

a = [1, 3, 5, 7, 9, 2, 4, 6, 8, 0]
b = a
c = a
# -*- 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("cities.txt")
cityPosition = mat(dataSet)
m, n = shape(cityPosition)
pn = m
# 将城市的坐标矩阵转换为邻接矩阵(城市间距离矩阵)
dist = Boltzmann.distM(cityPosition, cityPosition.transpose())

# 初始化
MAX_ITER = 2000  # 1000-2000
MAX_M = m
Lambda = 0.97
T0 = 1000
# 100-1000
# 构造一个初始可行解
x0 = arange(m)
random.shuffle(x0)
#
T = T0
iteration = 0
x = x0
# 路径变量
Beispiel #5
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(range(m),cityPosition)

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

# 绘制误差趋势线
x0 = range(len(di));
Untils.TrendLine(x0,di)
    def simulate(self, c, boltz, ucb):
        #tempTruth = [np.random.normal(self.truth_100[i], scale = 8) for i in range(100)]
        rewards = [[0 for j in range(82)] for k in range(4)]
        # Create prior mean
        pMean = [np.random.uniform(3, 18) for i in range(100)]
        priorMeans = [pMean for _ in range(4)]
        # Create prior covariance matrix
        self.fillCov()
        priorCov = [self.covariance.copy() for i in range(4)]

        #generate the truth from prior
        tempTruth = np.random.multivariate_normal(pMean, self.covariance)

        #EG policy initializes
        constant = c
        #Boltzmann policy initialize
        theta_b = boltz
        #UCB initializes
        theta_u = ucb
        #KG policy initialize
        precision = [1 / 22.5 for i in range(100)]
        num_selected = [1 for i in range(100)]

        def drawObservations(self, lineupChoice):
            return (tempTruth[lineupChoice] +
                    np.random.normal(0, scale=np.sqrt(22.5)))

        for i in range(0, 82):
            choices = [0 for j in range(4)]
            #get choices for all the policies and put in a list
            choices[0] = eg.EpsilonGreedy(priorMeans[0], constant, i)
            choices[1] = b.Boltzmann(priorMeans[1], theta_b, i)
            choices[2], numselected = UCB.UCB(priorMeans[2], theta_u, i,
                                              num_selected)
            choices[3] = KGCB.kgcb(priorMeans[3], precision, priorCov[3], i)
            #print('EGreedy choice {}, Boltzmann choice{}, UCB {}, KG {}'.format(choices[0], choices[1], choices[2], choices[3]))

            results = [drawObservations(self, j) for j in choices]

            for j in range(4):

                rewards[j][i] = results[j]

            ## THIS STUFF IS FOR UPDATING EQUATIONS

            # max_value is the best estimated value of the KG
            # x is the argument that produces max_value

            # observe the outcome of the decision
            # w_k=mu_k+Z*SigmaW_k where SigmaW is standard deviation of the
            # error for each observation
            for j in range(4):
                w_k = results[j]
                cov_m = np.asarray(priorCov[j])
                x = choices[j]
                # updating equations for Normal-Normal model with covariance
                addscalar = (w_k - priorMeans[j][x]) / (1 / precision[x] +
                                                        cov_m[x][x])
                # cov_m_x is the x-th column of the covariance matrix cov_m
                cov_m_x = np.array([row[x] for row in cov_m])
                priorMeans[j] = np.add(priorMeans[j],
                                       np.multiply(addscalar, cov_m_x))
                cov_m = np.subtract(
                    cov_m,
                    np.divide(np.outer(cov_m_x, cov_m_x),
                              1 / precision[x] + cov_m[x][x]))
                priorCov[j] = cov_m
        return (rewards)