AMPL:在集合的特定元素中定义约束

AMPL: define constraints in specific elements of a set

我有这样的结构:

set U;
param d {i in U};

如何在 d 的第一个、第二个和第三个元素中添加约束?

我抽象了U的大小,因为我猜它更好,但实际上,对于我的问题,U只有3 个元素 d.

我不想为 U 创建 3 个参数,为 d.

创建 3 个变量

正如您实现的那样,U 是一个无序集。这意味着 "first element of U" 没有明确定义。由于 U 是 d 的索引集,因此 "the first element of d" 也没有明确定义。

例如,考虑以下代码:

set U := {"fish","chips","salt"};
var d{U} := 0;
solve;
display d;

答案显示为:

d [*] :=
chips  0
 fish  0
 salt  0
;

请注意,AMPL 已按照与我声明它们的顺序不同的顺序对 U 的元素进行排序。 (具体来说,它按字母顺序排列了它们。)

所以这个问题的答案取决于你"add constraints in the first, second and third element of d"的确切意思。

如果您只想对 d 的每个成员应用 same 约束,您可以使用在 U:

上索引的单个约束
set U := {"fish","chips","salt"};
var d{U};

s.t. constrain_all{u in U}: d[u] >= 0;

如果你想对 d 的每个成员按名称应用特定的约束,你可以使用类似的格式:

set U := {"fish","chips","salt"};
var d{U};

s.t. constrain_fish: d["fish"] >= 0;
s.t. constrain_chips: d["chips"] >= 5;
s.t. constrain_salt: d["salt"] >= 10;

如果您确实对 U 有特定的排序,则需要将 U 声明为 有序集。 U 的每个元素在该集合中都有特定的基数,您可以使用 "member" 和 "ord" 函数按位置引用 U 的元素。例如:

set U := {"fish","chips","salt"} ordered;
var d{U};

s.t. constrain_fish: d["fish"] >= 0;
s.t. constrain_chips: d["chips"] >= 5;
s.t. constrain_salt: d["salt"] >= 10;

s.t. constrain_second_member_of_U: d[member(2,U)] >= 25;

minimize of: sum{u in U} d[u];
solve;
display d;

如所写,这要求 d["chips"] 大于或等于 25。但是,如果我将 U 的声明从 {"fish"、"chips" 更改为"salt"} 到 {"chips","fish","salt"},该约束现在将应用于 d["fish"] 而不是 d["chips" ].

如果我想对 d 的第 5-10 个成员设置约束,我可以这样写:

s.t. constrain_5th_to_10th{u in U: ord(u,U) >= 5, ord(u,U) <= 10}: d[u] >= 100;

有关有序集的详细信息,请参阅 chapter 5 of the AMPL Book