Convex constrained-optimization
1. Convex constrained-optimization
Another optimization problem we may encounter is convex constrained optimization or mixed integer linear programming. Let's start with the former.2. Convex constrained-optimization
Recall a convex function is U-shaped when visualized. Constraints are the limitations our variables have. The indifference curve represents a combination of variables. In convex optimization, we want to find the point on the indifference curve that minimizes or maximizes the objective function. Let's look at an example to understand this better.3. The indifference curve
Alexia is a freelance software engineer who works on a project-basis. She values both work, w, and leisure, l. Suppose her weekday preferences can be represented by a function U such that w raised to 0.4 times l raised to 0.6. The more hours of work or the more hours of leisure, the happier she is. This is her utility function. In optimization, this will be our objective function.4. Plotting the indifference curve
We can visualize the indifference curve by plotting different combinations of w and l using numpy and matplotlib. First, define some values for w and l. This can be anything to start with and can be adjusted later. Here we use np.linspace to create 100 values for w an l between 1 and 30. np.meshgrid creates the combinations of w and l. F is our objective function. We define a figure with plt.figure, the define contours with plt.contour specifying our values for w, l, the function, and different utility levels. The utility levels can also be chosen arbitrarily and then adjusted. We visualize the contours with plt.clabel and add some detail such as title, axis labels, and grid.5. The indifference curve
The result is this graph of w versus l with four indifference curves. They owe their name to the fact that on a specific curve all combinations of w and l yield the same utility. Hence, when looking at a particular curve, Alexia is indifferent about the specific combination of w and l. For instance every point on the bottom curve yields the same utility of five. The more w and the more l, the higher the utility, so as we move upwards and to the right we find indifference curves with higher utility.6. Time constraint
A day has 24 hours, so the optimization problem becomes max Utility subject to w plus l equals 24. Visualizing the constraint as a red line shows that it is linear, also known as a linear equality constraint. We add this line by adjusting one of our variables to the constraint, here l = 24 - w. and adding the line with plt.plot(). We are looking for a point where the constraint and indifference curve touch at one point; this is the point of maximum happiness for Alexia. If the indifference curve crosses the constraint at two points instead of one, like it does here, we need to adjust w and l slightly and look at a slightly higher indifference curve. This is visualized with the new dashed line.7. Solving with SciPy
Let's solve this optimization problem for maximum happiness using SciPy. As usual, we define our objective function, named utility function here. It's negated as we are using the minimize function. We've done something a little different here and used vars as the function parameter that represents the variables. Using vars allows functions to be more flexible. We then unpack vars into two variables, w and l, before returning the negated function. Next, we define our constraint. The total sum of work and leisure hours must equal 24. We make use of vars again here where np.sum(vars) is the sum of w and l. We define our initial guess and set up the constraint definition. Finally, we find the result of approximately 9 hours of work and 14 hours of leisure for maximum happiness.8. Let's practice!
Let's practice!Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.