|
| 1 | +# Copyright Wen-Chih Chen |
| 2 | +# the code is for computing basic DEA models based on orientation (input or output) |
| 3 | +# and returns-to-scale (crs or vrs) |
| 4 | + |
| 5 | +using JuMP |
| 6 | +using Gurobi # Gurobi is used as the LP solver |
| 7 | +using FrontierEfficiencyAnalysis # for large scale problems and/or with size-limited solver |
| 8 | + |
| 9 | +function basicDEAmodel() |
| 10 | + #---------------------------------------- |
| 11 | + # data section |
| 12 | + data = readcsv("example.csv") # input User's (.csv) data path |
| 13 | + # each row is for a DMU; each column is an input or output |
| 14 | + # in the example the first two columns are the inputs while the others are the outputs |
| 15 | + inputs = data[:, 1:2] |
| 16 | + outputs = data[:, 3:end] |
| 17 | + |
| 18 | + # --------------------------------------- |
| 19 | + # to determine the efficiencies of all DMUs |
| 20 | + results = zeros(size(inputs)[1]) |
| 21 | + getAllEfficiencies!(results, inputs, outputs) # get all DMU's efficiencies of input-oriented crs model |
| 22 | + ## or use the following to set the options of orientation, returns-to-scale and method |
| 23 | + # getAllEfficiencies!(results, inputs, outputs, orientation = "input", rts = "crs", method = 0) |
| 24 | + ## orientation = "input" (default) or "output" |
| 25 | + ## rts = "crs" (default) or "vrs" |
| 26 | + ## method = 0 (default) or 1 (by FrontierEfficiencyAnalysis for large scale problems and/or with size-limited solver) |
| 27 | + ## e.g. for output-oriented vsr model and solve by FrontierEfficiencyAnalysis do: |
| 28 | + # getAllEfficiencies!(results, inputs, outputs, orientation = "output", rts = "vrs", method = 1) |
| 29 | + println(results) |
| 30 | + |
| 31 | + # --------------------------------------- |
| 32 | + # to determine the efficiency of DMU k |
| 33 | + k = 2 |
| 34 | + score = getEfficiency(inputs, outputs, k) # get DMU k's efficiency of input-oriented crs model |
| 35 | + ## or use the following to set the options of orientation, returns-to-scale and method |
| 36 | + # score = getEfficiency(inputs, outputs, k, orientation = "input", rts = "crs", method = 0) |
| 37 | + ## orientation = "input" (default) or "output" |
| 38 | + ## rts = "crs" (default) or "vrs" |
| 39 | + ## method = 0 (default) or 1 (by FrontierEfficiencyAnalysis for large scale problems and/or with size-limited solver) |
| 40 | + ## e.g. for output-oriented vsr model and solve by FrontierEfficiencyAnalysis do: |
| 41 | + # score = getEfficiency(inputs, outputs, k, orientation = "output", rts = "vrs", method = 1) |
| 42 | + println(score) |
| 43 | + |
| 44 | +end |
| 45 | + |
| 46 | +# compute DEA efficiencies for all DMUs in the data set and store results in scores |
| 47 | +function getAllEfficiencies!(scores, inputData, outputData; orientation = "input", rts = "crs", method = 0) |
| 48 | + # parameters |
| 49 | + # scores: efficiency results |
| 50 | + # inputData: input data matrix (DMU size) x (input size) |
| 51 | + # outputData: output data matrix (DMU size) x (output size) |
| 52 | + # orientation: input- or output- oriented model (value= "input" or "output") |
| 53 | + # rts: returns to scale setting (value = "crs" or "vrs") |
| 54 | + # # method: how to solve LP (value = 0 (default) or 1 (by FrontierEfficiencyAnalysis.jl)) |
| 55 | + |
| 56 | + if !(orientation == "input" || orientation == "output") |
| 57 | + error("orientation not supported") |
| 58 | + end |
| 59 | + |
| 60 | + if !(rts == "crs" || rts == "vrs") |
| 61 | + error("returns to scale not supported") |
| 62 | + end |
| 63 | + |
| 64 | + for t = 1:size(inputData)[1] |
| 65 | + scores[t] = getEfficiency(inputData, outputData, t, orientation = orientation, rts = rts, method = method) |
| 66 | + end |
| 67 | +end |
| 68 | + |
| 69 | +# compute DEA efficiency for DMU k |
| 70 | +function getEfficiency(inputs, outputs, k; orientation = "input", rts = "crs", method = 0) |
| 71 | + # parameters |
| 72 | + # inputs: input data matrix (DMU size) x (input size) |
| 73 | + # outputs: output data matrix (DMU size) x (output size) |
| 74 | + # k: index of DMU to be evaluated |
| 75 | + # orientation: input- or output- oriented model (value= "input" or "output") |
| 76 | + # rts: returns to scale setting (value = "crs" or "vrs") |
| 77 | + # method: how to solve LP (value = 0 (default) or 1 (by FrontierEfficiencyAnalysis.jl)) |
| 78 | + scale, inputNo = size(inputs) |
| 79 | + outputNo = size(outputs)[2] |
| 80 | + |
| 81 | + model = Model(solver = GurobiSolver(OutputFlag=0)) # Gurobi is used as the LP solver here. Users can choose their favorite solver. |
| 82 | + @variable(model, Lambda[1:scale] >= 0) |
| 83 | + @variable(model, Theta) |
| 84 | + |
| 85 | + if orientation == "input" # input-oriented model |
| 86 | + @objective(model, Min, Theta) |
| 87 | + @constraint(model, inputCon[i=1:inputNo], sum(Lambda[r]*inputs[r,i] for r = 1:scale) <= Theta*inputs[k,i]) |
| 88 | + @constraint(model, outputCon[j=1:outputNo], sum(Lambda[r]*outputs[r,j] for r = 1:scale) >= outputs[k,j]) |
| 89 | + elseif orientation == "output" # output-oriented model |
| 90 | + @objective(model, Max, Theta) |
| 91 | + @constraint(model, inputCon[i=1:inputNo], sum(Lambda[r]*inputs[r,i] for r = 1:scale) <= inputs[k,i]) |
| 92 | + @constraint(model, outputCon[j=1:outputNo], sum(Lambda[r]*outputs[r,j] for r = 1:scale) >= Theta*outputs[k,j]) |
| 93 | + else |
| 94 | + error("orientation not support") |
| 95 | + end |
| 96 | + |
| 97 | + if rts == "vrs" |
| 98 | + @constraint(model, sum(Lambda[r] for r = 1:scale) == 1) |
| 99 | + elseif rts == "crs" |
| 100 | + else |
| 101 | + error("returns to scale not supported") |
| 102 | + end |
| 103 | + |
| 104 | + if method == 0 # defalut |
| 105 | + solve(model) |
| 106 | + else # using FrontierEfficiencyAnalysis for large scale problem or with size limited solver |
| 107 | + solveDEA(model) |
| 108 | + end |
| 109 | + |
| 110 | + return getvalue(Theta) |
| 111 | +end |
| 112 | + |
| 113 | +basicDEAmodel() |
0 commit comments