📄 ga.h
字号:
/*****************************************************************/
/* 基于遗传模拟退火算法的银行中心金库车辆数目的优化程序 */
/* 中国农业银行东莞分行 熊刚强 2006年09月 */
/*****************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "math.h"
#include <string>
//#define DEBUGFLAG 1
#define OK 1
#define NO 0
/* 全局变量 */
struct individual /* 个体*/
{
int *iChrom; /* 染色体 */
double dFitness; /* 个体适应度 */
int iCarNum; /* 车辆数目 */
double *dCarFactWeight; /* 每辆车的实际重量 */
double *dCarFactTime; /* 每辆车的实际容量 */
int *iLineStartPos; /* 每条线路的起始点 */
int iGeneration; /* 代数 */
int iXSite1; /* 交叉位置1 */
int iXSite2; /* 交叉位置2 */
int iParent[2]; /* 父个体 */
int *iUtility; /* 特定数据指针变量 */
};
struct bestever /* 最佳个体*/
{
int *iChrom; /* 最佳个体染色体*/
double dFitness; /* 最佳个体适应度 */
int iCarNum; /* 车辆数目 */
double *dCarFactWeight; /* 每辆车的实际重量 */
double *dCarFactTime; /* 每辆车的实际容量 */
int *iLineStartPos; /* 每条线路的起始点 */
int iGeneration; /* 最佳个体生成代 */
};
struct individual *stOldPopulation; /* 当前代种群 */
struct individual *stNewPopulation; /* 新一代种群 */
struct bestever stBestFit; /* 最佳个体 */
double dSumOfFitness=0.0; /* 种群中个体适应度累计 */
double dMaxFitness=0.0; /* 种群中个体最大适应度 */
double dAvgFitness=0.0; /* 种群中个体平均适应度 */
double dMinFitness=0.0; /* 种群中个体最小适应度 */
int iGeneration=0; /* 当前世代数 */
int iRun=0; /* 当前运行次数 */
FILE *outfp;
/*int iNumMutation=0; 当前代变异发生次数 */
/*int iNumCross=0; 当前代交叉发生次数 */
double dTimeLimit; /* 运钞工作的时间限制 */
double dPCross; /* 交叉概率 */
double dPMutation; /* 变异概率 */
int iPopSize; /* 种群大小 */
int iChromLong; /* 染色体长度 */
int iPrintFlag; /* 输出染色体编码的判断,0 -- 不输出, 1 -- 输出 */
double dMaxTemperature; /* 模拟退火算法的初始温度 */
double dMinTemperature; /* 模拟退火算法的冷却温度 */
int iNumCycle; /* 模拟退火算法的循环次数 */
double dDownRate; /* 模拟退火算法温度降低的系数 */
double dAlpha; /* 权系数 fBeta=100.0-fAlpha*/
double **dRunTime; /* 网点i到网点j的行驶时间 */
double *dWorkTime; /* 网点i的停留(工作)时间 */
double *dSiteWeight; /* 网点i的钱箱重量 */
double *dCarCapacity; /* 每台车载重量 */
int iMaxGen=0; /* 最大世代数 */
int iMaxRuns=0; /* 总运行次数 */
char filePara[81]; /* 参数文件 */
char fileData[81]; /* 数据文件 */
struct individual stOldMin, stNewMin, stNewMax;
/* 函数定义 */
int Randomize(int,int); /* 产生随机数 */
int CreatePopulation(); /* 初始化群体 */
int InitiateVMemory(); /* 分配内存空间 */
int FreeVMemory(); /* 释放内存空间 */
int SimulatedAnnealing(struct individual*); /* 模拟退火算法 */
int DecodeChromosome(struct individual*); /* 解码 */
int CalculateFitnessValue(struct individual*); /* 计算适应度值 */
int ReadParameterFromTXT(char*,double*); /* 读控制参数 */
int GetParameterValue(); /* 获取控制参数 */
int GetData(); /* 获取原始数据 */
int MutationOperator(struct individual *,int); /* 变异算子 */
int SelectionOperator(); /* 选择算子 */
int PartiallyMatchedCrossover(struct individual *,
struct individual *,
struct individual *,
struct individual *); /* PMX-1 */
int OrderedCrossover(struct individual *,
struct individual *,
struct individual *,
struct individual *); /* OX-2 */
int CycleCrossover(struct individual *,
struct individual *,
struct individual *,
struct individual *); /* CX-3 */
int CrossoverOperator(struct individual *,
struct individual *,
struct individual *,
struct individual *,
int); /* 交叉算子 */
int InverseMutation(struct individual *); /* 倒位算子-1 */
int TwoPointSwopMutation(struct individual *); /* 两点交换变异-2 */
int InsertMutation(struct individual *); /* 插入变异-3 */
int Statistics(); /* 统计计算 */
int CopyStruct(struct individual *,
struct individual *); /* 复制个体 */
int GenerationCalculate();
int GeneticSimulatedAnnealing(int, int);
int ReportData();
/* 测试函数定义 */
int TestVarMemory();
int TestGetParameter();
int TestGetData();
int TestInitiatePop();
int TestDecode();
int TestCalFitness();
int TestSA();
int TestCrossover();
int TestMutation();
int TestSelection();
int TestPara();
int TestData();
/* 测试数据 */
double TimeLimit = 90; /* 运钞工作的时间限制,单位:分钟*/
double PCross = 0.40; /* 交叉概率 */
double PMutation = 0.05; /* 变异概率 */
int PopSize = 10; /* 种群大小 */
int ChromLong = 10; /* 染色体长度 */
int PrintFlag = 1; /* 输出染色体编码的判断,0 - 不输出, 1 - 输出 */
double MaxTemperature = 1000.00; /* 模拟退火算法的初始温度 */
double MinTemperature = 0.05; /* 模拟退火算法的冷却温度 */
int NumCycle = 100; /* 模拟退火算法的循环次数 */
double DownRate = 0.90; /* 模拟退火算法温度降低的系数 */
double Alpha = 0.00; /* 关于时间的权系数 */
int MaxGen = 100; /* 最大世代数 */
int MaxRuns = 4; /* 最大运行次数 */
double WorkTime[10]={10.0, 13.0, 16.0, 14.0, 10.0, 12.0, 14.0, 11.0, 15.0, 13.0}; /* 每个网点卸钱箱的时间,即停留时间 */
double SiteWeight[10]={10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0}; /* 每个网点的钱箱数(个数) */
double CarCapacity[10]={100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0,100.0}; /* 每台车的容量(钱箱个数) */
double RunTime[11][11]={{0.0, 21.0, 23.0, 16.0, 13.0, 25.0, 30.0, 33.0, 23.0, 9.0, 18.0},
{21.0, 0.0, 17.0, 20.0, 9.0, 32.0, 25.0, 17.0, 32.0, 26.0, 25.0},
{23.0, 17.0, 0.0, 15.0, 30.0, 16.0, 18.0, 20.0, 14.0, 35.0, 27.0},
{16.0, 20.0, 15.0, 0.0, 32.0, 9.0, 19.0, 10.0, 21.0, 27.0, 10.0},
{13.0, 9.0, 30.0, 32.0, 0.0, 15.0, 9.0, 29.0, 34.0, 31.0, 25.0},
{25.0, 32.0, 16.0, 9.0, 15.0, 0.0, 8.0, 21.0, 27.0, 30.0, 32.0},
{30.0, 25.0, 18.0, 19.0, 9.0, 8.0, 0.0, 28.0, 18.0, 19.0, 13.0},
{33.0, 17.0, 20.0, 10.0, 29.0, 21.0, 28.0, 0.0, 29.0, 21.0, 30.0},
{23.0, 32.0, 14.0, 21.0, 34.0, 27.0, 18.0, 29.0, 0.0, 13.0, 18.0},
{9.0, 26.0, 35.0, 27.0, 31.0, 30.0, 19.0, 21.0, 13.0, 0.0, 25.0},
{18.0, 25.0, 27.0, 10.0, 25.0, 32.0, 13.0, 30.0, 18.0, 25.0, 0.0}}; /* 网点之间的行程时间值 */
/* 错误码定义 */
#define ERR_GA_PARA 100 //读参数错
#define ERR_GA_MEMO 200 //分配空间错
#define ERR_GA_DATA 300 //读数据错
#define ERR_GA_POPU 400 //创建初始群体错
#define ERR_GA_CROS 500 //交叉错
#define ERR_GA_CALC 600 //计算适应度错
#define ERR_GA_MUTA 700 //变异错
#define ERR_GA_SANN 800 //模拟退火算法错
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -