資源簡介
遺傳算法 CVRP VRP 使用遺傳算法求解帶容量的路徑選擇問題源代碼

代碼片段和文件信息
/*
Name:?GA算法實現?車輛的CVRP問題?
Copyright:?
Author:?GCJ?NEW?NEU?Labotatry?
Date:?12/11/17?20:49
Description:?本文件和附加的參數capacity.txt和data.txt文檔放在一個工程下即可,
????????????????然后修改readTxt()函數內部的路徑即可運行成功?
*/
#include?
#include“time.h“
#include ?
#include ?
#include
/*--------------------------------參數配置區---------------------------------------------------*/?
#define? CLIENT_NUM???50?? //客戶數量?為50??1個配送中心?
#define CAPACITY?????160 //車的容量為160???
#define? Population_size??50?? //種群大小
#define? iterations? ??50 //迭代次數
#define??ISGA_crossover???1 //是否可交叉?1:交叉??0:不交叉?
#define? PC ????????0.7??? //配置交叉率
#define??ISmutate?????????1 //是否可變異?1:?變異??0:不變異?
#define? PM ????????0.1 ? //變異率?
#define??IsChampionShip???1? //錦標賽參數是否可調節(默認值為0.1倍的種群大小)?1:可調?0:不可調?
/*--------------------------------宏配置區---------------------------------------------------*/?
#define?Min(xy)??????(?(?(x)?(y)?)??(x):(y)??)
#define?Max(xy)? ?(?(?(x)?>?(y)?)??(x):(y)??)
#define? f(x)??(x?-1)? //用f宏?作為index?因為在找商店的序號跟二維數組之間相差1?所以用f表示兩者之間的映射?
/*---錦標賽參數設置區---*/
#ifdef?ChampionShip
???double?championShip?=?0.2; //自己可隨意設置成0-1之間的小數?但是最好不要超過0.5?
#endif
typedef?int?ElementType;
using?namespace?std;
ElementType?**Distance; //存儲商店之間的距離?
ElementType?*?Capacity; //存儲車容量?
typedef?struct?_rand{? ?
int?flag; ?
ElementType?num;
}Rand;
?
class?Chromosome
{
public:
Chromosome(); ?
Chromosome(int?len?); //length表示染色體的長度 ?
virtual~Chromosome(); //析構函數?
Chromosome(const?Chromosome&a); //自定義拷貝構造函數??
const?Chromosome?&operator?=(const?Chromosome?&?o?)?;
void?initialize()?;??? //初始化染色體?調用newRandom函數?產生1-?length?的隨機數?
int??newRandom(int?lowint?high?);??//隨機產生?0-?num?個不重復的數字???
void?evaluate();
#if?ISmutate
void?mutate(); //采用逆轉變異算子?
#endif
//查看染色體內容的調試函數?
void?toprint(){
?????????int?i;
?// cout<<“染色體內容“< ? for(?i?=0?;?i ? ??cout< ???}??
? ???}
? ???void?printpath(); //打印最后的車輛安排路徑?
? ???ElementType?getFitness(){return?this->fitness;} //返回染色體適應值?
? ???int?getLength(){?return?length;?} //獲取染色體長度?
? ???int?getCar(){return?carNum;?} //獲取車的數量?
int?*codespace?=?NULL; //編碼空間?代表2-51商店的標號?
private:
int?length; //染色體的長度?
ElementType?fitness;?? //方便之后的數據的擴展?
int?carNum; //車數量?
};
typedef?struct?_Cross{
ElementType?one;
ElementType?two;
int?flag1?; //標記找到的one?
int?flag2?; //標記找到的two?
_Cross():flag1(0)flag2(0){}??
}Cross; //部分交叉映射需要用到的結構??記錄映射關系?
class?GA
{
public:
GA(){};
GA(int?popnumint?max); //popnum?種群大小,max表示迭代次數?
virtual~GA();
GA(const?GA&o); ??//自定義拷貝構造函數??把指針的情況考慮進去了?
const?GA?&operator?=(const?GA?&?o?); ??//自定義賦值函數??
//成員函數?
void?initializePop(); ??//初始化種群?
void?GArun(); ??//運行GA算法?
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件?????????336??2018-05-23?07:22??capacity.txt
?????文件???????22060??2017-11-12?21:55??GA.cpp
?????文件?????????495??2018-05-23?07:22??position.txt
?????文件??????????74??2018-05-23?07:21??readme.txt
- 上一篇:空間兩坐標系轉換的7參數求解
- 下一篇:MDPI_template.zip
評論
共有 條評論