Lecture 4 – Introduction to optimization methods in ...

15
Lecture 4 – Introduction to optimization methods in vehicle engineering Antti Ritari Last update: November 19, 2021 Abstract. In this lecture, we discuss optimization modelling with a focus on applications in vehicle engineering. We discuss basic concepts of convexity of functions and sets, constraints, feasible set and level curves of an objective function. A simple example problem of a least-cost fuel choice illustrates the basic concepts. Next, we build an instance of the fuel choice problem and solve it with an intuitive graphical approach. The final section covers MATLAB Optimization Toolbox for formulating and solving real- world problems. Outline of this lecture 1 Learning goals and lecture scope ....................................................................... 1 2 Optimization modelling ..................................................................................... 1 2.1 Mathematical optimization................................................................................... 1 2.2 Role of optimization in vehicle engineering design................................................ 2 2.3 Applications .......................................................................................................... 2 3 Convex optimization .......................................................................................... 4 3.1 Optimization problem classes ............................................................................... 4 3.2 Convex problems .................................................................................................. 4 3.3 Linear problems .................................................................................................... 6 3.4 Quadratic problems .............................................................................................. 7 4 Example problem: fuel choice for a dual fuel vehicle .......................................... 8 4.1 Problem formulation ............................................................................................ 8 4.2 Graphical solution approach ................................................................................. 9 4.3 Generalized form via vector variables ................................................................. 10 5 Modelling systems and solver packages........................................................... 11 5.1 MATLAB Optimization Toolbox ........................................................................... 11 5.2 Least-cost fuel choice example............................................................................ 12 5.3 Vector variables and indexing ............................................................................. 13

Transcript of Lecture 4 – Introduction to optimization methods in ...

Page 1: Lecture 4 – Introduction to optimization methods in ...

Lecture 4 – Introduction to optimization methods in vehicle engineering Antti Ritari Last update: November 19, 2021 Abstract. In this lecture, we discuss optimization modelling with a focus on applications in vehicle engineering. We discuss basic concepts of convexity of functions and sets, constraints, feasible set and level curves of an objective function. A simple example problem of a least-cost fuel choice illustrates the basic concepts. Next, we build an instance of the fuel choice problem and solve it with an intuitive graphical approach. The final section covers MATLAB Optimization Toolbox for formulating and solving real-world problems.

Outline of this lecture

1 Learning goals and lecture scope ....................................................................... 1

2 Optimization modelling ..................................................................................... 1

2.1 Mathematical optimization ................................................................................... 1

2.2 Role of optimization in vehicle engineering design ................................................ 2

2.3 Applications .......................................................................................................... 2

3 Convex optimization .......................................................................................... 4

3.1 Optimization problem classes ............................................................................... 4

3.2 Convex problems .................................................................................................. 4

3.3 Linear problems .................................................................................................... 6

3.4 Quadratic problems .............................................................................................. 7

4 Example problem: fuel choice for a dual fuel vehicle .......................................... 8

4.1 Problem formulation ............................................................................................ 8

4.2 Graphical solution approach ................................................................................. 9

4.3 Generalized form via vector variables ................................................................. 10

5 Modelling systems and solver packages ........................................................... 11

5.1 MATLAB Optimization Toolbox ........................................................................... 11

5.2 Least-cost fuel choice example ............................................................................ 12

5.3 Vector variables and indexing ............................................................................. 13

Page 2: Lecture 4 – Introduction to optimization methods in ...

1

1 Learning goals and scope The goal of this lecture is to provide overview of basic optimization concepts that are relevant for vehicle engineering. After completing this part of the course, the student will understand how important problems arising in vehicle engineering can be cast and solved as optimization problems. The student knows how to use software for implementing and solving real-world optimization problems. Search methods and algorithm implementations are outside of the scope of this material.

2 Optimization modelling

2.1 Mathematical optimization

