From 1b41ac0810bb524ef16e61e774e58d1544d6ea10 Mon Sep 17 00:00:00 2001 From: Manu Gond Date: Tue, 7 Aug 2018 10:38:52 +0530 Subject: [PATCH] initial commit --- TSP.cpp | 310 +++++++++++++++++++++ TSP_Input_2 | 49 ++++ TSP_Output | 208 ++++++++++++++ TSPusingGAwithRouletteWhellSelection.cpp | 330 +++++++++++++++++++++++ 4 files changed, 897 insertions(+) create mode 100644 TSP.cpp create mode 100644 TSP_Input_2 create mode 100644 TSP_Output create mode 100644 TSPusingGAwithRouletteWhellSelection.cpp diff --git a/TSP.cpp b/TSP.cpp new file mode 100644 index 0000000..a2dfdf3 --- /dev/null +++ b/TSP.cpp @@ -0,0 +1,310 @@ +#include +#include +#include +#include +#include +#include +#define random (rand()/(double)RAND_MAX) +#define ll int64_t +#define Add push_back +using namespace std; +//=======================================================================// +//=====================Class Definition of Graph=========================// +//=======================================================================// +class Graph{ + ll TotalVertices; + vector >Vertices; //pair of vertices between which path exists + vector >Edges; //first one is weight and second one is index corresponding to the vertex pair + vectorOptimalPath; //this will store the path for which TotalCost variable's value will be set + ll TotalCost; +public: + Graph(ll TotalVertices){ + TotalCost=0; + this->TotalVertices=TotalVertices; + } + Graph(){ //Add entries for the vertice and edges, addEdge(Vertex1,Vertex2,WeightOfEdge) + this->TotalVertices=5; + addEdge(1,2,7); + addEdge(1,3,50); + addEdge(1,4,6); + addEdge(1,5,4); + addEdge(2,3,11); + addEdge(2,4,5); + addEdge(2,5,15); + addEdge(3,4,8); + addEdge(3,5,2); + addEdge(4,5,27); + } + void addEdge(ll v1,ll v2,ll weight); //given verices and edge weight betwene them, add a new entry into graph + void display(void); //display the current graph contents + ll getCost(vectorPath); //give a path funciton will reutrn the travel cost or 0 in case path is not valid + ll getCost(); //in case the optimal path is set then return the cost of that path + ll getTotalVertices(); //return total number of vertices in the Graph + bool isPathValid(vectorPath); //for checking is path is valid + void setPath(vectorOptimalPath); //set the current optimal path as received from the calling function + ll getDecesionVariables(); //return the number of decesion variables, TotalVertices in this case; +}; +//===========================================================// +//===================Class definition of Individual==========// +//===========================================================// +class Individual{ + vectorGene; //Gene will hold the path + ll DecesionVariables; //Total number of vertices in this case + Graph Properties; //An ofject of Graph type to calculate the path cost and find validity of an randomly choosen path +public: + Individual(){ + Properties=Graph(); //Create a Graph type object + DecesionVariables=Properties.getDecesionVariables(); + Gene.resize(DecesionVariables,0); //Resize the Gene array to the size of TotalVertices and initialize them with 0 + do{ + Gene.clear(); + Gene.resize(DecesionVariables,0); + for(ll i=0;iFitness=Properties.getCost(); //get the cost of path form the Graph object corresponding to this individual's Gene + } + ll Fitness; + ll getFitness(); + vectorgetGene(); //return the Gene array + bool isGeneValid(vectorArray); //given an array of path check if it is valid path or not + void setGene(vectorGene); //set the Gene of this individual equal to the received path array + void displayIndividual(); + ll getDecesionVariables(); +}; +bool compare(const Individual & l,const Individual & r){ //this compartor function is required for 'sort()' function defined in + return l.Fitness < r.Fitness; //for sorting objects according to one of their data members (Cost/Fitness in this case) +} +//===========================================================// +//===================Class definition of SimulateGA==========// +//===========================================================// +class SimulateGA{ + vectorPopulation; //List of population + ll PopulationSize; //Initial population size (as for now in this program this must be even) +public: + SimulateGA(int PopulationSize){ + this->PopulationSize=PopulationSize; //Initialize the population with given size + Population.resize(PopulationSize); //Resize the population array to the Population size + } + void doCrossover(); + vector doMutation(vectorArray); //Mutation on the passed array of genes + void displayPopulation(); + void displayFittest(); +}; +//===========================================================// +//==============Member functions for SimulateGA Class========// +//===========================================================// +void SimulateGA::displayPopulation(){ + for(int i=0;iNewPopulation; + ll range=Population[0].getDecesionVariables(); + vector >MatingPair; + for(ll i=0;iGene1=Parent1.getGene(); //Obtain genes of first parent + vectorGene2=Parent2.getGene(); //Obtain genes of the second parent + vectorPoints; + Points.Add(random*Gene1.size()); //select two random points + Points.Add(random*Gene2.size()); + sort(Points.begin(),Points.end()); //sort the random points so that they can be used as a range for swapping + vectorChildGene1(range); + vectorChildGene2(range); + for(ll i=Points[0];iFamily; + Family.Add(Parent1); //add both parnets to family list + Family.Add(Parent2); + Individual Child1; + Individual Child2; + if(Child1.isGeneValid(ChildGene1)){ //add children if they have valid gene + Child1.setGene(ChildGene1); + Family.Add(Child1); + } + if(Child1.isGeneValid(ChildGene2)){ + Child1.setGene(ChildGene2); + Family.Add(Child2); + } + sort(Family.begin(),Family.end(),compare); //sort the family based on their fitness (lower fitness here in this case because of minimization problem) + NewPopulation.Add(Family[0]); //select top 2 individual whether its parent or child and add them into new list of population + NewPopulation.Add(Family[1]); + } + Population.clear(); + Population=NewPopulation; //replace the current population with new population +} +vector SimulateGA::doMutation(vectorArray){ + Individual Person; + ll r1=random*Array.size(); + ll r2=random*Array.size(); + vectorTemp=Array; + Temp=Array; + ll t=Temp[r1]; + Temp[r1]=Temp[r2]; + Temp[r2]=t; + return Temp; +} +//===========================================================// +//==============Member functions for Individual Class========// +//===========================================================// +ll Individual::getFitness(){ + return Fitness; +} +vector Individual::getGene(){ + return Gene; +} +bool Individual::isGeneValid(vectorArray){ + if(Properties.isPathValid(Array)){ + return true; + } + return false; +} +void Individual::setGene(vectorGene){ + this->Gene=Gene; + Properties.setPath(Gene); + this->Fitness=Properties.getCost(); +} +void Individual::displayIndividual(){ + cout<<"Path: "; + for(int i=0;iDecesionVariables; +} +//===========================================================// +//================Member functions for Graph Class===========// +//===========================================================// +bool Graph::isPathValid(vectorPath){ + if(getCost(Path)==0){ + return false; + } + return true; +} +void Graph::addEdge(ll v1,ll v2,ll weight){ + Vertices.Add(make_pair(v1,v2)); + Edges.Add(make_pair(weight,Vertices.size()-1)); +} +void Graph::display(void){ + cout<<"\nGraph Contents: \n"; + for(int i=0;iPath){ + ll cost=0; + vectorvisited; + for(ll i=0;iOptimalPath){ + this->OptimalPath=OptimalPath; + TotalCost=getCost(OptimalPath); +} +ll Graph::getCost(){ + return TotalCost; +} +ll Graph::getDecesionVariables(){ + return TotalVertices; +} +int main(){ + SimulateGA g=SimulateGA(100); + cout<<"\n==================Initial Population==============\n"; + //g.displayPopulation(); + g.displayFittest(); + for(int i=0;i<10;i++){ + cout<<"\n==================Generation "<TotalVertices=17; + addEdge(1,2,633);addEdge(1,3,257);addEdge(1,4,91);addEdge(1,5,412);addEdge(1,6,150);addEdge(1,7,80);addEdge(1,8,134);addEdge(1,9,259); + addEdge(1,10,505);addEdge(1,11,353);addEdge(1,12,324);addEdge(1,13,70);addEdge(1,14,211);addEdge(1,15,268);addEdge(1,16,246);addEdge(1,17,121); + //=================================================================================================================// + addEdge(2,3,390);addEdge(2,4,661);addEdge(2,5,227);addEdge(2,6,488);addEdge(2,7,572);addEdge(2,8,530);addEdge(2,9,555);addEdge(2,10,289); + addEdge(2,11,282);addEdge(2,12,638);addEdge(2,13,567);addEdge(2,14,466);addEdge(2,15,420);addEdge(2,16,745);addEdge(2,17,518); + //=================================================================================================================// + addEdge(3,4,228);addEdge(3,5,169);addEdge(3,6,112);addEdge(3,7,196);addEdge(3,8,154);addEdge(3,9,372);addEdge(3,10,262); + addEdge(3,11,110);addEdge(3,12,437);addEdge(3,13,191);addEdge(3,14,74);addEdge(3,15,53);addEdge(3,16,472);addEdge(3,17,142); + //=================================================================================================================// + addEdge(4,5,383);addEdge(4,6,120);addEdge(4,7,77);addEdge(4,8,105);addEdge(4,9,175);addEdge(4,10,476);addEdge(4,11,324); + addEdge(4,12,240);addEdge(4,13,27);addEdge(4,14,182);addEdge(4,15,239);addEdge(4,16,237);addEdge(4,17,84); + //=================================================================================================================// + addEdge(5,6,267);addEdge(5,7,351);addEdge(5,8,309);addEdge(5,9,338);addEdge(5,10,196);addEdge(5,11,61); + addEdge(5,12,421);addEdge(5,13,346);addEdge(5,14,243);addEdge(5,15,199);addEdge(5,16,528);addEdge(5,17,297); + //=================================================================================================================// + addEdge(6,7,63);addEdge(6,8,34);addEdge(6,9,264);addEdge(6,10,360);addEdge(6,11,208);addEdge(6,12,329); + addEdge(6,13,83);addEdge(6,14,105);addEdge(6,15,123);addEdge(6,16,364);addEdge(6,17,35); + //=================================================================================================================// + addEdge(7,8,29);addEdge(7,9,232);addEdge(7,10,444);addEdge(7,11,292);addEdge(7,12,297); + addEdge(7,13,47);addEdge(7,14,150);addEdge(7,15,207);addEdge(7,16,332);addEdge(7,17,29); + //=================================================================================================================// + addEdge(8,9,249);addEdge(8,10,402);addEdge(8,11,250);addEdge(8,12,314);addEdge(8,13,68); + addEdge(8,14,108);addEdge(8,15,165);addEdge(8,16,349);addEdge(8,17,36); + //=================================================================================================================// + addEdge(9,10,495);addEdge(9,11,352);addEdge(9,12,95);addEdge(9,13,189); + addEdge(9,14,326);addEdge(9,15,383);addEdge(9,16,202);addEdge(9,17,236); + //=================================================================================================================// + addEdge(10,11,154);addEdge(10,12,578);addEdge(10,13,439);addEdge(10,14,336); + addEdge(10,15,240);addEdge(10,16,685);addEdge(10,17,390); + //=================================================================================================================// + addEdge(11,12,435);addEdge(11,13,287);addEdge(11,14,184); + addEdge(11,15,140);addEdge(11,16,542);addEdge(11,17,238); + //=================================================================================================================// + addEdge(12,13,254);addEdge(12,14,391);addEdge(12,15,448); + addEdge(12,16,157);addEdge(12,17,301); + //=================================================================================================================// + addEdge(13,14,145);addEdge(13,15,202); + addEdge(13,16,289);addEdge(13,17,55); + //=================================================================================================================// + addEdge(14,15,57);addEdge(14,16,426); + addEdge(14,17,96); + //=================================================================================================================// + addEdge(15,16,483); + addEdge(15,17,153); + //=================================================================================================================// + addEdge(16,17,336); + } \ No newline at end of file diff --git a/TSP_Output b/TSP_Output new file mode 100644 index 0000000..33f21a1 --- /dev/null +++ b/TSP_Output @@ -0,0 +1,208 @@ +==================Initial Population============== + +Fittest Individual: +Path: 3 2 4 1 5 Fitness: 26 +==================Generation 1============== + +Fittest Individual: +Path: 3 5 4 2 1 Fitness: 41 +==================Generation 2============== + +Fittest Individual: +Path: 3 5 4 2 1 Fitness: 41 +==================Generation 3============== + +Fittest Individual: +Path: 3 5 4 2 1 Fitness: 41 +==================Generation 4============== + +Fittest Individual: +Path: 3 5 4 2 1 Fitness: 41 +==================Generation 5============== + +Fittest Individual: +Path: 3 5 4 2 1 Fitness: 41 +==================Generation 6============== + +Fittest Individual: +Path: 3 5 4 2 1 Fitness: 41 +==================Generation 7============== + +Fittest Individual: +Path: 3 5 4 2 1 Fitness: 41 +==================Generation 8============== + +Fittest Individual: +Path: 1 5 4 3 2 Fitness: 50 +==================Generation 9============== + +Fittest Individual: +Path: 1 5 4 3 2 Fitness: 50 +==================Generation 10============== + +Fittest Individual: +Path: 1 5 4 3 2 Fitness: 50 +==================Generation 11============== + +Fittest Individual: +Path: 1 5 4 3 2 Fitness: 50 +==================Generation 12============== + +Fittest Individual: +Path: 1 5 4 3 2 Fitness: 50 +==================Generation 13============== + +Fittest Individual: +Path: 3 2 5 1 4 Fitness: 36 +==================Generation 14============== + +Fittest Individual: +Path: 3 2 5 1 4 Fitness: 36 +==================Generation 15============== + +Fittest Individual: +Path: 3 2 5 1 4 Fitness: 36 +==================Generation 16============== + +Fittest Individual: +Path: 3 2 5 4 1 Fitness: 59 +==================Generation 17============== + +Fittest Individual: +Path: 1 2 5 3 4 Fitness: 32 +==================Generation 18============== + +Fittest Individual: +Path: 3 2 5 4 1 Fitness: 59 +==================Generation 19============== + +Fittest Individual: +Path: 3 2 5 4 1 Fitness: 59 +==================Generation 20============== + +Fittest Individual: +Path: 3 2 5 4 1 Fitness: 59 +==================Generation 21============== + +Fittest Individual: +Path: 3 2 5 4 1 Fitness: 59 +==================Generation 22============== + +Fittest Individual: +Path: 3 2 5 4 1 Fitness: 59 +==================Generation 23============== + +Fittest Individual: +Path: 3 2 5 4 1 Fitness: 59 +==================Generation 24============== + +Fittest Individual: +Path: 3 4 5 2 1 Fitness: 57 +==================Generation 25============== + +Fittest Individual: +Path: 3 2 5 4 1 Fitness: 59 +==================Generation 26============== + +Fittest Individual: +Path: 3 2 5 4 1 Fitness: 59 +==================Generation 27============== + +Fittest Individual: +Path: 3 2 5 4 1 Fitness: 59 +==================Generation 28============== + +Fittest Individual: +Path: 3 2 5 4 1 Fitness: 59 +==================Generation 29============== + +Fittest Individual: +Path: 5 3 1 2 4 Fitness: 64 +==================Generation 30============== + +Fittest Individual: +Path: 5 3 1 2 4 Fitness: 64 +==================Generation 31============== + +Fittest Individual: +Path: 5 3 1 2 4 Fitness: 64 +==================Generation 32============== + +Fittest Individual: +Path: 5 3 1 2 4 Fitness: 64 +==================Generation 33============== + +Fittest Individual: +Path: 5 3 1 2 4 Fitness: 64 +==================Generation 34============== + +Fittest Individual: +Path: 5 3 1 2 4 Fitness: 64 +==================Generation 35============== + +Fittest Individual: +Path: 5 3 1 2 4 Fitness: 64 +==================Generation 36============== + +Fittest Individual: +Path: 5 3 1 2 4 Fitness: 64 +==================Generation 37============== + +Fittest Individual: +Path: 5 3 1 2 4 Fitness: 64 +==================Generation 38============== + +Fittest Individual: +Path: 5 3 1 2 4 Fitness: 64 +==================Generation 39============== + +Fittest Individual: +Path: 5 3 1 2 4 Fitness: 64 +==================Generation 40============== + +Fittest Individual: +Path: 5 3 1 2 4 Fitness: 64 +==================Generation 41============== + +Fittest Individual: +Path: 5 3 4 2 1 Fitness: 22 +==================Generation 42============== + +Fittest Individual: +Path: 5 3 4 2 1 Fitness: 22 +==================Generation 43============== + +Fittest Individual: +Path: 5 3 4 2 1 Fitness: 22 +==================Generation 44============== + +Fittest Individual: +Path: 5 3 4 2 1 Fitness: 22 +==================Generation 45============== + +Fittest Individual: +Path: 5 3 4 2 1 Fitness: 22 +==================Generation 46============== + +Fittest Individual: +Path: 5 3 4 2 1 Fitness: 22 +==================Generation 47============== + +Fittest Individual: +Path: 5 3 4 2 1 Fitness: 22 +==================Generation 48============== + +Fittest Individual: +Path: 5 3 4 2 1 Fitness: 22 +==================Generation 49============== + +Fittest Individual: +Path: 5 3 4 2 1 Fitness: 22 +==================Generation 50============== + +Fittest Individual: +Path: 5 3 4 2 1 Fitness: 22 +Fittest Individual: +Path: 5 3 4 2 1 Fitness: 22 +-------------------------------- +Process exited after 0.8932 seconds with return value 0 \ No newline at end of file diff --git a/TSPusingGAwithRouletteWhellSelection.cpp b/TSPusingGAwithRouletteWhellSelection.cpp new file mode 100644 index 0000000..81ef194 --- /dev/null +++ b/TSPusingGAwithRouletteWhellSelection.cpp @@ -0,0 +1,330 @@ +#include +#include +#include +#include +#include +#include +#define random (rand()/(double)RAND_MAX) +#define ll int64_t +#define Add push_back +using namespace std; +//=======================================================================// +//=====================Class Definition of Graph=========================// +//=======================================================================// +class Graph{ + ll TotalVertices; + vector >Vertices; //pair of vertices between which path exists + vector >Edges; //first one is weight and second one is index corresponding to the vertex pair + vectorOptimalPath; + ll TotalCost; +public: + Graph(ll TotalVertices){ + TotalCost=0; + this->TotalVertices=TotalVertices; + } + Graph(){ + this->TotalVertices=5; + addEdge(1,2,7); + addEdge(1,3,50); + addEdge(1,4,6); + addEdge(1,5,4); + addEdge(2,3,11); + addEdge(2,4,5); + addEdge(2,5,15); + addEdge(3,4,8); + addEdge(3,5,2); + addEdge(4,5,27); + } + void addAll(vector >Vertices,vector >Edges); + void addEdge(ll v1,ll v2,ll weight); //given verices and edge weight betwene them, add a new entry into graph + void display(void); //display the current graph contents + ll getCost(vectorPath); //give a path funciton will reutrn the travel cost or 0 in case path is not valid + ll getCost(); //in case the optimal path is set then return the cost of that path + ll getTotalVertices(); //return total number of vertices in the Graph + bool isPathValid(vectorPath); //for checking is path is valid + void setPath(vectorOptimalPath); //set the current optimal path as received from the calling function + ll getDecesionVariables(); +}; +//===========================================================// +//===================Class definition of Individual==========// +//===========================================================// +class Individual{ + vectorGene; + ll DecesionVariables; + Graph Properties; +public: + Individual(){ + Properties=Graph(); + DecesionVariables=Properties.getDecesionVariables(); + Gene.resize(DecesionVariables,0); + do{ + Gene.clear(); + Gene.resize(DecesionVariables,0); + for(ll i=0;iFitness=Properties.getCost(); + } + ll Fitness; + ll getFitness(); + vectorgetGene(); + bool isGeneValid(vectorArray); + void setGene(vectorGene); + void displayIndividual(); + ll getDecesionVariables(); +}; +bool compare(const Individual & l,const Individual & r) +{ + return l.Fitness < r.Fitness; +} +//===========================================================// +//===================Class definition of SimulateGA==========// +//===========================================================// +class SimulateGA{ + vectorPopulation; + ll PopulationSize; +public: + SimulateGA(int PopulationSize){ + this->PopulationSize=PopulationSize; + Population.resize(PopulationSize); + } + void doCrossover(); + vector doMutation(vectorArray); + void displayPopulation(); + void displayFittest(); + vector > RouletteWheelSelection(); +}; +//===========================================================// +//==============Member functions for SimulateGA Class========// +//===========================================================// +void SimulateGA::displayPopulation(){ + for(int i=0;i > SimulateGA::RouletteWheelSelection(){ + vector >MatingPair; + ll SumFitness=0; + for(ll i=0;iNewPopulation; + ll range=Population[0].getDecesionVariables(); + vector >MatingPair; + MatingPair=RouletteWheelSelection(); //call Roulette Wheel Selection function to select the mating pairs + for(ll index=0;indexGene1=Parent1.getGene(); + vectorGene2=Parent2.getGene(); + vectorPoints; + Points.Add(random*Gene1.size()); + Points.Add(random*Gene2.size()); + sort(Points.begin(),Points.end()); + vectorChildGene1(range); + vectorChildGene2(range); + for(ll i=Points[0];iFamily; + Family.Add(Parent1); + Family.Add(Parent2); + Individual Child1; + Individual Child2; + if(Child1.isGeneValid(ChildGene1)){ + Child1.setGene(ChildGene1); + Family.Add(Child1); + } + if(Child1.isGeneValid(ChildGene2)){ + Child1.setGene(ChildGene2); + Family.Add(Child2); + } + sort(Family.begin(),Family.end(),compare); + NewPopulation.Add(Family[0]); + NewPopulation.Add(Family[1]); + } + Population.clear(); + Population=NewPopulation; +} +vector SimulateGA::doMutation(vectorArray){ + Individual Person; + ll r1=random*Array.size(); + ll r2=random*Array.size(); + vectorTemp=Array; + Temp=Array; + ll t=Temp[r1]; + Temp[r1]=Temp[r2]; + Temp[r2]=t; + return Temp; +} +//===========================================================// +//==============Member functions for Individual Class========// +//===========================================================// +ll Individual::getFitness(){ + return Fitness; +} +vector Individual::getGene(){ + return Gene; +} +bool Individual::isGeneValid(vectorArray){ + if(Properties.isPathValid(Array)){ + return true; + } + return false; +} +void Individual::setGene(vectorGene){ + this->Gene=Gene; + Properties.setPath(Gene); + this->Fitness=Properties.getCost(); +} +void Individual::displayIndividual(){ + cout<<"Path: "; + for(int i=0;iDecesionVariables; +} +//===========================================================// +//================Member functions for Graph Class===========// +//===========================================================// +bool Graph::isPathValid(vectorPath){ + if(getCost(Path)==0){ + return false; + } + return true; +} +void Graph::addAll(vector >Vertices,vector >Edges){ + this->Vertices=Vertices; + this->Edges=Edges; +} +void Graph::addEdge(ll v1,ll v2,ll weight){ + Vertices.Add(make_pair(v1,v2)); + Edges.Add(make_pair(weight,Vertices.size()-1)); +} +void Graph::display(void){ + cout<<"\nGraph Contents: \n"; + for(int i=0;iOptimalPath){ + this->OptimalPath=OptimalPath; + TotalCost=getCost(OptimalPath); +} +ll Graph::getCost(){ + return TotalCost; +} +ll Graph::getCost(vectorPath){ + ll cost=0; + vectorvisited; + for(ll i=0;i