Introducing Linear and Nonlinear Programming
5.5 Software Options for Optimization
5.5.1 M ATLAB Optimization Code—fmincon and linprog Nonlinear Optimization/Programming
Here, we describe the procedure for solving a constrained nonlinear optimization problem using MATLAB. The first step is to carefully examine how MATLAB defines the optimization problem, which is as follows (5.15) subject to (5.16) (5.17) (5.18) (5.19) (5.20) Each of the above variables is defined within the context of the following example. Figure 5.4 provides a top level view of the optimization algorithm. A more detailed presentation is provided in Sec. 7.3, and illustrated in Fig. 7.1. Note that if we wish to maximize a function f(x) and use the min MATLAB function, we simply need to perform the operation min x -f(x).
Figure 5.4. Optimization Flow Diagram Let’s use the following example to further explain the optimization problem definition given by Eqs. 5.15–5.20 Example 5.1 (5.21) subject to (5.22) (5.23) We would like to obtain the optimum values of the design variables x1 and x2 that minimize the objective function f(x) in Eq. 5.21, subject to the constraints in Eqs. 5.22
and 5.23 being satisfied.
The fmincon function
To solve the above problem, we use the fmincon function provided in MATLAB. The syntax of the function is as follows.
[xopt, fopt] = fmincon(‘fun’, x0, A, b, Aeq, beq, LB, UB, ‘nonlcon’)
where x0, A, b, Aeq, beq, LB, and UB are the input variables that need to be defined before calling fmincon. ‘fun’ is the name of the function file containing the definition of f(x), and ‘nonlcon’ is the name of the function file containing the nonlinear constraints. The variables xopt and fopt are the outputs of fmincon, where xopt is the optimum vector of variables [x1,x2] and fopt is the minimum value of the objective function f.
The Calling Function: main.m
Define the input variables: x0: This is the initial guess provided to fmincon. It is a vector of size equal to the number of variables in the optimization problem. In this case,
we have two variables, x1 and x2. Let us define x0 = [1;1].
A, b, Aeq, and beq: These variables need to be defined only if the problem has linear constraints. In many cases, all constraints (linear and nonlinear) can be defined in the nonlcon.m file, so these variables can simply be defined as empty matrices. For example, A=[ ]. However, there are great numerical advantages to defining the linear constraints within Eqs. 5.18 and 5.19, rather than within Eqs. 5.16 and 5.17, respectively.
LB, UB: These are the vectors that contain the lower and upper bounds on the variables x1 and x2, respectively. From the above problem definition (Eq. 5.23), we define: LB = [-5;-5]and UB = [5;5]. If a problem does not have bounds, these variables can simply be declared as empty vectors. We recommend creating a main.m file that defines all the above variables and calls fmincon for the optimization.
After defining the above quantities, we call the function fmincon.m. The function fmincon.m calls (i) nonlcon.m to evaluate the constraints and (ii) fun.m to evaluate the objective function as needed.
Objective Function file: fun.m
This file should be saved in the same folder as the above main M-file. This function returns the value of the objective function at any given point x. The contents of fun.m are as follows: function f = fun(x) f = x(1)^2 + 3*x(2)^2 - 2*x(1)*x(2) - 15; Nonlinear constraints file: nonlcon.m This file should be saved in the same folder as the main and the function files. It returns the values of the inequality and equality constraints at any given point x. Note that all inequality constraints should first be written in the form g(x) ≤ 0, as shown in Eq. 5.22. The expression on the left-hand-side of the inequality is then defined in the constraint file nonlcon.m as shown below. function [C,Ceq] = nonlcon(x) C(1) = -2*x(1) - 2*x(2) + 8; Ceq = [ ]; Note that C(1) is the first and only nonlinear inequality constraint in this problem. If the problem had more inequality constraints (say, n), they would be defined as C(2), C(3), …,C(n). Ceq is the vector of all equality constraints, which is empty in this problem.
Calling fmincon
Once all the variables and function files are defined, we can call the fmincon function as shown above, to obtain the output xopt = 2.6667 1.3333 fopt = -9.6667
also allows the user to set a number of different optimization parameters, such as the maximum number of iterations and other termination criteria. It also allows the user to pass problem parameters to the fun.m and the nonlcon.m files. More help on these features can be obtained by typing ‘help fmincon’ at the MATLAB command prompt.
Figure 5.5. Nonlinear Optimization
Options
Optimization options can be set for fmincon using the command optimset. Some options apply to all algorithms, while others are relevant to particular algorithms. You can use optimset to set or change the values of the options arguments. The options arguments include algorithms selection, information display settings and gradient estimation in series or parallel. Please see Optimization Options help in MATLAB for detailed information.
Display
Depending on the option selected for display, MATLAB can show different outputs in the Command Window. The information includes outputs at each iteration and provides technical exit messages. The exit messages can include hyperlinks. These hyperlinks bring up a window containing further information about the terms used in the exit messages.
Linear Optimization/Programming
Here, we describe the procedure for solving a linear programming problem using the MATLAB function linprog. The first step is to carefully examine the way MATLAB defines the optimization problem, which is as follows.
(5.24) subject to
(5.26) (5.27) where f is a so-called cost coefficient vector, A and Aeq are constant matrices, and b and beq are constant vectors. The quantities LB and UB are the lower and upper bounds on the design variables. This optimization statement is further explained within the context of the following example. Example 5.2 (5.28) subject to (5.29) (5.30) (5.31) The linprog function The basic syntax of the linprog function is as follows [xopt,fopt] = linprog(f, A, b) where xopt and fopt are the optimum values of the design variables and objective function, respectively. Before solving the linear program, we need to ensure that the objective function is of a minimization type, and that all the inequality constraints are written in the form of a1x1 + a2x2 ≤ b1, where a1,a2, and b1 are constants (see Eqs. 5.29 and 5.30).
Define the input variables
f: This is a row vector corresponding to the coefficients of the design variables in the objective function f. Thus, in the current problem,
f = [4;4]
A: This is a matrix in which every row corresponds to the coefficients of the design variables in each “≤” constraint. Therefore, A will have as many rows as we have inequality constraints, and as many columns as we have design variables. A = [-5 -3; -3 -5] Aeq and beq: These matrices do not exist in this example, so we write Aeq = [ ] beq = [ ] b: This is a vector in which each element is the number that appears on the right-hand-side of each “≤” constraint. We need to make sure that the order of the constraints used to define A and b is the same.
b = [-30; -15]
Calling linprog
xopt =
1.8750 1.8750 fopt = 15.0000
Figure 5.6 illustrates the objective function (dashed line) being minimized and the feasible region defined by the constraints in Eqs. 5.29 and 5.30. The dash lines represent constant values of the objective function. As this line moves downward, to decrease the value of the objective function, it reaches the optimum point. If it decreased any further, there would be no part of the line that would remain in the feasible region. Type ‘‘help linprog” at the MATLAB command prompt to explore other features and options.
Figure 5.6. Linear Optimization