Optimization is a subfield of applied mathematics that deals with the search of values for variables that minimize or maximize function values. It is almost always required at some stage of an engineering design work flow. Small optimization problems, involving only a few variables, can be solved easily by varying variable values by hand. For example, we could vary battery capacity in a Simulink-model of a hybrid vehicle and explore the optimal tradeoff between operation cost and investment cost. In this lecture we focus on large-scale optimization which requires an algorithm for carrying out the search for the minimum. Usually it’s not necessary to develop or implement an algorithm in a design process, because general-purpose software is available for this. Basic understanding of the key ideas in optimization is necessary though to avoid possible problems and make sense of the output of an algorithm. Variables. Variables are the basic components of a mathematical model of a problem. They represent quantities that we can choose or manipulate in some way. Control (or operation) variables are continuous variables as a function of time (e.g. engine fuel flow) and design variables define system configuration and component sizes (e.g. engine rated power, battery capacity). In standard notation, all the variables are aggregated to a real-valued n-vector 𝑥 = (𝑥!, 𝑥"… , 𝑥#). Constraints. The model must respect the underlying physics and limits of the system as well as the requirements of the design task. For example, longitudinal motion of a vehicle must respect Newton’s second law and requested engine brake power cannot exceed its rated power. The physical laws are encoded to the model as equations and performance limits and requirements as inequality constraints. Objective. The objective function encodes our preferences between different feasible designs, i.e. those variable values that satisfy all the constraints. A typical objective is to minimize cost of design, operation or a weighted sum of both. With the rise of environmental concerns in recent years, minimization of emissions is also often pursued in optimization modelling.

Page 3: Lecture 4 – Introduction to optimization methods in ...

2

Standard form. We express the general constrained optimization problem (P-G) with the decision variable 𝑛-vector 𝑥, 𝑚 number of inequality constraints, 𝑝 number of equality constraints as minimize 𝑓$(𝑥)

subjectto 𝑓%(𝑥) ≤ 0, 𝑖 = 1,… ,𝑚ℎ%(𝑥) = 0, 𝑖 = 1,… , 𝑝

(P-G)

where the functions 𝑓$(𝑥)…𝑓&(𝑥) and ℎ!(𝑥)…ℎ'(𝑥) map from a real valued 𝑛-vector to a scalar. A technical term for a problem of this type is a mathematical program. Neither 𝑓$ nor 𝑓!…𝑓& are required. If we do not formulate an objective and minimize a constant instead, the problem becomes a feasibility problem. Its solution is any point that satisfies all the constraints, i.e. any point in the feasible set. Similarly, we are not required to formulate constraints. In this case the problem is simply a function minimization problem.

2.2 Role of optimization in vehicle engineering design

Hybrid electric vehicles, plug-in hybrids and pure electric vehicles promise reduced greenhouse gas emissions, fossil fuel consumption and local air pollution. However, hybrid powertrains in particular are increasingly complex with respect to both design and operation. Optimization techniques can play an important role in studies during the early design phase of vehicles equipped with complex powertrains. The optimization approach has a number advantages compared to the conventional approach that relies on design engineer’s experience and rules of thumb decision making. The mains ones are:

1. Design quality. Instead of a small number of different design options, a large design space with possibly thousands of alternatives can be evaluated exhaustively. Complex multiple tradeoffs can be handled. The designer can focus their brain work to the selected few near-optimum solutions that are realistic candidates for the final choice. The final design choice is likely better, according to some criteria, compared to the manual approach in which only a few alternatives are investigated.

2. Speed. The design procedure can be automated and is capable of producing designs quickly and efficiently. This leads to reduced design cycle time. In practice the same amount of work hours (and cost, or price for customer) as any typical concept design task, delivers much greater value.

2.3 Applications

This section gives a brief overview of a few interesting and practical applications of optimization models in vehicle engineering and related fields. All the examples discussed here have in common that they are convex (we discuss what this means later), have only continuous variables (versus discrete) and that they include constraints for the variable values.

Page 4: Lecture 4 – Introduction to optimization methods in ...

3

Energy management. Powertrain hybridization gives rise to an additional degree of freedom in control compared to a conventional vehicle. Specifically, the control system needs to assign the battery charging or discharging powers in response to driver’s pedal command. This task is referred to as energy management. Optimization based approaches to hybrid vehicle energy management aim to minimize energy (fuel and electricity) cost. Common practical constraints include nonlinear fuel maps, battery charge level limits, battery efficiency, nonlinear vehicle dynamics due to aerodynamic drag and engine slew rate limits. The solution represents an ideal energy management strategy. The ideal solution is then used to benchmark simpler strategies for real-time use in the vehicle. Sizing of driveline components. Although conventional drivelines require thoughtful sizing of components, such as engine, the sizing design task is likely more challenging for hybrid electric configuration with batteries. This task requires assessing the tradeoff between battery investment cost and operation cost since a larger battery improves flexibility and efficiency while lowering energy cost. In this case optimization is useful for finding the optimal tradeoff point. Moreover, the component sizing design task is coupled with energy management. Ideally, we optimize design and control decisions simultaneously. Smooth autonomous maneuvering. Many ADAS functions complete maneuvers, such as lane changes, without driver involvement. The role of optimization is in determining the smoothest possible trajectory for the maneuver. Mathematically, the smooth maneuver goal is expressed as minimization of the total acceleration of the vehicle, which is a nonlinear quadratic function of positions in 𝑥 and 𝑦 coordinates. Constraints for initial and final position determine the type of maneuver. Active suspension. The controller of an actively actuated suspension system applies force in real-time, using a preview of the road profile ahead. The profile is obtained from sensors or maps. The controller cost function accounts for vehicle body vertical acceleration, which is an indicator of passenger comfort. Other terms in the cost function are actuator force and a penalty term that returns the suspension to equilibrium. Aim is to minimize the cost function while respecting suspension dynamics equations. Electric motor drive currents. This component level control problem involves choosing AC electric motor current and voltage waveforms. The objective is to maximize motor efficiency for given constant torque and speed. The motor model includes circuit dynamics and electromagnetic coupling between the rotor permanent magnets and the phase windings. Component cooling and cabin heating. Conventional internal combustion engine circulates coolant fluid which maintains the engine temperature within an acceptable range. Cooling is also required for batteries and electric machines. Passenger spaces need heating or cooling depending on outside conditions. Heat exchangers, e.g. radiators, transfer heat between two mediums without direct contact. Various cooling and heating requirements at different temperature intervals give rise to a complex heat

Page 5: Lecture 4 – Introduction to optimization methods in ...

4

exchanger network design problem. Optimization methods are widely used for evaluating tradeoffs in exchanger network design. Time-optimal trajectory generation for a race car. Racing benefits from, or even demands, advanced control near the physical limits of the vehicle. Race car optimization models evaluate the theoretic, optimal steering, gas and brake pedal inputs that lead to the best possible lap time. These models often use a reformulation technique that transforms a nonlinear aerodynamic drag term to a linear convex term in a lossless manner. Since the introduction of hybrid electric powertrains to racing, such as Formula 1 in 2014, energy management strategies have been included to these race car models.

3 Convex optimization

3.1 Optimization problem classes

Easy problems. Solving an optimization problem means that we find values of the decision variables that respect all the constraints while attaining the lowest possible value of the objective function. This solution is denoted as 𝑥∗ = (𝑥!∗, 𝑥"∗, … , 𝑥#∗). However, a solution is not guaranteed to always exist. This occurs when the constraints cannot be satisfied by any point. Such problem is labeled infeasible. A problem can also become unbounded when decision variable values approach infinity. Solving a problem with an arbitrary structure is trivial when the number of decision variables is small. We can try out different values for the variables and evaluate the objective for each try. Also, problems with a specific restricted form, least squares, have an analytical solution. They are easy to solve even when the number of decision variables is large. However, practical problems in vehicle engineering typically incorporate thousands of variables and do not take the least squares form. Algorithmic approach. Usually real-world optimization problems require the use of an iterative algorithm for finding the solution. This approach often involves a difficult compromise between computation time and solution quality: extremely long computation time guarantees the solution, while short computation time may attain some local point which satisfies the constraints but is suboptimal. Good news is that certain classes of optimization problems have favorable structures that can be exploited such that solving them algorithmically does not require compromising computation time or solution quality. In this case the algorithmic solution methods are extremely efficient, reliable, guarantee the global solution and do not require initial value guesses or parameter tuning.

3.2 Convex problems

The parent term for problems with a favorable structure is convex optimization. A convex optimization problem minimizes a convex function over a convex set. The convexity properties enable the design and application of efficient and reliable solution methods.

Page 6: Lecture 4 – Introduction to optimization methods in ...

5

Informally, we say that a function is convex if the line segment between any two points lies above the graph of the function. Similarly, a set is convex if the entire line segment between any two points in the set lies in the set. Figure 1 illustrates these properties.

Figure 1. Illustration of convexity property of functions and sets.

The profile of the nonconvex function exhibits many “peaks” and “valleys”. These are the stationary points where the gradient is zero. If the function is to be minimized, each valley represents a local optimum, and the lowest valley represents the global optimum. These multiple stationary points of nonconvex functions present a problem for any attempt to reach the optimum, i.e. the global solution. In contrast, a convex function provides a single optimum. If we find a stationary point, and know that the function is convex, then the point is the global optimum. The set of points that satisfy all the constraints is convex when the inequality constraint functions in the standard form are convex, and the equations are linear. The standard form is expressed as minimize 𝑓$(𝑥)

subjectto 𝑓%(𝑥) ≤ 0, 𝑖 = 1,… ,𝑚𝑎%)𝑥 = 𝑏% , 𝑖 = 1,… , 𝑝,

(P-CVX)

where all the functions map from an 𝑛-vector to scalar, as in the general form (P-G). The parameter 𝑎% is an 𝑛-vector and 𝑏% is a scalar. Moreover, the functions 𝑓$…𝑓& must be continuous, but they do not have to be differentiable. A convex maximization problem maximizes a concave function over a convex set. Since negative of a convex function 𝑓$ is a concave, the following problem is equivalent to the minimization problem (P-CVX):

Page 7: Lecture 4 – Introduction to optimization methods in ...

6

maximize −𝑓$(𝑥)subjectto 𝑓%(𝑥) ≤ 0, 𝑖 = 1,… ,𝑚

𝑎%)𝑥 = 𝑏% . 𝑖 = 1,… , 𝑝 (P-CVX-MAX)

