我有以下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'的初始化值具有无效的type-inst:预期的'int的array [int],实际的'float的array [int,int]'
我能否仅不基于函数(例如,求和),参数和决策变量来定义变量?我想我这里缺少基本的东西。(以下示例数据为可复制示例):
% 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
:它被声明为singe数组,但是在“ DATA”部分中,它被定义为2d矩阵。第二列的用途是什么,该列的值为0.5?
当然,感谢hakank!最初给我点作为xy坐标,但是当我意识到x,y坐标仅对计算距离矩阵有价值时,将点更改为代码中的整数数组。但是我不小心将它们留在了数据文件中。删除了它们,从而消除了此错误