在Cplex Java中如何使用以避免过度补偿的模型?

时间:2019-07-03 22:18:22

标签: java cplex

我正在Java应用程序中使用Cplex建立一个多目标优化模型。但是,当它求解模型时,它会过度补偿一个变量,而其余变量则为0。我可以用什么来获得更加分散的解决方案?

 int[] x = {49,43,43,44,48,49,51,54,51,52,57,59};
 double[] y = {10, 12, 13.2, 22.7, 17.1, 16.5, 14.87, 12, 16.5, 14.8, 12, 11.5};
int[] z = {59, 59, 57, 57, 53, 53, 52, 51, 51, 50, 50, 50};
int totalVacations = 73;

        try {
            IloCplex model = new IloCplex();
            int size = 12;
            IloNumVarType varType = IloNumVarType.Int;
            double[] lb = new double[size];
            double[] ub = new double[size];
            IloNumVarType[] varTypes = new IloNumVarType[size];
            for (int i = 0; i < lb.length ; i++) {
                lb[i] = 0.0;
                ub[i] = Double.MAX_VALUE;
                varTypes[i] = varType;
            }
            IloNumVar[] varUsed = model.numVarArray(size, lb, ub, varTypes);
            for (int i = 0; i < varUsed.length; i++) {
                model.add(varUsed[i]);
            }
            IloNumExpr[] objArray = new IloNumExpr[size];
            for (int i = 0; i < objArray.length; i++) {
                IloObjective next = model.maximize();
                IloNumExpr exprVar = varUsed[i];
                double setValue = z[i] - x[i] - y[i];
                next.setExpr(model.diff(setValue, exprVar));
                next.setSense(IloObjectiveSense.Maximize);
                objArray[i] = next.getExpr();
            }

            model.add(model.maximize(model.staticLex(objArray)));
            model.addEq(totalVacations, model.sum(varUsed), "c1");
            if (model.solve()) {
                double[] results = model.getValues(varUsed);
                for (int i = 0; i < results.length; i++) {
                    System.out.println(results[i]);
                }
            }
            System.out.println(model.toString());
            model.end();
        } catch (IloException e) {
            System.err.println("Concert exception caught: " + e);
        }

这些值是 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 73.0

当我希望它更加分散时(虽然不均匀)。在这种情况下有什么建议吗?

1 个答案:

答案 0 :(得分:2)

让我使用OPL来向您展示如何做:

您写了Java的等价物

int totalVacation=73;
range r=1..12;

int  x[r] = [49,43,43,44,48,49,51,54,51,52,57,59];
float y[r] = [10, 12, 13.2, 22.7, 17.1, 16.5, 14.87, 12, 16.5, 14.8, 12, 11.5];
float z[r] = [59, 59, 57, 57, 53, 53, 52, 51, 51, 50, 50, 50];

dvar int varUsed[r] in 0..totalVacation;

maximize sum(i in r) (z[i] - x[i] - y[i]-varUsed[i]);

subject to
{
c1:sum(i in r) varUsed[i]==totalVacation;
}

给出

varUsed = [73 0 0 0 0 0 0 0 0 0 0 0];

因为客观上没有关于公平的事情。

但是如果您将目标从...更改

maximize sum(i in r) (z[i] - x[i] - y[i]-varUsed[i]);

maximize staticLex(sum(i in r) (z[i] - x[i] - y[i]-varUsed[i]),
        -max(j in r) varUsed[j]+min(j in r) varUsed[j]);

然后您添加第二个字典目的以最大程度地提高公平性,然后您将得到

varUsed = [7 6 6 6 6 6 6 6 6 6 6 6];