Java API中是否有用于条件表达式的CPLEX函数?

时间:2019-01-23 11:11:22

标签: java cplex

是否可以在CPLEX Java API中包含条件表达式(“ condition?exp1:exp2”)? 在OPL中,我可以这样写(x,y,c是决策变量)

(c==1 ? x+y : x ) == 0

但是我没有在Java API中找到等效功能

1 个答案:

答案 0 :(得分:-1)

你可以写

solver.addEq((c==1)?solver.sum(x,y):x,0);

让我给你举个小例子

    IloCplex solver = new IloCplex();

    IloNumVar x = solver.numVar(0,10); 
    IloNumVar y = solver.numVar(0,10); 

    int c=1;
    solver.addEq((c==1)?solver.sum(x,y):x,0);
    solver.addMaximize(solver.sum(x,y));
    solver.solve();
    System.out.println("x = " + solver.getValue(x));
    System.out.println("y = " + solver.getValue(y));