I am trying to understand what exactly Expressions in cplex are and how I can use them in Java for creating a Linear Programm. Unfortunately there aren't any sufficient examples in the documentation for me to understand them.
What I am trying to write, is some Constraints with the following information:
- coefficient: c with three indices {i in I}, {j in J}, {k in K}
- variable: v with three indices {i in I}, {j in J}, {k in K}
- RHS: rhs with three indices {i in I}, {j in J}, {k in K}
sum {i in I}, {j in J}, {k in K} of c[i][j][k]*v[i][j][k] >= rhs[i][j][k]
sum {k in K} of c[i][j][k]*v[i][j][k] >= rhs[i][j][k] for all {i in I}, {j in J}
Would a possible solution for the 1. Question be, the initialization of the Expression before the three loops and for the 2. the initialization of the Expression before the third loop?
Inside the loop follows the expression.addTerm(...,...);
Also would be for the 2. Question between theese codes expression[i][j].addTerm(...,...);
and expression.addTerm(...,...);
a difference?
Here is also a code of my small minimization Linear Programm.
import java.util.ArrayList;
import java.util.HashMap;
import ilog.concert.IloIntVar;
import ilog.concert.IloLinearIntExpr;
import ilog.cplex.IloCplex;
public class MiniCplex {
public static void main(String[] args) {
miniCplex();
}
public static void miniCplex () {
try {
HashMap<Integer,Integer> zw = new HashMap<Integer,Integer>();
zw.put(0, 1);
zw.put(1, 1);
zw.put(2, 0);
zw.put(3, 0);
zw.put(4, 2);
zw.put(5, 2);
HashMap<Integer,Integer> rhs = new HashMap<Integer,Integer>();
rhs.put(0, 20);
rhs.put(1, 40);
rhs.put(2, 0);
rhs.put(3, 0);
rhs.put(4, 5);
rhs.put(5, 7);
ArrayList<HashMap<Integer,Integer>> forms = new ArrayList<HashMap<Integer,Integer>>();
HashMap<Integer,Integer> formGA = new HashMap<Integer,Integer>();
formGA.put(0, 1);
formGA.put(1, 1);
formGA.put(2, 0);
formGA.put(3, 0);
formGA.put(4, 1);
formGA.put(5, 1);
forms.add(formGA);
HashMap<Integer,Integer> formEZ1 = new HashMap<Integer,Integer>();
formEZ1.put(0, 3);
formEZ1.put(1, 0);
formEZ1.put(2, 0);
formEZ1.put(3, 0);
formEZ1.put(4, 0);
formEZ1.put(5, 0);
forms.add(formEZ1);
HashMap<Integer,Integer> formEZ2 = new HashMap<Integer,Integer>();
formEZ2.put(0, 0);
formEZ2.put(1, 4);
formEZ2.put(2, 0);
formEZ2.put(3, 0);
formEZ2.put(4, 0);
formEZ2.put(5, 0);
forms.add(formEZ2);
// System.out.println("forms: " + forms.get(0).get(3));
IloCplex cplex = new IloCplex();
//variables
IloIntVar [] p = new IloIntVar[3]; // Anzahl der Formen
for (int j = 0; j < 3; j++) {
p[j] = cplex.intVar(0, 8);
}
//objective
IloLinearIntExpr objective = cplex.linearIntExpr();
for (int l = 0; l < 3; l++) {
objective.addTerm(1,p[l]);
}
// define objective
cplex.addMinimize(objective);
//expressions
// IloLinearIntExpr expr = new IloLinearIntExpr;
for (int l = 0; l < formEZ2.size(); l++) {
IloLinearIntExpr expr = cplex.linearIntExpr();
for (int l2 = 0; l2 < forms.size(); l2++) {
// System.out.println("EZ index: " + l + " von der " + l2 + " form");
expr.addTerm(forms.get(l2).get(l), p[l2]);
}
cplex.addGe(expr, (rhs.get(l)-zw.get(l)));
}
if (cplex.solve()) {
System.out.println("An optimal solution for the LP has been found!");
double objectVal = cplex.getObjValue();
System.out.println("Objective function: " + objectVal);
for (int i = 0; i < p.length; i++) {
System.out.println("The value for the variable v(" + i + ") is " + cplex.getValue(p[i]));
}
} else {
System.out.println("LP not solved");
}
cplex.end();
} catch (Exception exc) {
exc.printStackTrace();
}
}
}