约束编程 - 使用变量作为矩阵索引
我是Or-Tools的新手,我很难使用它,这是我的问题:
假设我们有一张3分'1','2','3'的地图,并且我们有3个名字'a','' b','c'。每个要点都可以取所有名称,但必须有一个名称。 因此,我们有一个矩阵,说明所有点与另一个点之间的距离,这些距离说您必须从一个名字转到另一个名称。 目标是找到要设置为每个点的名称,以使其具有最小的距离。
使用代码:
'Distance matrix' 'Planning matrix'
'1' '2' '3' 'a' 'b' 'c'
'1' 0 10 20 'a' 0 1 3
'2' 10 0 30 'b' 1 0 2
'3' 20 30 0 'c' 3 2 0
在这里我们需要从A到B 1次。 在这里我如何创建变量和约束:
for x in range(len(planning)):
for y in range(len(planning)):
var_list[x, y] = model.NewBoolVar('')
for x in range(len(planning)):
model.AddExactlyOne(var_list[x, y] for y in range(len(planning)))
for y in range(len(planning)):
model.AddExactlyOne(var_list[x, y] for x in range(len(planning)))
我们有一个矩阵,其中每一行和列只有一个1 我的想法是使用此矩阵来定义每个点将哪个名称设置为每个点。 矩阵可以是这样的:
'a''b''c'
'1' 0 1 0
'2' 0 0 1
'3' 1 0 0
这是我尝试求解优化的方式,我将var用作距离矩阵的索引:
terms = []
for index_x in range(len(planning)):
for index_y in range(len(planning)):
terms.append(planning[index_x, index_y] * distance[np.where(var_list[index_x] == True)[0][0]][np.where(var_list[index_y] == True)[0][0]])
model.Minimize(sum(terms))
但是它不会运行,因为它找不到var_list是正确的
terms.append(planning[index_x, index_y] * distance[np.where(var_list[index_x] == True)[0][0]][np.where(var_list[index_y] == True)[0][0]])
IndexError: index 0 is out of bounds for axis 0 with size 0
索引我直接使用变量的另一个想法,但我的问题不再是线性的:
terms = []
for index_x in range(len(planning)):
for index_y in range(len(planning)):
terms.append(
planning[index_x, index_y] *
sum(
distance[i, j] * var_list[index_x, i] * var_list[index_y, j] for i in range(len(planning)) for j in range(len(planning))
)
)
model.Minimize(sum(terms))
有人知道如何更改代码以使其正常工作?即使我必须使用另一个库。
I am new to or-tools and I struggle to use it, here is my problem:
Let say we have a map with 3 points '1', '2', '3' and that we have 3 names 'a', 'b', 'c'. Each points can take all name but must have one name.
With that we have a matrix that says the distance between all points and another that say how many time you have to go from one name to another.
The goal is to find which name to set to each point to have the least distance to do.
With code:
'Distance matrix' 'Planning matrix'
'1' '2' '3' 'a' 'b' 'c'
'1' 0 10 20 'a' 0 1 3
'2' 10 0 30 'b' 1 0 2
'3' 20 30 0 'c' 3 2 0
Here we need to go from a to b 1 time.
Here how i create my variables and constraints:
for x in range(len(planning)):
for y in range(len(planning)):
var_list[x, y] = model.NewBoolVar('')
for x in range(len(planning)):
model.AddExactlyOne(var_list[x, y] for y in range(len(planning)))
for y in range(len(planning)):
model.AddExactlyOne(var_list[x, y] for x in range(len(planning)))
We have a matrix where each row and column have only one 1
My idea is to use this matrix to define which name is set to each point.
The matrix can be this for example:
'a''b''c'
'1' 0 1 0
'2' 0 0 1
'3' 1 0 0
And here is how i try to solve optimization, I use my var as an index for my distance matrix:
terms = []
for index_x in range(len(planning)):
for index_y in range(len(planning)):
terms.append(planning[index_x, index_y] * distance[np.where(var_list[index_x] == True)[0][0]][np.where(var_list[index_y] == True)[0][0]])
model.Minimize(sum(terms))
But it won't run because it can't find the index where var_list is True
terms.append(planning[index_x, index_y] * distance[np.where(var_list[index_x] == True)[0][0]][np.where(var_list[index_y] == True)[0][0]])
IndexError: index 0 is out of bounds for axis 0 with size 0
I had another idea where I use directly my variables but my problem was no longer linear:
terms = []
for index_x in range(len(planning)):
for index_y in range(len(planning)):
terms.append(
planning[index_x, index_y] *
sum(
distance[i, j] * var_list[index_x, i] * var_list[index_y, j] for i in range(len(planning)) for j in range(len(planning))
)
)
model.Minimize(sum(terms))
Anyone know how could I change my code to make it work ? Even if I have to use another library.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在这里如何应用它:
Here how I have applied it:
如果要使用变量索引中访问1D数组中的变量:
https://developers.google.com/optimization/reference/python/sat/python/cp_model#addelement
If you want to access a variable in a 1d array using a variable index:
https://developers.google.com/optimization/reference/python/sat/python/cp_model#addelement