メインコンテンツへスキップ

Unable to convert argument to an expression - Objective Function Error

回答済み

コメント

6件のコメント

  • 正式なコメント
    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 try Gurobot, our chatbot interface offering instant, expert-level support.
  • Jaromił Najman
    • Gurobi Staff

    Could you please provide a minimal reproducible example showing this issue. It would be best if you hard code a small example with your excel data which results in the error you describe.

    0
  • Dorthe Alida Slotvik
    • Gurobi-versary
    • First Comment
    • First Question

    The code runs when I hard code an example from the excel-data. So it seems like there is a problem in the definition of sets and parameters imported from the excel file. 

     

    The hard code:

    import pandas as pd
    import gurobipy as gp
    from gurobipy import *
    import numpy as np
    from gurobipy import GRB
    import matplotlib.pyplot as plt



    ## Sets ##
    Fuels = [0, 1, 2, 3]
    Systems = [0, 1, 2]

    ## Parameters ##
    C_NB = np.array([30, 37, 45]) #C_NB[s], data from systems
    C_fuel = np.array([30, 37, 45, 47]) #C_fuel[f], data from fuels
    C_LO = np.array([0, 0.4, 2.7]) #C_LO[s], data from systems
    B = 0.17
    ## Creating the model ##
    m = gp.Model('Model Name')

    ## Decision variables ##
    x = m.addVars(Fuels, vtype=GRB.BINARY, name='x')
    y = m.addVars(Systems, vtype=GRB.BINARY, name='y')
    ## Constraints ##

    m.addConstr((sum(x[f] for f in Fuels) == 1))

    m.addConstr((sum(y[s] for s in Systems) == 1))
    ## Objective function ##

    obj = m.setObjective(sum(C_NB[s]*y[s] for s in Systems) + sum(C_LO[s]*y[s] for s in Systems) + sum(B*C_fuel[f]*x[f] for f in Fuels), GRB.MINIMIZE)

    ## Solve Model ##
    m.optimize()
    m.write("solution.sol")

    obj = m.getObjective

    def printSolution():
    if m.status == GRB.OPTIMAL:
          print('\nTotal Cost: %g' % m.ObjVal)
    else:
            print('No solution')

    printSolution()
    0
  • Dorthe Alida Slotvik
    • Gurobi-versary
    • First Comment
    • First Question

    The code importing data from excel:

     

    import pandas as pd
    import gurobipy as gp
    from gurobipy import *
    import numpy as np
    from gurobipy import GRB
    import matplotlib.pyplot as plt
    dfF = pd.read_excel(r'C:\Users\xxx\Desktop\Progging\opt_input2.xlsx', sheet_name = 'fuels', index_col='Fuel') #place "r" before the path string to address special character, such as '\'
    dfF.index

    dfS = pd.read_excel(r'C:\Users\xxx\Desktop\Progging\opt_input2.xlsx', sheet_name = 'systems', index_col='Power System') #index_col=0?
    dfS.index

    ## Sets ##

    Fuels = [f for f in range(dfF.shape[0])]

    Systems = [s for s in range(dfS.shape[0])]
    ## Parameters ##

    C_NB1 = dfS[['Newbuild Cost']] # [mUSD]
    C_NB2 = C_NB1.values.tolist()
    C_NB = np.asarray(C_NB2)

    C_fuel1 = dfF[['Fuel Cost']] # [USD/MWh]
    C_fuel2 = C_fuel1.values.tolist()
    C_fuel = np.asarray(C_fuel2)

    C_LO1 = dfS[['Lost Opportunity Cost']] # Lost opportunity cost per 10 years[mUSD]
    C_LO2 = C_LO1.values.tolist()
    C_LO = np.asarray(C_LO2) 

    B = 0.17
    ## Creating the model ##
    m = gp.Model('Model Name')

    ## Creating decision variables ##
    x = m.addVars(Fuels, vtype=GRB.BINARY, name='x')
    y = m.addVars(Systems, vtype=GRB.BINARY, name='y')

    ## Constraints ##
    m.addConstr((sum(x[f] for f in Fuels) == 1))
    m.addConstr((sum(y[s] for s in Systems) == 1))
    ## Objective function ##

    obj = m.setObjective(sum(C_NB[s]*y[s] for s in Systems) + sum(C_LO[s]*y[s] for s in Systems) + sum(B*C_fuel[f]*x[f] for f in Fuels), GRB.MINIMIZE)

    ## Solve Model ##

    m.optimize()
    m.write("solution.sol")

    obj = m.getObjective


    def printSolution():
      if m.status == GRB.OPTIMAL:
          print('\nTotal Cost: %g' % m.ObjVal)
      else:
            print('No solution')
       
    printSolution()
     
    0
  • Jaromił Najman
    • Gurobi Staff

    You could check whether the lists you hard coded vs lists you construct from your Excel files have the same shape. In particular, check the data structures of 

    Fuels, Systems, C_NB, C_fuel, C_LO
    0
  • Dorthe Alida Slotvik
    • Gurobi-versary
    • First Comment
    • First Question

    Thank you for the help! I had added an extra [] in the definition of the parameters, which gave additional square brackets in the lists. 

    0

投稿コメントは受け付けていません。