r0737973 - Lab Session Unconstrained & Constrained Optimization
r0737973 - Lab Session Unconstrained & Constrained Optimization
Lightweight Design
Lab 1 – Sizing optimization of the cantilever beam
List of figures
Figure 1 - Drawing of the cantilever beam, from the assignment paper. ................................ 2
Figure 2 - Typical notation of an optimization problem (Martins & Ning, 2020). ..................... 2
Figure 3 - Gradient and eigenvalues of the Hessian of the final design point. ........................ 5
Figure 4 ................................................................................................................................. 5
Figure 5 - Weak minima. ....................................................................................................... 5
Figure 6 - Error message associated with incorrect design point. .......................................... 5
Figure 7 - Design space in 3D, t and w are fixed. .................................................................. 6
Figure 8 - Contour plot of the (active) deflection constraint, and the gradients. ...................... 6
Figure 10 - The gradient can be seen to be much better. ...................................................... 7
Figure 9 - Enlarged design space, yielding better values. ...................................................... 7
Figure 11 - 3D visualization of the incorrect final design point, t and w are again fixed. ......... 7
List of tables
Table 1 - Results for both using one design point and LHS. .................................................. 4
Table 2 - Second, incorrect result obtained from the LHS method. ........................................ 5
Table 3 - Results for the global search. ................................................................................. 6
1
1 PROBLEM FORMULATION
Given an I-beam subject to a load as shown in Figure 1. Our task is to minimize the mass of
the beam by finding the best values for the beam cross sectional dimensions while still obeying
the given constraints. To fill in the blanks in the required values, I will assume the steel is from
the series S235. The fixed parameters are:
F = 10 kN; L = 3000 mm; E = 210.000 N/mm²; ρ = 7.85 ∗ 10−6 kg/mm³;
The design variables are free to be optimized between the given bounds:
10 ≤ 𝑙, 𝑏 ≤ 250 [𝑚𝑚]; 1 ≤ 𝑡, 𝑤 ≤ 50 [𝑚𝑚];
While obeying the given constraints:
𝛿𝑚𝑎𝑥 = 5 𝑚𝑚; 𝜎𝑚𝑎𝑥 = 235 𝑁/𝑚𝑚²
(motivated by personal preference, I changed the units from m to mm)
With 𝑥
⃗⃗⃗ (= x in figure 2) being the design vector that represents one specific design in the model
space. In this case, 𝑥 ⃗⃗⃗ consists of four variables:
⃗⃗⃗ = [𝑙, 𝑏, 𝑡, 𝑤] = [𝑥1 , 𝑥2 , 𝑥3 , 𝑥4 ]
𝑥 (1.1)
2
1.1 Derivation of the functions
I choose to neglect the shear stress at the clamping caused by the transverse force 𝐹𝑦 , as its
influence will be negligible. The formula describing the maximum stress is then:
𝑀 𝑙
𝑔2 (𝑥 ) = 𝜎(𝑥 ) = 𝐼 𝑏 ∗ 2 ≤ 𝜎𝑚𝑎𝑥 = 235 [𝑁/𝑚𝑚²] (1.6)
𝑥𝑥
It is important to note that the applied constraint on stress is defined as the tensile stress at
the top of the beam, specifically at the location of clamping into the wall. In this scenario, this
tensile stress represents the maximum total stress in the beam. However, in cases where
additional loads, such as torsional moments, are applied on top of the normal load, the
constraint should be defined using the Von Mises stress. This is because the maximum stress
may no longer occur at the clamping point.
For simplicity, let’s isolate the derivation of 𝐼𝑥𝑥 . We can see the cross-section as a combination
of three rectangles, allowing us to apply Steiner's theorem:
𝑡(𝑙−2𝑤)3 𝑏𝑤 3 𝑙−𝑤 2
𝐼𝑥𝑥 (𝑥 ) = ∑ 𝐼𝑖 + ∑ 𝐴𝑖 ∗ 𝑒𝑖2 = 12
+2[ 12
+ 𝑏𝑤 ∗ ( 2
) ] [𝑚𝑚4 ] (1.7)
Implementing this in both 𝑔1 (𝑥) and 𝑔2 (𝑥 ), and transforming the inequality constraints so that
they comply with the expected form, defined in Figure 2.
−1
𝐹𝐿3 𝑡(𝑙−2𝑤)³ 𝑏𝑤 3 𝑙−𝑤 2
𝑔1 (𝑥 ) = [ 12 + 2[ + 𝑏𝑤 ∗ ( ) ]] − 𝛿𝑚𝑎𝑥 ≤ 0 (1.8)
3 12 2
−1
𝑙 𝑡(𝑙−2𝑤)3 𝑏𝑤 3 𝑙−𝑤 2
𝑔2 (𝑥 ) = M𝑏 ∗ 2 [ 12
+2[ 12
+ 𝑏𝑤 ∗ ( 2
) ] ] − 𝜎𝑚𝑎𝑥 ≤ 0 (1.9)
3
1.1.3 Bounded constraints
These were already defined earlier in the problem formulation, as previously discussed.
10 ≤ 𝑙, 𝑏 ≤ 250 [𝑚𝑚] (1.10)
1 ≤ 𝑡, 𝑤 ≤ 50 [𝑚𝑚] (1.11)
The algorithm applied is 'fmincon', which utilizes an interior penalty method, specifically
'interior-point' (MATLAB Help Center - fmincon, sd). It transforms the constrained problem into
an unconstrained one by creating a barrier in the function 𝑓(𝑥 ) at the location where the
gradient of the constraint function 𝑔j (𝑥 ) would become zero. The ‘interior’ in its name means
that the optimized design point must remain within the feasible design region (Martins & Ning,
2020).
Our task is to find an optimum using three different approaches, which are:
1. Choose an appropriate initial design point, optimize from there on;
2. Use the Latin Hypercube Sampling (LHS) method to generate 50 different design points,
increasing the likelihood of finding a better optimum;
3. Employ a global search function.
When choosing an initial point, I make sure that it is in the feasible design region, although the
interior point algorithm should work for infeasible starting points. For simplicity, I choose the
𝑥0 = [250, 250, 50, 50]. The optimum obtained from this point is the same as the
starting point ⃗⃗⃗⃗
one obtained using LHS:
Table 1 - Results for both using one design point and LHS.
From the design results, it is evident that the deflection constraint serves as the active
constraint, dictating the minimal design point, while the stress constraint remains inactive.
Since the deflection constraint reaches its limit, being zero, the design is indeed optimized.
But the design variables end up at the edge of the feasible design space, suggesting that a
real optimum has not been attained. For a design point to be considered a local minimum, it
should satisfy the sufficient optimality conditions: having a gradient of zero and a Hessian that
is positive-definite (its eigenvalues should be greater than zero).
When operating within a constrained design space, an optimum can be reached when the
gradients of the constraint function and the objective function are aligned, as a true local
minimum might not be feasible due to the imposed constraints. To illustrate this concept, we
can utilize the derivation of the Lagrange multiplier method for equality constraint optimization,
as an inequality constraint behaves similarly at its boundary. Given the newly created
Lagrange function for multiple constraints:
𝑗
Λ(𝑥1 , … , 𝑥𝑖 , 𝜆1 , … , 𝜆𝑗 ) = 𝑓(𝑥1 , … , 𝑥𝑖 ) − ∑𝑘=1 𝜆𝑘 ∙ 𝑔(𝑥1 , … , 𝑥𝑖 ) (2.1)
4
Suppose there is only one active constraint, as is the case in this exercise. We can simplify
the equation to:
Λ(𝑥1 , … , 𝑥𝑖 , λ) = 𝑓(𝑥1 , … , 𝑥𝑖 ) − 𝜆 ∙ 𝑔(𝑥1 , … , 𝑥𝑖 ) (2.2)
For a point of the Lagrange function to be considered as an optimum, its derivative it should
equal zero, giving:
⃗ Λ(𝑥1 , … , 𝑥𝑖 , λ) = 0 ⟺ ∇
∇ ⃗ 𝑓(𝑥1 , … , 𝑥𝑖 ) − λ ∙ ∇
⃗ 𝑔(𝑥1 , … , 𝑥𝑖 ) = 0 (2.3)
⃗ 𝑓(𝑥1 , … , 𝑥𝑖 ) = λ ∙ ∇
∇ ⃗ 𝑔(𝑥1 , … , 𝑥𝑖 ) (2.4)
Figure 3 - Gradient and eigenvalues of the Hessian of the final design point. Figure 4
Most of the starting points from LHS ended up at this optimum, but some of the optimized
designs using LHS yield other results, shown in Table 2. These resulting dimensions are
physically impossible, rendering this design meaningless. Additionally, the deflection
constraint is not satisfied. When obtaining this result, MATLAB provides the response shown
in Figure 6. MATLAB suggests using 'feasibility mode' to resolve the problem. However, this is
unnecessary because the LHS method is already applied, and the problem is successfully
tackled.
Table 2 - Second, incorrect result obtained from the LHS method.
5
When a global minimum is searched using ‘GlobalSearch’ and ‘createOptimProblem’, the
results shown in Table 3 are again physically meaningless, although mathematically the mass
is indeed minimized. This issue could be solved by defining additional constraints on the
dimensions of the beam, ensuring the discovery of a feasible solution.
Table 3 - Results for the global search.
Figure 7 - Design space in 3D, t and w are fixed. Figure 8 - Contour plot of the (active)
deflection constraint, and the gradients.
As observed from the contour lines shown in Figure 8, the optimizer reaches this barrier.
However, the gradients do not align with each other, contrary to expectations from the previous
chapter. The obtained design point is, in fact, the only feasible design point; all other values
for 𝑥 result in 𝑔1 (𝑥 ) values higher than zero, violating the constraint condition.
When the bounded constraint for ‘l’ is loosened to [10, 2500] and ‘b’ to [1, 250], the gradients
can then be observed to align with each other. Although this design is not permitted and is
subject to various physical issues, such as being highly susceptible to buckling, it represents
a mathematically superior result.
6
Figure 10 - Enlarged design space, yielding better values.
This process can be repeated for different fixed variables to examine the design space for the
other free variables. The results should be somewhat consistent.
In addition, the incorrect results of the LHS approach can also be plotted in 3D, giving a visual
explanation to the error.
7
3 APPENDIX
E=210000;
rho = 0.000007850;
L=3000;
F = 10000;
f = rho*L*((x1-2*x4)*x3+2*x2*x4);
func = matlabFunction(f,'vars', {[x1, x2, x3, x4]});
%{
%===============VERSION ONE -CHOOSE A STARTING POINT YOURSELF
x0=[250, 250, 50, 50]; % initial design point
[xstar, fstar, ~,~,lambda,grad,hessian] = ...
fmincon(func,x0,A,b_0,Aeq,beq,lb,ub, @con);
opt = fstar
values = xstar
%===============
%}
8
for i = 1:50
[xstar, fstar, ~,~,lambda,gradient,hessian] = ...
fmincon(func,points_scaled(i, :),A,b_0,Aeq,beq,lb,ub, @con);
xstar_tot(i,:) = xstar;
fstar_tot(i) = fstar;
end
opt = min(fstar_tot)
opt_index = find(fstar_tot == opt);
values = xstar_tot(opt_index,:)
[~,eigenvalues] = eig(hessian)
gradient
%===============
%{
%===============VERSION THREE - GLOBAL SEARCH
x0=[25, 25, 5, 5]; % initial design point
gs = GlobalSearch;
problem = createOptimProblem('fmincon', 'x0', x0, 'objective',func, ...
'lb',lb,'ub',ub,'nonlcon',@con);
[xstar, fstar] = run(gs,problem);
opt = fstar
values = xstar
%===============
%}
9
B Code for visualization in three-dimensional space
clc; clear; close all;
% Define Colorpalette in rgb
clp = [0.79, 0.17, 0.57;...
0.0, 0.29, 0.29;...
0.99, 0.078, 0.44];
E=210000;
rho = 0.000007850;
L=3000;
F = 10000;
f = rho*L*((x1-2*x4)*x3+2*x2*x4);
func = matlabFunction(f,'vars', {[x1, x2]});
x0=[50, 50]; % initial design point --> l en b
xstar
fstar
hessian
[eigvect,eigval] = eig(hessian)
plot_constr(f,fstar,xstar,clp,lb,ub,grad)
%%
function [cineq,ceq]=con(x)
E=210000;
L=3000;
F = 10000;
ceq =[];
cineq =[((F*L^3)/(3*E)*((t*(l-2*w)^3)/12+((b*w^3)/12+b*w*((l-w)/2)^2)*2)^(-1)-
5);
((F*L*l)/(2)*((t*(l-2*w)^3)/12+((b*w^3)/12+b*w*((l-w)/2)^2)*2)^(-1)-235)
];
end
10
%%
function plot_constr(f,fstar,xstar,clp,lb,ub,grad)
%{
Inputs: symbolic function, f
optimum value, fstar
optimum point, xstar
color palette, clp
side constraints, lb, ub
gradient of the objective, grad
%}
t = 1;
w = 11.9551;
E=210000;
rho = 0.000007850;
L=3000;
F = 10000;
% Define the (in)equality constraint as function handle
%stiffness constraint
%cineq_1 =@(x1,x2) ((F*L^3)/(3*E)*((t*(x1-2*w).^3)./12+((x2.*w^3)/12+x2*w*((x1-
w)./2).^2).*2).^(-1)-5);
cineq_1 =@(x1,x2) ((F.*L.^3)./(3.*E).*((t.*(x1-
2.*w).^3)./12+((x2.*w.^3)./12+x2.*w.*((x1-w)./2).^2).*2).^(-1)-5);
%strength constraint
cineq_2 =@(x1,x2) ((F.*L.*x1)./(2).*((t.*(x1-
2.*w).^3)./12+((x2.*w^3)./12+x2.*w.*((x1-w)./2).^2).*2).^(-1)-235);
x1_range = linspace(lb(1), ub(1), np); % Adjust the number of points np for finer
resolution
x2_range = linspace(lb(2), ub(2), np); % Adjust the number of points np for finer
resolution
figure('color','w');
%plotting the minimization function
ss = ezsurf(f,[lb(1),ub(1)],[lb(2),ub(2)]); ... Plot the function
set(ss ,'FaceColor','b','FaceAlpha',.5,'EdgeColor','k','EdgeAlpha',.8); hold
on;
surf(X1,X2,cineq_1(X1,X2),'FaceColor',clp(2,:),'EdgeColor','k','EdgeAlpha',.5);
hold on; % plot constraint 1
surf(X1,X2,cineq_2(X1,X2),'FaceColor',clp(3,:),'EdgeColor','k','EdgeAlpha',.5);
hold on; % plot constraint 1
11
%plotting the optimum
scatter3(xstar(1),xstar(2),fstar,'o','SizeData',40,'MarkerFaceColor','y','MarkerEd
geColor','k'); hold on;
grid on; grid minor;
title(['$ f(x_1,x_2) = ', latex(f),'$'],'interpreter','latex');
xlabel('$x_1$','interpreter','latex');
ylabel('$x_2$','interpreter','latex');
legend({'$f_{(x_1,x_2)}$','doorbuiging',
'spanning'},'interpreter','latex','box','off')
figure('color','w');
%stiffness constraint
ind = cineq_1(X1,X2)<=0; % find points that cineq_1<=0
X1d = X1(ind); X2d= X2(ind);
x1_range = linspace(min(X1d), max(X1d), np); % Adjust the number of points np for
finer resolution
x2_range = linspace(min(X2d), max(X2d), np); % Adjust the number of points np for
finer resolution
12
%bereken hoogte voor een infinitecimale stap met grootte sqrt(eps)
h_current_dx1 = cineq_1(xstar(1)+sqrt(eps),xstar(2));
h_current_dx2 = cineq_1(xstar(1),xstar(2)+sqrt(eps));
%verschil tussen beginpositie en stap geeft gradiëntbenadering
grad_h = [h_current_dx1-h_current, h_current_dx2-h_current]./sqrt(eps);
%./sqrt(eps)? --> definitie afgeleide navolgen
grad_h = grad_h./norm(grad_h);
quiver(xstar(1),xstar(2),grad_h(1)*50,grad_h(2)*50,'color',clp(2,:),'linewidth',1.
5,...
'AutoScaleFactor',.6); hold on;
scatter(xstar(1),xstar(2),'MarkerFaceColor','y','SizeData',40,'MarkerEdgeColor','k
'); % plot optimum
legend({'$h_{1(x_1,x_2)} \leq 0$','$ \nabla f({x}^{*}) $',...
'$ \nabla h({x}^{*}) $',['$ x^{*} = [ ',num2str(xstar(1)),', \
',num2str(xstar(2)),']$']},'interpreter','latex','box','off',
Location='northwest');
grid on; grid minor;
xlabel('$x_1$','interpreter','latex');
ylabel('$x_2$','interpreter','latex');
figure('color','w');
ind = cineq_2(X1,X2)<=0; % find points that cineq_2<=0
X1d = X1(ind); X2d= X2(ind);
x1_range = linspace(min(X1d), max(X1d), np); % Adjust the number of points np for
finer resolution
x2_range = linspace(min(X2d), max(X2d), np); % Adjust the number of points np for
finer resolution
[X1d, X2d]=meshgrid(x1_range, x2_range);
contourf(X1d, X2d, cineq_2(X1d,X2d), [-100:10:10],...
'ShowText','on','linecolor',clp(2,:),'fill','off'); hold on; % plot contours
of g1
% The range [-100:10:10] defines contours from the value -100 with step 10
% until 10; change accrodingly
colormap(bone);
% plot gradient of the objective
quiver(xstar(1),xstar(2),grad(1),grad(2),'color','b','linewidth',.5,...
'AutoScaleFactor',.6); hold on;
13
4 REFERENCES
14
FACULTY OF ENGINEERING TECHNOLOGY
DE NAYER (SINT-KATELIJNE-WAVER) CAMPUS
Jan De Nayerlaan 5
2860 SINT-KATELIJNE-WAVER, België
tel. + 32 16 30 10 30
[email protected]
www.fet.kuleuven.be