Note that the functions 𝑓$…𝑓& must be convex and the equations linear as previously. Instead of taking on the difficult task of analyzing convexity properties of high-dimensional functions and sets in general, we focus on two of the most common subclasses, or families, of convex optimization: linear programs and quadratic programs. All the optimization problems we will encounter in this lecture, example problems and exercises belong to either one of these classes. Since convexity is guaranteed, we will benefit from efficient and reliable solution methods.

3.3 Linear problems

Linear program (LP) is a problem in which the objective function, inequalities and equations are linear. Linearity means that the variables appear only to the first power. The standard form is minimize 𝑐)𝑥

subjectto 𝑔%)𝑥 ≤ ℎ% , 𝑖 = 1,… ,𝑚𝑎%)𝑥 = 𝑏% , 𝑖 = 1,… , 𝑝

(P-LP)

where 𝑐 and 𝑔 are 𝑛-vectors. Often the constraints are expressed more concisely as 𝐺𝑥 ≤ ℎ, 𝐴𝑥 = 𝑏, where 𝐺 is an 𝑚 by 𝑛 matrix and 𝐴 is an 𝑝 by 𝑛 matrix. A surprising number of real-world problems, which appear nonlinear on the surface, can be modeled accurately as linear programs. Consider an internal combustion engine, which is characterized by a decreasing efficiency with decreasing load (Figure 2, left). Here, the efficiency curve is both nonlinear and nonconvex. An engine model needs to capture the nonlinear trend of part load efficiency.

