简体   繁体   中英

Cplex java multidimensional decision variable, minimization

I'm using Cplex in Java and want to minimize the sum of the products of elements from two matrices (products of elements with the same index).

x[n][n] contains decisionvariables [0, 1]

cost[n][n] contains the cost for the way from i to j

I want to minimize the sum of costs x[i][j] * j[i][j] for all i..n; j..n.

I created the variables like this:

[...]
static double   lb = 0.0;
static double   ub = 1.0;
static double   cost[][] = new double[n][n];

IloNumVar[][] x = new IloNumVar[n][n];
        for(int i=0; i<n; i++){
            for(int j=0; j<n; j++){
                x[i][j] = cplex.numVar(lb, ub);
                }
        }

My Problem is that I don't know how to create the minimize part.

I found something that seems to be very similar to my problem ( Cplex c++ multidimensional decision variable ) but as I am not familiar to c++, I don't get any solution out of it.

This should do it:

IloLinearNumExpr obj = cplex.linearNumExpr();

for (int i = 0; i < n; i++) {
  for (int j = 0; j < n; j++) {
    obj.addTerm(cost[i][j], x[i][j]);
  }
}

cplex.addMinimize(obj);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM