1. Introduction to mathematical optimization
Welcome to the course! I'm Jasmin, your instructor.
2. Coming up...
Get ready to solve real-world optimization problems and build a toolkit of techniques to tackle different optimization problems.
Before starting, you should be familiar with using NumPy for data manipulation and numerical computation, as taught in the following course.
This course will introduce some mathematical concepts, including calculus and algorithms, but we won't assume prior knowledge in these areas. Let's get started!
3. What is mathematical optimization?
Mathematical optimization is a branch of applied and computational mathematics that aims to find the ideal inputs for a specific problem.
In agriculture, mathematical optimization maximizes crop yields by analyzing variables such as soil conditions and weather to have the best crop quantity and quality.
4. What is mathematical optimization?
In the supply chain industry, mathematical optimization finds optimal distribution routes by considering factors like distance and traffic to improve delivery times and reduce costs.
5. The objective function
To understand how we can apply mathematical optimization, let's dive into some math. Each optimization problem begins with an objective function. This function describes the mathematical relationship between input variables and the result we are trying to optimize. Suppose a furniture manufacturer wants to maximize profit, P, which is dependent on the quantity of furniture produced, q, through the following equation.
This is an optimization problem! We want to find the value of q that maximizes P. We could manually calculate a bunch of values of q and pick one. A better option is to make Python do the work for us and plot a range of quantities to see what the function looks like.
6. Optimize with Python
First we create a NumPy array, qs, containing a range of quantity values using np.arange(). We will use a range of 80 to start but may need to revise it if the plot isn’t clear.
Next, we define our objective function called profit, which takes a quantity, q, and does the work for us. Finally, we plot the quantities on the x-axis and profits on the y-axis. Notice that we call the profit function directly on the qs array, which utilizes NumPy's vectorized operations.
7. Optimization in manufacturing
Looking at the plot we can see that profit increases as quantity increases until it reaches a value around 40, then decreases for larger quantities. This maximum value is the optimum value. There are many real-world reasons a function could take on this shape, for example, the furniture maker may not have enough staff or equipment to create more than 40 pieces of furniture which is why we see the drop-off.
8. Exhaustive search
But how can we find this optimum value without creating a plot and eye-balling it?
One method that is quick to implement is an exhaustive search, a "brute force" method that involves calculating the profit for a range of quantities and selecting the one that produces the largest profit.
Let's try this with some Python code.
9. Exhaustive search in Python
We start by defining our range of quantities to search through and the objective function, just as we did when we plotted it.
Next, we calculate the profit for every quantity in the qs array and find the maximum profit with the max array method.
To find the optimal quantity value, we need to find the index of the maximum profit in the profits array and subset the quantity at that same index from the qs array.
We can use the NumPy argmax function to find the index of the maximum profit, and then use it to subset the qs array with square brackets.
We print the results in a formatted string, or F-string, specified with an "f" before the quotes. F-strings allow us to insert variables into strings to create nicely formatted statements.
The optimum quantity was 40 pieces of furniture, which matches what we saw in the plot. This would yield 800 dollars profit.
10. Exhaustive search advantages and disadvantages
Exhaustive search is quick to implement, does not require expensive software, and requires minimal assumptions.
This method will not scale for more complex cases, as complexity increases brute force becomes rapidly inefficient. To tackle these cases, we'll investigate more efficient optimization techniques throughout the course.
11. Let's practice!
But first, let's practice!