我正在尝试定义一种结构来捕获如下内容:
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 # ??? ;
请帮助。
答案 0 :(得分:1)
鉴于您的路径没有重复的节点,我想到的最自然的定义路径的方法是将节点按顺序排列。
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.
这不是您真正想要的,因为它将路径定义为一系列节点而不是链接,但这是我能获得的最接近的路径。