presentation optimization 2-libre
TRANSCRIPT
-
7/23/2019 Presentation Optimization 2-Libre
1/18
Institute of Transportation Engineers, Purdue Student Chapter engineering.purdue.edu/ITE
Introduction to Optimization Page 1 of 18
Introduction to Optimization in
M TL B
Updated October 2, 2010 Compiled by Amit Kumar and Sushant Sharma
Table of Contents
MATLAB BASICS ..................................................................................................................................................... 2
WHAT IS MATLAB? ...................................................................................................................................................... 2
USAGEOFMATLAB .................................................................................................................................................... 2
MATLAB GETTING STARTED ................................................................................................................................... 2
WORKINGWITHMATRICES: ...................................................................................................................................... 5
CREATING LOOPS ............................................................................................................................................................ 7For loop ................................................................................................................................................................. 7
While Loop ............................................................................................................................................................ 8
CREATING LOGICAL OPERATIONS ....................................................................................................................................... 8
RIDING THE TIGER .................................................................................................................................................. 9
HOW TO WRITE CODE THAT RUNS FASTER .......................................................................................................................... 9
Preallocation ......................................................................................................................................................... 9
Vectorization ....................................................................................................................................................... 10
Logical Indexing .................................................................................................................................................. 10
MATLAB PROGRAMMING .................................................................................................................................... 11
WRITEYOUROWNCODE ......................................................................................................................................... 11
WRITE SIMPLE M-FILES .................................................................................................................................................. 11
OPTIMIZATION TOOLBOX .................................................................................................................................... 13
LINEAR CONSTRAINED OPTIMIZATION PROBLEM ................................................................................................................ 13
PROBLEM FORMULATION ............................................................................................................................................... 14
NON-LINEAR OPTIMIZATION PROBLEM ............................................................................................................................ 15
PRACTICE EXERCISE .............................................................................................................................................. 17
PROBLEM 1: ................................................................................................................................................................ 17
-
7/23/2019 Presentation Optimization 2-Libre
2/18
Institute of Transportation Engineers, Purdue Student Chapter engineering.purdue.edu/ITE
Introduction to Optimization Page 2 of 18
MATLAB BASICS
What is MATLAB?
MATLAB is an interactive, matrix-based system for scientific and engineering numericcomputation and visualization. The word MATLAB stands for MATrix LABoratory. Each entry
is taken as a matrix in it, in particular scalar is considered as a 1 by 1 matrix. MATLAB is
available for a number of operating systems like Windows, Linux etc. MATLAB was originallywritten in FORTRAN and is licensed by The Math Works, Inc,
(http://www.mathworks.com).
USAGE OF MATLAB
MATLAB is well known software for numerical linear algebra and matrix computation. Industryuse it for research and to solve practical engineering and mathematical problems. Also, in
automatic control theory, statistics and digital signal processing (Time-Series Analysis) one can
use MATLAB. The following tool boxes make it useful in soft computing at various industrial
and scientific areas:(i) Neural Networks (ii) Optimization
(iii) Genetic Algorithms (iv) Wavelets
(v) Fuzzy Logic (vi) Control systems
(vi) Signal Processing
MATLAB GETTING STARTED
By clicking the MATLAB shortcut icon on the desktop of your computer (or selecting from the
program menu) you can access MATLAB. This results in getting MATLAB command window
with its prompt:
>>with a blinking cursor appearing right of the prompt, telling you that MATLAB is waiting to
perform a mathematical operation you would like to give.
http://www.mathworks.com/http://www.mathworks.com/http://www.mathworks.com/http://www.mathworks.com/ -
7/23/2019 Presentation Optimization 2-Libre
3/18
Institute of Transportation Engineers, Purdue Student Chapter engineering.purdue.edu/ITE
Introduction to Optimization Page 3 of 18
Simple Math Calculations
( i ) If you want to add two numbers say 7 & 12, type as follows
>> 5+10
and press the ENTER or return key, you see the following output:
ans =
15
Here, ans stands for the answer of computation. Similarly, the following gives product anddifference of these numbers,
>> 5*10
ans = 50
-
7/23/2019 Presentation Optimization 2-Libre
4/18
Institute of Transportation Engineers, Purdue Student Chapter engineering.purdue.edu/ITE
Introduction to Optimization Page 4 of 18
( ii ) If you want to store the values 5 and 10 in MATLAB variables a & b and store the values
of their product and division in c and d, do as follows:
>> a =5
a =5
>>b =10
b =
10
>>c= a*b
c =
50
>>d = 10/5 d =2
You can exit MATLAB with the command exit or quit. A computation can be stopped with
[ctrl-c]
-
7/23/2019 Presentation Optimization 2-Libre
5/18
Institute of Transportation Engineers, Purdue Student Chapter engineering.purdue.edu/ITE
Introduction to Optimization Page 5 of 18
The basic arithmetic operations are given by:
Operation Symbol
Addition
a+b
+
Subtractiona-b
-
Multiplicationa.b
*
Divisiona/b
/ or \
Exponentialab
^
WORKING WITH MATRICES:
MATLAB works with essentially only one kind of objects, i.e. a rectangular numerical matrix with possibly
complex entries. All variables represent matrices.
Scalars - 1 by 1 matrix
Row vector - matrix with one row
Column vector - matrix with one column.
If you want to store a matrix
1 2 3
4 5 6
7 8 9
you type the following in the MATLAB prompt.
>> a = [1 2 3; 4 5 6; 7 8 9]
a =
1 2 3
4 5 6
7 8 9
-
7/23/2019 Presentation Optimization 2-Libre
6/18
Institute of Transportation Engineers, Purdue Student Chapter engineering.purdue.edu/ITE
Introduction to Optimization Page 6 of 18
The rows are separated by semicolons and elements are separated by space or by comma. To transpose
a matrix and store in b we can run following command
>> b = a
b =
1 4 7
2 5 8
3 6 9
Matrix Operations available in MATLAB
Function Task
+ Addition
- Subtraction
* Multiplication
^ Power
Transpose
\ Left Division
/ Right Division
Examples matrix addition:
>> a1 = a+b (matrices a and bare added and stores in a1)
a1 =
2 6 10
6 10 14
10 14 18
Examples matrix multiplication:
-
7/23/2019 Presentation Optimization 2-Libre
7/18
Institute of Transportation Engineers, Purdue Student Chapter engineering.purdue.edu/ITE
Introduction to Optimization Page 7 of 18
>>a2= a * b
= 14 32 50
32 77 122
50 122 194
>>a3= a.*b
= 1 8 21
8 25 48
21 48 81
Creating loops
Similar to other programming language MATLAB also has two looping constructs: for loops and while
loops.
For loop
For loops are useful for repeating a process or a set of processes (or operations) a certain number of
times. Each for loop should have counter variable, starting value of the counter variable, increment
(default 1 if not specified) and upper limit of counter variable. In addition all for loops should be closed
by an end statement.
An example of for loop
fori=1:1:10disp(i);
end
Here, initial value of counter variable iis 1, increment is defined as 1 and upper limit is 10. Hence it willexecute the loop ten times. In each loop it will display the value of the counter variable. An another loop
can be nested inside a loop as below-
fori=1:1:10forj=1:1:3disp(i);
endend
Now this nested loop will execute the value of counter variable ithree times.
-
7/23/2019 Presentation Optimization 2-Libre
8/18
Institute of Transportation Engineers, Purdue Student Chapter engineering.purdue.edu/ITE
Introduction to Optimization Page 8 of 18
While Loop
While loops are useful for repeating a process or a set of processes (or operations) until a specified
condition(s) is (are) met. Following is the example of a while loop
i=1;while~(i==10)
disp(i);i=i+1;
end
In this example of while loop, the loop runs 9 times as opposed to for loop. The while loop first
evaluates the conditional statement and then executes the processes inside the loop if conditional
statement is true else the loop is terminated. So in this example as soon as the value of iwill turn 10 the
loop will be terminated.
So, each while loop needs a logical test and it runs while the answer to the logical test is valid. Hence
next we discuss how to create the logical statements.
Creating Logical Operations
The logical statements can be combination of variables, arithmetic operators and relational operators
and results in true or false. Following table shows the list of relational operators.
For more details visit the mathworks web page
http://www.mathworks.com/help/techdoc/matlab_prog/f0-40063.html
The local operations can be used by inbuilt function logical or under the if(else) statement. Following
example shows use of inbuilt function logical.
clear; %clear all variables
clc; %clear screena=[1 2 3 2 4 2 5];disp('values of vector "a" before');disp(a);b=logical(a==2);disp('after modifying "a" using logical operations');a(b)=99;disp(a);
Operator Description
= Greater than or equal to
== Equal to
~=Not equal to
http://www.mathworks.com/help/techdoc/matlab_prog/f0-40063.htmlhttp://www.mathworks.com/help/techdoc/matlab_prog/f0-40063.htmlhttp://www.mathworks.com/help/techdoc/matlab_prog/f0-40063.html -
7/23/2019 Presentation Optimization 2-Libre
9/18
Institute of Transportation Engineers, Purdue Student Chapter engineering.purdue.edu/ITE
Introduction to Optimization Page 9 of 18
Following is an example of using logical operation in an if-else statement-
clear; %clear all variablesclc; %clear screena=[1 2 3 2 4 2 5];disp('values of vector "a" before');
disp(a);m=length(a);fori=1:1:m
ifa(i)==2a(i)=999;
elsea(i)=0;
endenddisp('after modifying "a" using logical operations');disp(a);
RIDING THE TIGER
How to Write Code that Runs Faster
There are three important techniques to speed up the execution of MATLAB code namely,preallocation,
vectorizationand indexingby logical expression.
Preallocation
It means defining the size of a variable (matrix or vector) and giving some initial values. Run the
following two codes below see the difference in execution time-
Code with preallocation Code without preallocation%clc;clear;tic; %start of measuring timea=zeros(50000,1); %preallocationsum=0;fori=1:1:50000
a(i)=i;sum=sum+a(i);
enddisp('sum of 1 to 50,000 is =');
disp(sum);toc; %End of measuring time
clc;clear;tic; %start of measuring time%a=zeros(50000); % No preallocationsum=0;fori=1:1:50000
a(i)=i;sum=sum+a(i);
enddisp('sum of 1 to 50,000 is =');
disp(sum);toc;%End of measuring time
-
7/23/2019 Presentation Optimization 2-Libre
10/18
Institute of Transportation Engineers, Purdue Student Chapter engineering.purdue.edu/ITE
Introduction to Optimization Page 10 of 18
Vectorization
Vectorization gives faster performance compared to loop. Run the following codes to see the benefit of
vectorization.
Code with vectorization Code without vectorization
% Vectorization example for%elementwise multiplication%clc;clear;tic; %start of measuring timea=ones(50000,1);b=ones(50000,1);c=zeros(50000,1);b=b*2;%for i=1:1:50000
c=a.*b;s=sum(c);disp(s);
%endtoc;%End of measuring time
% No_Vectorization example for%elementwise multiplication%clc;clear;tic; %start of measuring timea=ones(50000,1);b=ones(50000,1);c=zeros(50000,1);b=b*2;s=0;fori=1:1:50000
c(i)=a(i)*b(i);s=s+c(i);
enddisp(s);toc;%End of measuring time
Logical Indexing
Indexing also gives faster performance compared to loop. Run the following codes to see the benefit of
indexing-
Code with indexing Code without indexing%clc;clear;tic; %start of measuring timea=1:50000;
b=logical(rem(a,2)==0);a(b)=0;
s=sum(a);disp('sum of odd integers between 1and 50,000 is =');disp(s);toc;%End of measuring time
clc;clear;
tic; %start of measuring timea=1:50000;fori=1:1:50000
ifrem(a(i),2)==0a(i)=0;end
ends=sum(a);disp('sum of odd integers between 1and 50,000 is =');disp(s);toc;%End of measuring time
-
7/23/2019 Presentation Optimization 2-Libre
11/18
Institute of Transportation Engineers, Purdue Student Chapter engineering.purdue.edu/ITE
Introduction to Optimization Page 11 of 18
MATLAB PROGRAMMINGWe can also do programming in MATLAB as we are doing in FORTRAN, C & C++. To make a file in
MATLAB e hae to lik o Ne i the file eu i euar. It ill ope a e file as e are doig
i word; ad if e sae this file alled -file, ill e saed i i folder of MATLAB.
Such files are alled M-files eause the hae a etesio of . i its fileae. Muh of our ork
with MATLABwill be creating and refining M-files.
There are two types of M-files: Script Files and Function Files.
WRITE YOUR OWN CODE
Write Simple m-files
A m-file consists of a sequence of normal MATLABstatements. If the file has the filename, say, test.m,
then the MATLAB command >> testwill cause the statements in the file to be executed. Variables in a
script file are global and will change the value of variables of the same name in the environment of the
current MATLAB session.
M files are often used to enter data into a large matrix; in such a file, entry errors can be easily edited
out.
Example:In a file test.m enter the following:
% This is a sample m-file
a = [4,2,3;0,1,2;1,2,3]
b =a;
c = a+b
d = inv(c)
save this file. Then to execute this file, type
>>test
-
7/23/2019 Presentation Optimization 2-Libre
12/18
Institute of Transportation Engineers, Purdue Student Chapter engineering.purdue.edu/ITE
Introduction to Optimization Page 12 of 18
The % symbol indicates that the rest of the line is a comment. MATLAB will ignore the rest of the line.
-
7/23/2019 Presentation Optimization 2-Libre
13/18
Institute of Transportation Engineers, Purdue Student Chapter engineering.purdue.edu/ITE
Introduction to Optimization Page 13 of 18
OPTIMIZATION TOOLBOX
Linear Constrained Optimization Problem
Example 1:
Apple manufactures multiple products including iphone and ipad. It has three manufacturing
bases. The products are assembled at assembling unit by the component parts arriving from
three manufacturing bases.
Following table describes the percentage of capacity available at the manufacturing bases due
to the other products being developed at same location. The table also shows percentage of
capacity required per million units production of iphone and ipad, in addition to the profit.
Table 1: Capacity of manufacturing base for iphone and ipad
Capacity Used (per million unit)
Manufacturing base iphone ipad Capacity Available
(per million unit)California 1 0 4
Illinois 0 2 12
Atlanta 3 2 18
Profit (in hundred million USD per
million unit)
3 5
-
7/23/2019 Presentation Optimization 2-Libre
14/18
Institute of Transportation Engineers, Purdue Student Chapter engineering.purdue.edu/ITE
Introduction to Optimization Page 14 of 18
Problem Formulation
Objective function:
x1= number of iphones (in million) and x2= number of ipads (in million)
Goal :To maximize profit (Z)
Or,
Decision variable:
Constraints: See Table 1 to understand the constraints
-
7/23/2019 Presentation Optimization 2-Libre
15/18
Institute of Transportation Engineers, Purdue Student Chapter engineering.purdue.edu/ITE
Introduction to Optimization Page 15 of 18
Non-Linear Optimization Problem
Create following M files
1.
Fun.m
functionz = fun(x)z = x(1)^2+x(2)^2+x(3)^2;end
-
7/23/2019 Presentation Optimization 2-Libre
16/18
Institute of Transportation Engineers, Purdue Student Chapter engineering.purdue.edu/ITE
Introduction to Optimization Page 16 of 18
2. Constr.m
%function[g, h] = constr(x)g(1) = x(3)/x(2)-1;%h = x(1)-x(2)^2+x(2)*x(3)-4;%%EOF
-
7/23/2019 Presentation Optimization 2-Libre
17/18
Institute of Transportation Engineers, Purdue Student Chapter engineering.purdue.edu/ITE
Introduction to Optimization Page 17 of 18
PRACTICE EXERCISE
Problem 1:
Write a MATLAB code for golden section method to solve the following optimization problem. You can
use the given flow chart of the method provided in this handout as a reference.
Given the initial interval of deisio ariale as , ad fial ofidee iteral to e ..
-
7/23/2019 Presentation Optimization 2-Libre
18/18
Institute of Transportation Engineers, Purdue Student Chapter engineering.purdue.edu/ITE
Introduction to Optimization Page 18 of 18
Figure: Flow chart of golden section method