Skip to content

Commit

Permalink
change varable num to num_location
Browse files Browse the repository at this point in the history
  • Loading branch information
tnlin committed Aug 14, 2016
1 parent efa8230 commit a5383a9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@ numpy==1.11.1, python2/3的環境下皆可運行

### Annealing parameters

markov_step = 10 * num # 內循環次數
T = 100 # 初始溫度
T = 1 # 最低溫度
T_ALPHA = 0.99 # 退溫常數
markov_step = 10 * num_location # 內循環次數
T = 100 # 初始溫度
T = 1 # 最低溫度
T_ALPHA = 0.99 # 退溫常數

因為是隨機搜尋演算法,不一定能保證每次都得到最佳解答

如果想要得到更好的解,增加markov_step是個好辦法

markov_step = 100 * num # 內循環次數
markov_step = 100 * num_location # 內循環次數

### Execute
直接執行`python tsp.py`, 預設會讀取`data/pokestops.csv`
Expand Down
22 changes: 11 additions & 11 deletions tsp.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
#coding:utf-8
import numpy as np
import num_locationpy as np
import sqlite3
import json
from util import plot

def sum_distmat(p, distmat):
dist = 0
num = p.shape[0]
for i in range(num-1):
num_location = p.shape[0]
for i in range(num_location-1):
dist += distmat[p[i]][p[i+1]]
dist += distmat[p[0]][p[num-1]]
dist += distmat[p[0]][p[num_location-1]]
return dist

def get_distmat(p):
num = p.shape[0]
num_location = p.shape[0]
# 1 degree of lat/lon ~ 111km = 111000m in Taiwan
p *= 111000
distmat = np.zeros((num, num))
for i in range(num):
for j in range(i, num):
distmat = np.zeros((num_location, num_location))
for i in range(num_location):
for j in range(i, num_location):
distmat[i][j] = distmat[j][i] = np.linalg.norm(p[i] - p[j])
return distmat

Expand Down Expand Up @@ -78,15 +78,15 @@ def main():
coordinates = np.loadtxt(filename, delimiter=',')

# Params Initial
num = coordinates.shape[0]
markov_step = 10 * num
num_location = coordinates.shape[0]
markov_step = 10 * num_location
T, T_MIN, T_ALPHA = 100, 1, 0.99

# Build distance matrix to accelerate cost computing
distmat = get_distmat(coordinates)

# States: New, Current and Best
sol_new, sol_current, sol_best = (np.arange(num), ) * 3
sol_new, sol_current, sol_best = (np.arange(num_location), ) * 3
cost_new, cost_current, cost_best = (float('inf'), ) * 3

# Record costs during the process
Expand Down

0 comments on commit a5383a9

Please sign in to comment.