Figure 2. Efficiency versus brake power (left), brake power versus fuel mass flow (right).

Although the part load efficiency curve is strongly nonlinear, we see that the relationship between fuel mass flow (power input) and brake power (output) is almost linear for the same engine (Figure 2, right). With this insight in hand, we encode the fuel-power relationship to the optimization model, instead of attempting to model the part load efficiency directly.

Page 8: Lecture 4 – Introduction to optimization methods in ...

7

Figure 3. Willans line approximation.

The line that approximates the relationship between fuel flow and brake power is called Willans line. It is accurate for a wide power range (Figure 3). The brake power is 𝑊 =𝑘𝑚 −𝑊*+), where k is the slope of the line and 𝑊*+) is the intercept. This expression is compatible with the linear program form (P-LP).

3.4 Quadratic problems

Quadratic programs (QPs) have linear inequalities and equations like LPs, but the objective function is nonlinear convex quadratic. The standard form is minimize J

12L𝑥)𝑃𝑥 + 𝑞)𝑥

subjectto 𝑔%)𝑥 ≤ ℎ% , 𝑖 = 1,… ,𝑚𝑎%)𝑥 = 𝑏% , 𝑖 = 1,… , 𝑝

(P-QP)

where 𝑃 is a square 𝑛 by 𝑛 matrix, 𝑞 is an 𝑛-vector and the other parameters are of the same dimensions as in LP. QPs are a bit more complex than LPs, because an arbitrary 𝑃 may give rise to a nonconvex 𝑥)𝑃𝑥 expression. Convexity of the QP is ensured by a positive semidefinite 𝑃, which is denoted as 𝑃 ∈ 𝑺,# or 𝑃 > 0. Often 𝑃 is diagonal, and all the diagonal entries 𝑝!…𝑝# are nonnegative. In this case 𝑃 is guaranteed positive semidefinite, and the quadratic term takes the sum of squares form

𝑝!𝑥!" + 𝑝"𝑥"" +⋯+ 𝑝#𝑥#". If 𝑃 = 0, the nonlinear quadratic term vanishes and the QP reduces to an LP. Quadratic programs often arise in control applications. The equality constraints capture the discretized linear system dynamics and boundary conditions. The objective is to minimize a sum of squares cost function, which is a sum of the input quantities we can manipulate (e.g. active suspension actuator force) and a deviation from some desired state over time.

Page 9: Lecture 4 – Introduction to optimization methods in ...

8

4 Example problem: fuel choice for a dual fuel vehicle In this section we formulate and solve a simple linear programming problem. This example illustrates how linear constraints form a polyhedral convex solution space and how the optimum is found at the boundary of the solution space. Although the example is small involving only two variables, the key concepts apply to more complex problems. We will later discuss how to use MATLAB for solving real-world problems involving many thousands of variables and constraints.

4.1 Problem formulation

Consider a vehicle equipped with separate fuel tanks for two types of fuels and an engine that supports fuel switching, e.g. Skoda Octavia G-TEC (Figure 4). Let 𝑥! and 𝑥" denote the masses of the two fuels consumed on a given trip and 𝑋!-./, 𝑋"-./ tank capacities. The fuels differ in price (𝐶!, 𝐶"), specific energy (𝐸!, 𝐸"), and consumption ratio (𝑅!, 𝑅"), which is the ratio of fuel combustion chemical energy to mechanical energy of the engine output shaft.

Figure 4. Octavia G-TEC. Image by Skoda.

The trip is characterized by the total required engine output energy, denoted by 𝐵. The decision problem involves choosing fuel use quantities 𝑥! and 𝑥" that satisfy the trip energy requirement at the least cost. However, we are not allowed to choose negative quantities or exceed tank capacities. With the objective and constraints defined, we formulate the problem mathematically as minimize 𝐶!𝑥! + 𝐶"𝑥"

subjectto𝐸!𝑅!𝑥! +

𝐸"𝑅"𝑥" = 𝐵,

𝑥! ≤ 𝑋!-01,𝑥" ≤ 𝑋"-01,

(ex1)

where we have omitted the nonnegativity constraints. The standard linear programming (P-LP) form of the problem is:

𝑥 = Y𝑥!𝑥"Z =

(𝑥!, 𝑥"), 𝑐 = [𝐶!𝐶"\ , 𝑏 = 𝐵, ℎ = ]𝑋!

-01

𝑋"-01^ ,

𝐴 = [−𝐸!𝑅!

−𝐸"𝑅"\ , 𝐺 = Y1 0

0 1Z.

Page 10: Lecture 4 – Introduction to optimization methods in ...

9

Now we will consider a problem instance with gasoline and biofuel as the fuel options 𝑥! and 𝑥", respectively. Gasoline is more expensive and has lower consumption ratio than biofuel (Table 1). On the other hand, gasoline benefits from significantly higher specific energy. The vehicle is also equipped with a smaller tank for gasoline than biofuel.

Table 1. Problem instance data.

Parameter Value Unit (𝐶!, 𝐶") (2, 1.3) €/kg (𝐸!, 𝐸") (12, 9), kWh/kg (𝑅!, 𝑅") (3.33, 3.2), - (𝑋!#$%, 𝑋"#$%) (35, 40) kg 𝐵 160 kWh

4.2 Graphical solution approach

To support graphical analysis of the problem, we substitute the parameters in (ex1) with problem data and rearrange the energy demand equation. The decision variable 𝑥" is now the only term on the left-hand side of the equation. The problem instance formulation takes the form

minimize 2𝑥! + 1.3𝑥"

subjectto 𝑥" =3.29 J160 −

123.33

𝑥!L,

𝑥! ≤ 35,𝑥" ≤ 40.

Figure 5 shows a graphical representation of the constraints. The solid lines illustrate the equation and points where the inequalities hold with equality. The horizontal (green) is for biofuel tank, the vertical (blue) is for gasoline tank and the skewed (orange) is for energy balance. The shaded regions represent points that satisfy the inequalities. The points that satisfy all constraints, called feasible solution set, is the line segment AB.

Figure 5. Feasible set shown as bolded interval AB of the energy balance equation.

Page 11: Lecture 4 – Introduction to optimization methods in ...

10

The objective can be illustrated graphically as lines where the objective receives a given value, in this case fuel cost. Figure 6 shows these lines, called level curves, as dotted lines. The lines represent all the points (𝑥!, 𝑥") that satisfy:

2𝑥! + 1.3𝑥" = 𝑓234 = constant,

=> 𝑥" =𝑓2341.3 −

21.3 𝑥!.

Adjusting the constant value shifts the line horizontally while the slope stays constant. The cost increases with increasing distance from the origin.

Figure 6. Selected level curves of the objective function.

All the points in the line for 𝑓234 = 71 are invalid for the minimum point because they lie outside the feasible solution space. On the other hand, the level curve for 𝑓234 = 85 contains at least one point in the feasible set. However, these points are suboptimal because a level curves corresponding to smaller 𝑓234 also intersect the feasible set. Optimum is found at the point where a level curve of 𝑓234 intersects the feasible set with the minimal value. The optimal point (𝑥!∗, 𝑥"∗) is about (13,40) and the minimum cost 𝑓234(𝑥!∗, 𝑥"∗) is about 78. Note that the inequality constraint 𝑥! ≤ 35 is inactive because the optimal gasoline consumption is lower than the tank capacity. Thus, we could downsize the fuel tank without effecting the solution.

4.3 Generalized form via vector variables

We can express the least-cost fuel choice problem (ex1) in a general form for 𝑛 number of fuels. Let 𝑥 denote an 𝑛-vector variable for the fuel quantities with entries 𝑥!, … , 𝑥#. The 𝑖th entry of the vector 𝑥 is 𝑥%. The same notation applies to the problem data parameters, except 𝐵, which is a scalar. The general form is

Page 12: Lecture 4 – Introduction to optimization methods in ...

11

minimize f𝐶%𝑥%

#

%5!

subjectto f𝐸%𝑅%𝑥%

#

%5!

= 𝐵,

𝑥% ≤ 𝑋%-01,𝑖 = 1…𝑛,

(ex1-g)

where the notation ∑ 𝑥%#

%5! means the sum over all 𝑥% for which 𝑖 is in the set {1, … , 𝑛}. The last expression is the tank capacity constraint, i.e. the upper bound for the fuel quantity. We build 𝑛 number of inequality constraints with this single expression by using indexing.

5 Modelling systems and solver packages Modelling systems allow expressing optimization problems in a high-level user-friendly form instead of the inconvenient standard form that solvers require as input. These systems are typically implemented as libraries of general purpose programming languages, such as Python or MATLAB. Problem decision variables, constraints, and objective are specified using the standard syntax of these languages. Next, we discuss the basic features of MATLAB Optimization Toolbox. Many other modelling systems are available, although we don’t cover those. CVX is a popular free MATLAB based system for general nonlinear convex optimization. Python users have CVXPY, YALMIP and others.

5.1 MATLAB Optimization Toolbox

Optimization Toolbox is an add-on to MATLAB. It is included in Aalto MATLAB installation. If you use your own computer, you can install the toolbox under the Aalto license. Decision variables. The optimization problem formulation starts with the decision variables, which are declared using optimvar as depicted below. The first parameter is the name of the variable. The convention is to set the name to be the same as the MATLAB variable name. Common optional properties are upper and lower bounds. x1 = optimvar(‘x1’, ‘LowerBound’, 0, UpperBound, 1) x2 = optimvar(‘x2’, ‘LowerBound’, 0, UpperBound, 1)

After all the variables have been declared, they can be used in constraints and objective function. In MATLAB the constraints and the objective are expressions in the decision variables. Problem container. This step involves creating an optimization problem instance. It is built from an objective function and a set of constraints, which are added to it one-by-one in the later steps in the workflow. We construct the problem with optimproblem and assign it to either maximize or minimize the objective.

Page 13: Lecture 4 – Introduction to optimization methods in ...

12

problem = optimproblem('ObjectiveSense', 'minimize');

Here we have called the instance simply problem but of course it could be called anything we want. Objective and constraints. The objective function must have a scalar value. It is simply declared as follows and attached to the problem object. problem.Objective = 2*x1 + 3*x2;

We can use ==, <= and >= to declare constraints. Keep in mind the distinction between assignment = and equality ==. Each constraint receives a unique label, which is up to us to choose. problem.Constraints.cons1 = x1 <= 3*x2; problem.Constraints.cons2 = x2 >= x2 + 4;

We cannot chain constraints together! For example, the declarations below are illegal. problem.Constraint.cons1 = 0 <= x1 <= 2; problem.Constraint.cons2 = x1 == x2 == 2;

The show(problem) method displays the problem. It is useful for checking the complete problem after the objective and constraints have been added one by one. Solver call. The solve method transforms the problem encoded by the instance into the standard form required by solvers. Then it calls the solver, which either solves the problem, or reports that the problem is infeasible or unbounded requiring reformulation. If the problem is feasible, the method returns the optimal objective value (fval) and the minimizing point, accessed via the MATLAB structure array sol. The variables values are accessed by dot notation e.g. sol.x1. [sol, fval, exitflag, output] = solve(problem);

The problem convexity shows its usefulness at this stage. We don’t need to provide the solver any initial guesses of 𝑥 or bother configuring the solver in any way. We don’t even need to know anything about what happens “under the hood”.

5.2 Least-cost fuel choice example

We begin by creating the data for the problem on lines 1-5 below. All parameters, except energy demand 𝐵, are MATLAB arrays with two elements. Variable declarations follow next (lines 7 and 8). They incorporate the nonnegativity and upper bound constraints.

Page 14: Lecture 4 – Introduction to optimization methods in ...

13

This works because the bounds are fixed problem data parameters. If the bounds were decision variables instead, we would need to formulate inequality constraints.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

B = 160; % Mechanical energy demand [kWh] C = [2 1.3]; % Fuel price [€/kg] E = [12 9]; % Specific energy [kWh/kg] R = [3.33 3.2]; % Consumption ratio X_max = [35 40]; % Tank capacity [kg] x1 = optimvar('x1', 'LowerBound', 0, 'UpperBound', X_max(1)); x2 = optimvar('x2', 'LowerBound', 0, 'UpperBound', X_max(2)); f_obj = C(1)*x1 + C(2)*x2; cons1 = (E(1)/R(1))*x1 + (E(2)/R(2))*x2 == B; prob = optimproblem('ObjectiveSense', 'minimize'); prob.Objective.f_obj = f_obj; prob.Constraints.cons1 = cons1; [sol, fval, exitflag, output] = solve(prob); x_star = [sol.x1; sol.x2];

The objective function (line 10) and the energy balance inequality (line 11) constraint declarations follow exactly the math of the algebraic problem form of (ex1). Once we have assigned the objective and the constraint to MATLAB variables f_obj and cons1, we create the problem container, attach these elements to it (lines 13-15) and call the solver (line 17). The MATLAB variable sol contains the optimal point (lines 17-18).

5.3 Vector variables and indexing

In this subsection, we discuss how to formulate vector variables and indexed expressions with Optimization Toolbox. These modelling techniques are important, because they allow us to formulate real-world problems, incorporating thousands of variables and constraints, by using only a few lines of code. The code excerpt below declares a nonnegative 𝑁-vector variable. The only difference to the scalar variable declaration is the dimension property which comes after the variable name and overrides the default value (one). Usually we want to declare multiple vector variables with the same dimension and assign the dimension to a MATLAB variable. The example below uses 𝑁. x = optimvar(‘x’, N, ‘LowerBound’, 0)

Constraints are applied elementwise if the variables and data are vectors. For example, if 𝑥 and 𝐴 are 𝑁-vectors, the one-line declaration below translates to 𝑁 inequalities. problem.Constraints.cons1 = x <= A;

Page 15: Lecture 4 – Introduction to optimization methods in ...

14

Alternatively, we can use an indexing vector. The first constraint declaration below results in the exact same output as the previous example. Indexing vectors are particularly useful for linking consecutive elements of a vector, as in the second constraint declaration below. i = 1:N; problem.Constraints.cons1 = x(i) <= A(i); problem.Constraints.cons2 = x(i+1) == x(i) + A(i);

Objective declarations make use of the sum() function for summing over the elements of a vector. The summation works in constraints as well. problem.Objective = sum(A(i).*x(i));

Additional material Example problems and solutions