66
RAS Software Architecture & Development methodology : (via the TSP Exercise) Melvin, Max, and Hon Wai (2008) based on notes from ShuaiCheng (2003) Chong Ket Fah and Ning Kang (2005) Chong Ket Fah, NingKang and Melvin (2006)

RAS Software Architecture & Development methodology : (via the TSP Exercise)

Embed Size (px)

DESCRIPTION

RAS Software Architecture & Development methodology : (via the TSP Exercise). Melvin, Max, and Hon Wai (2008) based on notes from ShuaiCheng (2003) Chong Ket Fah and Ning Kang (2005) Chong Ket Fah, NingKang and Melvin (2006). Outline. Overview and Purpose TSP problem - PowerPoint PPT Presentation

Citation preview

RAS Software Architecture & Development methodology : (via the TSP Exercise)

Melvin, Max, and Hon Wai (2008)

based on notes from ShuaiCheng (2003) Chong Ket Fah and Ning Kang (2005) Chong Ket Fah, NingKang and Melvin (2006)

Outline

Overview and Purpose TSP problem TSP System Architecture Data & Parameter files Exercises Program & Coding Standard Using SVN and Getting Started

Overview and Purpose

Understand RAS Software Methodology (as defined in the RAS-handbook)

Using TSP as an example to show methodology at work

Principles behind the RAS Architecture

Allow seamless integration of multiple algorithms (solvers)

Allow independent, concurrent development by several students

Support fast, automated evaluation of several algorithms

Principles behind the RAS Architecture (continued…)

Common, consistent software style and standards enhance code understanding and

maintainability

Enhance reusability across multiple batches of students

Outline

Overview and Purpose TSP problem TSP System Architecture Data & Parameter files Exercises Program & Coding Standard Using CVS

Traveling Salesman Problem

Given the positions of N cities, what is the shortest closed tour in which each city is visited exactly once?

Problem is NP-complete

Some References: http://www.tsp.gatech.edu//index.html http://itp.nat.uni-magdeburg.de/~mertens/TSP/TSP.html

Algorithms For TSP

Nearest Neighbour Heuristic Closest Insertion Heuristics 2-opt Exchange Heuristics Many others too: (for your ref…)

2.0 Approximation Algorithm Furthest Insertion Heuristic Edge Insertion Heuristic Simulated Annealing

Nearest Neighbour heuristic

Start with any vertex v While (not all the vertices are visited) do

Select unvisited vertex u that is nearest to v; Add edge (v,u) to T; v u;

Animation: (nearest neighbour heuristics) http://itp.nat.uni-magdeburg.de/~mertens/TSP/TSP.html

Closest Insertion heuristic

Start with a partial tour T of a single vertex v While (not all the vertices are in the tour)

Select an unused vertex u such that the distance from u to a vertex v in T is minimum

Insert u into the tour T (by connecting v to u)

Animation: (Cheapest Insertion from Convex Hull) http://itp.nat.uni-magdeburg.de/~mertens/TSP/TSP.html

Question: What initial tour T to start with? Note: There are other variations too…

2-opt Exchange Heuristics

Start with a any TSP tour T ; While (there is improvement) do

“Exchange” two edges e1 and e2 in tour T to decrease the length of the tour

Animation: (2-opt exchange heuristics) http://itp.nat.uni-magdeburg.de/~mertens/TSP/TSP.html

Note: O(n2) pairs of edges to consider each iteration

2 Approximation Algorithm

Algorithm Overview Construct the minimal spanning tree Duplicate all its edges. This gives us an

Euler cycle. Traverse the Euler cycle, skipping

already visited nodes.

2.0 Approximation Algorithm

Furthest Insertion heuristic

Start with a partial tour T of a single vertex v While (not all the vertices are in the tour)

Select an unused vertex u such that the minimum distance from u to a vertex v in T is maximum

Insert u into T such that the increase in the length of the tour is minimum

Edge Insertion Heuristic

Sort the edges (in increasing edge weights) T { }; Scan edge e (in increasing cost) and add

edge e to T only if T {e} does not form a cycle (unless |T|=n). does not have vertex of degree three or more

Simulated annealing

S = random tour t = some large number // initial “temperature” repeat

randomly swap two edges of the tour S to obtain S’ change = cost(S) – cost(S’) if change > 0 then S = S’ else x := random (0,1); if (x < exp(-change/T)) then S = S’ T = a * T; // choose a between 0.8 and 0.99

until stopping condition

Outline

Overview and Purpose TSP problem TSP System Architecture Data & Parameter files Exercises Program & Coding Standard Using CVS

RAS Software Design

