Gurobi / modeling-examples

Gurobi modeling examples
https://gurobi.github.io/modeling-examples/
Apache License 2.0
615 stars 272 forks source link

I have an issue using this model with different data. Can anyone help? #9

Closed matthewc5 closed 2 years ago

matthewc5 commented 2 years ago

Hi everyone! I'm modeling with Gurobi, and I'm trying to model an efficiency analysis using different sets of data. I can't solve the model because it says: "ValueError: not enough values to unpack (expected 3, got 2)". I attach my model, can anyone understand why it doesn't work? Thank you in advance! Matteo

` import pandas as pd from itertools import product

import gurobipy as gp from gurobipy import GRB

def solve_DEA(target, verbose=True):

input-output values for the garages

inattr = ['Cash', 'LEV']
outattr = ['EPS', 'ROA']
dmus,inputs, outputs = gp.multidict({
    'DMU1':[{'Cash':0.2485, 'LEV':0.4688 , 'EPS':4, 'ROA':0.2002 }], 'DMU2':[{'Cash':0.1284, 'LEV':0.6539 , 'EPS':3.70, 'ROA':0.0902 }], 'DMU3':[{'Cash':0.293, 'LEV':0.5644 , 'EPS':4.20, 'ROA':0.1627 }]

})

### Create LP model
model = gp.Model('DEA')

# Decision variables
wout = model.addVars(outattr, name="outputWeight")
win = model.addVars(inattr, name="inputWeight")

# Constraints
ratios = model.addConstrs( ( gp.quicksum(outputs[h][r]*wout[r] for r in outattr ) 
                            - gp.quicksum(inputs[h][i]*win[i] for i in inattr ) 
                            <= 0 for h in dmus ), name='ratios' )

normalization = model.addConstr((gp.quicksum(inputs[target][i]*win[i] for i in inattr ) == 1 ),
                                name='normalization')

# Objective function

model.setObjective( gp.quicksum(outputs[target][r]*wout[r] for r in outattr ), GRB.MAXIMIZE)

# Run optimization engine
if not verbose:
    model.params.OutputFlag = 0
model.optimize()

# Print results
print(f"\nThe efficiency of target DMU {target} is {round(model.objVal,3)}") 

print("__________________________________________________________________")
print(f"The weights for the inputs are:")
for i in inattr:
    print(f"For {i}: {round(win[i].x,3)} ") 

print("__________________________________________________________________")
print(f"The weights for the outputs are")
for r in outattr:
    print(f"For {r} is: {round(wout[r].x,3)} ") 
print("__________________________________________________________________\n\n")  

return model.objVal

dmus = ['DMU1', 'DMU2', 'DMU3']

performance = {} for h in dmus:
performance[h] = solveDEA(h, verbose=False) `

mattmilten commented 2 years ago

Hi Matthew,

Could you please edit your post so the code is correctly formatted?

Thanks, Matthias