<jupyter_start><jupyter_text>Optimization<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from pulp import *<jupyter_output><empty_output><jupyter_text>Case Study 1 - Whiskas cat food is manufactured by Uncle Ben’s. 
- Uncle Ben’s want to produce their cat food products as cheaply as possible 
    - while ensuring they meet the stated nutritional analysis requirements shown on the cans. 
    - Thus they want to vary the quantities of each ingredient used


![alt text](Cost_Ingredient.jpg "Cost of Ingredients")


![alt text](Ingredient_Nutrition.jpg "Nutritional Values of Ingredient")<jupyter_code>prob = LpProblem("The Whiskas Problem",LpMinimize)
x1 = LpVariable("ChickenPercent",0,None,LpInteger)
x2 = LpVariable("BeefPercent",0,None,LpInteger)
x3 = LpVariable("MuttonPercent",0,None,LpInteger)
x4 = LpVariable("RicePercent",0,None,LpInteger)
x5 = LpVariable("WheatPercent",0,None,LpInteger)
x6 = LpVariable("GelPercent",0,None,LpInteger)

prob += 0.013*x1 + 0.008*x2 + 0.010*x3 + 0.002*x4 + 0.005*x5 + 0.001*x6, "Total Cost of Ingredients Per Can"
prob += x1 + x2 + x3 + x4 + x5 + x6 == 100, "PercentagesSum"
prob += 0.100*x1 + 0.200*x2 + 0.150*x3 + 0.000*x4 + 0.040*x5 + 0.000*x6 >= 8.0, "ProteinRequirement"
prob += 0.080*x1 + 0.100*x2 + 0.110*x3 + 0.010*x4 + 0.010*x5 + 0.000*x6 >= 6.0, "FatRequirement"
prob += 0.001*x1 + 0.005*x2 + 0.003*x3 + 0.100*x4 + 0.150*x5 + 0.000*x6 >= 2.0, "FibreRequirement"
prob += 0.002*x1 + 0.005*x2 + 0.007*x3 + 0.002*x4 + 0.008*x5 + 0.000*x6 <= 0.4, "SaltRequirement"
prob.writeLP("WhiskasModel.lp")
prob.solve()
print("Status:",LpStatus[prob.status])
for v in prob.variables():
    print(v.name,"=",v.varValue)
print ("Total Cost of Ingredients per can = ", value(prob.objective))
prob.solutionTime<jupyter_output><empty_output><jupyter_text>End of Problem Case Study2 Consider the following linear programming (LP) problem:
- Suppose there are two products that need to be manufactured: Tables and Chairs
- To manufacture a product, it requires two resources: budget ($) and labor (man-days)
- The resources required to manufacture each product as well as the total available amounts of each resource are given in the table below
- Objective is to maximize the total number of products manufactured

![alt text](CaseStudy_2.jpg "Case Study 2") Formulate Linear Programming Problem

Define Decision Variables
- x1 :  of Tables to be manufactured
- x2 :  of Chairs to be manufactured

Define Constraints
- 8x1 + 4x2 <= 60
- 4x1 + 8x2 <= 48
- x1 > 0
- x2 > 0

Objective Function
- Maximize z=x1+x2<jupyter_code># Create LP Problem
prob = LpProblem("The Tables and Chairs Problem",LpMaximize)
# Create Decision Variables
x1 = LpVariable("Tables",0,None,LpInteger)
x2 = LpVariable("Chairs",0,None,LpInteger)
# Create Constraints
prob += 8*x1 + 4*x2 <= 60, "Budget Constraint"
prob += 4*x1 + 8*x2 <= 48, "Labor Constraint"
# Objective Function
prob += x1 + x2, "Total Tables and Chairs"
prob.writeLP("TablesnChairs.lp")
prob.solve()
print("Status:",LpStatus[prob.status])
for v in prob.variables():
    print(v.name,"=",v.varValue)
print ("Total Products To Be Manufactured = ", value(prob.objective))
prob.solutionTime<jupyter_output><empty_output><jupyter_text>End of Problem Case Study 3 Consider the following data for a linear program, where the objective is to maximize profit by allocating three resources (repair, paint, and labor) to fix two types of cars (Honda and Toyota). This data is given below:

