I started using scipy.optimize.minimize for a work project in which I try to optimize the allocation of product across stores given historical sales data for each store.
I obtain data for ~300 stores from an excel sheet and store it in a Store class. With the data I build a histogram and fit a normal distribution to the histogram, and save the mean and standard deviation of the fitted normal distribution in their respective Store object. (no problem so far with this part).
The problem comes when I try to to maximize the total "Probability" that the product will be sold depending on how I distribute the product across the stores.
There is more stuff going on in my program, but this is the relevant part (missing is the acquisition of data from the excel file, and the fitting of the normal distribution to each store):
import math, numpy
from scipy.optimize import minimize
unitsAvailable = 400
def distribution_objective(allocations):
target = 0
for i in range(len(stores)):
target += normal(allocations[i], stores[i].mu, stores[i].std)
return - target
def constraint1(allocations):
return unitsAvailable - sum(allocations)
def constraint2(allocations):
return min(allocations)
cons = [{'type':'eq', 'fun':constraint1}, {'type':'ineq', 'fun':constraint2}]
guess_allocs = []
for i in range(len(stores)):
guess_allocs.append(unitsAvailable / len(stores))
distribution_solution = minimize(distribution_objective, guess_allocs, method='SLSQP', constraints=cons, options={'disp':True})
Running my program I obtain the following message:
Iteration limit exceeded (Exit mode 9)
Current function value: -124.1033692190603
Iterations: 101
Function evaluations: 46808
Gradient evaluations: 101
Why does this happen?
The optimizer fails to reach convergence within the default number of iterations. Both the convergence tolerance & the number of iterations can be set in the call to the method by setting the tol
and options
parameters, see the docs here for further details