# Maximize revenue from smartphone sales

Idea: You have a company selling eight different smartphone models at different price points. Unfortunately, you sell them only after your supplier fulfills your order and delivers them to you. Because of production bottlenecks outside your control, the supplier informs you that only a certain amount of devices of each type (or a combination of types) can be delivered to you on time. Given this information, how many units of each device do you need to sell in order to maximize your revenue (profits would work similarly if you discounted the costs associated to each device)?

```from scipy.optimize import minimize, show_options smartphone_prices = [219, 189, 489, 109, 739, 139, 153, 115] f = lambda xs: -sum([pr*xs[idx] for idx, pr in enumerate(smartphone_prices)]) # minus since we want to maximize the function # -(219*x1 + 189*x2 + 489*x3 + 109*x4 + 739*x5 + 139*x6 + 153*x7 + 115*x8) # All constraints are >=0. To think in terms of x[0] <= 10, we have 10 - x[0]. constraints = ( {'type': 'ineq', 'fun': lambda x: 10 - x[0]}, {'type': 'ineq', 'fun': lambda x: 240 - x[1] - x[4]}, {'type': 'ineq', 'fun': lambda x: 80 - x[1] - x[6]}, {'type': 'ineq', 'fun': lambda x: 65 - x[2]}, {'type': 'ineq', 'fun': lambda x: 90 - x[3]}, {'type': 'ineq', 'fun': lambda x: 130 - x[2] - x[3]}, {'type': 'ineq', 'fun': lambda x: 160 - x[3] - x[5]}, {'type': 'ineq', 'fun': lambda x: 90 - x[4]}, {'type': 'ineq', 'fun': lambda x: 65 - x[5]}, {'type': 'ineq', 'fun': lambda x: 280 - x[6] - x[7]}, {'type': 'ineq', 'fun': lambda x: 235 - x[1] - x[7]}, {'type': 'ineq', 'fun': lambda x: 420 - x[4] - x[5]}, {'type': 'ineq', 'fun': lambda x: 280 - x[5] - x[6]}, {'type': 'ineq', 'fun': lambda x: 300 - x[5] - x[6] - x[7]}, ) # SLSQP can accept constraints unlike L-BFGS-B #print(show_options('minimize', 'SLSQP')) res = minimize(f, [1]*len(smartphone_prices), constraints=constraints, method='SLSQP', options={'disp': True}) """ Optimization terminated successfully. (Exit mode 0) Current function value: -152710.00009271657 Iterations: 4 Function evaluations: 51 Gradient evaluations: 4 """ # The maximal revenue of print(-res.fun) # 152710.00009271657 # is reached when we sell these units of each device print(res.x) # [ 10.00000001 40.00000003 65.00000004 65.00000003 90.00000006 65.00000003 40.00000002 195.00000007] # It is impossible to sell broken smartphones, so we account for this by converting the floats to integers and computing the closest possible revenue counts = list(map(int, res.x)) print(counts) # [10, 40, 65, 65, 90, 65, 40, 195] print("\$%.2f" % (-f(counts))) # \$152710.00```

Working with this supplier constrains your maximal revenue (given you sell all delivered smartphones) to 152710 dollar. Whether there is more to be gained by switching to another supplier?