home *** CD-ROM | disk | FTP | other *** search
-
-
- /********************************************************************
- (c) Copyright 1993 Toms Computer Solutions,Inc. All rights reserved.
- ********************************************************************/
-
-
-
- #include<string.h>
- #include<fstream.h>
- #include<iostream.h>
- #include<iomanip.h>
- #include<stdlib.h>
- #include<conio.h>
- #include<math.h>
- #include<stdio.h>
-
- // 1 2 3 4 5 6 7
- // 1234567890123456789012345678901234567890123456789012345678901234567890
- char *TARGET_STRING="Genetic algorithm at work!";
- #define TARGET_STRING_LENGTH 27
-
-
-
- #include"genfuncs.h"
-
-
-
- extern ofstream outfile;
-
-
- 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 trying to guess a target string.\n");
- printf("\nTarget String = Genetic algorithm at work!\n");
- printf("\nPress Q to quit.\n");
-
-
-
- printf("\n\n\nPress any key to continue\n");
-
- getch();
-
- randomize();
- clrscr();
- }
-
- void init_round()
- {
- }
-
-
- double score_func(unsigned char *gene_ptr)
- {
- char s[TARGET_STRING_LENGTH];
- strncpy(s,gene_ptr,TARGET_STRING_LENGTH-1);
- s[TARGET_STRING_LENGTH-1]='\0';
- long double score=0,temp;
- for(int i=0;i<TARGET_STRING_LENGTH;i++)
- {
- score-=((double)abs(s[i]-TARGET_STRING[i]));
- }
- return (double) score;
- }
-
- // 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)
- {
- unsigned char s[TARGET_STRING_LENGTH];
- clrscr();
- cout<<"Score = "<<score<<"\n\n";
- strncpy(s,gene_ptr,TARGET_STRING_LENGTH-1);
- s[TARGET_STRING_LENGTH-1]='\0';
- for(int i=0;i<TARGET_STRING_LENGTH-1;i++)
- {
- if((s[i]>=' ')&&(s[i]<=128))
- {
- cout<<s[i];
- }
- else
- {
- cout<<'*';
- }
- }
- }
-
-