<jupyter_start><jupyter_text>Objective function is to minimize cost subject to contraints like total conversion(number of people enquired about the product), conversion costs(costs per conversion) and converted(approved conversions).<jupyter_code># First, we create a LP problem with the method LpProblemin PuLP.
prob=LpProblem("Optimzation Problem", LpMinimize)

# Then, we need to create bunches of Python dictionary objects with the information we have from the table. 
# The code is shown below,

# Create a list of advertisement id
advertisement_id=list(df.ad_id)
#print(advertisement_id)

# Create a dictionary of cost for all advertisement ids
costs=dict(zip(advertisement_id, df.Spent))
#print(costs)

# Create a dictionary of total conversion for all advertisement ids
total_conversions = dict(zip(advertisement_id,df.Total_Con))

# Create a dictionary of cost per conversion for all advertisement ids
conversion_costs = dict(zip(advertisement_id,df.Cost_Per_Conversion))

# Create a dictionary return on adverstising spend for all advertisement ids
Returns = dict(zip(advertisement_id,df.Return_on_advertising_spend))

# Create a dictionary return on adverstising spend for all advertisement ids
Converted = dict(zip(advertisement_id,df.Approved_Conversion))

# we create a dictionary of advertisement id variables with lower bound =0 and category 
# continuous i.e. the optimization solution can take any real-numbered value greater than zero.
advertising = LpVariable.dicts("Advertisement",advertisement_id,lowBound=0,cat='Continuous')
# the main objective function
prob+=lpSum([costs[i]*advertising[i] for i in advertisement_id])  # Total cost in which a_id company should invest more.
# total conversion
prob += lpSum([total_conversions[f] * advertising[f] for f in advertisement_id]) >= 1, "Total_Conversion minimum"
prob += lpSum([total_conversions[f] * advertising[f] for f in advertisement_id]) <= 100, "TotalConversion maximum"
#Conversion costs
prob += lpSum([conversion_costs[f] * advertising[f] for f in advertisement_id]) >=2, "Conversion Costs minimum"
prob += lpSum([conversion_costs[f] * advertising[f] for f in advertisement_id]) <=4, "Conversion Costs maximum"
# Converted
prob+= lpSum([Converted[f] * advertising[f] for f in advertisement_id]) >=1, "Converted minimum"
prob+= lpSum([Converted[f] * advertising[f] for f in advertisement_id]) <=100, "Converted maximum"
prob.solve()
print("Status:", LpStatus[prob.status])
for v in prob.variables():
    if v.varValue>0:
        print(v.name, "=", v.varValue)
        

print("The total cost per add that xyz company should spend given the contraints to acquire 1 customer: {}".format(round(value(prob.objective),2)))
df[(df.ad_id==777105)|(df.ad_id==781354)][["ad_id","Total_Con","Cost_Per_Conversion",
"Approved_Conversion"]]
df[df.ad_id==781354]
# $2 to acquire one customer.
df[df.ad_id==777105]<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10357880247763249, 'fraction_numerical': 0.013764624913971095, 'mean_word_length': 6.670184696569921, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 9, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '157560', 'license': 'MIT', 'n_tokens_mistral': 932, 'n_tokens_neox': 862, 'n_words': 285, 'path': 'optimizing_conversion_rate.ipynb', 'repo_name': 'depriyadarshini/Optimization_Practical', 'size': '2906'}