Get startedGet started for free

Bound-constrained optimization

1. Bound-constrained optimization

Optimization problems often come with limiting constraints.

2. Bound constrained optimization

So far, we have been working with unconstrained optimization problems. SciPy's default solver for unconstrained problems is Broyden-Fletcher-Goldfarb-Shanno (BFGS), which is what we have been using. Let's now look at how we approach optimization problems with bounds on our inputs. A bound-constrained problem means that its variables are limited to a range of values. For example, if a factory could only produce between 50 and 100 units per day. These bounds are specified with SciPy's Bounds class. It takes two values, the first is the lower bound values and the second is the upper bound values.

3. L-BFGS-B

Let's look at a new objective function. We've negated it because we are finding the minima of a maximization problem. We are back at the biscuit factory. Our bounded constraint is that the factory can produce between 10 and 30 chocolate biscuits per day, and between 5 and 25 vanilla biscuits. Bounds takes the input in as two lists, the first list will be the lower bound values and the second list the upper bound values. We want to find the output quantities needed to maximize profit. We specify our initial guess as x0 and construct the minimize function. This time, we specify that the method is L-BFGS-B which is the Limited-memory Broyden-Fletcher-Goldfarb-Shanno with Bounds solver; and we include the bounds argument. The result tells us that we should aim for maximum output of 5 vanilla and 30 chocolate biscuits.

4. Linear constrained optimization

Before we wrap up, let's look at some linear constraints. If our objective function is linear or it has linear constraints, it is likely the problem we are dealing with is a linear constrained optimization problem. We can also plot both functions and see they produce a linear graph. If both are linear, then we are dealing with linear programming which we will look at in the next video.

5. Hard inequality

A hard inequality constraint refers to a specific condition. For example, a biscuit factory can produce biscuits up to the point where they have used all of their flour.

6. Solve a linear constrained problem

To solve hard inequality linear constraint problem, we define our multivariate objective function that takes the argument b. The multiple variables are different types of biscuits and the goal is to minimize costs. We also define our linear constraint function defining the limits of our ingredients. In this example, the objective function is not linear but the constraint function is linear. We define the constraint as a dictionary to specify the type of constraint and the constraint function. The options are eq for equality and ineq for inequality. Since our constraint function uses a conditional rather than an equal, we select inequality. We include our initial guess, x0, for the optimal value of each variable and construct the minimize function by filling in the arguments for the objective function, the initial guess, and constraints as a list. The result is the array of approximately 0.2 and 1.9 giving us the optimal ingredient quantities.

7. Let's practice!

Time to practice!