TSP Software architecture diagram TSP Class diagram File formats and related issues:

TSP project file, parameter files TSP Input/output data formats use the RAS-standards, unless std exists

Sample files

RAS Software Architecture Template

Principles behind the RAS Architecture

Separate domain classes (package) from algorithms classes (solvers)

Separate engine from GUI

Separate datafiles/database from domain and algorithms classes

High Level TSP System Architecture

Library Components

TSP Classes

TSP PackageTSP Solver

TSPSolver

Components

User Interface

TSP System Design

TSPCISolver

TSP Solver

TSPPackage

GUIModule

TSPEngine

TSPNNSolver

TSP2OptSolver

Data Sources

TSP Package

Encode the TSP Domain objects Provides important interface methods Entire package is sent to solvers Solvers updates info inside package Package read from / write to DB

TSP Solver Class(es)

Suppose there is only 1 algorithm One solver class is enough What if there are several algorithms?

Our TSP Solver Class

TSP Solver Class is a “generic” solver It is used to “call” different algorithms

Each algorithm is Implemented as a different solver class Done by different people But uses the standard TSP Package

Then, integrate into the generic TSP Solver class

TSP Package Class

CISolver

TSP Solver

TSPPackage

GUIModule

TSPEngine

NNSolver 2-OptSolverReadProjFile() –

interface to initiate reading of data and parameters files as specified in project file

Parameter Files

Data Sources

Project File

Show the TSPPackage.h file

TSP System Architecture -Interface

CISolver

TSP Solver

TSPPackage

GUIModule

TSPEngine

NNSolver 2-OptSolver

Input -

TSPSolver::TSPSolver(TSPPackage& aPackage)

Interface -

TSPPackage::GetSolverVersion()

TSPPackage::GetCostRepOpt()

TSPPackage::GetCostMatrix()

TSP Solver Class

CISolver

TSP Solver

TSPPackage

GUIModule

TSPEngine

NNSolver 2-OptSolver

Solve() –

Determines algo to use, and instantiate and run algo (call Solve() function in particular algo).

WriteSolutionFile() –

Write solution in file which can be read by GUI Module and the results displayed

Show the TSPSolver.h file

TSPSolver calling different Solvers

CISolver

TSP Solver

TSPPackage

GUIModule

TSPEngine

NNSolver 2-OptSolver

Input –

TSPNNSolver::TSPNNSolver

(TSPPackage& aPackage,

vector<int>* aSolution)

Interface –

Specific solver read data and internal representation parameters from interface functions in TSPPackage

Running with different Solvers

CISolver

TSP Solver

TSPPackage

GUIModule

TSPEngine

NNSolver 2-OptSolver

solve() –

Run specific algorithm that solver represents and store result in solution representation (aSolution)

Show the TSPSolver.cc file

TSP Solver Class

…void TSPSolver::Solve() // the generic solve() method{ if (mPackage.GetSolverVersion()=="CIG") {… TSPCIGSolver solver(mPackage, mSolution); solver.Solve(); } else if (mPackage.GetSolverVersion()=="NNG") { … TSPNNGSolver solver(mPackage, mSolution); solver.Solve(); } else if (mPackage.GetSolverVersion()=="2OPTM") { … }}

Main Program

#include "TSPPackage.h“ // include important stuff used#include "TSPSolver.h"int main(int argc, char* argv[]){ if (argc!=2) // CHECK command-line arguments!! {…} …. TSPPackage myTSPPackage(ProjectFile); // Initializes the TSPPackage

TSPSolver myTSPSolver(myTSPPackage); // Initializes the Solver

myTSPSolver.Solve(); // Invokes Solve() method and

…. // Update solution to TSPPackage return 0; }

Outline

Overview and Purpose TSP problem TSP System Architecture Data & Parameter files Exercises Program & Coding Standard Using CVS

Data & Parameter files

Data File – city.dat Parameter File – default.para Solution File – solution000.dat Other files:

Summary file Trace files

Sample city.dat file

_NUM_CITIES27_CITY_LOCATION0 14.0 27.01 2.0 31.02 35.0 30.03 11.0 17.04 0.0 7.05 39.0 29.06 16.0 29.07 3.0 49.08 40.0 40.09 26.0 32.010 29.0 32.011 14.0 15.012 11.0 23.013 25.0 38.014 23.0 40.015 43.0 37.016 18.0 46.017 19.0 5.0..._END

Parameter File

_COST_REPRENTATION_FORMAT#0 means use a graph#1 means use a mattix0_SOLVER_VERSIONNNG_END

Solution File

_SOLUTION0 6 26 19 9 18 10 2 5 15 8 13 14 16 25 23 24 1 27 12 3 11 21 4 17 20 22 7 _COST263.852

