<jupyter_start><jupyter_text>Introduction<jupyter_code>from IPython.display import HTML

HTML('''<script>
code_show=true; 
function code_toggle() {
 if (code_show){
 $('div.input').hide();
 } else {
 $('div.input').show();
 }
 code_show = !code_show
} 
$( document ).ready(code_toggle);
</script>
<form action="javascript:code_toggle()"><input type="submit" value="Click here to toggle on/off the raw code."></form>''')<jupyter_output><empty_output><jupyter_text>Analyst:<NAME>                                                                     Date: 2019/11/10The Westvaco Company is facing distribution problem. They want to minimize their cost for truckloads to carriers that meet the necessary requirements. There are 6 carriers who are willing to provide their services: ABCT, IRST, LAST, MRST, NEST, PSST. Each of them have a different cost per mile, the stops to the same destination are the same, but the Stop-off charge is also part of the company cost. For each destination, the company should choose number carriers that equal to the total trips required (same carrier can be chosen several times). On the other hand, each carrier also has a minimum charge per truckload. If the total cost for one trip belows this minimum charge amount, the carrier would still charge the company the minimum charge they asked. This case is to help Westvoca Company to find a most optimize way to lower their transportation cost.  Model<jupyter_code>from gurobipy import*
m = Model("Minimum Cost")
Destination = ['Atlanta','Everett','Ephrata','Riverview','Carson','Chamblee','Roseville','Hanover','Sparks','Parsippany','Effingham','Kearny','MinCharge','StopOffCharge']
State = ['GA','MA','PA','MI','CA','GA','MN','PA','NV','NJ','IL','NJ','','']
trips = [4,1,3,5,1,1,1,1,2,1,5,7,'','']
stops = [0,3,0,0,2,0,3,0,0,1,0,0,'','']
miles = [612, 612, 190, 383, 3063, 429, 600, 136, 2439, 355, 570, 324,'','']
minCharge = [350,400,350,300,350,300]
stopoffCharge = [50,75,50,35,50,50]
ABCT = ['*','*','*',0.79,'*','*',1.24,'*','*','*',0.87,'*',350,50]
IRST = [0.88,1.18,3.42,1.01,0.8,1.23,1.13,4.78,1.45,1.62,0.87,2.01,400,75]
LAST = [1.15,1.27,1.73,1.25,0.87,1.61,1.89,2.23,'*',1.36,1.25,1.54,350,50]
MRST = [0.87,1.39,1.71,0.96,'*',1.22,1.32,2.39,1.2,1.39,0.87,1.53,300,35]
NEST = [0.95,1.35,1.82,0.95,1.00,1.33,1.41,2.26,'*',1.03,0.9,1.28,350,50]
PSST = [1.05,1.28,2,1.11,'*',1.47,1.41,2.57,'*',1.76,1.31,1.95,300,50]<jupyter_output><empty_output><jupyter_text>Current Distribution Data for Westvaco Case Study<jupyter_code>import pandas as pd
ak = pd.DataFrame.from_dict({
        'Destination': Destination,
        'State': State,
        'Trips': trips,
        'Stops': stops,
        'Miles': miles,
        'ABCT': ABCT,
        'IRST': IRST,
        'LAST': LAST,
        'MRST': MRST,
        'NEST': NEST,
        'PSST': PSST
     })
ak
zlb = [ABCT[0:12],IRST[0:12],LAST[0:12],MRST[0:12],NEST[0:12],PSST[0:12]]
ub = 9999999
cost1 = []
for i in range(len(zlb)):
    cost=[]
    for j in range(len(zlb[i])):
        if zlb[i][j] == "*":
            cost.append(ub)
        elif zlb[i][j]*miles[j]+stops[j]*stopoffCharge[i] > minCharge[i]:
            cost.append(zlb[i][j]*miles[j]+stops[j]*stopoffCharge[i])
        else:    
            cost.append(minCharge[i])
    cost1.append(cost)<jupyter_output><empty_output><jupyter_text>Cost for each Carrier to different destinations<jupyter_code>ap = pd.DataFrame.from_dict({
        'Destination': Destination[0:12],
        'ABCT': cost1[0],
        'IRST': cost1[1],
        'LAST': cost1[2],
        'MRST': cost1[3],
        'NEST': cost1[4],
        'PSST': cost1[5]
     })
