1、背景
起因是导师和我谈到用deep learning预测traveling salesman problem问题,这是一个np-hard问题,导师的意思是或许直接去解决它可能有点困难,不如先试试其他的,然后一步一步靠近,比如dijkstra、floyed。然后就看到这篇blog:关于仓库取货这么一个问题,找到最短路径(当然实际上是找到拿完一个pick list里所有items的最短时间)。因为大型电商每时每刻都会有大量订单,这些订单要被划分为许多pick lists,又因为一个订单通常会有许多items,比如你在天猫超市一卖就是好多东西,这里的假设是每个订单里的items都要放在一个pick list里,货架存放物品可能是按照类别,或者根据以往订单数据统计结果把销量大的、经常一起搭配购买的放一起而且靠近depot。一个order会需要跑很多地方,怎么样划分这些orders,才能让所有的pick lists整体travel time达到最小,是他们的目标。因为划分的可能性太多了,用一种近似最优的解就可以了,blog里介绍的similated annealing,当然模拟退火算法每一次迭代都要计算travel time,而每一次计算都要耗费几秒钟,这样划分一次可能会很慢,比如迭代10000次,每次三秒钟就是30000秒,怎么保证当日达次日达的?这里他们的想法就是用deep learning去预测travel time,他们用坐标和travel time 输入 CNN,结果让seconds 减小到了milliseconds,实验结果很漂亮,预测误差只有0.895%。
所以我想能不能用来预测图中的两个点之间的距离
2、实验
step1:生成数据
随机生成了十万个联通图(当然生成的是邻接矩阵),具体是用DFS验证是否联通,保留联通图,然后用dijkstra计算Vstart -> Vend的距离,这里起始和终点就选的就是结点标号的0,max。
这里有个要提前说的,邻接矩阵距离不存在话一般用无穷表示,这里我之前觉得用无穷的话学习效果会不会很差就把无穷都换零表示。因为要使loss最小,用零肯定会有问题的,用一个很大的数,就可以排除这种误差。
把邻接矩阵当做X,距离当做y。
step2:
用一个含有两个卷积层和一个全连接层的CNN去学习,结果效果很差,accuracy都是零。。。(当然这里我是把无穷用零表示)
3、总结
几点觉得有问题的地方:
1)考虑到这个仓库很special,和一般的图那种很随机的还是有点差距蛮大的
2)就是边不存在的表示方法
4、后续
后续工作,应该会先改进一下当前的例子,然后试一下导师和我说的规则的结构比如类似棋盘的布局,不过当中有些边不存在
数据生成:
import numpy as np
import random
np.set_printoptions(threshold=np.inf)
n=20
low = 0
high = 30
density = 0.15
_ = 9999
start = 0
end = n - 1
# graph=[
# [0,6,3,_,_,_],
# [6,0,2,5,_,_],
# [3,2,0,3,4,_],
# [_,5,3,0,2,3],
# [_,_,4,2,0,5],
# [_,_,_,3,5,0],
# ]
graph = []
flag = [False]*n
def mat_init(low=0, high = 10, size = 20, density=0.3):
mp = np.random.random_integers(low, high, size*size).reshape(size,size)
#symmetric matrix
# mp2 = mp
for i in range(size):
for j in range(i):
if mp[i,j] < high-(high-low)*density:
mp[i,j] = _
# mp2[i,j] = 0
else:
mp[i,j] = random.randint(low, high)
# mp2[i,j] = mp[i,j]
mp[j,i] = mp[i,j]
# mp2[j,i] = mp2[i,j]
mp[i,i] = 0
# mp2[i,i] = 0
return mp
def is_connect_graph(graph):
# flag=[False]*n
count = 0
for i in range(n):
if flag[i] == False:
dfs(i)
count += 1
if count == 1:
return count
else:
return count
def dfs(v):
flag[v] = True
for i in range(n):
if graph[v][i] != 0 and graph[v][i] != _ and flag[i] == False:
dfs(i)
def dijkstra(graph,n):
dis=[0]*n
flag=[False]*n
pre=[0]*n
flag[0]=True
k=0
for i in range(n):
dis[i]=graph[k][i]
for j in range(n-1):
mini=_
for i in range(n):
if dis[i]<mini and not flag[i]:
mini=dis[i]
k=i
if k==0:
return
flag[k]=True
for i in range(n):
if dis[i]>dis[k]+graph[k][i]:
dis[i]=dis[k]+graph[k][i]
pre[i]=k
# print(k)
return dis,pre
dist=0
X = np.zeros((1,n*n))
y = np.zeros(1)
first = 0
count = 0
for i in range(9999999):
flag = [False]*n
graph = mat_init(low,high,n,density)
# print graph
a = is_connect_graph(graph)
if i%1000 == 0:
print i,a
# print a
if a == 1:
dis, pre = dijkstra(graph, n)
dist = dis[n-1]
graph[graph == 9999] = 0
if first == 0:
X += graph.reshape(-1,n*n)
y += dist
first = 1
# print X,y
count += 1
else:
X = np.vstack((X,graph.reshape(-1,n*n)))
# print dis[n-1]
y = np.vstack((y,dist))
count += 1
if count == 100000:
break
# print X ,y
np.save("X2.npy", X)
np.save("y2.npy", y)
把mnist例子修改的CNN模型:
from __future__ import print_function
import tensorflow as tf
import numpy as np
# Import data
X_all = np.load("X.npy")
y_temp = np.load("y.npy").astype(np.int32)
N = X_all.shape[0]
y_all = np.zeros((N,270))
y_all[range(N),list(y_temp)] = 1
X_train = X_all[:79999]
X_val = X_all[80000:94999]
X_test = X_all[95000:]
y_train = y_all[:79999]
y_val = y_all[80000:94999]
y_test = y_all[95000:]
# Parameters
learning_rate = 0.001
training_iters = 200000
capacity = X_train.shape[0]
min_after_dequeue = 10000
batch_size = 128
display_step = 10
# Network Parameters
n_input = 400 # MNIST data input (img shape: 28*28)
n_classes = 270 # MNIST total classes (0-9 digits)
dropout = 0.75 # Dropout, probability to keep units
# tf Graph input
x = tf.placeholder(tf.float32, [None, n_input])
y = tf.placeholder(tf.float32, [None, n_classes])
keep_prob = tf.placeholder(tf.float32) #dropout (keep probability)
# Create some wrappers for simplicity
def conv2d(x, W, b, strides=1):
# Conv2D wrapper, with bias and relu activation
x = tf.nn.conv2d(x, W, strides=[1, strides, strides, 1], padding='SAME')
x = tf.nn.bias_add(x, b)
return tf.nn.relu(x)
def maxpool2d(x, k=2):
# MaxPool2D wrapper
return tf.nn.max_pool(x, ksize=[1, k, k, 1], strides=[1, k, k, 1],
padding='SAME')
# Create model
def conv_net(x, weights, biases, dropout):
# Reshape input picture
x = tf.reshape(x, shape=[-1, 20, 20, 1])
# Convolution Layer
conv1 = conv2d(x, weights['wc1'], biases['bc1'])
# Max Pooling (down-sampling)
conv1 = maxpool2d(conv1, k=2)
# Convolution Layer
conv2 = conv2d(conv1, weights['wc2'], biases['bc2'])
# Max Pooling (down-sampling)
conv2 = maxpool2d(conv2, k=2)
# Fully connected layer
# Reshape conv2 output to fit fully connected layer input
fc1 = tf.reshape(conv2, [-1, weights['wd1'].get_shape().as_list()[0]])
fc1 = tf.add(tf.matmul(fc1, weights['wd1']), biases['bd1'])
fc1 = tf.nn.relu(fc1)
# Apply Dropout
fc1 = tf.nn.dropout(fc1, dropout)
# Output, class prediction
out = tf.add(tf.matmul(fc1, weights['out']), biases['out'])
return out
# Store layers weight & bias
weights = {
# 5x5 conv, 1 input, 32 outputs
'wc1': tf.Variable(tf.random_normal([5, 5, 1, 32])),
# 5x5 conv, 32 inputs, 64 outputs
'wc2': tf.Variable(tf.random_normal([5, 5, 32, 64])),
# fully connected, 5*5*64 inputs, 1024 outputs
'wd1': tf.Variable(tf.random_normal([5*5*64, 1024])),
# 1024 inputs, 10 outputs (class prediction)
'out': tf.Variable(tf.random_normal([1024, n_classes]))
}
biases = {
'bc1': tf.Variable(tf.random_normal([32])),
'bc2': tf.Variable(tf.random_normal([64])),
'bd1': tf.Variable(tf.random_normal([1024])),
'out': tf.Variable(tf.random_normal([n_classes]))
}
# Construct model
pred = conv_net(x, weights, biases, keep_prob)
# Define loss and optimizer
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred, labels=y))
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)
# Evaluate model
correct_pred = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
# batch_x, batch_y = tf.train.shuffle_batch([X_train, y_train], batch_size, capacity, min_after_dequeue)
# Initializing the variables
init = tf.initialize_all_variables()
# Launch the graph
with tf.Session() as sess:
sess.run(init)
step = 1
# Keep training until reach max iterations
while step * batch_size < training_iters:
# batch_x, batch_y = tf.train.shuffle_batch([X_train, y_train], batch_size, capacity, min_after_dequeue)
# Run optimization op (backprop)
indeices = np.random.choice(capacity, batch_size)
batch_x = X_train[indeices,:]
batch_y = y_train[indeices,:]
sess.run(optimizer, feed_dict={x: batch_x, y: batch_y,
keep_prob: dropout})
if step % display_step == 0:
# Calculate batch loss and accuracy
loss, acc = sess.run([cost, accuracy], feed_dict={x: batch_x,
y: batch_y,
keep_prob: 1.})
print("Iter " + str(step*batch_size) + ", Minibatch Loss= " + \
"{:.6f}".format(loss) + ", Training Accuracy= " + \
"{:.5f}".format(acc))
step += 1
print("Optimization Finished!")
# Calculate accuracy for 256 test adj mat
print("Testing Accuracy:", \
sess.run(accuracy, feed_dict={x: X_test[:256],
y: y_test[:256],
keep_prob: 1.}))