my code doesnt work well
AnsweredI want to solve this problem below ( which is BCC model )

This is the code that I wrote
def solve_DEA(target, verbose=True):
df_main = pd.read_csv('./df_31_1_outlier_drops_with_SAS.csv')
inattr = df_main.columns[1:5]
outattr = df_main.columns[5:]
dmus = [k for k in range(66)]
inputs = df_main.iloc[:,1:5].to_dict('index')
outputs = df_main.iloc[:,5:].to_dict('index')
u0 = {}
### Create LP model
model = gp.Model('VRS_model')
# Decision variables
wout = model.addVars(outattr, name="outputWeight")
win = model.addVars(inattr, name="inputWeight")
u_vrs =model.addVars(dmus, name ="u_vrs")
model.update()
# Constraints
ratios = model.addConstrs( ( gp.quicksum(outputs[h][r]*wout[r] - u_vrs[target] 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] - u_vrs[target] for r in outattr ), GRB.MAXIMIZE)
# Run optimization engine
if not verbose:
model.params.OutputFlag = 0
model.params.NonConvex = 2
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)} ")
return model.objVal
But what I got from output is same as this problem below (which is CCR model)

def solve_DEA(target, verbose=True):
df_main = pd.read_csv('./df_31_1_outlier_drops_with_SAS.csv')
inattr = df_main.columns[1:5]
outattr = df_main.columns[5:]
dmus = [k for k in range(66)]
inputs = df_main.iloc[:,1:5].to_dict('index')
outputs = df_main.iloc[:,5:].to_dict('index')
### 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)} ")
i dont know what is wrong with my code ( or equation)
these are data of my lp model
| NAME | x_1 | x_2 | x_3 | x_4 | x_5 | x_6 | y_1 |
| 18 | 0.337957 | 0.026104 | 0.03177 | 0.166129 | 0.836921 | 0.025097 | 0.000467 |
| 19 | 1 | 0.799197 | 0.969743 | 1 | 0.836921 | 0.025097 | 8.23E-05 |
| 20 | 0 | 0.236948 | 0.122542 | 0.225806 | 0.836921 | 0.025097 | 0.000151 |
| 21 | 0 | 0.056225 | 0.122542 | 0.090323 | 0.836921 | 0.025097 | 4.71E-05 |
| 44 | 0.631641 | 0.160643 | 0.077156 | 0.072258 | 0.327234 | 0.443382 | 0 |
| 51 | 0.347107 | 0.036145 | 0.092284 | 0.096774 | 0.347578 | 0.311622 | 0.000284 |
| 6 | 0.209563 | 0.196787 | 0.077156 | 0.012903 | 0.169214 | 0.862265 | 0.005062 |
| 7 | 0.727568 | 0.046185 | 0.228442 | 0.085484 | 0.169214 | 0.862265 | 0.000513 |
| 16 | 0.061246 | 0 | 0.122542 | 0.413497 | 0.63972 | 0.123992 | 0.000146 |
| 22 | 0.020661 | 0.37751 | 0.213313 | 0.167742 | 0.823897 | 0.046908 | 2.54E-05 |
| 23 | 0.01771 | 0.196787 | 0.107413 | 0.125161 | 0.823897 | 0.046908 | 0.000105 |
| 24 | 0.005903 | 0.518072 | 0.288956 | 0.145161 | 0.823897 | 0.046908 | 2.11E-06 |
| 25 | 0.01771 | 0.313253 | 0.742814 | 0.212903 | 0.809042 | 0.042725 | 2.28E-05 |
| 26 | 0.27745 | 0.287149 | 0.213313 | 0.274194 | 0.809042 | 0.042725 | 4.05E-06 |
| 53 | 0.596222 | 0.698795 | 0.016641 | 0.2 | 0.373735 | 0.371377 | 0.001455 |
| 8 | 0.324675 | 0.337349 | 0.319213 | 0.383226 | 1 | 0.072005 | 0.462689 |
| 27 | 0.079693 | 0.206827 | 0.183056 | 0.004032 | 0.806243 | 0.01225 | 4.54E-05 |
| 28 | 0.042798 | 0.34739 | 0.273828 | 0.009623 | 0.806243 | 0.01225 | 4.50E-05 |
| 29 | 0.070838 | 0.34739 | 0.379728 | 0.126842 | 0.806243 | 0.01225 | 2.42E-05 |
| 30 | 0.005903 | 0.096386 | 0.100227 | 0.16129 | 0.806243 | 0.01225 | 0.000133 |
| 31 | 0.566706 | 1 | 0.440242 | 0.677419 | 0.806243 | 0.01225 | 0.00011 |
| 40 | 0.035419 | 0.538153 | 0.364599 | 0.064516 | 0.543272 | 0.279952 | 0.000129 |
| 0 | 0.024351 | 0.299197 | 0.167731 | 0.141935 | 0.329279 | 0.429638 | 5.63E-05 |
| 1 | 0.024351 | 0.337349 | 0.183056 | 0.080645 | 0.329279 | 0.429638 | 5.02E-05 |
| 2 | 0.005903 | 0.427711 | 0.030257 | 0 | 0.329279 | 0.429638 | 0.000225 |
| 3 | 0.011806 | 0.598394 | 0.288956 | 0.235484 | 0.329279 | 0.429638 | 6.78E-05 |
| 4 | 0.024351 | 0.879518 | 0.515885 | 0.387097 | 0.329279 | 0.429638 | 6.85E-05 |
| 9 | 0.289256 | 0.267068 | 0.440242 | 0.110403 | 0.249839 | 0.390798 | 0.000104 |
| 11 | 0.815821 | 0.457831 | 0.001513 | 0.274194 | 0.643488 | 0.239618 | 0.000267 |
| 55 | 0.036895 | 0.297189 | 0.288956 | 0.096774 | 0 | 1 | 2.03E-05 |
| 57 | 0.636806 | 0.096386 | 0.13767 | 0.145161 | 0.788159 | 0.072304 | 0.000121 |
| 5 | 0.005903 | 0.146586 | 0.1059 | 0.032258 | 0.266093 | 0.63161 | 9.38E-05 |
| 33 | 0.047963 | 0.457831 | 0.203253 | 0.548387 | 0.710549 | 0.02928 | 4.77E-06 |
| 34 | 0.01771 | 0.698795 | 1 | 0.422581 | 0.710549 | 0.02928 | 3.59E-05 |
| 35 | 0.153926 | 0.066265 | 0.145234 | 0.032258 | 0.710549 | 0.02928 | 1.96E-06 |
| 36 | 0.116588 | 0.136546 | 0.13767 | 0.548387 | 0.710549 | 0.02928 | 8.87E-06 |
| 37 | 0.079693 | 0.497992 | 0.13767 | 0.145161 | 0.710549 | 0.02928 | 1.83E-05 |
| 41 | 0.631641 | 0.497992 | 0.288956 | 0.267742 | 0.419591 | 0.401554 | 0.000135 |
| 42 | 0.005903 | 0.196787 | 0.591528 | 0.122903 | 0.419591 | 0.401554 | 0.000219 |
| 58 | 0.024351 | 0.056225 | 0.258699 | 0.513871 | 0.493434 | 0.272483 | 0.000319 |
| 12 | 0.665289 | 0.317269 | 0.379728 | 0.306452 | 0.623466 | 0.208844 | 0.015387 |
| 54 | 0.144628 | 0.166667 | 0.34947 | 0.195161 | 0.283854 | 0.373768 | 0.000281 |
| 38 | 0.079693 | 0.648594 | 0.440242 | 0.315435 | 0.795264 | 0 | 3.00E-05 |
| 39 | 0.318772 | 0.548193 | 0.288956 | 0.29129 | 0.795264 | 0 | 4.38E-05 |
| 13 | 0.061246 | 0.046185 | 0 | 0 | 0.55479 | 0.226471 | 6.07E-05 |
| 47 | 0.011806 | 0.120482 | 0.426626 | 0.096774 | 0.189882 | 0.310128 | 8.45E-05 |
| 59 | 0.301063 | 0.216867 | 0.742814 | 0.630174 | 0.727234 | 0.150881 | 1 |
| 60 | 0.43595 | 0.146586 | 0.24357 | 0.225806 | 0.567815 | 0.25366 | 3.53E-06 |
| 45 | 0.324675 | 0.156627 | 0.281392 | 0.064516 | 0.33746 | 0.453839 | 2.04E-05 |
| 48 | 0.063164 | 0.899598 | 0.288956 | 0.258065 | 0.251346 | 0.18046 | 0.000145 |
| 43 | 0.005903 | 0.365462 | 0.319213 | 0.387097 | 0.386006 | 0.494771 | 8.00E-05 |
0
-
Official comment
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. -
Please note that your code is not reproducible because a minimal set of data is missing.
It looks like
( gp.quicksum(outputs[h][r]*wout[r] - u_vrs[target] for r in outattr )
should be
( gp.quicksum(outputs[h][r]*wout[r] for r in outattr ) + u_vrs[target]
in your \(\texttt{ratios}\) constraints. The same applies to your \(\texttt{normalization}\) constraint.
It is best to write out the LP file
model.write("myModel.lp")to check whether the constraints look what they should like. This is the easiest way to debug issues like this one.
1 -
i update the post so now you could access the data that i use
and i solve the problem thank you!
0
Post is closed for comments.
Comments
3 comments