479 lines
24 KiB
C++
479 lines
24 KiB
C++
![]() |
#include "GeneticAlgorithm.h"
|
|||
|
#include <algorithm>
|
|||
|
|
|||
|
|
|||
|
// <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
|
|||
|
GeneticAlgorithm::GeneticAlgorithm(std::map<std::string, int> redLst, std::map<std::string, int> blueLst,\
|
|||
|
std::map<std::string, ABILITY*>& flst,std::map<std::string, ABILITY*>& slst,\
|
|||
|
std::map<std::string, ABILITY*>& alst,std::map<std::string, ABILITY*>& glst)
|
|||
|
{
|
|||
|
// <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵ<EFBFBD><CFB5>
|
|||
|
AbilityFactor wurenji = {100,1,1,10};
|
|||
|
AbilityFactor yourenji = {100,1,1000,1/2.5};
|
|||
|
AbilityFactor ship = {8000,1,8000,0.1};
|
|||
|
AbilityFactor zhs = {100,1,1,1.0/50.0};
|
|||
|
AbilityFactor fangkong = {100,1,1,1};
|
|||
|
|
|||
|
Factor.push_back(wurenji);
|
|||
|
Factor.push_back(yourenji);
|
|||
|
Factor.push_back(ship);
|
|||
|
Factor.push_back(zhs);
|
|||
|
Factor.push_back(fangkong);
|
|||
|
|
|||
|
// <20>췽<EFBFBD><ECB7BD><EFBFBD><EFBFBD>װ<EFBFBD><D7B0>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
mkj500 = redLst["kj500"];
|
|||
|
mkj2000 = redLst["kj2000"];
|
|||
|
mj16d = redLst["j16d"];
|
|||
|
mj20 = redLst["j20"];
|
|||
|
mwg2 = redLst["gj2"];
|
|||
|
mgj11 = redLst["gj11"];
|
|||
|
mh6k = redLst["h6k"];
|
|||
|
mj11 = redLst["j11"];
|
|||
|
myg9 = redLst["yg9"];
|
|||
|
mwuzhen10 = redLst["wuzhen10"];
|
|||
|
|
|||
|
// <20>췽<EFBFBD><ECB7BD><EFBFBD><EFBFBD>ֵ
|
|||
|
if(flst.find("kj500")!= flst.end()){kj500DA = flst["kj500"]->DetectAbility/Factor[1].DetectFactor;kj500AA = flst["kj500"]->AttackAbility / Factor[1].AttackFactor;kj500IA = flst["kj500"]->InterfereAbility / Factor[1].InterfereFactor;kj500SA = flst["kj500"]->SurAbility / Factor[1].SurFactor;ckj500 = flst["kj500"]->Cost;}else{kj500DA = 0;kj500AA = 0;kj500IA = 0;kj500SA = 0;ckj500 = 0;}
|
|||
|
if(flst.find("kj2000")!= flst.end()){kj2000DA = flst["kj2000"]->DetectAbility/Factor[1].DetectFactor;kj2000AA = flst["kj2000"]->AttackAbility / Factor[1].AttackFactor;kj2000IA = flst["kj2000"]->InterfereAbility / Factor[1].InterfereFactor;kj2000SA = flst["kj2000"]->SurAbility / Factor[1].SurFactor;ckj2000 = flst["kj2000"]->Cost;}else{kj2000DA = 0;kj2000AA = 0; kj2000IA = 0;kj2000SA = 0;ckj2000 = 0;}
|
|||
|
if(flst.find("wuzhen10")!= flst.end()){wuzhen10DA = flst["wuzhen10"]->DetectAbility/Factor[0].DetectFactor;wuzhen10AA = flst["wuzhen10"]->AttackAbility / Factor[0].AttackFactor;wuzhen10IA = flst["wuzhen10"]->InterfereAbility / Factor[0].InterfereFactor;wuzhen10SA = flst["wuzhen10"]->SurAbility / Factor[0].SurFactor;cwuzhen10 = flst["wuzhen10"]->Cost;}else{wuzhen10DA = 0;wuzhen10AA = 0;wuzhen10IA = 0;wuzhen10SA = 0;cwuzhen10 = 0;}
|
|||
|
if(flst.find("j20")!= flst.end()){j20DA = flst["j20"]->DetectAbility/Factor[1].DetectFactor;j20AA = flst["j20"]->AttackAbility / Factor[1].AttackFactor;j20IA = flst["j20"]->InterfereAbility / Factor[1].InterfereFactor;j20SA = flst["j20"]->SurAbility / Factor[1].SurFactor;cj20 = flst["j20"]->Cost;}else{j20DA = 0;j20AA = 0;j20IA = 0;j20SA = 0;cj20 = 0;}
|
|||
|
if(flst.find("j11")!= flst.end()){j11DA = flst["j11"]->DetectAbility/Factor[1].DetectFactor;j11AA = flst["j11"]->AttackAbility / Factor[1].AttackFactor;j11IA = flst["j11"]->InterfereAbility / Factor[1].InterfereFactor;j11SA = flst["j11"]->SurAbility / Factor[1].SurFactor;cj11 = flst["j11"]->Cost;}else{j11DA = 0;j11AA = 0;j11IA = 0;j11SA = 0;cj11 = 0;}
|
|||
|
if(flst.find("gj11")!= flst.end()){gj11DA = flst["gj11"]->DetectAbility/Factor[0].DetectFactor; gj11AA = flst["gj11"]->AttackAbility / Factor[0].AttackFactor;gj11IA = flst["gj11"]->InterfereAbility / Factor[0].InterfereFactor;gj11SA = flst["gj11"]->SurAbility/Factor[0].SurFactor;cgj11 = flst["gj11"]->Cost;}else{gj11DA = 0;gj11AA = 0;gj11IA = 0;gj11SA = 0;cgj11 = 0;}
|
|||
|
if(flst.find("gj2")!= flst.end()){gj2DA = flst["gj2"]->DetectAbility/Factor[0].DetectFactor;gj2AA = flst["gj2"]->AttackAbility / Factor[0].AttackFactor;gj2IA = flst["gj2"]->InterfereAbility / Factor[0].InterfereFactor;gj2SA = flst["gj2"]->SurAbility/Factor[0].SurFactor;cwg2 = flst["gj2"]->Cost;}else{gj2DA = 0;gj2AA = 0;gj2IA = 0;gj2SA = 0;cwg2 = 0;}
|
|||
|
if(flst.find("h6k")!= flst.end()){h6kDA = flst["h6k"]->DetectAbility/Factor[1].DetectFactor;h6kAA = flst["h6k"]->AttackAbility / Factor[1].AttackFactor;h6kIA = flst["h6k"]->InterfereAbility / Factor[1].InterfereFactor;h6kSA = flst["h6k"]->SurAbility / Factor[1].SurFactor;ch6k = flst["h6k"]->Cost;}else{h6kDA = 0;h6kAA = 0;h6kIA = 0;h6kSA = 0;ch6k = 0;}
|
|||
|
if(flst.find("j16d")!= flst.end()){j16dDA = flst["j16d"]->DetectAbility/Factor[1].DetectFactor;j16dAA = flst["j16d"]->AttackAbility / Factor[1].AttackFactor;j16dIA = flst["j16d"]->InterfereAbility/Factor[1].InterfereFactor;j16dSA = flst["j16d"]->SurAbility / Factor[1].SurFactor;cj16d = flst["j16d"]->Cost;}else{j16dDA = 0;j16dAA = 0;j16dIA = 0;j16dSA = 0;cj16d = 0;}
|
|||
|
if(flst.find("yg9")!= flst.end()){yg9DA = flst["yg9"]->DetectAbility/Factor[1].DetectFactor;yg9AA = flst["yg9"]->AttackAbility / Factor[1].AttackFactor;yg9IA = flst["yg9"]->InterfereAbility/Factor[1].InterfereFactor;yg9SA = flst["yg9"]->SurAbility / Factor[1].SurFactor;cyg9 = flst["yg9"]->Cost;}else{yg9DA = 0;yg9AA = 0;yg9IA = 0;yg9SA = 0;cyg9 = 0;}
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD>װ<EFBFBD><D7B0>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
hm = blueLst["hangmu"];
|
|||
|
agz = blueLst["agz"];
|
|||
|
f15 = blueLst["f15ex"];
|
|||
|
zhihuisuo = blueLst["zhihuisuo"];
|
|||
|
tiangong = blueLst["tiangong"];
|
|||
|
taiwan = blueLst["taiwan"];
|
|||
|
riben = blueLst["riben"];
|
|||
|
hanguo = blueLst["hanguo"];
|
|||
|
aliboke = blueLst["aliboke"];
|
|||
|
ea18g = blueLst["ea18g"];
|
|||
|
tank = blueLst["tank"];
|
|||
|
zhuangjiache = blueLst["zhuangjiache"];
|
|||
|
gaopao = blueLst["gaopao"];
|
|||
|
|
|||
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
|
|||
|
if(slst.find("hangmu")!= slst.end()){hangmuDA = slst["hangmu"]->DetectAbility /Factor[2].DetectFactor;hangmuAA = slst["hangmu"]->AttackAbility / Factor[2].AttackFactor;hangmuIA = slst["hangmu"]->InterfereAbility/Factor[2].InterfereFactor;hangmuSA = slst["hangmu"]->SurAbility / Factor[2].SurFactor;}else{hangmuDA = 0;hangmuAA = 0;hangmuIA =0;hangmuSA = 0;}
|
|||
|
if(slst.find("taiwan")!= slst.end()){taiwanDA = slst["taiwan"]->DetectAbility /Factor[2].DetectFactor;taiwanAA = slst["taiwan"]->AttackAbility / Factor[2].AttackFactor;taiwanIA = slst["taiwan"]->InterfereAbility /Factor[2].InterfereFactor;;taiwanSA = slst["taiwan"]->SurAbility / Factor[2].SurFactor;}else{taiwanDA = 0;taiwanAA = 0;taiwanIA =0;taiwanSA = 0;}
|
|||
|
if(slst.find("riben")!= slst.end()){ribenDA = slst["riben"]->DetectAbility/Factor[2].DetectFactor;ribenAA = slst["riben"]->AttackAbility / Factor[2].AttackFactor;ribenIA = slst["riben"]->InterfereAbility/Factor[2].InterfereFactor;ribenSA = slst["riben"]->SurAbility / Factor[2].SurFactor;}else{ribenDA = 0;ribenAA = 0;ribenIA =0;ribenSA = 0;}
|
|||
|
if(slst.find("hanguo")!= slst.end()){hanguoDA = slst["hanguo"]->DetectAbility/Factor[2].DetectFactor;hanguoAA = slst["hanguo"]->AttackAbility / Factor[2].AttackFactor;hanguoIA = slst["hanguo"]->InterfereAbility/Factor[2].InterfereFactor;hanguoSA = slst["hanguo"]->SurAbility / Factor[2].SurFactor;}else{hanguoDA = 0;hanguoAA = 0;hanguoIA =0;hanguoSA = 0;}
|
|||
|
if(slst.find("aliboke")!= slst.end()){alibokeDA = slst["aliboke"]->DetectAbility/Factor[2].DetectFactor;alibokeAA = slst["aliboke"]->AttackAbility / Factor[2].AttackFactor;alibokeIA = slst["aliboke"]->InterfereAbility/Factor[2].InterfereFactor;alibokeSA = slst["aliboke"]->SurAbility / Factor[2].SurFactor;}else{alibokeDA = 0;alibokeAA = 0;alibokeIA =0;alibokeSA = 0;}
|
|||
|
if(flst.find("f15ex")!= flst.end()){f15DA = flst["f15ex"]->DetectAbility/Factor[1].DetectFactor;f15AA = flst["f15ex"]->AttackAbility * Factor[1].AttackFactor;f15IA = flst["f15ex"]->InterfereAbility / Factor[1].InterfereFactor;f15SA = flst["f15ex"]->SurAbility / Factor[1].SurFactor;}else{f15DA = 0;f15AA = 0;f15IA = 0;f15SA = 0;}
|
|||
|
if(flst.find("ea18g")!= flst.end()){ea18gDA = flst["ea18g"]->DetectAbility/Factor[1].DetectFactor;ea18gAA = flst["ea18g"]->AttackAbility / Factor[1].AttackFactor;ea18gIA = flst["ea18g"]->InterfereAbility/Factor[1].InterfereFactor;ea18gSA = flst["ea18g"]->SurAbility / Factor[1].SurFactor;}else{ea18gDA = 0;ea18gAA = 0;ea18gIA = 0;ea18gSA = 0;}
|
|||
|
if(alst.find("agz")!= alst.end()){agzDA = alst["agz"]->DetectAbility/Factor[4].DetectFactor;agzAA = alst["agz"]->AttackAbility / Factor[4].AttackFactor;agzIA = alst["agz"]->InterfereAbility / Factor[4].InterfereFactor;agzSA = alst["agz"]->SurAbility / Factor[4].SurFactor;}else{agzDA = 0;agzAA = 0;agzIA = 0;agzSA = 0;}
|
|||
|
if(alst.find("tiangong")!= alst.end()){tiangongDA = alst["tiangong"]->DetectAbility/Factor[4].DetectFactor;tiangongAA = alst["tiangong"]->AttackAbility / Factor[4].AttackFactor;tiangongIA = alst["tiangong"]->InterfereAbility / Factor[4].InterfereFactor;tiangongSA = alst["tiangong"]->SurAbility / Factor[4].SurFactor;}else{tiangongDA = 0;tiangongAA = 0;tiangongIA = 0;tiangongSA = 0;}
|
|||
|
if(glst.find("zhihuisuo")!= glst.end()){zhihuisuoDA = glst["zhihuisuo"]->DetectAbility /Factor[3].DetectFactor;zhihuisuoAA = glst["zhihuisuo"]->AttackAbility / Factor[3].AttackFactor;zhihuisuoIA = glst["zhihuisuo"]->InterfereAbility / Factor[3].InterfereFactor;zhihuisuoSA = glst["zhihuisuo"]->SurAbility / Factor[3].SurFactor;}else{zhihuisuoDA = 0;zhihuisuoAA = 0;zhihuisuoIA = 0;zhihuisuoSA = 0;}
|
|||
|
if(alst.find("gaopao")!= alst.end()){gaopaoDA = alst["gaopao"]->DetectAbility/Factor[4].DetectFactor;gaopaoAA =alst["gaopao"]->AttackAbility / Factor[4].AttackFactor;gaopaoIA = alst["gaopao"]->InterfereAbility / Factor[4].InterfereFactor;gaopaoSA = alst["gaopao"]->SurAbility / Factor[4].SurFactor;}else{gaopaoDA = 0;gaopaoAA = 0;gaopaoIA = 0;gaopaoSA = 0;}
|
|||
|
|
|||
|
// <20>Ŵ<EFBFBD><C5B4>㷨<EFBFBD><E3B7A8><EFBFBD><EFBFBD>
|
|||
|
populationSize = 50;
|
|||
|
generations = 350;
|
|||
|
mutationRate = 0.05;
|
|||
|
tournamentSize = 5;
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD>ֵ
|
|||
|
void GeneticAlgorithm::Individual::calculateFitness(GeneticAlgorithm* ga)
|
|||
|
{
|
|||
|
fitness = ga->calculateEfficiency(kj500, j16d, j20, wg2, gj11, h6k, j11, yg9, wuzhen10, kj2000);
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
// <20><>Ӧ<EFBFBD>Ⱥ<EFBFBD><C8BA><EFBFBD>
|
|||
|
double GeneticAlgorithm::logDef(double num)
|
|||
|
{
|
|||
|
return pow(num,0.5);
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD>Ч<EFBFBD>ܺ<EFBFBD><DCBA><EFBFBD>
|
|||
|
double GeneticAlgorithm::calculateEfficiency(int kj500, int j16d, int j20, int wg2, int gj11, int h6k, int j11, int yg9, int wuzhen10, int kj2000)
|
|||
|
{
|
|||
|
// <20>췽<EFBFBD><ECB7BD><EFBFBD><EFBFBD>
|
|||
|
double RedAttack = logDef(j20) * j20AA + logDef(wg2) * gj2AA + logDef(gj11) * gj11AA + logDef(h6k) * h6kAA +\
|
|||
|
logDef(j11) * j11AA + logDef(kj500) * kj500AA + logDef(kj2000) * kj2000AA +\
|
|||
|
logDef(wuzhen10) * wuzhen10AA + logDef(yg9) * yg9AA + logDef(j16d) * j16dAA;
|
|||
|
|
|||
|
// <20>췽̽<ECB7BD><CCBD>
|
|||
|
double RedDetect = logDef(kj500) * kj500DA/1300.0 + logDef(kj2000) * kj2000DA/1300.0 +\
|
|||
|
logDef(wuzhen10) * wuzhen10DA/130.0 + logDef(j16d) * j16dDA + logDef(j20) * j20DA +\
|
|||
|
logDef(wg2) * gj2DA + logDef(gj11) * gj11DA + logDef(h6k) * h6kDA + logDef(j11) * j11DA +\
|
|||
|
logDef(yg9) * yg9DA;
|
|||
|
|
|||
|
// <20>췽<EFBFBD><ECB7BD><EFBFBD><EFBFBD>
|
|||
|
double RedInterfere = logDef(j16d) * j16dIA/1300.0 + logDef(yg9) * yg9IA/1300.0 + logDef(kj500) * kj500IA +\
|
|||
|
logDef(kj2000) * kj2000IA + logDef(j20) * j20IA + logDef(wg2) * gj2IA + logDef(gj11) * gj11IA +\
|
|||
|
logDef(h6k) * h6kIA + logDef(j11) * j11IA + logDef(wuzhen10) * wuzhen10IA;
|
|||
|
|
|||
|
// <20>췽<EFBFBD><ECB7BD><EFBFBD><EFBFBD>
|
|||
|
double RedSur = logDef(kj500) * kj500SA + logDef(j16d) * j16dSA + logDef(j20) * j20SA + logDef(wg2) * gj2SA +logDef(gj11) *\
|
|||
|
gj11SA + logDef(h6k) * h6kSA + logDef(j11) * j11SA + logDef(yg9) * yg9SA + logDef(wuzhen10) * wuzhen10SA +\
|
|||
|
logDef(kj2000) * kj2000SA;
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
double BlueAttack = logDef(hm) * hangmuAA + logDef(agz) * agzAA + logDef(f15) * f15AA + logDef(tiangong) * tiangongAA +\
|
|||
|
logDef(taiwan) * taiwanAA + logDef(riben) * ribenAA + logDef(hanguo) * hanguoAA + logDef(aliboke)*\
|
|||
|
alibokeAA +logDef(gaopao) * gaopaoAA + logDef(ea18g) * ea18gAA + logDef(yg9) * yg9AA;
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD>̽<EFBFBD><CCBD>
|
|||
|
double BlueDetect = logDef(hm) * hangmuDA + logDef(taiwan) * taiwanDA + logDef(riben) * ribenDA + logDef(hanguo) * hanguoDA +\
|
|||
|
logDef(aliboke) * alibokeDA + logDef(f15) * f15DA + logDef(ea18g) * ea18gDA + logDef(gaopao) * gaopaoDA +\
|
|||
|
logDef(agz) * agzDA + logDef(tiangong) * tiangongDA;
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
double BlueInterfere = logDef(ea18g) * ea18gIA + logDef(hm) * hangmuIA + logDef(aliboke) * alibokeIA + logDef(hanguo) * hanguoIA +\
|
|||
|
logDef(riben) * ribenIA + logDef(taiwan) * taiwanIA + logDef(f15) * f15IA + logDef(agz) * agzIA +\
|
|||
|
logDef(tiangong) * tiangongIA + logDef(zhihuisuo) * zhihuisuoIA + logDef(gaopao) * gaopaoIA;
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
double BlueSur = logDef(hm) * hangmuSA + logDef(agz) * agzSA + logDef(f15) * f15SA +logDef(zhihuisuo) * zhihuisuoSA + logDef(tiangong) * tiangongSA +\
|
|||
|
logDef(taiwan) * taiwanSA + logDef(riben) *ribenSA + logDef(hanguo) * hanguoSA + logDef(aliboke) * alibokeSA + logDef(ea18g) * ea18gSA +\
|
|||
|
logDef(gaopao) * gaopaoSA;
|
|||
|
|
|||
|
double RedEffe = 0.8 * RedAttack * (0.3 *RedDetect - 0.3 *BlueInterfere) - 0.2 * BlueSur;
|
|||
|
|
|||
|
double BlueEffe = 0.8 * BlueAttack * (0.3 * BlueDetect - 0.3 * RedInterfere) - 0.2 * RedSur;
|
|||
|
|
|||
|
//double RedEffe = RedAttack * (RedDetect - BlueInterfere) - BlueSur;
|
|||
|
|
|||
|
//double BlueEffe = BlueAttack * (BlueDetect - RedInterfere) - RedSur;
|
|||
|
//if(BlueEffe <= 0)
|
|||
|
//{
|
|||
|
// BlueEffe = 1;
|
|||
|
//}
|
|||
|
if(BlueEffe <= 0)
|
|||
|
{
|
|||
|
BlueEffe = abs(RedEffe)/abs(BlueEffe-RedEffe);
|
|||
|
}
|
|||
|
|
|||
|
// <20>ɱ<EFBFBD>
|
|||
|
double C = kj500 * ckj500 + j16d * cj16d + j20 * cj20 + wg2 * cwg2 + gj11 * cgj11 + h6k * ch6k +\
|
|||
|
j11 * cj11 + yg9 * cyg9 + wuzhen10 * cwuzhen10 + kj2000 * ckj2000;
|
|||
|
if(C == 0.0) {
|
|||
|
C = 1;
|
|||
|
}
|
|||
|
|
|||
|
// Ч<><D0A7>
|
|||
|
double Effe = RedEffe/BlueEffe;
|
|||
|
|
|||
|
return Effe / C;
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
// <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>Ⱥ
|
|||
|
void GeneticAlgorithm::initializePopulation(std::vector<Individual>& population, int hm, int ali, int tw, int hg, int rb, int zhihuisuo, int f15, int agz ,int tg,int gaopao)
|
|||
|
{
|
|||
|
for (int i = 0; i < populationSize; ++i)
|
|||
|
{
|
|||
|
population[i].kj500 = rand() % (mkj500 + 1);
|
|||
|
population[i].j16d = rand() % (mj16d + 1);
|
|||
|
population[i].j20 = rand() % (mj20 + 1);
|
|||
|
population[i].wg2 = rand() % (mwg2 + 1);
|
|||
|
population[i].gj11 = rand() % (mgj11 + 1);
|
|||
|
population[i].h6k = rand() % (mh6k + 1);
|
|||
|
population[i].j11 = rand() % (mj11 + 1);
|
|||
|
population[i].yg9 = rand() % (myg9 + 1);
|
|||
|
population[i].wuzhen10 = rand() % (mwuzhen10 + 1);
|
|||
|
population[i].kj2000 = rand() % (mkj2000 + 1);
|
|||
|
population[i].calculateFitness(this);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
// ѡ<><D1A1>
|
|||
|
GeneticAlgorithm::Individual GeneticAlgorithm::tournamentSelection(const std::vector<Individual>& population)
|
|||
|
{
|
|||
|
std::vector<Individual> tournament;
|
|||
|
for (int i = 0; i < tournamentSize; ++i) {
|
|||
|
int randomIndex = rand() % populationSize;
|
|||
|
tournament.push_back(population[randomIndex]);
|
|||
|
}
|
|||
|
std::sort(tournament.begin(), tournament.end(), [](Individual a, Individual b) {
|
|||
|
return a.fitness > b.fitness;
|
|||
|
});
|
|||
|
return tournament[0];
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD>
|
|||
|
GeneticAlgorithm::Individual GeneticAlgorithm::crossover(const Individual& parent1, const Individual& parent2)
|
|||
|
{
|
|||
|
Individual offspring;
|
|||
|
offspring.kj500 = (rand() % 2) ? parent1.kj500 : parent2.kj500;
|
|||
|
offspring.j16d = (rand() % 2) ? parent1.j16d : parent2.j16d;
|
|||
|
offspring.j20 = (rand() % 2) ? parent1.j20 : parent2.j20;
|
|||
|
offspring.wg2 = (rand() % 2) ? parent1.wg2 : parent2.wg2;
|
|||
|
offspring.gj11 = (rand() % 2) ? parent1.gj11 : parent2.gj11;
|
|||
|
offspring.h6k = (rand() % 2) ? parent1.h6k : parent2.h6k;
|
|||
|
offspring.j11 = (rand() % 2) ? parent1.j11 : parent2.j11;
|
|||
|
offspring.yg9 = (rand() % 2) ? parent1.yg9 : parent2.yg9;
|
|||
|
offspring.wuzhen10 = (rand() % 2) ? parent1.wuzhen10 : parent2.wuzhen10;
|
|||
|
offspring.kj2000 = (rand() % 2) ? parent1.kj2000 : parent2.kj2000;
|
|||
|
return offspring;
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD>
|
|||
|
void GeneticAlgorithm::mutate(Individual& individual)
|
|||
|
{
|
|||
|
if (rand() / double(RAND_MAX) < mutationRate) individual.kj500 = rand() % (mkj500 + 1);
|
|||
|
if (rand() / double(RAND_MAX) < mutationRate) individual.j16d = rand() % (mj16d + 1);
|
|||
|
if (rand() / double(RAND_MAX) < mutationRate) individual.j20 = rand() % (mj20 + 1);
|
|||
|
if (rand() / double(RAND_MAX) < mutationRate) individual.wg2 = rand() % (mwg2 + 1);
|
|||
|
if (rand() / double(RAND_MAX) < mutationRate) individual.gj11 = rand() % (mgj11 + 1);
|
|||
|
if (rand() / double(RAND_MAX) < mutationRate) individual.h6k = rand() % (mh6k + 1);
|
|||
|
if (rand() / double(RAND_MAX) < mutationRate) individual.j11 = rand() % (mj11 + 1);
|
|||
|
if (rand() / double(RAND_MAX) < mutationRate) individual.yg9 = rand() % (myg9 + 1);
|
|||
|
if (rand() / double(RAND_MAX) < mutationRate) individual.wuzhen10 = rand() % (mwuzhen10 + 1);
|
|||
|
if (rand() / double(RAND_MAX) < mutationRate) individual.kj2000 = rand() % (mkj2000 + 1);
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD>㷨
|
|||
|
void GeneticAlgorithm::run()
|
|||
|
{
|
|||
|
if((hm + agz + f15 + zhihuisuo + tiangong + taiwan + riben + hanguo + aliboke + ea18g + gaopao) != 0)
|
|||
|
{
|
|||
|
srand(time(0));
|
|||
|
std::vector<Individual> population(populationSize);
|
|||
|
initializePopulation(population,hm,aliboke,taiwan,hanguo,riben,zhihuisuo,f15,agz,tiangong,gaopao);
|
|||
|
|
|||
|
for (int generation = 0; generation < generations; ++generation) {
|
|||
|
std::vector<Individual> newPopulation;
|
|||
|
|
|||
|
for (int i = 0; i < populationSize; ++i) {
|
|||
|
Individual parent1 = tournamentSelection(population);
|
|||
|
Individual parent2 = tournamentSelection(population);
|
|||
|
Individual offspring = crossover(parent1, parent2);
|
|||
|
mutate(offspring);
|
|||
|
offspring.calculateFitness(this);
|
|||
|
newPopulation.push_back(offspring);
|
|||
|
}
|
|||
|
|
|||
|
population = newPopulation;
|
|||
|
|
|||
|
// <20><>ÿ<EFBFBD><C3BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ÿ<EFBFBD><C5B8><EFBFBD>
|
|||
|
Individual bestIndividual = *std::max_element(population.begin(), population.end(), [](Individual a, Individual b) {
|
|||
|
return a.fitness < b.fitness;
|
|||
|
});
|
|||
|
|
|||
|
std::cout << "Generation " << generation + 1 << ": Best Efficiency = " << bestIndividual.fitness << std::endl;
|
|||
|
}
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ⱥ<EFBFBD><C8BA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ÿ<EFBFBD><C5B8><EFBFBD>
|
|||
|
Individual bestIndividual = *std::max_element(population.begin(), population.end(), [](Individual a, Individual b) {
|
|||
|
return a.fitness < b.fitness;
|
|||
|
});
|
|||
|
|
|||
|
//// <20><><EFBFBD>ӹ<EFBFBD><D3B9><EFBFBD>Լ<EFBFBD><D4BC>
|
|||
|
//if(hm!=0 || taiwan!=0 || riben!=0 || hanguo!=0 || aliboke!=0)
|
|||
|
//{
|
|||
|
// bestIndividual.j16d += 1;
|
|||
|
// bestIndividual.yg9 += 1;
|
|||
|
// if(bestIndividual.j16d >= mj16d)
|
|||
|
// {
|
|||
|
// bestIndividual.j16d = mj16d;
|
|||
|
// }
|
|||
|
// if(bestIndividual.yg9 >= myg9)
|
|||
|
// {
|
|||
|
// bestIndividual.yg9 = myg9;
|
|||
|
// }
|
|||
|
// // <20><><EFBFBD><EFBFBD><EFBFBD>˻<EFBFBD>
|
|||
|
// bestIndividual.wg2 += 1;
|
|||
|
// bestIndividual.gj11 += 1;
|
|||
|
// if(bestIndividual.wg2 >= mwg2)
|
|||
|
// {
|
|||
|
// bestIndividual.wg2 = mwg2;
|
|||
|
// }
|
|||
|
// if(bestIndividual.gj11 >= mgj11)
|
|||
|
// {
|
|||
|
// bestIndividual.gj11 = mgj11;
|
|||
|
// }
|
|||
|
// // <20>н<EFBFBD><D0BD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA>Ԥ<EFBFBD><D4A4><EFBFBD><EFBFBD>
|
|||
|
// if(bestIndividual.kj500 == 0 && bestIndividual.kj2000 == 0)
|
|||
|
// {
|
|||
|
// bestIndividual.kj500 += 1;
|
|||
|
// }
|
|||
|
// if(bestIndividual.kj500 >= mkj500)
|
|||
|
// {
|
|||
|
// bestIndividual.kj500 = mkj500;
|
|||
|
// }
|
|||
|
// if(bestIndividual.kj2000 >= mkj2000)
|
|||
|
// {
|
|||
|
// bestIndividual.kj2000 = mkj2000;
|
|||
|
// }
|
|||
|
//}
|
|||
|
//// ָ<><D6B8><EFBFBD><EFBFBD>ֻ<EFBFBD>ú<EFBFBD>ը<EFBFBD><D5A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˻<EFBFBD>
|
|||
|
//if(zhihuisuo!=0)
|
|||
|
//{
|
|||
|
// bestIndividual.h6k += 1;
|
|||
|
// if(bestIndividual.h6k >= mh6k)
|
|||
|
// {
|
|||
|
// bestIndividual.h6k = mh6k;
|
|||
|
// }
|
|||
|
// // <20><><EFBFBD><EFBFBD><EFBFBD>˻<EFBFBD>
|
|||
|
// bestIndividual.wg2 += 1;
|
|||
|
// bestIndividual.gj11 += 1;
|
|||
|
// if(bestIndividual.wg2 >= mwg2)
|
|||
|
// {
|
|||
|
// bestIndividual.wg2 = mwg2;
|
|||
|
// }
|
|||
|
// if(bestIndividual.gj11 >= mgj11)
|
|||
|
// {
|
|||
|
// bestIndividual.gj11 = mgj11;
|
|||
|
// }
|
|||
|
//}
|
|||
|
//if(f15!=0)
|
|||
|
//{
|
|||
|
// bestIndividual.j20 += 1;
|
|||
|
// bestIndividual.j11 += 1;
|
|||
|
// if(bestIndividual.j20 >= mj20)
|
|||
|
// {
|
|||
|
// bestIndividual.j20 = mj20;
|
|||
|
// }
|
|||
|
// if(bestIndividual.j11 >= mj11)
|
|||
|
// {
|
|||
|
// bestIndividual.j11 = mj11;
|
|||
|
// }
|
|||
|
// // <20><><EFBFBD><EFBFBD><EFBFBD>˻<EFBFBD>
|
|||
|
// bestIndividual.wg2 += 1;
|
|||
|
// bestIndividual.gj11 += 1;
|
|||
|
// if(bestIndividual.wg2 >= mwg2)
|
|||
|
// {
|
|||
|
// bestIndividual.wg2 = mwg2;
|
|||
|
// }
|
|||
|
// if(bestIndividual.gj11 >= mgj11)
|
|||
|
// {
|
|||
|
// bestIndividual.gj11 = mgj11;
|
|||
|
// }
|
|||
|
//}
|
|||
|
//if(agz!=0 || tiangong!=0 || gaopao != 0 )
|
|||
|
//{
|
|||
|
// bestIndividual.wg2 += 1;
|
|||
|
// bestIndividual.gj11 += 1;
|
|||
|
// if(bestIndividual.wg2 >= mwg2)
|
|||
|
// {
|
|||
|
// bestIndividual.wg2 = mwg2;
|
|||
|
// }
|
|||
|
// if(bestIndividual.gj11 >= mgj11)
|
|||
|
// {
|
|||
|
// bestIndividual.gj11 = mgj11;
|
|||
|
// }
|
|||
|
//}
|
|||
|
//// <20><>װ<EFBFBD>׳<EFBFBD><D7B3><EFBFBD>̹<EFBFBD>˾<EFBFBD><CBBE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˻<EFBFBD>,<2C><>һ<EFBFBD><D2BB>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
//if(tank!=0 || zhuangjiache!=0)
|
|||
|
//{
|
|||
|
// bestIndividual.wg2 += tank;
|
|||
|
// bestIndividual.gj11 += zhuangjiache;
|
|||
|
// if(bestIndividual.wg2 >= mwg2)
|
|||
|
// {
|
|||
|
// bestIndividual.wg2 = mwg2;
|
|||
|
// }
|
|||
|
// if(bestIndividual.gj11 >= mgj11)
|
|||
|
// {
|
|||
|
// bestIndividual.gj11 = mgj11;
|
|||
|
// }
|
|||
|
//}
|
|||
|
//if(gaopao!=0)
|
|||
|
//{
|
|||
|
// bestIndividual.j20 += 1;
|
|||
|
// bestIndividual.j11 += 1;
|
|||
|
// if(bestIndividual.j20 >= mj20)
|
|||
|
// {
|
|||
|
// bestIndividual.j20 = mj20;
|
|||
|
// }
|
|||
|
// if(bestIndividual.j11 >= mj11)
|
|||
|
// {
|
|||
|
// bestIndividual.j11 = mj11;
|
|||
|
// }
|
|||
|
//}
|
|||
|
//// h6k<36><6B><EFBFBD><EFBFBD>ս<EFBFBD><D5BD>
|
|||
|
//if((bestIndividual.h6k!=0) && (hm==0) && (taiwan==0) &&\
|
|||
|
// (riben==0) && (hanguo==0) && (aliboke==0) && (zhihuisuo==0) &&\
|
|||
|
// (agz==0) && (gaopao==0) && (tiangong==0) && (tank==0) && (zhuangjiache==0))
|
|||
|
//{
|
|||
|
// bestIndividual.h6k = 0;
|
|||
|
//}
|
|||
|
//// gj2<6A><32><EFBFBD><EFBFBD>ս<EFBFBD><D5BD>
|
|||
|
//if((bestIndividual.wg2!=0) && (hm==0) && (taiwan==0) &&\
|
|||
|
// (riben==0) && (hanguo==0) && (aliboke==0) && (zhihuisuo==0) &&\
|
|||
|
// (agz==0) && (gaopao==0) && (tiangong==0) && (tank==0) && (zhuangjiache==0))
|
|||
|
//{
|
|||
|
// bestIndividual.wg2 = 0;
|
|||
|
//}
|
|||
|
//// gj11<31><31><EFBFBD><EFBFBD>ս<EFBFBD><D5BD>
|
|||
|
//if((bestIndividual.gj11!=0) && (hm==0) && (taiwan==0) &&\
|
|||
|
// (riben==0) && (hanguo==0) && (aliboke==0) && (zhihuisuo==0) &&\
|
|||
|
// (agz==0) && (gaopao==0) && (tiangong==0) && (tank==0) && (zhuangjiache==0))
|
|||
|
//{
|
|||
|
// bestIndividual.gj11 = 0;
|
|||
|
//}
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɷ<EFBFBD><C9B7><EFBFBD>
|
|||
|
std::cout << "------------------------------------------------------------------------" <<std::endl;
|
|||
|
std::cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɷ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>"<< std::endl;
|
|||
|
std::cout <<"kj500: " << bestIndividual.kj500<< std::endl;
|
|||
|
std::cout << "j16d: " << bestIndividual.j16d<< std::endl;
|
|||
|
std::cout << "j20: " << bestIndividual.j20<< std::endl;
|
|||
|
std::cout << "wg2: " << bestIndividual.wg2<< std::endl;
|
|||
|
std::cout << "gj11: " << bestIndividual.gj11<< std::endl;
|
|||
|
std::cout << "h6k: " << bestIndividual.h6k<< std::endl;
|
|||
|
std::cout << "j11: " << bestIndividual.j11<< std::endl;
|
|||
|
std::cout << "yg9: " << bestIndividual.yg9<< std::endl;
|
|||
|
std::cout << "wuzhen10: " << bestIndividual.wuzhen10<< std::endl;
|
|||
|
std::cout << "kj2000: " << bestIndividual.kj2000<< std::endl;
|
|||
|
}
|
|||
|
else if(((hm + agz + f15 + zhihuisuo + tiangong + taiwan + riben + hanguo + aliboke + ea18g + gaopao) == 0) && (tank != 0) || (zhuangjiache != 0))
|
|||
|
{
|
|||
|
Individual bestIndividual;
|
|||
|
bestIndividual.wg2 = tank;
|
|||
|
bestIndividual.gj11 = zhuangjiache;
|
|||
|
std::cout << "------------------------------------------------------------------------" <<std::endl;
|
|||
|
std::cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɷ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>"<< std::endl;
|
|||
|
std::cout <<"kj500: " << bestIndividual.kj500<< std::endl;
|
|||
|
std::cout << "j16d: " << bestIndividual.j16d<< std::endl;
|
|||
|
std::cout << "j20: " << bestIndividual.j20<< std::endl;
|
|||
|
std::cout << "wg2: " << bestIndividual.wg2<< std::endl;
|
|||
|
std::cout << "gj11: " << bestIndividual.gj11<< std::endl;
|
|||
|
std::cout << "h6k: " << bestIndividual.h6k<< std::endl;
|
|||
|
std::cout << "j11: " << bestIndividual.j11<< std::endl;
|
|||
|
std::cout << "yg9: " << bestIndividual.yg9<< std::endl;
|
|||
|
std::cout << "wuzhen10: " << bestIndividual.wuzhen10<< std::endl;
|
|||
|
std::cout << "kj2000: " << bestIndividual.kj2000<< std::endl;
|
|||
|
}
|
|||
|
else if((hm + agz + f15 + zhihuisuo + tiangong + taiwan + riben + hanguo + aliboke + ea18g + gaopao + tank + zhuangjiache) == 0)
|
|||
|
{
|
|||
|
Individual bestIndividual;
|
|||
|
bestIndividual.kj500 = 0;
|
|||
|
bestIndividual.j16d = 0;
|
|||
|
bestIndividual.j20 = 0;
|
|||
|
bestIndividual.wg2 = 0;
|
|||
|
bestIndividual.gj11 = 0;
|
|||
|
bestIndividual.h6k = 0;
|
|||
|
bestIndividual.j11 = 0;
|
|||
|
bestIndividual.yg9 = 0;
|
|||
|
bestIndividual.wuzhen10 = 0;
|
|||
|
bestIndividual.kj2000 = 0;
|
|||
|
std::cout << "------------------------------------------------------------------------" <<std::endl;
|
|||
|
std::cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɷ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>"<< std::endl;
|
|||
|
std::cout <<"kj500: " << bestIndividual.kj500<< std::endl;
|
|||
|
std::cout << "j16d: " << bestIndividual.j16d<< std::endl;
|
|||
|
std::cout << "j20: " << bestIndividual.j20<< std::endl;
|
|||
|
std::cout << "wg2: " << bestIndividual.wg2<< std::endl;
|
|||
|
std::cout << "gj11: " << bestIndividual.gj11<< std::endl;
|
|||
|
std::cout << "h6k: " << bestIndividual.h6k<< std::endl;
|
|||
|
std::cout << "j11: " << bestIndividual.j11<< std::endl;
|
|||
|
std::cout << "yg9: " << bestIndividual.yg9<< std::endl;
|
|||
|
std::cout << "wuzhen10: " << bestIndividual.wuzhen10<< std::endl;
|
|||
|
std::cout << "kj2000: " << bestIndividual.kj2000<< std::endl;
|
|||
|
}
|
|||
|
}
|