I’d like to find the optimal values for Input A for Product 1 and for Input A for Product 2 with the aim to maximize Total Output and subject to a given constraint. I’ve tried using Python’s Scipy minimize function and it works if I have just one Product but it does not work for multiple Products.
Can Python Find Optimal Input Values When Multiple Products are Involved?
Here is what I tried assuming just two Products exist (In reality I have several thousand such Products):
import numpy as np
import scipy
from scipy.optimize import minimize
Product1_InputB = np.array([0.5])
Product1_InputC = np.array([1])
Product1_InputD = np.array([1])
Product1_InputE = np.array([0.08])
Product2_InputB = np.array([0.5])
Product2_InputC = np.array([1])
Product2_InputD = np.array([2])
Product2_InputE = np.array([0.1])
def Neg_Product1_Output(Product1_InputA):
return -1 * ((2.71828**((Product1_InputA-Product1_InputB)*(0.5*Product1_InputC-1.5*Product1_InputD)))/(1+(2.71828**((Product1_InputA-Product1_InputB)*(0.5*Product1_InputC-1.5*Product1_InputD))))*(Product1_InputA-Product1_InputB))
def Neg_Product2_Output(Product2_InputA):
return -1 * ((2.71828**((Product2_InputA-Product2_InputB)*(0.5*Product2_InputC-1.5*Product2_InputD)))/(1+(2.71828**((Product2_InputA-Product2_InputB)*(0.5*Product2_InputC-1.5*Product2_InputD))))*(Product2_InputA-Product2_InputB))
def Neg_Total_Output(Product1_InputA,Product2_InputA):
return Neg_Product1_Output + Neg_Product2_Output
def constraint(Product1_InputA, Product2_InputA):
return (((Product1_InputA - Product1_InputE) * Neg_Product1_Output) + ((Product2_InputA - Product2_InputE) * Neg_Product2_Output)) / Neg_Total_Output - 2
con = {'type':'ineq', 'fun': constraint}
Product1_InputA_Initial_Guess = np.array([0.1])
Product1_InputA_Initial_Guess = np.asarray([0.1])
Product2_InputA_Initial_Guess = np.array([0.1])
Product2_InputA_Initial_Guess = np.asarray([0.1])
Product1_bound = [(0.3,4)]
optimized_results = minimize(Neg_Total_Output,Product1_InputA_Initial_Guess,bounds=Product1_bound,constraints=con)
Product1_InputA_Optimal = optimized_results.x
Product1_InputA_Optimal
When I run the line optimized_results = … I get the below error:
Am not sure how to include Product2 in the optimized_results minimize function above.
TypeError: constraint() missing 1 required positional argument: 'Product2_InputA'