pythonscipy-optimizeeconomics

maximize (with scipy) production having constrained budget


I tried to solve common optimization problem: to maximize profit from certain production function having limited budget (7800$), but still something is wrong in such logics of optimization:

from scipy.optimize import minimize

def max_out(x):     # Q_func
    return 36*x[0]**(1/2) * x[1]**(1/3) * x[2]**(1/4)

def obj(x):     # maximize total P
    y= -(25*x[0] + 20*x[1] + 10*x[2])  # maximize production revenue
    return y

def constr(x):
    return ( 7800- (25*x[0] + 20*x[1] + 10*x[2])  )  # constraint is budget 7800

cons = ({'type': 'ineq', 'fun':  constr },
        {'type': 'ineq', 'fun': lambda x: x[0] },
        {'type': 'ineq', 'fun': lambda x: x[1] },
        {'type': 'ineq', 'fun': lambda x: x[2] })

##bnds = ((0, None), (0, None), (0, None))      # bounds= bnds,
res = minimize(obj, (10, 10, 10), method='SLSQP',  constraints=cons)
print(res.x)
r= [round(x) for x in res.x]

print("raw materials needed for production func:", r)
print(f'to maximize production revenue to {-obj(r):.2f} $')
print("cost of product: ", max_out(r))

# raw materials needed for production func: [171, 139, 74]
# to maximize production revenue to 7795.00 $
# cost of product:  7152.316966012599

as is resulted: I've, probably, got the closest to budget total_price of produced goods from given raw materials [x0, x1, x2]... but max_out func gives cheaper total_price for all produced. But I need max_out being close to the budget (as is production price), in order to find the price for sale (overall cost, that should be higher than inputted budget)... something is wrong!.. or have I formulated the task to the python in an incorrect way ??

p.s. frankly speaking I'm getting smaller price of product compared to the raw materials not for the first time, while trying to solve tasks like this - but this sounds strange for me... what is my misunderstanding ? and how to change the code to input raw_materials to budget & maximize the total_cost of produced units ?


Solution

  • I finally got correct results (compared to manual calculus - p9 here):

    from scipy.optimize import minimize
    
    def max_out(x):     # Q_func
        return -(36*x[0]**(1/2) * x[1]**(1/3) * x[2]**(1/4))    # maximize to budget limit
    
    def max_revenue(x):     #  total P
        y= (25*x[0] + 20*x[1] + 10*x[2])  #  product P
        return y
    
    def constr(x):
        return ( 78000- (25*x[0] + 20*x[1] + 10*x[2])  )  # constraint is budget 7800
    
    cons = ({'type': 'ineq', 'fun':  constr },
            {'type': 'ineq', 'fun': lambda x: x[0] },
            {'type': 'ineq', 'fun': lambda x: x[1] },
            {'type': 'ineq', 'fun': lambda x: x[2] })
    
    ##bnds = ((0, None), (0, None), (0, None))      # bounds= bnds,
    res = minimize(max_out, (10, 10, 10), method='SLSQP',  constraints=cons)
    print(res.x)
    r= [round(x) for x in res.x]
    
    print("raw materials needed for production func:", r)
    print(f'to input cost of raw materials into budget {max_revenue(r):.2f} $')
    print("cost of product: ", -max_out(r))
    

    I was mistaken in used func for optimization - correct is: res = minimize(max_out, (10, 10, 10), method='SLSQP', constraints=cons)