This code piece in AMPL solves correctly the pair matching algorithm:
set Student;.
# student tuples inside Student x Student
set Pair within {Student, Student};
# sparse value in that tuples
param value {Pair};
var x {Pair} binary; # decision var
maximize total_value: sum {(i,j) in Pair} value[i,j] * x[i,j];
s.t. perfect_match {i in Student}: # constraint
sum {(i,j) in Pair} x[i,j] + sum {(j,i) in Pair} x[j,i] = 1;
data;
set Student := A B C D E F G H I J;
# sparse set of student tuples
set Pair: A B C D E F G H I J :=
A - - - - - - - - - -
B + - - - - - - - - -
C + + - - - - - - - -
D + + + - - - - - - -
E + + + + - - - - - -
F + + + + + - - - - -
G + + + + + + - - - -
H + + + + + + + - - -
I + + + + + + + + - -
J + + + + + + + + + -;
# sparse matrix with scores
param value: A B C D E F G H I J :=
A . . . . . . . . . .
B 3 . . . . . . . . .
C 5 8 . . . . . . . .
D 1 -4 7 . . . . . . .
E 2 -1 9 2 . . . . . .
F 2 5 3 2 9 . . . . .
G 8 2 1 1 3 -2 . . . .
H 2 3 3 4 5 1 1 . . .
I 13 -1 3 4 4 -5 2 2 . .
J 1 2 6 6 7 -4 5 6 1 .;
This code works smoothly, but it is cumbersome because one has to declare 2 times what student tuples are valid and a value associated with that tuples.
Other question shows an alternative syntax (showed below) for that kind of situation in an answer. The same syntax is showed in a 2011 paper from Darin England from University of Minnesota (Mathematical programming formulations using AMPL) on page 5.
# sparse matrix with scores
param: pair: value: A B C D E F G H I J :=
A . . . . . . . . . .
B 3 . . . . . . . . .
C 5 8 . . . . . . . .
D 1 -4 7 . . . . . . .
E 2 -1 9 2 . . . . . .
F 2 5 3 2 9 . . . . .
G 8 2 1 1 3 -2 . . . .
H 2 3 3 4 5 1 1 . . .
I 13 -1 3 4 4 -5 2 2 . .
J 1 2 6 6 7 -4 5 6 1 .;
However, its generates an error in an updated AMPL version:
malformed header: extra ':' (last colon)
Is there some solution for defining set tuples and values of set tuples in just one declaration?
Tonight the AMPL support gave me a answer.
I'm quite surprised by the efficiency of AMPL support team!
Using Windows, We found that AMPL version 20171122 and later reject the user's examples with the "malformed header" message, but versions 20171103 and earlier accept the examples (including perfectEr.mod). We have been working to fix the problem. Meanwhile, a workaround is to replace "param: Pair: value: . . ." by "param: Pair: value [,]: . . .".
I've tested it and it works in my AMPL version. I only have added [*.*]
after param: Pair: value
(Line 23) and before colon (:
). Everything works smoothly.
reset;
set Student;
set Pair within {Student, Student}; # Student cross Student;
param value {Pair};
# Decision Variable
var x {Pair} binary;
# Objective
maximize total_value: sum {(i,j) in Pair} value[i,j] * x[i,j];
# Constraint
s.t. perfect_match {i in Student}:
sum {(i,j) in Pair} x[i,j] + sum {(j,i) in Pair} x[j,i] = 1;
data;
# Students names
set Student := A B C D E F G H I J;
# affinity in defined tuples (Pair)
param: Pair: value [*,*]:
A B C D E F G H I J :=
A . . . . . . . . . .
B 3 . . . . . . . . .
C 5 8 . . . . . . . .
D 1 -4 7 . . . . . . .
E 2 -1 9 2 . . . . . .
F 2 5 3 2 9 . . . . .
G 8 2 1 1 3 -2 . . . .
H 2 3 3 4 5 1 1 . . .
I 13 -1 3 4 4 -5 2 2 . .
J 1 2 6 6 7 -4 5 6 1 .;
option solver CPLEX;
solve;
option omit_zero_rows 1;
display x