简体   繁体   中英

creating two kinds of dependent variable using cplex and java

I am new to work by CPLEX. My simplified optimization problem is:

 objective function: 
            Maximize z1 + z2 + z3
 Subject to:   
            c1: x1 - 3 x2 + x3 <= 30
            c2: x1 + x2 + x3 >= z1
            ...
 Bounds
            x1=[0,1]
            x2=[0,1] 
            ...

To model this problem, my code is:

public static void main(String[] args) {
    try {

        IloCplex cplex = new IloCplex();            
        IloNumVar[] z = cplex.numVarArray(3, 0.0, 1.0);
        IloLinearNumExpr objectiveExpr = cplex.linearNumExpr();

        IloLinearNumExpr constraintExpr1 = cplex.linearNumExpr();
        IloLinearNumExpr constraintExpr3 = cplex.linearNumExpr();
        IloNumVar[] x = cplex.numVarArray(3, 0.0, 1.0);
        for (int i = 1; i < 3; i++){
            objectiveExpr.addTerm(1, z[i]);
        }
        IloObjective obj = cplex.maximize(objectiveExpr);
        cplex.add(obj); 
        constraintExpr1.addTerm(1, x[1]);
        constraintExpr1.addTerm(-3, x[2]);
        constraintExpr1.addTerm(1, x[3]);
        cplex.addLe(constraintExpr1, 30);

        constraintExpr2.addTerm(-1, x[1]);
        constraintExpr2.addTerm(1, x[2]);
        constraintExpr2.addTerm(1, x[3]);
        cplex.addGe(constraintExpr2, z[1]);
        .
        .
        .
    }
    catch (IloException e){
        System.err.println("Concert exception '" + e + "' caught");
    }


}

The generated model of this code is:

objective function:  
           Maximize x1 + 2 x2 + 3 x3
Subject To:               
           c1: x7 - 3 x8 + x9 <= 30
           c2: - x1 - x7 + x8 + x9 <= 0
Bounds
           0 <= x1 <= 1
           ...

If it starts to model the constraints from x4 instead of x7, I could distinguish x and z easily.

Unlike David, I do know CPLEX for about 17 years, but I also don't understand what your problem is. Can you explain in what way the generated model is different from what you expected - we can probably work this out ourselves, but it seems daft for potentially many of us to have to spend extra time understanding your problem when you could explain better.

Can I suggest that you name your variables and constraints using setName(...) so that you can see better what is happening in the generated model.

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