# optimization of multiple variable function

1 view

I am trying to maximize revenue using costs for several products.

Here is my code:

In this piece of code, I calculate revenue for each product and then sum it.

list = [65,78,90] #list of products

def calcRev(x,list):

sum = 0

x = pd.DataFrame(x)

for i in list:

count = 0

df_temp = df[df.app_id == i].reset_index()

df_temp['cost'] = x[count]

df_temp['inst'] = df_temp['cost']/df_temp['cpi']

df_temp['rev'] = (df_temp['LTV_accum']* df_temp['inst'])-df_temp['cost']

count = count+1

sum = sum+df_temp.rev.sum()

return sum

objective function:

def obj(x,list):

return -calcRev(x,list)

constraints:

def constrglobal(x,list):

return  10000-calcRev(x,list)

cons = ({'type':'ineq','fun':constrglobal,'args':(list,)})

then I initialize costs as initial guess x. Since I have two products as input x has two dims. x = np.full((15,), 0)

then I apply minimize function:

sol = minimize(obj,x,args=(list,),method='SLSQP',constraints=cons,options={'disp':True})

But as solution I have data distributed only for first product:

sol.x

array([1467.64152006, 1442.41330009, 1417.18518617, 1391.95714139,

1366.1311581 ,    0.        ,    0.        ,    0.        ,

0.        ,    0.        ])

Can you please suggest what is wrong and how I can have sol.x distributed among all products?

by (39.1k points)

This problem can be solved by adding revenue sum for each product as shown in the below code:

return df_temp65.rev.sum()+df_temp78.rev.sum() in calcRev function.