18
7/25/2019 Optimal control short course http://slidepdf.com/reader/full/optimal-control-short-course 1/18 Implementation of Dynamic programming MS Excel - Prof. Sanj Poo Aas

Optimal control short course

Embed Size (px)

Citation preview

Page 1: Optimal control short course

7/25/2019 Optimal control short course

http://slidepdf.com/reader/full/optimal-control-short-course 1/18

Implementation of

Dynamic programming MS Excel

- Prof. SanjPoo

Aas

Page 2: Optimal control short course

7/25/2019 Optimal control short course

http://slidepdf.com/reader/full/optimal-control-short-course 2/18

Purpose of the project

● Spreadsheet engines such as MS Excel are powerful tools for cformulae in cells. As soon as the value of a cell changes, othe

which depend on the altered cell change automatically to ref

new value. This feature can be used to demonstrate state of

dynamic programs as the input value changes.

● Implementing an algorithm in excel can be a tedious job, as i

writing long formulae in every cell. To solve this problem we create a tool (perl program) that converts any algorithm imp

perl to the corresponding excel representation.

● Further, macros in Excel have been employed to enhance inte

and allow the user to step through the states.

Page 3: Optimal control short course

7/25/2019 Optimal control short course

http://slidepdf.com/reader/full/optimal-control-short-course 3/18

Part I : Implementing Matrix-chamultiplication problem in Ms Ex

In Part I, we implemented a script which given the number of matmultiplied and an excel file containing the input data generates t

corresponding excel program.

Follow the instructions given in the documentation. For assistance

look at the following slides.

Matrix chain multiplication is an optimization problem that can busing dynamic programming. Given a sequence of matrices, the go

the most efficient way to multiply the matrices. The problem is not

 perform the multiplications, but merely to decide the sequence of t

multiplications involved.

Page 4: Optimal control short course

7/25/2019 Optimal control short course

http://slidepdf.com/reader/full/optimal-control-short-course 4/18

Example : Input for Part I

The input file contains dimensions of the matrices to be multiplie

Page 5: Optimal control short course

7/25/2019 Optimal control short course

http://slidepdf.com/reader/full/optimal-control-short-course 5/18

Example : Command in the term

Command:

perl matrixChainMult.pl <excel_filename> <number of matrices to be mu

Page 6: Optimal control short course

7/25/2019 Optimal control short course

http://slidepdf.com/reader/full/optimal-control-short-course 6/18

Example : Output for Part I

Each cell in the lower halfreflects the matrices

considered for multiplying,

whereas the corresponding

entries in the upper half

reflect the cost.

Page 7: Optimal control short course

7/25/2019 Optimal control short course

http://slidepdf.com/reader/full/optimal-control-short-course 7/18

Part II : Implementing Floyd-Waalgorithm in Ms Excel

In this part, we have implemented a script which given the dimenweights of the graph and an excel file containing the input data ge

corresponding excel program.

Follow the instructions given in the documentation. For assistance

look at the following slides.

The Floyd-Warshall algorithm is used for finding shortest paths bepair of vertices in a weighted graph. Worst case time complexity of

algorithm is O(|V |3) and space complexity is O(|V |2). 

Page 8: Optimal control short course

7/25/2019 Optimal control short course

http://slidepdf.com/reader/full/optimal-control-short-course 8/18

Example : Input for Part II

The input file contains the weights of the graph.

Page 9: Optimal control short course

7/25/2019 Optimal control short course

http://slidepdf.com/reader/full/optimal-control-short-course 9/18

Example : Command in the term

Command (without macro):

perl floydWarshall.pl <excel_filename> <number of vertices in the graph>

Command (with macro):perl floydWarshall.pl <excel_filename> <# of vertices> floydMacro.bin floy

Page 10: Optimal control short course

7/25/2019 Optimal control short course

http://slidepdf.com/reader/full/optimal-control-short-course 10/18

Example : Output for Part II

All the iterations of

the floyd-warshall

algorithm would be

displayed. Path

weight changes as

the iterations

proceed. This isreflected by red

font-colour.

Page 11: Optimal control short course

7/25/2019 Optimal control short course

http://slidepdf.com/reader/full/optimal-control-short-course 11/18

Example : Macro of the Part II o

Page 12: Optimal control short course

7/25/2019 Optimal control short course

http://slidepdf.com/reader/full/optimal-control-short-course 12/18

Part III:Transforming Input Perl

● Here we introduce the tool we implemented which can be use

algorithms implemented in perl to excel.

● The project, contains implementations of floyd warshall, gaus

elimination and the matrix multiplication problem. These serv

to demonstrate the use of the program.

The following slides, demonstrates the functioning of the progra

can be used.

Page 13: Optimal control short course

7/25/2019 Optimal control short course

http://slidepdf.com/reader/full/optimal-control-short-course 13/18

Part III: Flow of the Process

A: The input perl script we wish to transform

B: The output of perlToExcel.pl on the input, results in a script sa

C: The final excel file obtained by executing output.pl. At this sta

file can also be passed as an argument which will then be added t

generated excel file.

 A B

perlToExcel.pl

 Add a macro(optional)

Page 14: Optimal control short course

7/25/2019 Optimal control short course

http://slidepdf.com/reader/full/optimal-control-short-course 14/18

Part III: Commands on terminal

For first step:

● perl perlToExcel.pl <name of input filename> <name for outp

 e.g. perl perlToExcel.pl matrixChain.pl output.pl

For second step:

● perl <name of output script>. e.g. perl output.pl● perl <name of output script> <macro file> <helper file to plac

 e.g.perl output.pl matrixChainMacro.bin matrixChainMacroHelper.pl

 

After the second step, output.xlsx/output.xlsm is generated for u

Page 15: Optimal control short course

7/25/2019 Optimal control short course

http://slidepdf.com/reader/full/optimal-control-short-course 15/18

Part III: Example input

An example input file isshown here. The arrays used

in the main loop of thealgorithm are declared as

shown.

Page 16: Optimal control short course

7/25/2019 Optimal control short course

http://slidepdf.com/reader/full/optimal-control-short-course 16/18

Part III:Output without macro

All the arrays usedin the the program

are displayed inthe spreadsheet

Page 17: Optimal control short course

7/25/2019 Optimal control short course

http://slidepdf.com/reader/full/optimal-control-short-course 17/18

Part III: Macro in the output file

The macro is displayed here. It explains the workthe program in a controlled animation.

*The macro would be displayed in sheet-2 of the output excel

Page 18: Optimal control short course

7/25/2019 Optimal control short course

http://slidepdf.com/reader/full/optimal-control-short-course 18/18

Further work

Several improvements are possible in the work we have done. The

script in Part-III imposes some restrictions on the format of the inprogram. These restrictions include:

● Each array declared in the perl program must be followed wit

correct dimensions (e.g my @p; #dim:p[5][5])

● For array assignment steps, temporary variables can’t be used$m[$i][$j] = min($m[$i][$k] + $m[$k+1][$j]; is usable.

 my $temp = $m[$i][$k];my $ar[4][5] = $temp + $m[$k+1][$j]; is not usable.

● The value assigned to the array element is interpreted and th

references are transformed to corresponding excel cell addre

if the value contains complex formulae, they should be comp

excel.$path[$i][$j][$k] = IF($m[$i-1][$j][$k] > ($m[$i-1][$j][$i-1] + $m[$i-1][$i-1][$k]),$i,$pat

* ‘ ’