Decision Optimization

Decision Optimization

Delivers prescriptive analytics capabilities and decision intelligence to improve decision-making.


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  Scal prod with both IntVar and NumVar

    Posted 01/23/15 11:19 AM

    Originally posted by: davidoff


    Hi

    I have a lloNumVar array made of both IloIntVar 

    if I create a scalProd expression with a double[] of coefficients,it triggers an error 

    java.lang.ClassCastException: [Lilog.concert.IloNumVar; incompatible with [Lilog.concert.IloIntVar

    I found a workaround creating my integer variables with cplex.numVar(ln,ub,IloNumVar.Int) and boolean variables with IloNumVar.Bool, otherwise I get an error (if these are created with cplex.intVar or cplex.boolVar)

     

    Is this error normal ?

    Thanks

    David


    #DecisionOptimization
    #MathematicalProgramming-General


  • 2.  Re: Scal prod with both IntVar and NumVar

    Posted 01/25/15 04:49 AM

    This code works fine for me:

    import ilog.cplex.*;
    import ilog.concert.*;
    
    public final class ScalProd {
       public static void main(String[] args) throws IloException {
          final IloCplex cplex = new IloCplex();
          IloNumVar[] array = new IloNumVar[3];
          array[0] = cplex.numVar(0, Double.POSITIVE_INFINITY, "x");
          array[1] = cplex.intVar(0, 10, "y");
          array[2] = cplex.boolVar("z");
          System.out.println(cplex.scalProd(array, new double[]{ 2.0, 2.0, 2.0 }));
       }
    }
    

    It prints

    (2.0*x + 2.0*y + 2.0*z)

    So you can mix different sub-classes of IloNumVar in arrays passed to scalProd(). Can you please show us your code?

    From your error message it looks more like you trying to explicitly cast an 'IloNumVar[]' to an 'IloIntVar[]'? That may of course fail if the array instance is not an IloIntVar[].


    #DecisionOptimization
    #MathematicalProgramming-General


  • 3.  Re: Scal prod with both IntVar and NumVar

    Posted 01/27/15 02:01 PM

    Originally posted by: davidoff


    My code is not Embedded in a unit test so I can't show the full code right now. But the code is pretty similar to your example. The only difference that I see is that I transformed a List<IloNumVar> to the IloNumVar[] before, and in this case, I had the exception using cplex.intVar or cplex.boolVar

     

     


    #DecisionOptimization
    #MathematicalProgramming-General


  • 4.  Re: Scal prod with both IntVar and NumVar

    Posted 01/28/15 04:06 AM

    Can you at least show us the stracktrace from your exception? And maybe also the statement that triggers the exception?


    #DecisionOptimization
    #MathematicalProgramming-General


  • 5.  Re: Scal prod with both IntVar and NumVar

    Posted 02/10/15 06:11 PM

    Originally posted by: davidoff


    On your example, if you simply switch array[0] and array[1] , putting the IloIntVar as the first element of the array, you will get an exception :

    java.lang.ClassCastException: [Lilog.concert.IloNumVar; incompatible with [Lilog.concert.IloIntVar;

    The reason is that method scalProd will try to conver the array of variables to an array of integer variables. See the implementation of method scalProd (thanks to the decompiler for those who are using IntelliJ version 14):

    public IloLinearNumExpr scalProd(double[] vals, IloNumVar[] vars, int start, int num) throws IloException {
            ilog.concert.cppimpl.IloNumExpr expr;
            if(IloConcertUtils.isIntVarArray(vars)) {
                expr = new ilog.concert.cppimpl.IloNumExpr(concert_wrap.IloScalProd(IloConcertUtils.ToCppIloNumArray(this.getEnvImpl(), vals, start, num), IloConcertUtils.ToCppIloIntVarArray(this.getEnvImpl(), (IloIntVar[])((IloIntVar[])vars), start, num)));
            } else {
                expr = new ilog.concert.cppimpl.IloNumExpr(concert_wrap.IloScalProd(IloConcertUtils.ToCppIloNumArray(this.getEnvImpl(), vals, start, num), IloConcertUtils.ToCppIloNumVarArray(this.getEnvImpl(), vars, start, num)));
            }
    
            return expr;
        }
    
    And method isIntVarArray is implemeted as:
    
        public static boolean isIntVarArray(IloNumVar[] vars) {
            return vars.length > 0 && vars[0] instanceof IloIntVar;
        }
    


     


    #DecisionOptimization
    #MathematicalProgramming-General


  • 6.  Re: Scal prod with both IntVar and NumVar

    Posted 02/11/15 07:33 AM

    Ah, you are using oplall.jar. Are you forced to use that or can you use cplex.jar instead?

    This seems to be indeed a bug in oplall.jar.


    #DecisionOptimization
    #MathematicalProgramming-General