Outline

Overview and Purpose TSP problem TSP System Architecture Data & Parameter files Exercises Program & Coding Standard Using CVS

Exercises

Write your own new TSPSolver Choose any algorithm you like Some possibilites:

Further insertion, simulated annealing, your own heuristics

Integrate your TSPSolver Need to modify “some parts of the code” Create new project and parameter files

Exercises (optional…)

To help clean up the TSP code… Architecture

Modify TSPPackage and TSPSolver Write the solution into TSPPackage class Move the function of writing the solution to the output file

from the TSPSolver class to the TSPPackage class Make all solvers to be subclass of an abstract

TSPSolver class [done?]

Rectify issue of graph rep (Matrix vs Adj list) Others?

Outline

Overview and Purpose TSP problem TSP System Architecture Data & Parameter files Exercises Program & Coding Standard Using CVS

Coding Standard

See other ppt file (by David Ong)

To be updated… All names must be alpha-numeric strings. All

characters are in lower case, with uppercase being used to separate a multi-word variable. int myLocalVariable, callMyFunction, CallAFunction

Class name begin with an upper case Class Variable start with m (stand for member)

Coding Standard

See other ppt file (by David Ong)

To be updated… Function argument are prefixed with ‘a’

Eg: fun(int aTime) Avoid global variables

If really necessary, start with Global Eg: GlobalVariable

Outline

Overview and Purpose TSP problem TSP System Architecture Data & Parameter files Exercises Program & Coding Standard Using SVN

Using SVN

SVN (Subversion)is a version control system Intended as a replacement for CVS Enables multiple developers to work on the same project Keep track of changes to files and allows developers to

rollback to a previous version Basic concepts

Checking out a repository Updating your working copy Adding/removing files to/from the repository Committing changes to the repository

Using SVN (II)

Checking out a repository Browse to a folder that you want to store the

branch (e.g. TSP/branches/) $ svn co <repository_name> $ svn co

http://ras-5.ddns.comp.nus.edu.sg/svn/TSP/branches/TSP-<yourunixname>

Updating your working copy $ svn update Recursively updates the current directory and

all subdirectories

Using SVN (III)

Adding/removing files to/from the repository Create the file in your working copy $ svn add <filename> Delete the file from your working copy $ svn remove <filename>

Using SVN (IV)

Committing changes to the repository No changes are made to the repository

until a commit command is given $ svn commit Recursively commits changes in the

current directory and all subdirectories

Getting Started

To compile your project $ ./compile To run your project $ ./run <proj> $ ./run project000.prj

Getting Started

You will have to create two files, your TSP<your initials>Solver.h and the cpp file

Create them in src/ E.g. my solver would be called TSPTHYSolver

Getting Started

There are already 3 working solvers in the src/ folder, you may wish to reference them as an example

TSP2OPTMSolver.cpp TSPCIGSolver.cpp TSPNNGSolver.cpp

Getting Started

TSPPXRSolver TSPDNHLSolver TSPDKHSolver TSPVDTSolver TSPZZYSolver TSPOCPSolver TSPSSSolver

Getting Started

There are some parts of code you need to change to call your own solver method

include/TSPSolver.h src/TSPSolver.cpp src/makefile

Getting Started

include/TSPSolver.h http://ras-5.ddns.comp.nus.edu.sg/projects/TSP/browser/trunk/include/TSPSolver.h

Getting Started

src/TSPSolver.cpp http://ras-5.ddns.comp.nus.edu.sg/projects/TSP/browser/trunk/src/TSPSolver.cpp

Use your initials

Getting Started

src/makefile http://ras-5.ddns.comp.nus.edu.sg/projects/TSP/browser/trunk/src/makefile

Add your solver here

Getting Started

src/makefile http://ras-5.ddns.comp.nus.edu.sg/projects/TSP/browser/trunk/src/makefile

Add your solver here

Testing your solver

The software reads in a project as an input (project/projectxxx.prj)

Each project specifies a one of the following Data set to use (database/dxxx/) Data format (0: adjacency list, 1: matrix)

Data d0xx has format 0 Data d9xx has format 1

Testing your solver

Each project specifies a one of the following The file name of the city

(database/dxxx/filename) The file name which specify weights for

edges, if not specified, defaults to equal weights (database/dxx/filename)

Parameter file Output solution file

Testing your solver

To test your solver, create a new parameter file in the param/ folder

Testing your solver

Next, create a new project file in the project/ folder

Change the parameter file name to the param file that you have created

Demonstration

Demo of using svn on ras-5.ddns

The End