670 likes | 820 Views
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
E N D
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) • Question: What initial tour T to start with? • Note: There are other variations too… Animation: (Cheapest Insertion from Convex Hull) http://itp.nat.uni-magdeburg.de/~mertens/TSP/TSP.html
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 Note: O(n2) pairs of edges to consider each iteration Animation: (2-opt exchange heuristics) http://itp.nat.uni-magdeburg.de/~mertens/TSP/TSP.html
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.
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)) thenS = 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
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
TSP Solver TSP Package TSP Solver Components TSP Classes High Level TSP System Architecture User Interface Library Components
TSP System Design TSP Engine TSP Package Data Sources GUI Module TSP Solver TSPCI Solver TSPNN Solver TSP2OptSolver
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 Engine TSP Package GUI Module TSP Solver CISolver NNSolver 2-OptSolver TSP Package Class Data Sources Parameter Files Project File ReadProjFile() – interface to initiate reading of data and parameters files as specified in project file
TSP Engine TSP Package GUI Module TSP Solver CISolver NNSolver 2-OptSolver TSP System Architecture -Interface Input - TSPSolver::TSPSolver(TSPPackage& aPackage) Interface - TSPPackage::GetSolverVersion() TSPPackage::GetCostRepOpt() TSPPackage::GetCostMatrix() …
TSP Engine TSP Package GUI Module TSP Solver CISolver NNSolver 2-OptSolver TSP Solver Class 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
TSP Engine TSP Package GUI Module TSP Solver CISolver NNSolver 2-OptSolver TSPSolver calling different Solvers Input – TSPNNSolver::TSPNNSolver (TSPPackage& aPackage, vector<int>* aSolution) Interface – Specific solver read data and internal representation parameters from interface functions in TSPPackage
TSP Engine TSP Package GUI Module TSP Solver CISolver NNSolver 2-OptSolver Running with different Solvers solve() – Run specific algorithm that solver represents and store result in solution representation (aSolution)
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_CITIES 27 _CITY_LOCATION 0 14.0 27.0 1 2.0 31.0 2 35.0 30.0 3 11.0 17.0 4 0.0 7.0 5 39.0 29.0 6 16.0 29.0 7 3.0 49.0 8 40.0 40.0 9 26.0 32.0 10 29.0 32.0 11 14.0 15.0 12 11.0 23.0 13 25.0 38.0 14 23.0 40.0 15 43.0 37.0 16 18.0 46.0 17 19.0 5.0 . . . _END
Parameter File _COST_REPRENTATION_FORMAT #0 means use a graph #1 means use a mattix 0 _SOLVER_VERSION NNG _END
Solution File _SOLUTION 0 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 _COST 263.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>