Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (17.6k points)

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?

1 Answer

0 votes
by (41.4k 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.

Related questions

0 votes
0 answers
asked Jul 13, 2019 in Python by sourav (17.6k points)
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories

...