Minizinc错误:无效的类型输入:预期的'float',实际的'var float'

问题描述 投票:-1回答:1

我有以下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 |];
algorithm traveling-salesman constraint-programming minizinc integer-programming
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.