![alt text](CaseStudy_3.jpg "Case Study 2") Formulate Linear Programming Problem

Define Decision Variables
- x1 :  of Honda cars to be fixed
- x2 :  of Toyota cards to be fixed

Define Constraints
- 2x1 + 1x2 <= 10
- 3x1 + 3x2 <= 28
- 2x1 + 4x2 <= 20
- x1 > 0
- x2 > 0

Objective Function
- Maximize z=20x1+30x2<jupyter_code># Create LP Problem
prob = LpProblem("Vehicle Allocation Problem",LpMaximize)
# Create Decision Variables
x1 = LpVariable("Honda Cars",0,None,LpInteger)
x2 = LpVariable("Toyota Cars",0,None,LpInteger)
# Create Constraints
prob += 2*x1 + 1*x2 <= 10, "Repair Constraint"
prob += 3*x1 + 3*x2 <= 20, "Paint Constraint"
prob += 2*x1 + 4*x2 <= 20, "Labor Constraint"
# Objective Function
prob += 20*x1 + 30*x2, "Total Profit By Fixing Honda and Toyota Cars"
prob.writeLP("FixCars.lp")
prob.solve()
print("Status:",LpStatus[prob.status])
for v in prob.variables():
    print(v.name,"=",v.varValue)
print ("Total Products To Be Manufactured = ", value(prob.objective))
prob.solutionTime<jupyter_output><empty_output><jupyter_text>End of Problem - Transportation problem
- Integer Programming
- Machine Learning as Optimization Problem Transportation Problem
- Given a set of supply nodes indexed by the set and demand nodes indexed by the set
- Supply nodes have available supply, and demand nodes require volumne
- Every supply node can ship to every demand node, and the per- unit cost of shipping from supply node i to demand node j

![alt text](CaseStudy_4.jpg "Case Study 2")<jupyter_code># Define Nodes
supply_nodes = dict(s1=15,s2=25)
demand_nodes = dict(d1=15,d2=12,d3=13)
print ('Total Supply Nodes: ',len(supply_nodes))
print ('Total Demand Nodes: ',len(demand_nodes))

# Sum-up Supply Availability and Demand Requirement
total_supply = np.sum(list(supply_nodes.values()))
total_demand = np.sum(list(demand_nodes.values()))
print ('Total Supply Available: ',total_supply)
print ('Total Demand Required: ',total_demand)
# Evaluate Problem Based on Total Supply and Demand
if total_supply == total_demand:
    print ('Balanced Solution')
elif total_supply > total_demand:
    print ('Feasible Solution')
else:
    print ('Infeasible Solution Due to Demand Not Met')
# Define Cost Matrix, Assuming Every Supply Node Can Satisfy Every Demand Node
route_costs = dict(c11=10,c12=7,c13=3,c21=4,c22=9,c23=8)
print ('Total Routes: ',len(route_costs))
# Define Transportation Problem To Minimize Overall Cost
prob = LpProblem("The Transportation Problem",LpMinimize)
# Define Decision Variables, How much to move from Supply Node i to Demand Node j
x11 = LpVariable("1to1",1,None,LpInteger)
x12 = LpVariable("1to2",1,None,LpInteger)
x13 = LpVariable("1to3",1,None,LpInteger)
x21 = LpVariable("2to1",1,None,LpInteger)
x22 = LpVariable("2to2",1,None,LpInteger)
x23 = LpVariable("2to3",1,None,LpInteger)
# Total Cost of Transportation
prob += route_costs['c11']*x11 + route_costs['c12']*x12 + route_costs['c13']*x13 + route_costs['c21']*x21 + route_costs['c22']*x22 + route_costs['c23']*x23, "Total Cost of Transportation"
# Supply and Demand Constraints
prob += x11 + x12 + x13 <= supply_nodes['s1'], "Supply Constraint S1"
prob += x21 + x22 + x23 <= supply_nodes['s2'], "Supply Constraint S2"
prob += x11 + x21 >= demand_nodes['d1'], "Demand Constraint D1"
prob += x12 + x22 >= demand_nodes['d2'], "Demand Constraint D2"
prob += x13 + x23 >= demand_nodes['d3'], "Demand Constraint D3"
# Write Linear Programming Formulation
prob.writeLP("Transportation.lp")
# Solve
prob.solve()
# Is it Optimal
print("Status:",LpStatus[prob.status])
# Variable Values, Parameters
for v in prob.variables():
    print(v.name,"=",v.varValue)
