Chapter 01
Chapter 01
1 Introduction to Optimization 6
1.1 Optimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.2 Engineering Applications of Optimization . . . . . . . . . . . . . . . . . . . . . . . 7
1.3 Convexity and Concavity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.4 Duality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.5 Statement of an Optimization Problem . . . . . . . . . . . . . . . . . . . . . . . . 10
1.5.1 Examples on real optimization problems: . . . . . . . . . . . . . . . . . . . 10
1.5.2 The general form of optimization problems: . . . . . . . . . . . . . . . . . 11
1.5.3 Convex optimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
1.6 Unconstrained and constrained optimization . . . . . . . . . . . . . . . . . . . . . 12
1.7 Gradient Descent Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.7.1 What is the gradient of a function? . . . . . . . . . . . . . . . . . . . . . . 12
1.7.2 What is Gradient Descent? . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
1.7.3 What is the idea of Gradient Descent? . . . . . . . . . . . . . . . . . . . . 13
1.8 Coding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
3 Machine Learning 30
3.1 Introduction to Machine Learning . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
3.2 How does Machine Learning work . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
3.3 Need for Machine Learning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
3.4 Classification of Machine Learning . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
3.4.1 Supervised Learning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
3.4.2 Unsupervised Learning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
3.4.3 Reinforcement Learning . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
1
4.2.2 Steps in Using a Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
4.3 Training and Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
4.4 Overfitting versus Underfitting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
4.4.1 Overfitting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
4.4.2 Underfitting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
8 ML Polynomial Regression 59
8.1 Need for Polynomial Regression: . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
8.2 Implementation of Polynomial Regression using Python . . . . . . . . . . . . . . . 60
8.3 Steps for Polynomial Regression . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
8.3.1 Data Pre-processing Step . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
2
10.7.4 Creating the Confusion Matrix . . . . . . . . . . . . . . . . . . . . . . . . 77
10.8 Visualizing the Training set result . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
10.9 Visualizing the Test set result . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
10.10Combining all the steps: . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
13 Reinforcement Learning 97
13.1 What is Reinforcement Learning? . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
13.2 Reinforcement Learning Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
13.3 Markov Decision Process (MDP) . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
13.4 Types of Reinforcement Learning . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
13.5 Q-Learning Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
13.6 Applications of Reinforcement Learning . . . . . . . . . . . . . . . . . . . . . . . . 99
3
14 Introduction to Neural Networks 100
14.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
14.2 Biological Inspiration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
14.3 Neuron Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
14.3.1 Single-Input Neuron . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
14.3.2 Transfer (Activation) Functions . . . . . . . . . . . . . . . . . . . . . . . . 103
14.3.3 Multiple-Input Neuron . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
14.4 Network Architectures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
14.4.1 A Layer of Neurons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
14.4.2 Multiple Layers of Neurons . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
14.5 Implementation of neural network . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
14.5.1 Implementation of neural network from scratch using NumPy . . . . . . . 106
14.5.2 Activation Functions: Adding Non-Linearity . . . . . . . . . . . . . . . . . 106
14.6 Neural Networks using NumPy . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
14.6.1 Make a Dataset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
14.6.2 Characterize Labels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
14.6.3 Import Libraries and Characterize Activation Function . . . . . . . . . . . 108
14.6.4 Initialize Weights . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
14.6.5 Characterize the Feedforward Neural Network . . . . . . . . . . . . . . . . 109
14.6.6 Characterize the Loss Function . . . . . . . . . . . . . . . . . . . . . . . . 109
14.6.7 Backpropagation of Error . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
14.6.8 Training the Neural Network . . . . . . . . . . . . . . . . . . . . . . . . . . 110
14.6.9 Predicting with a Trained Model . . . . . . . . . . . . . . . . . . . . . . . 111
14.6.10 Initialize Weights for Training . . . . . . . . . . . . . . . . . . . . . . . . . 112
14.6.11 Train the Neural Network . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
14.6.12 Print Trained Weights . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
14.6.13 Make Predictions with a Trained Model . . . . . . . . . . . . . . . . . . . 113
4
List of Figures
10.1 KNN. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
5
Chapter 1
Introduction to Optimization
1.1 Optimization
Definition of Optimization: Optimization is the process of obtaining the best result
under given circumstances by minimizing effort or maximizing benefit.
Role in Engineering: Engineers make various technological and managerial decisions
at different stages of system design, construction, and maintenance, aiming for optimal
outcomes.
Mathematical Formulation: Optimization involves expressing effort or benefit as a func-
tion of decision variables and finding conditions that yield the maximum or minimum value.
Optimization Methods: No single method can solve all optimization problems efficiently,
leading to the development of different mathematical programming techniques.
Operations Research: A branch of mathematics that applies scientific methods to decision-
making problems, seeking optimal solutions.
Historical Background: Operations research originated during World War II when the
British military sought systematic methods for resource allocation, leading to techniques
such as linear programming.
optimization techniques
Calculus methods
Nonlinear programming
Geometric programming
Linear programming
Dynamic programming
Integer programming
Stochastic programming
Separable programming
Multiobjective programming
Modern or nontraditional optimization techniques
Genetic algorithms
Simulated annealing
Ant colony optimization
6
1.2 Engineering Applications of Optimization
Optimization, in its broadest sense, can be applied to solve any engineering problem. Some typical
applications from different engineering disciplines indicate the wide scope of the subject [1]:
2. Design of civil engineering structures such as frames, foundations, bridges, towers, chimneys,
and dams for minimum cost.
4. Shortest route taken by a salesperson visiting various cities during one tour.
Although this example only uses two points, for a function to be concave, the rule must be
true for all combinations of points on that function, in the given range.
7
What is a Convex Function?
A convex function is a function where a straight segment between any two points on the graph
does not lie below the curve of the graph, In other words, the straight line is always above or at
the same place as the function’s curve. It is the opposite of a concave function.
Fig. 1.2 shows an example of a convex function. You can see that if we pick any two points on
the curve, and draw a line segment between them, the line segment will always above or at the
same level as the function itself.
Although this example only uses two points, for a function to be concave the rule must be true
for all combinations of points on that function, in the given range.
Convex Function: A function f (x) defined over an interval I is convex if its second derivative
f ′′ (x) is non-negative for all x in I. Mathematically, if f ′′ (x) ≥ 0 for all x in I, then f (x) is
convex.
Concave Function: A function f (x) defined over an interval I is concave if its second deriva-
tive f ′′ (x) is negative for all x in I. Mathematically, if f ′′ (x) ≤ 0 for all x in I, then f (x) is
concave.
8
Example 1.1 For the following functions, determine whether they are concave, convex or both.
The cubic is neither concave nor convex, but it is convex when x > 0 and concave when x < 0.
Now, above we have a quadratic function. We can see that any line segment drawn will lie
below the curve. Thus, the function is concave.
Finally, we have a straight line. Any line segment will lie on the line and hence it is both
concave and convex.
9
1.4 Duality
It can be seen from Fig. 1.6 that if a point x∗ corresponds to the minimum value of function
f (x), the same point also corresponds to the maximum value of the negative of the function,
−f (x). Thus without loss of generality, optimization can be taken to mean minimization since
the maximum of a function can be found by seeking the minimum of the negative of the same
function.
2. Decision Variables: Decision variables are the variables that can be adjusted or controlled
to optimize the objective function. They represent the choices or decisions to be made in
the optimization process. Example: The lengths of the square.
3. Constraints: Constraints are conditions or limitations that must be satisfied during the
optimization process. They represent the restrictions on the values of the decision variables.
Example: The perimeter of the square must be less than a certain value.
10
2. Optimal Design of a Steel Bridge
where
Goal: Find the optimal value of ⃗x that minimizes f (⃗x) while satisfying all the constraints.
Note: An affine function is a type of mathematical function that combines a linear transfor-
mation with a translation. It is a function of the form: f (x) = A⃗x + b.
11
1.6 Unconstrained and constrained optimization
If we need to optimize the objective function without any additional constraints, then it is called
unconstrained optimization. For example, find the minimum of the function f (x) = x2 − 2x − 3.
Whenever, the objective function is accomplished with another correlation (constrained func-
tion), these optimization problems are called constrained optimization problems. For example,
find the minimum of the function f (x) = x2 − 2x − 3 where x ≥ 2.
For the unconstrained problem, the solution is fmin = −4 whereas for the constrained problem
the solution is fmin = −3.
Example 1.2 For the given simple objective function f (x) = x2 −2x−3 where x is a real number.
2. If the function is convex, find the value of x at which we get the minimum value of the
function.
Solution:
f ′ (x) = 2x − 2 = 0 ⇒ x∗ = 1
12
Geometric Interpretation: The gradient points in the direction of the steepest ascent of
the function at a given point.
Magnitude Significance: The magnitude of the gradient represents the rate of change of
the function in the steepest direction.
Key Insight: The gradient provides both the direction and the steepness of the slope at a
specific point in the input space.
2. Iterative Update:
At each iteration t, compute the gradient of the objective function f (x) at the current
point xt . The gradient represents the direction of steepest ascent.
Update the current point xt by taking a small step in the opposite direction of the
gradient, scaled by a learning rate α. This step is crucial for converging towards the
minimum:
Repeat this process until a stopping criterion is met, such as reaching a maximum
number of iterations, achieving a sufficiently small gradient magnitude, or converging
to a predefined minimum threshold.
3. Convergence Criteria:
Common convergence criteria include reaching a maximum number of iterations, achieving
a sufficiently small gradient magnitude, or reaching a minimum threshold for the change in
objective function value between iterations.
4. Learning Rate:
The learning rate α controls the step size in each iteration. It is a hyperparameter that
needs to be carefully chosen:
13
Example 1.3 Let’s consider the function f (x) = x2 , and we want to find the minimum value of
this function using gradient descent.
Solution:
1. Objective Function:
f (x) = x2 (1.11)
2. Gradient:
The gradient of f (x) with respect to x is:
∇x f (x) = 2x (1.12)
4. Initialization:
We choose an initial value for x, let’s say x0 = 3.
5. Iterative Updates:
We’ll perform iterative updates using the gradient descent update rule until convergence or
a predefined number of iterations:
6. Convergence Criterion:
We’ll stop the iterations when either the absolute difference between consecutive values of
x becomes smaller than a predefined threshold or when we reach a maximum number of
iterations.
Let’s assume we choose a learning rate α = 0.1 and set a maximum of 10 iterations.
Now, let’s perform the iterative updates:
14
1.8 Coding
”fmincon” is a MATLAB function for constrained optimization. Below are examples for codes
demonstrating how to use ”fmincon” to solve an optimization problem with constraints:
Example 1.4 A network operator allocates bandwidth to a premium user. The objective is to
minimize the allocated bandwidth while satisfying multiple linear constraints related to quality of
service (QoS) and system capacity.
Given Parameters:
Decision Variable:
Rate Relationship:
R = ηx
4x ≥ 20
min x (1.15)
x
s.t. 4x ≥ 20 (1.16)
x ≤ 10 (1.17)
x≥3 (1.18)
min x (1.19)
x
s.t. − 4x + 20 ≤ 0 (1.20)
x − 10 ≤ 0 (1.21)
−x+3≤0 (1.22)
Note: The objective function is linear, and all constraints are linear. Therefore, this is a
convex optimization problem.
15
2. Solve the problem using “fmincon”.
% Objective function
objective = @(x) x;
% Initial guess
x0 = 1;
% No equality constraints
Aeq = [];
beq = [];
% No additional bounds
lb = [];
ub = [];
% Solve
[x_opt, fval, exitflag] = fmincon(objective, x0, A, b, Aeq, beq, lb, ub);
% Display results
fprintf(’Optimal bandwidth: %.4f MHz\n’, x_opt);
fprintf(’Minimum objective value: %.4f\n’, fval);
The results:
16
Example 1.5 For the given simple objective function f (x, y) = (x − 2)2 + (y − 3)2 subject to
2x + 3y ≤ 0 and x − 2y ≤ −3, where x and y are real numbers.
s.t. 2x + 3y ≤ 0 (1.24)
x − 2y + 3 ≤ 0 (1.25)
% Initial guess
x0 = [0, 0];
The results:
17
Example 1.6 For the given simple objective function f (x, y) = (x)2 +(y)2 −3 subject to x+y ≥ 2,
where x and y are real numbers.
min x2 + y 2 − 3 (1.26)
x,y
% Initial guess
x0 = [0, 0];
The results:
18
Example 1.7 For the given simple objective function f (⃗x) = x21 + x22 + x23 subject to x1 + x2 = 7
and x1 − x3 = 2, where ⃗x is a real vector.
s.t. x1 + x2 − 7 = 0 (1.29)
x1 − x3 − 2 = 0 (1.30)
% Initial guess
x0 = [4, 5, -2];
The results:
19
Example 1.8 Consider a firm that makes two kinds of chocolate: Type A and Type B. Only Milk
and Choco are required for both types. One unit of Milk and three units of Choco are required to
make one piece of Type A. To make a piece of Type B, however, we need one unit of Milk and
two units of Choco. The firm only has 5 Milk units and 12 Choco units. The factory’s profit is as
follows. Each piece of Type A is sold by $6, while each piece of Type B is sold by $5.
The firm tries to produce as many pieces of Type A and Type B to maximize its profit, but
the resources (i.e., Milk and Choco) are limited.
Each piece of Type A and Type B needs 1 unit of Milk. The total amount of Milk available
is 5 units. Thus, this constraint can be represented as x + y ≤ 5.
Each piece of Type A needs 3 units of Choco while each piece of Type B needs 2 units
of Choco. The total amount of Choco available is 12 units. Thus, this constraint can be
represented mathematically as 3x + 2y ≤ 12.
2. The factory’s goal is to increase earnings. How many units of Type A and
Type B should be produced in order to maximize profits?
% Initial guess
x0 = [0, 0];
20
% Solve the optimization problem
[x, fval, exitflag] = fmincon(fun, x0, A, b, Aeq, beq, lb, ub);
The results:
21
Example 1.9 A wireless transmitter communicates with a receiver over a single channel. The
achievable data rate follows Shannon’s formula. The objective is to determine the optimal trans-
mission power that maximizes the data rate subject to a maximum power constraint.
Given Parameters:
Channel coefficient: h = 2
Noise power: σ 2 = 1
Bandwidth: B = 1 MHz
Decision Variable:
p (transmission power in Watts)
General Shannon Capacity Formula:
C = B log2 (1 + SNR)
where h is the channel coefficient, x is the transmitted signal, n is the additive noise, and
σ 2 represents the noise power.
1. Formulate the following problem in the standard form.
|2|2 p
6
max 1 × 10 × log2 1+ (1.32)
p 1
s.t. 0≤p≤5 (1.33)
(1.34)
|2|2 p
6
max 1 × 10 × log2 1+ (1.35)
p 1
s.t. p≤5 (1.36)
p≥0 (1.37)
(1.38)
|2|2 p
6
min − 1 × 10 × log2 1 + (1.39)
p 1
s.t. p − 5 ≤ 0 (1.40)
−p≥0 (1.41)
(1.42)
Note: The objective function log2 (1 + 2p) is convex in p, and the constraints are convex.
Therefore, this is a convex optimization problem.
22
2. Solve the problem using ”fmincon”.
% Parameters
h = 2;
sigma2 = 1;
Pmax = 5;
% Initial point
p0 = 1;
% Display results
fprintf(’Optimal power: %.4f\n’, p_opt);
fprintf(’Maximum rate: %.4f bits/s\n’, -fval);
The results:
23
Example 1.10 A wireless transmitter communicates with a legitimate receiver in the presence of
an eavesdropper.
The transmitter wants to minimize its transmission power while guaranteeing a minimum se-
crecy rate. The secrecy rate is defined as the difference between the legitimate channel capacity
and the eavesdropper channel capacity.
Given Parameters:
Noise power: σ 2 = 1
Bandwidth: B = 1 Hz
Decision Variable:
|hb |2 p |he |2 p
Rs (p) = log2 1+ − log2 1+
σ2 σ2
min p (1.43)
p
|2|2 p |0.5|2 p
s.t. log2 1 + − log2 1 + ≥1 (1.44)
1 1
p≥0 (1.45)
min p (1.46)
p
24
2. Solve the problem using “fmincon”. Solution:
%Parameters
hb = 2;
he = 0.5;
sigma2 = 1;
Rmin = 1;
%Initial guess
p0 = [1];
%Display results
fprintf(’Optimal power: %.4f\n’, p_opt);
25