Help with Steiner Tree

102 views
Skip to first unread message

Vice

unread,
May 1, 2021, 7:23:34 PM5/1/21
to AMPL Modeling Language
Hello everybody.
I need help with a code in AMPL.
I am trying to create a Steiner tree in AMPL, based on a delivered Graph (Param C)
This should create a Subgraph of Terminal Nodes with the lowest possible Edge cost (The Cost in C).
However, I still can't complete it (It keeps throwing errors at me)

This is what my code looks like:
.dat
param n := 7; 
param C:
    1 2 3 4 5 6 7 :=
1  0 6 4 0 0 0 0
2  6 0 3 3 4 0 0
3  4 3 0 1 0 6 0
4  0 3 1 0 3 4 0
5  0 4 0 3 0 4 1
6  0 0 6 4 4 0 4
7  0 0 0 0 1 4 0
;

param R:
    1 2 3 4 5 6 7 :=
1  0 6 4 0 0 0 0
2  6 0 0 3 0 0 0
3  4 0 0 1 0 0 0
4  0 3 1 0 3 4 0
5  0 0 0 3 0 0 1
6  0 0 0 4 0 0 4
7  0 0 0 0 1 4 0
;

.mod
set T := {1,4,7}; #Terminal Nodes
set V := {1..7}; # Nodes
set E :={(1,2),(1,3),(2,3),(2,4),(3,4),(3,6),(4,5),(4,6),(5,6),(5,7),(6,7)};
set r := {(1,2),(1,3),(2,4),(3,4),(4,5),(4,6),(5,7),(6,7)};


param n;
param C{E} integer;
param R{r} integer;


var x{(i,j) in E} >= 0 binary;
var y{T, (i,j) in E, T} >= 0 integer;


minimize Z: sum{(i,j) in E}C[i,j]*x[i, j];

s.t.
R1{(i,j) in E, k in T, l in T : k<>l}:
x[i,j] >= y[k,i,j,l] + y[k,j,i,l];

R2{k in T, l in T : k<>l}:
sum{(k,j) in E}y[k,k,j,l] >= R[k,l];

R3{k in T, l in T : k<>l}:
sum{(i,l) in E}y[k,i,l,l] >= R[k,l];

R4{k in T, l in T, p in V diff T}:
sum{(p,j) in E}y[k,p,j,l] - sum{(i,p) in E}y[k,i,p,l] >=0;

I would appreciate if you could help me complete it

AMPL Google Group

unread,
May 3, 2021, 11:45:30 AM5/3/21
to AMPL Modeling Language
In your model, you define "param C {E}" where E is {(1,2),(1,3),(2,3),(2,4),(3,4),(3,6),(4,5),(4,6),(5,6),(5,7),(6,7)}. But in your data, you give values of C for every pair of values i in V and j in V. Thus AMPL raises an "invalid subscript" error:

error processing param C:
	38 invalid subscripts discarded:
	C[1,1]
	C[1,4]
	C[1,5]
	and 35 more.

AMPL is telling you that (1,1), (1,4), (1,5), etc. are not in E, and thus the corresponding data values are invalid. To fix this, put a "." character rather than a 0 in your table where no data is to be given:

param C:
  1 2 3 4 5 6 7 :=
1 . 6 4 . . . .
2 6 . 3 3 4 . .
3 4 3 . 1 . 6 .
4 . 3 1 . 3 4 .
5 . 4 . 3 . 4 1
6 . . 6 4 4 . 4
7 . . . . 1 4 .
;

You will still have a problem, because your table has data corresponding to, for example, C[1,2] and C[2,1], while E only contains (1,2). You will have to change the data for E or else change to "." all the members of the C data table that are below the diagonal. (There is a similar problem with the data for param R.)

Here's a data tip: You can change the definition of E in your model to "set E dimen 2;" and then change the first line of the C table in your data to "param: E: C:". Then both the members of E and the values of C will be determined from the C table when you read the data file.


--
Robert Fourer
am...@googlegroups.com
{#HS:1501098563-104011#}

Stefan Gałkiewicz

unread,
Jan 4, 2024, 10:46:37 PM1/4/24
to AMPL Modeling Language
Could you send the the whole code of the .dat and of .mod please ?

AMPL Google Group

unread,
Jan 5, 2024, 9:50:27 PM1/5/24
to AMPL Modeling Language
The attached files show what the model and data would look like, after making the changes described in this thread. There is still something wrong with the definition of param R, however, and I do not see any obvious way to fix it.


--
Robert Fourer

We're switching to a new, enhanced user forum.
Join it now at discuss.ampl.com.
{#HS:1501098563-104011#}
On Fri, Jan 5, 2024 at 3:46 AM UTC, AMPL Modeling Language <am...@googlegroups.com> wrote:
Could you send the the whole code of the .dat and of .mod please ?

On Mon, May 3, 2021 at 3:45 PM UTC, AMPL Google Group <am...@googlegroups.com> wrote:
In your model, you define "param C {E}" where E is {(1,2),(1,3),(2,3),(2,4),(3,4),(3,6),(4,5),(4,6),(5,6),(5,7),(6,7)}. But in your data, you give values of C for every pair of values i in V and j in V. Thus AMPL raises an "invalid subscript" error:

error processing param C:
	38 invalid subscripts discarded:
	C[1,1]
	C[1,4]
	C[1,5]
	and 35 more.

AMPL is telling you that (1,1), (1,4), (1,5), etc. are not in E, and thus the corresponding data values are invalid. To fix this, put a "." character rather than a 0 in your table where no data is to be given:

param C:
  1 2 3 4 5 6 7 :=
1 . 6 4 . . . .
2 6 . 3 3 4 . .
3 4 3 . 1 . 6 .
4 . 3 1 . 3 4 .
5 . 4 . 3 . 4 1
6 . . 6 4 4 . 4
7 . . . . 1 4 .
;

You will still have a problem, because your table has data corresponding to, for example, C[1,2] and C[2,1], while E only contains (1,2). You will have to change the data for E or else change to "." all the members of the C data table that are below the diagonal. (There is a similar problem with the data for param R.)

Here's a data tip: You can change the definition of E in your model to "set E dimen 2;" and then change the first line of the C table in your data to "param: E: C:". Then both the members of E and the values of C will be determined from the C table when you read the data file.


--
Robert Fourer

We're switching to a new, enhanced user forum.
Join it now at discuss.ampl.com.
steiner.mod
steiner.dat
Reply all
Reply to author
Forward
0 new messages