# Objective Value
print ("Total Cost of Transportation = ", value(prob.objective))
prob.constraints
prob.solutionTime<jupyter_output><empty_output><jupyter_text>End of Problem Case Study 4 What is Meant By Mean ? Formulate Linear Programming Problem

Define Decision Variables / Parameter
- x :  Identify that numbers which is closer to all members of data

Define Constraints
- x >

Objective Function
- Maximize z=20x1+30x2<jupyter_code>data = [4.18,1.94,3.72,3.87,6.95,6.86,6.17,1.89,6.86,5.46,2.28,7.01]
# Create LP Problem
prob = LpProblem("Number Closer To Entire Data Elements",LpMinimize)

# Create Decision Variables
x = LpVariable("Average",0,None)
prob.solve()
print("Status:",LpStatus[prob.status])
for v in prob.variables():
    print(v.name,"=",v.varValue)
print ("Total Value = ", value(prob.objective))
sum = 0
for i in range(0,len(data)):
    sum+=(x-data[i])*(x-data[i])<jupyter_output><empty_output><jupyter_text>Sudoku Problem<jupyter_code>Boxes = [
    [(3 * i + k + 1, 3 * j + l + 1) for k in range(3) for l in range(3)]
    for i in range(3)
    for j in range(3)
]
prob = LpProblem("Sudoku Problem")
VALS = ROWS = COLS = range(1,10)
# Decision Variables
choices = LpVariable.dicts("Choice",(VALS,ROWS,COLS),cat="Binary")
for r in ROWS:
    for c in COLS:
        prob += lpSum([choices[v][r][c] for v in VALS]) == 1
for v in VALS:
    for r in ROWS:
        prob += lpSum([choices[v][r][c] for c in COLS]) == 1

    for c in COLS:
        prob += lpSum([choices[v][r][c] for r in ROWS]) == 1

    for b in Boxes:
        prob += lpSum([choices[v][r][c] for (r, c) in b]) == 1
input_data = [
    (5, 1, 1),
    (6, 2, 1),
    (8, 4, 1),
    (4, 5, 1),
    (7, 6, 1),
    (3, 1, 2),
    (9, 3, 2),
    (6, 7, 2),
    (8, 3, 3),
    (1, 2, 4),
    (8, 5, 4),
    (4, 8, 4),
    (7, 1, 5),
    (9, 2, 5),
    (6, 4, 5),
    (2, 6, 5),
    (1, 8, 5),
    (8, 9, 5),
    (5, 2, 6),
    (3, 5, 6),
    (9, 8, 6),
    (2, 7, 7),
    (6, 3, 8),
    (8, 7, 8),
    (7, 9, 8),
    (3, 4, 9),
    (1, 5, 9),
    (6, 6, 9),
    (5, 8, 9),
]

for (v, r, c) in input_data:
    prob += choices[v][r][c] == 1
prob.writeLP("Sudoku.lp")
prob.solve()
print("Status:", LpStatus[prob.status])
sudokuout = open("sudokuout.txt", "w")
for r in ROWS:
    if r in [1, 4, 7]:
        sudokuout.write("+-------+-------+-------+\n")
    for c in COLS:
        for v in VALS:
            if value(choices[v][r][c]) == 1:
                if c in [1, 4, 7]:
                    sudokuout.write("| ")
                sudokuout.write(str(v) + " ")
                if c == 9:
                    sudokuout.write("|\n")
sudokuout.write("+-------+-------+-------+")
sudokuout.close()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1451370069256248, 'fraction_numerical': 0.05650908360935461, 'mean_word_length': 3.9919839679358717, 'pattern_counts': {'":': 0, '<': 42, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '634079', 'license': 'Apache-2.0', 'n_tokens_mistral': 4012, 'n_tokens_neox': 3638, 'n_words': 1055, 'path': 'Python_Optimization.ipynb', 'repo_name': 'santoshvutukuri/MUST_Research-Optimization', 'size': '9963'}