我正在尝试定义一个结构来捕获如下所示的内容:
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.
这不是你要求的,因为它将路径定义为一系列节点而不是链接,但它是我能得到的最接近的。