当 dvar B 依赖于 A 时如何更改 dvar B 的值作为 dvar A 更改
How to change value of dvar B as dvar A changes when dvar B depends on A
现在我遇到了一个关于'changing value of dvar B as dvar A changes when dvar B depends on A.'
的问题
有一个代码是我自己写的,简单
int a = 3;
int e = 4;
int t = 6;
int h = 2;
range aRange = 1..a;
range eRange = 1..e;
range tRange = 1..t;
range hRange = 1..h;
int C[aRange][eRange] = [[2,5,8,7]
[3,6,8,1]
[8,7,1,0]];
int N[eRange][hRange] = [[8,6]
[4,1]
[9,7]
[5,4]];
dvar boolean A[aRange][eRange][tRange];
dvar int B[eRange][hRange][tRange] in 0..100;
maximize
sum(a in aRange, e in eRange, t in tRange, h in hRange) C[a][e] * A[a][e][t] * B[e][h][t];
subject to {
cons01 :
forall(e in eRange, h in hRange, t in tRange) {
B[e][h][t] <= N[e][h]; }
}
在这种情况下,我想实现一个条件
'if x[a][e][t] = 0, then B[e][h][t] = B[e][h][t-1] + B[e][h][t-1]/N[e][h].
else x[a][e][t] = 1, B[e][h][t] = 0.'
如何在我的代码中实现上述条件(约束或主题函数或使用数据脚本..?)
您可以使用逻辑约束。
例如你可以写:
forall(a in aRange,e in eRange,h in hRange,t in tRange:t!=1)
(x[a][e][t] == 0) => (B[e][h][t] == B[e][h][t-1] + B[e][h][t-1]/N[e][h]);
在"subject to"块
现在我遇到了一个关于'changing value of dvar B as dvar A changes when dvar B depends on A.'
的问题有一个代码是我自己写的,简单
int a = 3;
int e = 4;
int t = 6;
int h = 2;
range aRange = 1..a;
range eRange = 1..e;
range tRange = 1..t;
range hRange = 1..h;
int C[aRange][eRange] = [[2,5,8,7]
[3,6,8,1]
[8,7,1,0]];
int N[eRange][hRange] = [[8,6]
[4,1]
[9,7]
[5,4]];
dvar boolean A[aRange][eRange][tRange];
dvar int B[eRange][hRange][tRange] in 0..100;
maximize
sum(a in aRange, e in eRange, t in tRange, h in hRange) C[a][e] * A[a][e][t] * B[e][h][t];
subject to {
cons01 :
forall(e in eRange, h in hRange, t in tRange) {
B[e][h][t] <= N[e][h]; }
}
在这种情况下,我想实现一个条件
'if x[a][e][t] = 0, then B[e][h][t] = B[e][h][t-1] + B[e][h][t-1]/N[e][h]. else x[a][e][t] = 1, B[e][h][t] = 0.'
如何在我的代码中实现上述条件(约束或主题函数或使用数据脚本..?)
您可以使用逻辑约束。 例如你可以写:
forall(a in aRange,e in eRange,h in hRange,t in tRange:t!=1)
(x[a][e][t] == 0) => (B[e][h][t] == B[e][h][t-1] + B[e][h][t-1]/N[e][h]);
在"subject to"块