0

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 |];
Max Power
  • 8,265
  • 13
  • 50
  • 91
  • 2
    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? – hakank Mar 16 '20 at 06:43
  • of course, thanks hakank! I was originally given `points` as x.y coords but changed `points` to an array of ints in the code when I realized the x,y coords are only valuable for calculating the distance matrix. but I accidentally left them like that in the data file. removing them now. – Max Power Mar 16 '20 at 13:56
  • Great. I've added a "formal" answer so you can accept it. – hakank Mar 16 '20 at 16:59

1 Answers1

0

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?

hakank
  • 6,629
  • 1
  • 17
  • 27
  • of course, thanks hakank! I was originally given points as x.y coords but changed points to an array of ints in the code when I realized the x,y coords are only valuable for calculating the distance matrix. but I accidentally left them like that in the data file. removed them and that eliminates this error – Max Power Mar 16 '20 at 17:02