home *** CD-ROM | disk | FTP | other *** search
-
-
- /********************************************************************
- (c) Copyright 1993 Toms Computer Solutions,Inc. All rights reserved.
- ********************************************************************/
-
-
-
- #include"grafopen.h"
- #include<string.h>
- #include<fstream.h>
- #include<iostream.h>
- #include<iomanip.h>
- #include<stdlib.h>
- #include<stdio.h>
- #include<conio.h>
-
-
-
- #define number_of_cities 25
-
-
-
-
-
- struct city
- {
- unsigned char id;
- unsigned long order;
- }
- ;
-
-
- struct city_loc
- {
- int x,y;
- }
- ;
-
-
-
- city cities[number_of_cities];
- city_loc cities_loc[number_of_cities];
-
-
-
- // This function randomly sets up the locations of the cities in a 10000 by
- // 10000 square.
- void set_up_cities_locs()
- {
- for(int i=0;i<number_of_cities;i++)
- {
- cities_loc[i].x=random(10000);
- cities_loc[i].y=random(10000);
- }
- }
-
-
- // This function is used by the qsort function called in the score_func.
- city_sort_func(const void *c1, const void *c2)
- {
- unsigned long c1_order,c2_order;
- city *city1,*city2;
- city1=(city *)c1;
- city2=(city *)c2;
- c1_order=(*city1).order;
- c2_order=(*city2).order;
- if(c1_order>c2_order)
- {
- return 1;
- }
- else if(c1_order<c2_order)
- {
- return -1;
- }
- else
- {
- return 0;
- }
- }
-
-
- void pre_init()
- {
-
- clrscr();
- printf("/********************************************************************\n");
- printf("(c) Copyright 1993 Toms Computer Solutions,Inc. All rights reserved.\n");
- printf("********************************************************************/\n");
-
-
- printf("\n\nDemo of a genetic algorithm solving the traveling salesman problem.\n");
- printf("\nPress Q to quit.\n");
-
-
-
- printf("\n\n\nPress any key to continue\n");
-
- getch();
-
- randomize();
- set_up_cities_locs();
- tomopen();
- }
-
-
-
- void init_round()
- {
- }
-
-
- // This is the scoreing function passed to the population class.
- // It returns the distance traveled by the salesman times -1.
- // It is multiplied by -1 so that the higher numbers represent better
- // solutions.
-
- double score_func(unsigned char *gene_ptr)
- {
- double dist;
- unsigned long *lptr;
- lptr=(unsigned long *)gene_ptr;
- for(int i=0;i<number_of_cities;i++)
- {
- (cities[i]).id=i;
- (cities[i]).order=*lptr;
- lptr++;
- }
- qsort(&(cities[0]),number_of_cities,sizeof(cities[0]),city_sort_func);
- dist=0.0;
- int x1,x2,y1,y2;
- double dx,dy;
- x1=cities_loc[cities[number_of_cities-1].id].x;
- y1=cities_loc[cities[number_of_cities-1].id].y;
- for(i=0;i<number_of_cities;i++)
- {
- x2=cities_loc[cities[i].id].x;
- y2=cities_loc[cities[i].id].y;
- dx=fabs(x1-x2);
- dy=fabs(y1-y2);
- dist+=sqrt(dx*dx+dy*dy);
- x1=x2;
- y1=y2;
- }
- return -dist;
- }
-
-
- // This is a function passed to the population class to display a given
- // solution. In this case the solution is displayed graphically as a map
- // of the cities and the route taken by the salesman.
-
- void print_solution(unsigned char *gene_ptr,double score,ofstream *ofs)
- {
- clearviewport();
- char gstring[256];
- sprintf(gstring,"Total Distance = %f ",-score);
- setcolor(LIGHTMAGENTA);
- outtextxy(25,25,gstring);
- unsigned long *lptr;
- lptr=(unsigned long *)gene_ptr;
- for(int i=0;i<number_of_cities;i++)
- {
- (cities[i]).id=i;
- (cities[i]).order=*lptr;
- lptr++;
- }
- qsort(&(cities[0]),number_of_cities,sizeof(cities[0]),city_sort_func);
- long x1,x2,y1,y2;
- x1=cities_loc[cities[number_of_cities-1].id].x;
- y1=cities_loc[cities[number_of_cities-1].id].y;
- x1=((x1*max_x*0.8)/10000)+(max_x*0.1);
- y1=((y1*max_y*0.9)/10000)+(max_y*0.1);
- for(i=0;i<number_of_cities;i++)
- {
- x2=cities_loc[cities[i].id].x;
- y2=cities_loc[cities[i].id].y;
- x2=((x2*max_x*0.8)/10000)+(max_x*0.1);
- y2=((y2*max_y*0.9)/10000)+(max_y*0.1);
- setcolor(BLUE);
- line(x1,y1,x2,y2);
- setcolor(RED);
- circle(x1,y1,4);
- setcolor(YELLOW);
- circle(x1,y1,2);
- x1=x2;
- y1=y2;
- }
- }
-
-