Exception in thread "main" java.lang.UnsupportedOperationException: CpxRange for general expressions error

Viewed 30

i want to find solution of this problem plz:i use java,cplex .eclipse-java-2022-03-R-win32-x86_64 : the details are:

  obj : min x1x4(x1+x2+x3)+x4
  s.t :x1.x2.x3.x4>=25
  (square x1)+(square x2)+(square x3)+(square x4)=40
  1=<x1,x2,x3,x4<=5

the code is: public class bb4 {

    public static void main (String args[])  {
    try {
        IloCplex cplex = new IloCplex();
        IloNumVar x1 = cplex.numVar(1, 5, "x1");
        IloNumVar x2 = cplex.numVar(1, 5, "x2");
        IloNumVar x3 = cplex.numVar(1, 5, "x3");
        IloNumVar x4 = cplex.numVar(1, 5, "x4");
        
        IloLinearNumExpr c= cplex.linearNumExpr();
        cplex.addEq(c, cplex.sum(x3,
                  cplex.prod(
                          cplex.prod(x1, x4) , cplex.sum(x1,x2,x3))
                                ));
    cplex.addMinimize(c );
        

    cplex.addGe(cplex.prod(cplex.prod( x1,x2),cplex.prod( x3, x4)), 25);
    cplex.addEq(cplex.sum(cplex.square(x1),
                      cplex.square(x2),
                      cplex.square(x3),
                      cplex.square(x4)      )
                                       , 40);

         }
        catch (IloException exc) {
        exc.printStackTrace();
        
    }
    }
    }

the result is error like this :

    Exception in thread "main" java.lang.UnsupportedOperationException: CpxRange for 
    general expressions
    at ilog.cplex.CpxRange.<init>(CpxRange.java:1335)
    at ilog.cplex.IloCplexModeler.addRange(IloCplexModeler.java:2716)
    at ilog.cplex.IloCplexModeler.addEq(IloCplexModeler.java:2781)
    at ms.bb4.main(bb4.java:24)

any help?

1 Answers

Your model is not linear so within CPLEX I encourage you to use CPOptimizer.

With the OPL API you can write

using CP;

int scale=1000;

int m=1;
int M=5;

dvar int scalex[1..4] in m*scale..M*scale;
dexpr float x[i in 1..4]=scalex[i]/scale;

minimize x[1]*x[4]*(x[1]+x[2]+x[3])+x[4];

subject to
{
  abs(sum(i in 1..4) x[i]*x[i]-40)<=0.00001;
  prod(i in 1..4) x[i]>=25;
}

execute
{
  writeln(x);
}

which gives

// solution with objective 14.27099
 [1.09 4.311 4.326 1.23]

Of course you could do the same with all CPLEX APIs.

Related