温馨提示:本文翻译自stackoverflow.com,查看原文请点击:algorithm - Minizinc error: invalid type-inst: expected `float', actual `var float'
algorithm constraint-programming integer-programming minizinc traveling-salesman

algorithm - Minizinc错误:无效的类型输入:预期为“ float”,实际为“ var float”

发布于 2020-05-31 10:48:39

我有以下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-in​​st:预期的'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 |];

查看更多

提问者
Max Power
被浏览
42
hakank 2020-03-17 00:58

使用和声明方式的一个问题points:它被声明为singe数组,但是在“ DATA”部分中,它被定义为2d矩阵。第二列的用途是什么,该列的值为0.5?