如何在 AMPL 中定义一组路径或一组链接序列?

How to define a set of paths, or set of sequences of links, in AMPL?

我正在尝试定义一个结构来捕获如下内容:

set NODES := A B C;
set LINKS := (A,B) (B,C);
set PATHS := ((A,B)) 
             ((A,B), (B,C))
             ((B,C));

节点是一个集合。链接是一组节点对。

我无法将路径定义为一组链接序列。我没有在 AMPL 图形示例中看到任何明确使用路径的解决方案,我想知道是否有一种简单的方法来构造它们?

这是我的 .mod 文件中的定义:

set NODES; 
set LINKS within (NODES cross NODES);
set PATHS # ??? ;

请帮忙。

鉴于您的路径没有重复节点,我能想到的定义路径的最自然方式是将其作为有序节点集的集合。

reset;
model;
set NODES;
set LINKS within {NODES,NODES};

param n_paths;
set PATHS{1..n_paths} within NODES ordered;

# Optional: identify all of the links implied by these paths, so we can
# check that they are in fact within LINKS. 

param longest_path_length := max{i in 1..n_paths} card(PATHS[i]);
set LINKS_IMPLIED_BY_PATHS within LINKS := setof{
      i in 1..n_paths, 
      j in 1..(longest_path_length-1): j < card(PATHS[i])
    } (member(j,PATHS[i]),member(j+1,PATHS[i]))
    ;

data;
set NODES := A B C;
set LINKS := (A,B) (B,C);

param n_paths := 3;
set PATHS[1] := A B;
set PATHS[2] := A B C;
set PATHS[3] := B C;

display LINKS_IMPLIED_BY_PATHS;
# if we include a path like C A, we will get an error here because ("C","A") 
# is not within LINKS.
# It should be possible to do this more tidily with a check statement but
# for the moment the syntax escapes me.
# Note that this error will ONLY appear at the point where we try to
# do something with LINKS_IMPLIED_BY_PATHS; it's not calculated or checked
# until then. 

这不是您所要求的,因为它将路径定义为一系列节点而不是链接,但这是我能得到的最接近的。