Search code examples
javamathematical-optimizationlinear-programmingcplex

Java Cplex Expressions and Constraints


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}
    1. sum {i in I}, {j in J}, {k in K} of c[i][j][k]*v[i][j][k] >= rhs[i][j][k]
    2. 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();
        }
    }

}

Solution

  • An expression is a combination of constants (i.e., fixed numbers) and variables that later can be used in a model. A very important type of expressions are linear expressions, which are linear combinations of variables (i.e., sums of terms, where each term is a variable multiplied by a constant).

    The code expr = cplex.linearIntExpr(); creates and assigns to expr an empty linear expression, and then you can add terms to the expression with expr.addTerm(constant, variable);. To this end, you must provide (in the second argument) a reference to the variable participating in the term, and the easiest way is to keep such references in an array. When you create a new variable (e.g., with cplex.intVar(...) or cplex.boolVar(...)) you get a reference to the new variable. You can also extend an expression by expr = cplex.sum(expr, cplex.prod(constant, variable));.

    In a linear or integer linear model, you use linear expressions in the objective function and for creating constraints. In order to specify the model objective function, you must create an expression, say obj, representing the objective function and then execute cplex.addMaximize(obj);. Constraints also have a right-hand-side, so you can create an expression, say lhs, with the left-hand-side (involving the model variables), and then add a constraint with cplex.addLe(lhs, 1, "constraint_name");. This last statement creates the constraint "lhs <= 1".

    You must create one expression for each constraint in the model, and then execute cplex.addLe(...); or model.addGe(...); or model.addEq(...); to add the constraint to the model. You must also create one expression for the objective function, and add the objective function with cplex.addMaximize(...); or cplex.addMinimize(...);.

    I hope this helps!