ap
m.remove(m.getVars())
m.remove(m.getConstrs())<jupyter_output><empty_output><jupyter_text>Decision Variables There are 72 decision variables in total: 12 cities and 6 carriers.<jupyter_code>#Decision Variables

var = [[m.addVar(vtype=GRB.INTEGER, name="Carrier"+str(i+1)+"_"+"Dest"+str(j+1)) for j in range(len(zlb[i]))] for i in range(len(zlb))]
m.update()<jupyter_output><empty_output><jupyter_text>Constraints The sum of truckloads for each carrier should less than the available pulls for each carrier, but greater than the commitment. The total truckloads for each destination should equal to the total trips needed for each destination.<jupyter_code>#Constraints

Available_Pulls = [4,8,7,7,3,4]
Commitment = [1,7,6,0,0,4]
for i in range(len(var)):
    m.addConstr(sum(var[i]),GRB.LESS_EQUAL,Available_Pulls[i])
    m.addConstr(sum(var[i]),GRB.GREATER_EQUAL,Commitment[i])
trips1 = [4,1,3,5,1,1,1,1,2,1,5,7]
for j in range(12):
    m.addConstr(var[0][j]+var[1][j]+var[2][j]+var[3][j]+var[4][j]+var[5][j],GRB.EQUAL,trips1[j])
m.update()<jupyter_output><empty_output><jupyter_text>Objective The objective function is minimize of the total cost. Total cost is the sumproduct of decision variables and Cost for each Carrier to different destinationstable for each carrier.<jupyter_code>#Objective

k = []
for i in range(len(var)):
    for j in range(len(var[i])):
        k.append(cost1[i][j]*var[i][j])
m.setObjective(sum(k),GRB.MINIMIZE)
m.update()<jupyter_output><empty_output><jupyter_text>Optimization<jupyter_code># Optimization

m.optimize()<jupyter_output>Optimize a model with 24 rows, 72 columns and 216 nonzeros
Variable types: 0 continuous, 72 integer (0 binary)
Coefficient statistics:
  Matrix range     [1e+00, 1e+00]
  Objective range  [3e+02, 1e+07]
  Bounds range     [0e+00, 0e+00]
  RHS range        [1e+00, 8e+00]
Found heuristic solution: objective 5.001613e+07
Presolve removed 3 rows and 0 columns
Presolve time: 0.00s
Presolved: 21 rows, 72 columns, 180 nonzeros
Variable types: 0 continuous, 72 integer (36 binary)

Root relaxation: objective 2.239438e+04, 30 iterations, 0.00 seconds

    Nodes    |    Current Node    |     Objective Bounds      |     Work
 Expl Unexpl |  Obj  Depth IntInf | Incumbent    BestBd   Gap | It/Node Time

*    0     0               0    22394.380000 22394.3800  0.00%     -    0s

Explored 0 nodes (30 simplex iterations) in 0.01 seconds
Thread count was 12 (of 12 available processors)

Solution count 2: 22394.4 5.00161e+07 

Optimal solution found (tolerance 1.00e-04)
Best objective 2.239438000000e+04,[...]<jupyter_text>Result<jupyter_code>a = []
for v in m.getVars():
    if '%g' % (v.x) == -0:
        a.append(0)
    else:
        a.append(int('%g' % (v.x)))
        
b = [('$%g' % m.objVal),'','','','','','','','','','','']
        
ad = pd.DataFrame.from_dict({
        'Destination': Destination[0:12],
        'ABCT': a[0:12],
        'IRST': a[12:24],
        'LAST': a[24:36],
        'MRST': a[36:48],
        'NEST': a[48:60],
        'PSST': a[60:72],
        'MinCost': b
     })
ad
print('The optimal result in this case will be:','$%g' % m.objVal)<jupyter_output>The optimal result in this case will be: $22394.4
{'fraction_non_alphanumeric': 0.1720902961008502, 'fraction_numerical': 0.08282028730577544, 'mean_word_length': 4.087994034302759, 'pattern_counts': {'":': 1, '<': 41, '<?xml version=': 0, '>': 42, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '444880', 'license': 'CC0-1.0', 'n_tokens_mistral': 2877, 'n_tokens_neox': 2574, 'n_words': 602, 'path': 'code/Zhang-westvaco.ipynb', 'repo_name': 'zhangluoben/zhangluoben.github.io', 'size': '6828'}