I have the following model:
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);
Although ei and ek are equally weighted, the solution I get is:
############################################
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.
I would prefer a solution in which ei and ek are as balanced as possible, (ei == 360, ek == 360). Is there some way of encoding that requirement for "as balanced as possible" as a weighted expression?
In this specific example I have commented out the lower value constraint that actually result in the behavior I want. In real life, the model is dynamic and I may have 5 equally weighted expressions. I won't know the correct values in order to set the lower constraints.
For future readers, I was able to do this by first converting the values represented by
eiandekinto new variables, with the existing expressionseiandekconstraining the value of the new variables to the value these expressions previously had. Then I added a new variable, weighted it heavily, and constrained it to be less than each of the new variables.became:
Weighting became:
And added balance variable and constraints:
This results in a solution that is as balanced as possible: