如何在 ojAlgo ExpressionBasedModel 中鼓励平衡解决方案
How to encourage a balanced solution in ojAlgo ExpressionBasedModel
我有以下型号:
ExpressionsBasedModel model = new ExpressionsBasedModel();
Variable va = model.addVariable("va")
.lower(0);
Variable vb = model.addVariable("vb")
.lower(0);
Variable vc = model.addVariable("vc")
.lower(0);
Variable vd = model.addVariable("vd")
.lower(0);
Variable ve = model.addVariable("ve")
.lower(0);
Expression ef = model.addExpression("ef")
.lower(0);
Expression eg = model.addExpression("eg")
.lower(0);
Expression eh = model.addExpression("eh")
.lower(-240);
Expression ei = model.addExpression("ei")
.lower(0);
Expression ej = model.addExpression("ej")
.lower(0);
Expression ek = model.addExpression("ek")
.lower(0);
Expression el = model.addExpression("el")
.lower(-2000);
ef.set(va, -50);
el.set(va, -100);
eg.set(va, 100);
eh.set(vb, -30);
ef.set(vb, 40);
ej.set(vb, 20);
ek.set(vc, -30);
eg.set(vc, -30);
ei.set(vc, 60);
ei.set(vd, -30);
eg.set(vd, -30);
ek.set(vd, 60);
ej.set(ve, -40);
el.set(ve, -40);
ek.set(ve, 20);
ei.weight(1);
ek.weight(1);
/*
* These next 2 lines required to get balanced solution.
* Without them ei = 720 and ek = 0.
*/
// ek.lower(360);
// ei.lower(360);
model.maximise();
BasicLogger.debug(model);
虽然ei
和ek
权重相等,但是我得到的解是:
############################################
0 <= va: 6.4 <= 6.4
0 <= vb: 8 <= 8
0 <= vc: 15.111111 (300)
0 <= vd: 6.222222 (300)
0 <= ve: 4 (200) <= 4
0 <= ef: 0.0
0 <= eg: 0.0
-240 <= eh: -240.0
0 <= ei: 720.0
0 <= ej: 0.0
0 <= ek: -0.0
-2000 <= el: -800.0
############################################
ei == 720, ek == 0
.
我更喜欢 ei
和 ek
尽可能平衡的解决方案,(ei == 360, ek == 360
)。是否有某种方法可以将“尽可能平衡”的要求编码为加权表达式?
在这个具体示例中,我注释掉了实际导致我想要的行为的较低值约束。在现实生活中,模型是动态的,我可能有 5 个同等权重的表达式。我不知道正确的值以设置较低的约束。
对于未来的读者,我能够通过首先将 ei
和 ek
表示的值转换为新变量,使用现有表达式 ei
和 [=16] 来做到这一点=] 将新变量的值限制为这些表达式以前具有的值。然后我添加了一个新变量,给它很大的权重,并限制它小于每个新变量。
Expression ei = model.addExpression("ei")
.lower(0);
Expression ek = model.addExpression("ek")
.lower(0);
变成了:
Expression ei = model.addExpression("ei")
.level(0);
Variable vei = model.addVariable("vei")
.lower(0);
ei.set(vei, -1);
Expression ek = model.addExpression("ek")
.level(0);
Variable vek = model.addVariable("vek")
.lower(0);
ek.set(vek, -1);
权重变为:
vei.weight(1);
vek.weight(1);
并添加了余额变量和约束:
Variable balance = model.addVariable("balance")
.weight(100);
model.addExpression("balance constraint for vei")
.set(vei, 1)
.set(balance, -1)
.lower(0);
model.addExpression("balance constraint for vek")
.set(vek, 1)
.set(balance, -1)
.lower(0);
这会产生一个尽可能平衡的解决方案:
############################################
0 <= va: 6.4 <= 6.4
0 <= vb: 8 <= 8
0 <= vc: 11.111111
0 <= vd: 10.222222
0 <= ve: 4 <= 4
0 <= vei: 360 (1)
0 <= vek: 360 (1)
balance: 360 (100)
0 <= ef: 0.0
0 <= eg: 0.0
-240 <= eh: -240.0
0 <= ei: 0.0 <= 0
0 <= ej: 0.0
0 <= ek: -0.0 <= 0
-2000 <= el: -800.0
0 <= balance constraint for vei: 0.0
0 <= balance constraint for vek: 0.0
############################################
我有以下型号:
ExpressionsBasedModel model = new ExpressionsBasedModel();
Variable va = model.addVariable("va")
.lower(0);
Variable vb = model.addVariable("vb")
.lower(0);
Variable vc = model.addVariable("vc")
.lower(0);
Variable vd = model.addVariable("vd")
.lower(0);
Variable ve = model.addVariable("ve")
.lower(0);
Expression ef = model.addExpression("ef")
.lower(0);
Expression eg = model.addExpression("eg")
.lower(0);
Expression eh = model.addExpression("eh")
.lower(-240);
Expression ei = model.addExpression("ei")
.lower(0);
Expression ej = model.addExpression("ej")
.lower(0);
Expression ek = model.addExpression("ek")
.lower(0);
Expression el = model.addExpression("el")
.lower(-2000);
ef.set(va, -50);
el.set(va, -100);
eg.set(va, 100);
eh.set(vb, -30);
ef.set(vb, 40);
ej.set(vb, 20);
ek.set(vc, -30);
eg.set(vc, -30);
ei.set(vc, 60);
ei.set(vd, -30);
eg.set(vd, -30);
ek.set(vd, 60);
ej.set(ve, -40);
el.set(ve, -40);
ek.set(ve, 20);
ei.weight(1);
ek.weight(1);
/*
* These next 2 lines required to get balanced solution.
* Without them ei = 720 and ek = 0.
*/
// ek.lower(360);
// ei.lower(360);
model.maximise();
BasicLogger.debug(model);
虽然ei
和ek
权重相等,但是我得到的解是:
############################################
0 <= va: 6.4 <= 6.4
0 <= vb: 8 <= 8
0 <= vc: 15.111111 (300)
0 <= vd: 6.222222 (300)
0 <= ve: 4 (200) <= 4
0 <= ef: 0.0
0 <= eg: 0.0
-240 <= eh: -240.0
0 <= ei: 720.0
0 <= ej: 0.0
0 <= ek: -0.0
-2000 <= el: -800.0
############################################
ei == 720, ek == 0
.
我更喜欢 ei
和 ek
尽可能平衡的解决方案,(ei == 360, ek == 360
)。是否有某种方法可以将“尽可能平衡”的要求编码为加权表达式?
在这个具体示例中,我注释掉了实际导致我想要的行为的较低值约束。在现实生活中,模型是动态的,我可能有 5 个同等权重的表达式。我不知道正确的值以设置较低的约束。
对于未来的读者,我能够通过首先将 ei
和 ek
表示的值转换为新变量,使用现有表达式 ei
和 [=16] 来做到这一点=] 将新变量的值限制为这些表达式以前具有的值。然后我添加了一个新变量,给它很大的权重,并限制它小于每个新变量。
Expression ei = model.addExpression("ei")
.lower(0);
Expression ek = model.addExpression("ek")
.lower(0);
变成了:
Expression ei = model.addExpression("ei")
.level(0);
Variable vei = model.addVariable("vei")
.lower(0);
ei.set(vei, -1);
Expression ek = model.addExpression("ek")
.level(0);
Variable vek = model.addVariable("vek")
.lower(0);
ek.set(vek, -1);
权重变为:
vei.weight(1);
vek.weight(1);
并添加了余额变量和约束:
Variable balance = model.addVariable("balance")
.weight(100);
model.addExpression("balance constraint for vei")
.set(vei, 1)
.set(balance, -1)
.lower(0);
model.addExpression("balance constraint for vek")
.set(vek, 1)
.set(balance, -1)
.lower(0);
这会产生一个尽可能平衡的解决方案:
############################################
0 <= va: 6.4 <= 6.4
0 <= vb: 8 <= 8
0 <= vc: 11.111111
0 <= vd: 10.222222
0 <= ve: 4 <= 4
0 <= vei: 360 (1)
0 <= vek: 360 (1)
balance: 360 (100)
0 <= ef: 0.0
0 <= eg: 0.0
-240 <= eh: -240.0
0 <= ei: 0.0 <= 0
0 <= ej: 0.0
0 <= ek: -0.0 <= 0
-2000 <= el: -800.0
0 <= balance constraint for vei: 0.0
0 <= balance constraint for vek: 0.0
############################################