当在多个集合上定义参数时,如何在 AMPL 中初始化参数?
how I can initialize a parameter in AMPL when it is defined on multiple sets?
假设我有
param m; #number of modes
param n; #number of individual
param a; #number of alternatives
param f; #number of household
set M, default{1..m}; #set of modes
set N, default{1..n}; #set of individuals
set A, default{1..a}; #set of alternatives
set F, default{1..f}; #set of family
set E, within F cross N
如何初始化参数 X{E,M,A}?
假设
a:=2 , m:=3 , n:= 4 f:=2;
并定义集合 E:
set E:= 1 1 1 2 2 3 2 4 ;
您可以按照您的建议声明参数:
param X{E,M,A};
现在,如果您想提供一个默认值(我假设这就是您要问的),您可以按照通常的方式进行:
param X{E,M,A} default 0;
然后在 .dat
文件中提供一些 non-default 值,例如:
param: X :=
1 1 1 2 5
2 3 2 1 6;
请注意,在您调用 solve
之前,AMPL 不会将默认值填充到参数中。从 AMPL book, p. 120:
The expression that gives the default value of a parameter is evaluated only when the parameter’s value is first needed, such as when an objective or constraint that uses the parameter is processed by a solve
command.
因此,如果您在发出 model
和 data
命令之后但在发出 solve
命令之前键入 display X;
,您将只会得到non-default 值,例如:
X :=
1 1 1 2 5
2 3 2 1 6
;
但是,如果您在调用 solve
后使用 display X;
,您将获得完整列表:
X [1,*,*,1] (tr)
: 1 2 :=
1 0 0
2 0 0
3 0 0
[1,*,*,2] (tr)
: 1 2 :=
1 5 0
2 0 0
3 0 0
[2,*,*,1] (tr)
: 3 4 :=
1 0 0
2 6 0
3 0 0
[2,*,*,2] (tr)
: 3 4 :=
1 0 0
2 0 0
3 0 0
;
为了完整起见,这里是我用于此答案的 .mod
和 .dat
文件:
.mod:
param m; #number of modes
param n; #number of individual
param a; #number of alternatives
param f; #number of household
set M, default{1..m}; #set of modes
set N, default{1..n}; #set of individuals
set A, default{1..a}; #set of alternatives
set F, default{1..f}; #set of family
set E, within F cross N;
param X{E,M,A} default 0;
var myVar{E,M,A} >= 0;
minimize Obj: sum {(i,j) in E, mm in M, aa in A} X[i,j,mm,aa] * myVar[i,j,mm,aa];
.dat:
param a:=2;
param m:=3;
param n:= 4;
param f:=2;
set E:= 1 1 1 2 2 3 2 4 ;
param: X :=
1 1 1 2 5
2 3 2 1 6;
假设我有
param m; #number of modes
param n; #number of individual
param a; #number of alternatives
param f; #number of household
set M, default{1..m}; #set of modes
set N, default{1..n}; #set of individuals
set A, default{1..a}; #set of alternatives
set F, default{1..f}; #set of family
set E, within F cross N
如何初始化参数 X{E,M,A}?
假设
a:=2 , m:=3 , n:= 4 f:=2;
并定义集合 E:
set E:= 1 1 1 2 2 3 2 4 ;
您可以按照您的建议声明参数:
param X{E,M,A};
现在,如果您想提供一个默认值(我假设这就是您要问的),您可以按照通常的方式进行:
param X{E,M,A} default 0;
然后在 .dat
文件中提供一些 non-default 值,例如:
param: X :=
1 1 1 2 5
2 3 2 1 6;
请注意,在您调用 solve
之前,AMPL 不会将默认值填充到参数中。从 AMPL book, p. 120:
The expression that gives the default value of a parameter is evaluated only when the parameter’s value is first needed, such as when an objective or constraint that uses the parameter is processed by a
solve
command.
因此,如果您在发出 model
和 data
命令之后但在发出 solve
命令之前键入 display X;
,您将只会得到non-default 值,例如:
X :=
1 1 1 2 5
2 3 2 1 6
;
但是,如果您在调用 solve
后使用 display X;
,您将获得完整列表:
X [1,*,*,1] (tr)
: 1 2 :=
1 0 0
2 0 0
3 0 0
[1,*,*,2] (tr)
: 1 2 :=
1 5 0
2 0 0
3 0 0
[2,*,*,1] (tr)
: 3 4 :=
1 0 0
2 6 0
3 0 0
[2,*,*,2] (tr)
: 3 4 :=
1 0 0
2 0 0
3 0 0
;
为了完整起见,这里是我用于此答案的 .mod
和 .dat
文件:
.mod:
param m; #number of modes
param n; #number of individual
param a; #number of alternatives
param f; #number of household
set M, default{1..m}; #set of modes
set N, default{1..n}; #set of individuals
set A, default{1..a}; #set of alternatives
set F, default{1..f}; #set of family
set E, within F cross N;
param X{E,M,A} default 0;
var myVar{E,M,A} >= 0;
minimize Obj: sum {(i,j) in E, mm in M, aa in A} X[i,j,mm,aa] * myVar[i,j,mm,aa];
.dat:
param a:=2;
param m:=3;
param n:= 4;
param f:=2;
set E:= 1 1 1 2 2 3 2 4 ;
param: X :=
1 1 1 2 5
2 3 2 1 6;