分类: 技术 |
This is the program using Genetic Algorithm(GA)
to solve the Traveling Salesman Problem(TSP),but
unfortunately,there are still some bugs I was unable to handle.Take
care!
Note that,I wrote this program
several months ago in TC,but I debug it this afternoon with
VC++.net,however, it won't take you any longer to handle this
slight difference.
In passing,this program cannot run smoothly and
correctly in my notebook computer,from which I
doubt the pc may not so reliable
or available as you think ,just as the Ian Sommerville's
<<Software Engeering>> says,maybe.

/*******************coded by
Atlantis*******************/
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <time.h>
#define GENE_TOTAL
10
#define MAX_GEN 5
#define LOW 3
#define UP 6
#define INHERIT_RATE 20
#define HYBRID_RATE 67
#define RANDOM rand()%GENE_TOTAL
#define MAX_GEN 5
#define LOW 3
#define UP
#define INHERIT_RATE 20
#define HYBRID_RATE 67
#define RANDOM rand()%GENE_TOTAL
enum operators
{
inherit,hybrid,mutation,
}op;
{
}op;
struct individual
{
int val;
int gene[GENE_TOTAL];
};
{
};
typedef enum operators operators;
typedef struct individual individual;
typedef struct individual individual;
int Dist[GENE_TOTAL][GENE_TOTAL]={
0,
1, 4, 6, 8, 1, 3, 7, 2, 9,
1,
0, 7, 5, 3, 8, 3, 4, 2, 4,
4,
7, 0, 3, 8, 3, 7, 9, 1, 2,
6,
5, 3, 0, 3, 1, 5, 2, 9, 1,
8,
3, 8, 3, 0, 2, 3, 1, 4, 6,
1,
8, 3, 1, 2, 0, 3, 3, 9, 5,
3,
3, 7, 5, 3, 3, 0, 7, 5, 9,
7,
4, 9, 2, 1, 3, 7, 0, 1, 3,
2,
2, 1, 9, 4, 9, 5, 1, 0, 1,
9,
4, 2, 1, 6, 5, 9, 3, 1, 0,
};
individual Race[R_AMOUNT];
int Total,Total_temp;
void init(void);
void eval_r(void);
operators sel_op(void);
individual* sel_mem(void);
individual Race[R_AMOUNT];
int Total,Total_temp;
void init(void);
void eval_r(void);
operators sel_op(void);
individual* sel_mem(void);
int main()
{
int gen=0;
{