Hi,
so let me share a complete tiny example.
Consider the very good example in opl\examples\opl_interfaces\java\mulprod and change the model into
{string} Products = ...;
{string} Resources = ...;
int NbPeriods = ...;
range Periods = 1..NbPeriods;
float Consumption[Resources][Products] = ...;
float Capacity[Resources] = ...;
float Demand[Products][Periods] = ...;
float InsideCost[Products] = ...;
float OutsideCost[Products] = ...;
float Inventory[Products] = ...;
float InvCost[Products] = ...;
range Periods0 = 0..NbPeriods;
dvar float+ Inside[Products][Periods];
dvar float+ Outside[Products][Periods];
dvar float+ Inv[Products][Periods0];
dvar float costInside;
dvar float costOutside;
dvar float costInv;
/*minimize
sum( p in Products, t in Periods )
(InsideCost[p]*Inside[p][t] +
OutsideCost[p]*Outside[p][t] +
InvCost[p]*Inv[p][t]);*/
minimize costInside+costOutside+costInv;
subject to {
costInside==
sum( p in Products, t in Periods )
InsideCost[p]*Inside[p][t];
costOutside==
sum( p in Products, t in Periods )
OutsideCost[p]*Outside[p][t] ;
costInv==
sum( p in Products, t in Periods )
InvCost[p]*Inv[p][t];
forall( r in Resources, t in Periods )
ctCapacity:
sum( p in Products )
Consumption[r][p] * Inside[p][t] <= Capacity[r];
forall( p in Products , t in Periods )
ctDemand:
Inv[p][t-1] + Inside[p][t] + Outside[p][t] == Demand[p][t] + Inv[p][t];
forall( p in Products )
ctInventory:
Inv[p][0] == Inventory[p];
};
tuple plan {
float inside;
float outside;
float inv;
}
plan Plan[p in Products][t in Periods] = <Inside[p,t],Outside[p,t],Inv[p,t]>;
execute DISPLAY {
writeln("plan=",Plan);
}
tuple InsideSolutionT{
string Products;
int Periods;
float value;
};
{InsideSolutionT} InsideSolution = {<i0,i1,Inside[i0][i1]> | i0 in Products,i1 in Periods};
tuple OutsideSolutionT{
string Products;
int Periods;
float value;
};
{OutsideSolutionT} OutsideSolution = {<i0,i1,Outside[i0][i1]> | i0 in Products,i1 in Periods};
tuple InvSolutionT{
string Products;
int Periods0;
float value;
};
{InvSolutionT} InvSolution = {<i0,i1,Inv[i0][i1]> | i0 in Products,i1 in Periods0};
in order to have 3 KPIs in the objective
Then when you run you get
[java] costInside = 22.333;
[java] costOutside = 434.67;
[java] costInv = 0;
[java] Inside = [[10 0 0]
[java] [0 0 0]
[java] [50 66.667 66.667]];
[java] Outside = [[0 100 50]
[java] [20 200 100]
[java] [0 33.333 33.333]];
[java] Inv = [[0 0 0 0]
[java] [0 0 0 0]
[java] [0 0 0 0]];
but then if in mulprod.java
you change
opl.generate();
if (cplex.solve())
{
System.out.println("OBJECTIVE: " + opl.getCplex().getObjValue());
opl.postProcess();
opl.printSolution(System.out);
}
into
opl.generate();
IloObjective masterObj = opl.getObjective();
IloNumVar insideCost = opl.getElement("costInside").asNumVar();
cplex.setLinearCoef(masterObj, insideCost, 10);
if (cplex.solve())
{
System.out.println("OBJECTIVE: " + opl.getCplex().getObjValue());
opl.postProcess();
opl.printSolution(System.out);
}
then you ll get
[java] costInside = 0;
[java] costOutside = 516;
[java] costInv = 0;
[java] Inside = [[0 0 0]
[java] [0 0 0]
[java] [0 0 0]];
[java] Outside = [[10 100 50]
[java] [20 200 100]
[java] [50 100 100]];
[java] Inv = [[0 0 0 0]
[java] [0 0 0 0]
[java] [0 0 0 0]];
we simply changed the coefficient of the inside cost in the orbjective
regards
https://www.linkedin.com/pulse/making-decision-optimization-simple-alex-fleischer/
#DecisionOptimization#OPLusingCPLEXOptimizer