Originally posted by: SystemAdmin
From script you can just change objective co-efficients as Alex showed. You can change objective completely by making external java call. Here is an example based on earlier example by Alex:
dvar
int x; dvar
int y; minimize x+y; subject to
{ 5<=x<=15; 5<=y<=15;
} execute
{ writeln(
"x=",x,
" y=",y);
} main
{ IloOplImportJava(
"Test.jar"); thisOplModel.generate(); cplex.solve(); thisOplModel.postProcess(); IloOplCallJava(
"com.opl.sample.Test",
"ChangeObjective",
"", thisOplModel); cplex.solve(); thisOplModel.postProcess();
} On Java Side:
package com.opl.sample;
import ilog.concert.IloException;
import ilog.concert.IloIntVar;
import ilog.concert.IloNumExpr;
import ilog.cplex.IloCplex;
import ilog.opl.IloOplModel;
public
class Test
{
public
static
void ChangeObjective(IloOplModel model)
{
try
{ IloCplex cplex = model.getCplex(); IloIntVar x = model.getElement(
"x").asIntVar(); IloIntVar y = model.getElement(
"y").asIntVar(); IloNumExpr expr = cplex.linearNumExpr(); expr = cplex.diff(expr, cplex.prod(x, 2)); expr = cplex.diff(expr, cplex.prod(y, 2)); cplex.remove(cplex.getObjective()); cplex.addMinimize(expr);
}
catch(IloException ex)
{ ex.printStackTrace();
}
}
}
#DecisionOptimization#OPLusingCPLEXOptimizer