Decision Optimization

 View Only
  • 1.  Problem with addTerm in IloLinearNumExpr

    Posted Mon July 22, 2019 12:12 PM

    Originally posted by: brown_rice


    Hi,

    I am trying to add a term [alpha*(1- x -y) ] into linear expression, but I receive an error. What could I be doing wrong? or is there a better way to state this? 

     

    IloLinearNumExpr expr = cplex.linearNumExpr();

    expr.addTerm(myArray[i], cplex.diff(1, cplex.sum(firstVar[i],secVar[i])));

     

    The method addTerm(double, IloNumVar) in the type IloLinearNumExpr is not applicable for the arguments (double, IloNumExpr)


    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: Problem with addTerm in IloLinearNumExpr

    Posted Mon July 22, 2019 07:46 PM

    You just need an explicit cast of the second argument:

    expr.addTerm(myArray[i], (IloLinearNumExpr) cplex.diff(1, cplex.sum(firstVar[i],secVar[i])));
    

    Because sum() and diff() can be applied to certain nonlinear expressions (such as quadratics), their argument and return types are the more general IloNumExpr. The addTerm() method needs a linear expression. Your expression is linear, so you just need to cast it as such.


    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: Problem with addTerm in IloLinearNumExpr

    Posted Tue July 23, 2019 10:14 AM

    Originally posted by: brown_rice


    Thanks for the clear explanation but I still receive the same error.

    "The method addTerm(double, IloNumVar) in the type IloLinearNumExpr is not applicable for the arguments (double, IloLinearNumExpr)"
    

    I also tried; 

    expr.addTerm(myArray[i], (IloLinearNumExpr) cplex.diff(1, (IloLinearNumExpr) cplex.sum(firstVar[i],secVar[i])));
    

    but did not work. Just for now, I am going with

    expr.setConstant(Arrays.stream(myArray).sum());
    for(){
        expr.addTerm(-myArray[i],firstVar[i]);
        expr.addTerm(-myArray[i],secVar[i]);
    }
    

    and it works.


    #CPLEXOptimizers
    #DecisionOptimization


  • 4.  Re: Problem with addTerm in IloLinearNumExpr

    Posted Tue July 23, 2019 03:58 PM

    Sorry, I missed a couple of things in your original question. First, I saw "addTerm()" but thought "add()". As the name implies, "addTerm()" wants a single term, whereas "add()" takes as its argument an expression. The more subtle thing I missed is that you cannot cast sum() or diff() to IloLinearNumExpr. I'm not sure why not, and I thought I had done so in the past, but apparently not.

     


    #CPLEXOptimizers
    #DecisionOptimization


  • 5.  Re: Problem with addTerm in IloLinearNumExpr

    Posted Wed July 24, 2019 08:36 AM

    Originally posted by: brown_rice


    In the original question, I was still using addTerm in a for loop, but just did not show that (for some reason, I thought it'd be redundant). The important part is that I did not know this "you cannot cast sum() or diff() to IloLinearNumExpr". I guess, defining the function component by component is the way to go in this case. Thanks for the help. 


    #CPLEXOptimizers
    #DecisionOptimization