【发布时间】:2020-03-16 02:23:02
【问题描述】:
我有以下 Minizinc 程序,该程序正在为解决旅行推销员问题 (TSP) 进行工作。我知道它缺少的不仅仅是修复这个错误,但我仍然想了解为什么我会收到这个错误。下面的可复制示例:
include "globals.mzn";
% Input Parameters
int: NUM_POINTS;
float: MAX_TOTAL_DISTANCE;
array[0..NUM_POINTS-1] of int: points;
array[0..NUM_POINTS-1, 0..NUM_POINTS-1] of float: distance_matrix;
% Decision Variable: where to go next, from each point (indexed on points)
array[0..NUM_POINTS-1] of var 0..NUM_POINTS-1: next_point;
% Constraints that define a valid tour
constraint alldifferent(next_point); % each point only visited once
constraint next_point[NUM_POINTS-1] == points[0]; % not sure if this is helpful or not?
% see if we can find a feasible solution below max-total-distance
float: total_distance = sum(p in points)(distance_matrix[points[p],next_point[p]]);
constraint total_distance < MAX_TOTAL_DISTANCE;
solve satisfy;
output[show(total_distance) ++ "\n" ++ show(next_point)];
我得到的错误是:
MiniZinc:类型错误:“total_distance”的初始化值有 无效的类型输入:预期的 'float',实际的 'var float'
我猜是因为next_point用于total_distance的计算,而next_point是一个决策变量(var),这意味着total_distance也需要吗?但是,如果我将 float: total_distance... 更改为 var float: total_distance... 我会在其他地方收到一个新错误:
MiniZinc:类型错误:'points' 的初始化值具有无效的类型-inst:预期的 'array[int] of int',实际的 'array[int,int] of float'
我可以不根据函数(例如求和)参数和决策变量来定义变量吗?我想我在这里遗漏了一些基本的东西。 (以下示例数据为可复制示例):
% DATA (in my setup this is in a dzn file)
NUM_POINTS = 5;
points = [|
0, 0|
0, 0.5|
0, 1|
1, 1|
1, 0|];
distance_matrix = [|
0.0, 0.5, 1.0, 1.41, 1.0 |
0.5, 0.0, 0.5, 1.12, 1.12 |
1.0, 0.5, 0.0, 1.0, 1.41 |
1.41, 1.12, 1.0, 0.0, 1.0 |
1.0, 1.12, 1.41, 1.0, 0.0 |];
【问题讨论】:
-
如何使用和声明
points的一个问题:它被声明为单个数组,但在“DATA”部分它是定义的 作为二维矩阵。第二列包含 0.5 的值有什么用? -
当然,谢谢哈坎克!我最初将
points指定为 x.y 坐标,但当我意识到 x,y 坐标仅对计算距离矩阵有价值时,我将points更改为代码中的整数数组。但我不小心把它们留在了数据文件中。立即删除它们。 -
太棒了。我添加了一个“正式”答案,以便您接受。
标签: algorithm traveling-salesman constraint-programming minizinc integer-programming