Warm tip: This article is reproduced from stackoverflow.com, please click
algorithm constraint-programming integer-programming minizinc traveling-salesman

Minizinc error: invalid type-inst: expected `float', actual `var float'

发布于 2020-05-29 18:08:19

I have the following Minizinc program which is a work in progress towards being a solution for the Traveling Salesman Problem (TSP). I know it's missing more than just fixing this error, but I would still like to understand why I'm getting this error. Reproduceable example below:

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)];

The error I'm getting is:

MiniZinc: type error: initialisation value for 'total_distance' has invalid type-inst: expected 'float', actual 'var float'

I guess it's saying because next_point is used in the calculation of total_distance, and next_point is a decision variable (var), that means total_distance needs to be too? But if I change float: total_distance... to var float: total_distance... I get a new error elsewhere on points:

MiniZinc: type error: initialisation value for 'points' has invalid type-inst: expected 'array[int] of int', actual 'array[int,int] of float'

Can I just not define a variable based on a function (e.g. sum across) a parameter and a decision variable? I think I'm missing something fundamental here. (Example data below for a reproduceable example):

% 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 |];
Questioner
Max Power
Viewed
26
hakank 2020-03-17 00:58

One problem in how you use and declare points: It is declared as a singe array but in the "DATA" section it is defined as a 2d matrix. What is the use of the second column, the one that contain the value of 0.5?