<jupyter_start><jupyter_text>**Digital Marketing Spend Optimisation Using Linear Programming**In any business, we would like to make unlimited profit/zero loss/unlimited utilization of resources but there are constraints like capacity constraints/budget constraints/production constraints/resource constraints because of which we cannot achieve them.There are 3 characteristics of an optimization problem:1. **Decision Variables** -The variables whose value is to be decided in order to achieve our objective2. **Objective Function** – linear function of decision variables3. **Constraints** – restrictions which cause hindrance in achieving the objective.Linear programming is a simple technique to solve optimisation problems.In Lp, the business problem and constraints are all formulated as a linear function of the variables and hence the name linear programming.Chief Marketing Officer(CMO) of a company asks the digital marketing manager to allocate an annual budget of 10,00,000 Rs among 4 channels: AdWords, Facebook, email and affiliated such that the number of users visits their website. Cost of acquisition per user from each channel will be 250,200,150 and 100 respectively. Lifetime value (LTV) of the customer from each channel will be 1500,800,300 and 100 respectively. CMO further says that he wants a minimum of 1000 users from each of Adwords and facebook channels and a minimum of 500 users from each email and affiliated. Budget for facebook and adwords together should not exceed 600000. LTV should be more than 500.| CHANNEL   |  CAC    |  LTV  | Min_Users  ||-----------|:-------:|:-----:|:----------:||  AdWords  |  250    | 1500  |  1000      ||  FaceBook |  200    |  800  |  1000      ||   Email   |  150    |  300  |   500      || Affiliated|  100    |  100  |   500      |* **Adwords** :: B1/250 >=1000* **FB** :: B2/200 >=1000* **Email** :: B3/150 >=500* **Affiliated** :: B4/100>=500* **FB and Adwords Budget Constraint**:: B1+B2<=600000* **Total Budget Constraint** :: B1+B2+B3+B4 <=1000000* **LTV** :: +4 Adword +1.5 Facebook -1.33333333333 Email -4 Affiliated >= 0;* **Objective function** :   Maximize Number  of  Users = B1/250+B2/200+B3/10+B4/150 **To install ortools**pip install ortools<jupyter_code>from __future__ import print_function
from ortools.linear_solver import pywraplp



def LinearProgrammingExample():
    """Linear programming sample."""
    # Instantiate a Glop solver, naming it LinearExample.
    solver = pywraplp.Solver('LinearProgrammingExample',
                             pywraplp.Solver.GLOP_LINEAR_PROGRAMMING)

    # Create the 4 variables and let them take on any non-negative value.
    B1 = solver.NumVar(0, solver.infinity(), 'B1')
    B2 = solver.NumVar(0, solver.infinity(), 'B2')
    B3 = solver.NumVar(0, solver.infinity(), 'B3')
    B4 = solver.NumVar(0, solver.infinity(), 'B4')


    # Constraint  | B1/250 >=1000
    constraint0 = solver.Constraint(250000, solver.infinity())
    constraint0.SetCoefficient(B1, 1)

    # Constraint  |  B2/200 >=1000
    constraint1 = solver.Constraint(200000, solver.infinity())
    constraint1.SetCoefficient(B2, 1)

    # Constraint 1: 3x - y >= 0. |  B3/150 >=500
    constraint2 = solver.Constraint(75000, solver.infinity())
    constraint2.SetCoefficient(B3, 1)

    # Constraint  |  B4/100>=500
    constraint3 = solver.Constraint(50000, solver.infinity())
    constraint3.SetCoefficient(B4, 1)


    # Constraint  B1+B2<=600000
    constraint4 = solver.Constraint(-solver.infinity(), 600000)
    constraint4.SetCoefficient(B1, 1)
    constraint4.SetCoefficient(B2, 1)

    # Constraint  B1+B2+B3+B4 <=1000000
    constraint5 = solver.Constraint(-solver.infinity(), 1000000)
    constraint5.SetCoefficient(B1, 1)
    constraint5.SetCoefficient(B2, 1)
    constraint5.SetCoefficient(B3, 1)
    constraint5.SetCoefficient(B4, 1)

    

    # Constraint   LTV: +4 Adword +1.5 Facebook -1.33333333333 Email -4 Affiliated >= 0;
    constraint6 = solver.Constraint(0, solver.infinity())
    constraint6.SetCoefficient(B1,4)
    constraint6.SetCoefficient(B2,1.5)
    constraint6.SetCoefficient(B3,-1.333)
    constraint6.SetCoefficient(B4,-4)    



    

    # B1/250+B2/200+B3/100+B4/150
    #max: +0.004 Adword +0.005 Facebook +0.00666666666666 Email +0.01 Affiliated;
    objective = solver.Objective()
    objective.SetCoefficient(B1, 0.004)
    objective.SetCoefficient(B2, 0.005)
    objective.SetCoefficient(B3, 0.00666666666666)
    objective.SetCoefficient(B4, 0.01)
    objective.SetMaximization()

    # Solve the system.
    solver.Solve()
    opt_solution = (0.004) * B1.solution_value() + (0.005) * B2.solution_value()+(0.006667) * B3.solution_value()+(0.01) * B4.solution_value()
    print('Number of variables =', solver.NumVariables())
    print('Number of constraints =', solver.NumConstraints())
    # The value of each variable in the solution.
    print('Solution:')
    print('B1 = ', B1.solution_value())
    print('B2 = ', B2.solution_value())
    print('B3 = ', B3.solution_value())
    print('B4 = ', B4.solution_value())
    # The objective value of the solution.
    print('Optimal objective value =', opt_solution)


LinearProgrammingExample()<jupyter_output>Number of variables = 4
Number of constraints = 7
Solution:
B1 =  337496.8749999999
B2 =  200000.0
B3 =  75000.0
B4 =  387503.12499999994
Optimal objective value = 6725.043749999999
{'fraction_non_alphanumeric': 0.11645616641901932, 'fraction_numerical': 0.09546805349182763, 'mean_word_length': 3.8037466547725245, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 7, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '577568', 'license': 'MIT', 'n_tokens_mistral': 2060, 'n_tokens_neox': 1663, 'n_words': 497, 'path': 'Digital_Marketing_Spend_Optimisation_Using_Linear_Programming.ipynb', 'repo_name': 'Abhishek-Gargha-Maheshwarappa/Digital-Marketing-Spend-Optimisation-Using-Linear-Programming', 'size': '5384'}