Skip to main content

Modelling where objective for a variable depends on other variables




  • Matthias Miltenberger
    Gurobi Staff Gurobi Staff

    Hi Saurabh,

    I am not sure I fully understand the issue. In our Python interface, you can just add those binary variables like this:

    import gurobipy as gp
    from gurobipy import GRB

    I = range(10)
    J = range(15)

    m = gp.Model()
    x = m.addVars(I, J, vtype=GRB.BINARY)

    Then you can access those variables in this way


    for certain values of i and j within the specified ranges of I and J.

    The dependencies between variables are modeled via constraints and do not directly affect or concern the objective function.

    To query all variables of an existing model, you can use this code:

    x = m.getVars()

    I hope that helps.


  • Saurabh Agarwal
    First Comment
    First Question

    Hi Matthias, 

    Thanks for your reply. Apologies that the example is not clear. 

    To clarify the problem - Suppose factories can produce multiple products, each product needs different raw materials. Some factories have some raw materials available some would have to transport. We assume that cost of transportation is constant immaterial of the amount transferred. The objective is to reduce the amount of transportation. 

    So for example if we manufacture related products in the same factory, it will reduce the transportation cost, e.g., let's say there are four products to manufacture - couches, futons, chairs and desks. We observe that couches and futons share a lot of materials which will reduce the raw material transport cost. Similarly chairs and desk should be manufactured together. There are some other constraints regarding factory and load but they are easy to write. 

    I am trying to model this using MILP, where $x_{i,j}$ are binary variables and $i$ represents the product and $j$ represents factory. If $x_{i,j}$ is 1, it indicates product $i$ will be produced in factory $j$. My issue is that I can only calculate the cost function when I have access to all x_{i,j}, i.e., to calculate the cost need to know what other products are scheduled in factory j. 

    I am not sure if I can do something like this in Gurobi. Will be great if you can help, thanks. 



Please sign in to leave a comment.