Skip to main content

Code not working

Awaiting user input

Comments

8 comments

  • Official comment
    Simranjit Kaur
    Gurobi Staff 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?.
  • Alison Cozad
    Gurobi Staff Gurobi Staff

    Hi Rithika, it's great to hear you are able to print the values from Excel.  Now you need to define the variables you are looping over later in the code.

    # Input excel files with arcs data(sheet 1) and commodities data (sheet2)
    # Generate lists to store 'objects'

    Arcs = []
    Nodes = [] # I assume you mean Nodes
    Commodities = []

    [...]
    """ You need to give values to the variables Arcs, Commodities, and Nodes."""
    """ When you use them in the loops below they have a length of 0, """
    """ so the loop never populates anything in the model """
    [...]

    for m in range(len(Arcs)): # Arcs has length of 0
    for k in range(len(Commodities)): # Commodities has a length of 0
    [...]
    Capacity = {}
    for m in range(len(Arcs)): # Arcs has length of 0
    [...]
    # conservation constraint
    Continuity = {}
    for k in range(len(Commodities)): # Commodities has a length of 0
    for j in range(len(Nodes)): # Nodes has a length of 0
    [...]

     

    Also, please see Can't get the results – Gurobi Support Portal for the remainder of this conversation.

     

     

    0
  • Rithika Jaiswal
    Gurobi-versary
    First Question
    Conversationalist

    Hello Alison, thank you for helping me out, I'm kind of confused by 

    You need to give values to the variables Arcs, Commodities, and Nodes."""

    cause we have the values from the sheet. Could you please explain??

    0
  • Alison Cozad
    Gurobi Staff Gurobi Staff

    Hi Rithika,

    Toward the top of your code, you create empty lists for these variables.  For example, \(\texttt{Arcs=[]}\).  At this point, there are no Arcs in your list.  Later, you loop over \(\texttt{range(len(Arcs))}\).  However, \(\texttt{len(Arcs) = 0}\) because \(\texttt{Arcs}\) is still an empty list.  At some point between creating the empty \(\texttt{Arcs}\) and using it to define your loop, you should populate \(\texttt{Arcs}\).  And you are right, you have the values from the sheet.  Now, you need to set \(\texttt{Arcs}\) equal to those values. 

    0
  • Rithika Jaiswal
    Gurobi-versary
    First Question
    Conversationalist

    Hi Alison, thank you. I'll work on it and also I have another question, if you can help: 

    In our objective function we are not able to get other commodities, it's printing only Instagram. Not sure where the problem is

     

    0
  • Alison Cozad
    Gurobi Staff Gurobi Staff

     The first post looks like you are using are defining the coefficient when you are adding the variables.  Is this still the case? Can you show me how you are defining your objective in your latest code? 

    0
  • Rithika Jaiswal
    Gurobi-versary
    First Question
    Conversationalist

    This is the objective function: 

       
       
       
       
       
       
       
       
       
       
    0
  • Alison Cozad
    Gurobi Staff Gurobi Staff

    Thank you for providing this. When you call \(\texttt{m.addVars(..., obj=cost,...)}\) you are setting the coefficients in the objective function based on the values in \(\texttt{cost}\).  A variable appears in your objective function if it has a non-zero coefficient. The variable does not appear in the objective if the coefficient is zero.

    Is it possible that the variables with the missing commodities all have an objective function coefficient of zero?  To check this, 

    1. Find a variable that you would expect to appear in the objective function.  Specifically, this should be a variable you would expect to see with an objective coefficient that is non-zero.
    2. Check to see if the objective function coefficient is non-zero.  To do this, look at the variable.Obj attribute of this variable.  For examples on how to query variable attributes see Python Attribute Examples.

    If \(\texttt{variable.Obj}\) is zero for a variable you would expect to see in the objective, this is why it is missing.  In this case, I recommend checking the corresponding value in your \(\texttt{cost}\) variable.  You have used this to define your objective function coefficient when you used model.addVars().  If the corresponding value in \(\texttt{cost}\) is also zero, then I would not expect it to appear in the objective.

    0

Post is closed for comments.