-1

There is an incomplete graph (e.g. including 5 vertices). The adjacency matrix "a" is available. I want to define the set which includes all edges but exclude any other pair of vertices. That is, the pair of vertices belongs to the set of edges iff the element in matrix "a" is positive. The last line of following code does not work!

sets i "Set of vertices" /1*5/    ;  
alias(i,j);  
set a(i,j)  "Adjacency matrix"    ;  
Table a(i,j)  
      1   2   3   4   5  
1     0   1   0   1   1  
2     1   0   1   0   0  
3     0   1   0   0   0  
4     1   0   0   0   1  
5     1   0   0   1   0;  
Set edges(i,j);  
edges(i,j) = a(i,j)$(a(i,j)>0);
Lutz
  • 2,197
  • 1
  • 10
  • 12
Mehdi
  • 1
  • 1

2 Answers2

1

If you want to have edge , you must define a set and parameter like this :

  sets i "Set of vertices" /1*5/    ;  
  alias(i,j);  
  set a(i,j)  "Adjacency matrix"    ;  
 Table a(i,j)  
  1   2   3   4   5  
 1     0   1   0   1   1  
 2     1   0   1   0   0  
 3     0   1   0   0   0  
4     1   0   0   0   1  
5     1   0   0   1   0;  
Set edges(i,j);  
edges(i,j) $ a(i,j) =yes;
Richard
  • 177
  • 1
  • 9
  • Thank you Richard. But, there are many graphs to be solved. Further, the incidence matrix of each graph is large and saved in Excel. So it is not efficient to define the edges manually in GAMS. I need an automatic set definition. – Mehdi Aug 05 '18 at 08:31
  • 1
    @Mehdi hi you're welcome , you can export matrix from excel to game . I mean you can define parameter edge(I,j) , and then export data . for example: $call gdxxrw.exe results.xls par=edge rng=sheet1!A1:E33 Parameter edg(i,j); $gdxin results.gdx $load edge $gdxin – Richard Aug 05 '18 at 11:15
  • Thank you. I 'm sorry but I think I misled you. Consider a graph with n vertices and an incidence matrix. Assume that the set of vertices and the incidence matrix is created in GAMS (e.g. imported from Excel). I need a conditional set definition method to exclude row-column pairs of vertices with zero intersection element from the edges set and include others. – Mehdi Aug 05 '18 at 11:53
  • Mehdi, maybe it helps if you post the code you tried and explain based on that what does not work for you. – Lutz Aug 06 '18 at 07:10
  • I rewrote the problem and explained it in more details – Mehdi Aug 06 '18 at 11:10
  • @Mehdi hi , this maybe useful edges(i,j)$(a(i,j)>0) = yes; or edges(i,j)$(a(i,j)) = yes; both work – Richard Aug 06 '18 at 16:57
0

You can simplify your last line to

edges(i,j) = a(i,j);

This automatically acts as if you wrote something like $(a<>0). However, since you defined your symbol a as set already and not as parameter, I think you actually do not have to do anything. A just is what you are looking for. Just do

display a;

and look at the result in the lst file.

Lutz
  • 2,197
  • 1
  • 10
  • 12