Skip to main content

Using a class that returns a double as a GRBVar

Answered

Comments

5 comments

  • Official comment
    Simranjit Kaur
    • Gurobi Staff
    This post is more than three years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum. Or why not try our AI Gurobot?.
  • Eli Towle
    • Gurobi Staff

    Hi,

    How is this risk calculation performed? It would be helpful to see a short, self-contained code snippet.

    If the risk calculation depends on the variables of the MIP model, I don't see why it is returned as a double instead of something like a GRBLinExpr or GRBQuadExpr object. You should give Gurobi the explicit linear/quadratic expression that calculates the risk. Once you have that, you could always create an auxiliary variable, add a constraint to set it equal to the appropriate risk expression, then use the addTerm() method to add that variable to an existing linear expression object.

    Note that GRBLinExpr.addTerm() is specifically for adding a single variable term to the linear expression. You can use GRBLinExpr.addConstant() to add a constant value (like a double) to the expression. But I suspect the risk calculation needs to be changed somehow to not return a double.

    Eli

    0
  • S.J.R. van Meetelen
    • Gurobi-versary
    • First Comment
    • First Question

    Hi, 

    First I want to thank you for your very fast comment! The class called NAVcalc.java is the class that returns the double that returns the NAV-risk calculation shown below. The risk calculation is indeed dependent on a variable of the MIP model. The int stock that it uses to calculate the NAV-risk is the amount of stock that the model assigns to a certain warehouse.

    The objective function of my MIP is shown below. I want to multiply my riskCost (not a decision variable) by the 'value' returned by the NAVcalc calculation. 

    0
  • S.J.R. van Meetelen
    • Gurobi-versary
    • First Comment
    • First Question

    Now i changed the calculation class so that it returns a GRBLinExpr (as can be seen in the picture below). However the int stock is 0 all the time. So i think i don't give it to this class in the right way.  

    I give the int stock by (int) solution.getohInvAfter(i, j). ohInvAfter is a GRBVar and if i just fill in ohInvAfter[i][j] then i get the error message cannot cast from GRBVar to int. But if i change the int stock in my NAVcalc class in GRBVar stock i get other errors. Is there a way for me to give the GRBVar ohInvAfter as an integer to NAVcalc class?

    0
  • Eli Towle
    • Gurobi Staff

    Hi,

    You shouldn't cast the \( \texttt{GRBVar} \) object to an \( \texttt{int} \), since you want to build your objective expression using the variable objects that you've added to the model.

    But I think the bigger problem is that your objective should be explicitly given in a form like \( \texttt{x[1] + 5x[2] - 3x[3]} \) (you could also include quadratic terms like \( \texttt{x[1]*x[2]} \)). This means that you can't use arbitrary Java math functions like \( \texttt{Math.max()} \); instead, functions like this must be explicitly formulated using model constraints.

    For example, if you want to model \( \texttt{max}(0, x) \) for a variable \( x \), you can introduce a new variable \( y \) to be equal to this expression, then add the appropriate constraint to the model with the GRBModel.addGenConstrMax() method. Depending on the objective/constraints, you might be able to model this constraint by instead using the two linear constraints \( y \geq 0 \) and \( y \geq x \). Then, whenever you want to use \( \texttt{max}(0, x) \) in your model, you use the new \( y \) variable instead.

    So, at the very least, it looks like you will have to break up the objective expression into a few steps by adding new constraints and auxiliary variables to the model. You should also make sure that the intermediate functions that accept \( \texttt{GRBVar} \) objects return/use linear/quadratic expressions (not doubles) for use in the constraints/objective.

    I hope this helps!

    Eli

    0

Post is closed for comments.