tuiliji/GenAlogrithm/GeneticAlgorithm.h

209 lines
4.1 KiB
C++

#ifndef GENETICALGORITHM_H
#define GENETICALGORITHM_H
#pragma once
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <cstdlib>
#include<map>
#include"datadef.h"
#include <string>
class GeneticAlgorithm {
public:
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);
void init();
void run();
private:
// 红方装备要素数量限制
int mkj500;
int mj16d;
int mj20;
int mwg2;
int mgj11;
int mh6k;
int mj11;
int myg9;
int mwuzhen10;
int mkj2000;
// 红方装备要素单个成本
double ckj500;
double cj16d;
double cj20;
double cwg2;
double cgj11;
double ch6k;
double cj11;
double cyg9;
double cwuzhen10;
double ckj2000;
//----------------------------------------------------------------------//
// 红蓝双方各个装备要素的能力值
// 探测
double kj500DA;
double kj2000DA;
double wuzhen10DA;
double hangmuDA;
double taiwanDA;
double ribenDA;
double hanguoDA;
double alibokeDA;
// 新增
double j16dDA;
double j20DA;
double gj2DA;
double gj11DA;
double h6kDA;
double j11DA;
double yg9DA;
double f15DA;
double ea18gDA;
double gaopaoDA;
double zhihuisuoDA;
double agzDA;
double tiangongDA;
// 攻击
double j20AA;
double j11AA;
double gj11AA;
double gj2AA;
double h6kAA;
double f15AA;
double taiwanAA;
double ribenAA;
double hanguoAA;
double alibokeAA;
double hangmuAA;
double agzAA;
double tiangongAA;
double gaopaoAA;
// 新增
double kj500AA;
double kj2000AA;
double wuzhen10AA;
double ea18gAA;
double yg9AA;
double j16dAA;
double zhihuisuoAA;
// 干扰
double j16dIA;
double yg9IA;
double ea18gIA;
double hangmuIA;
double alibokeIA;
double hanguoIA;
double ribenIA;
double taiwanIA;
// 新增
double kj500IA;
double kj2000IA;
double j20IA;
double gj2IA;
double gj11IA;
double h6kIA;
double j11IA;
double wuzhen10IA;
double f15IA;
double agzIA;
double tiangongIA;
double zhihuisuoIA;
double gaopaoIA;
// 生存
double kj500SA;
double kj2000SA;
double j16dSA;
double j20SA;
double gj2SA;
double gj11SA;
double h6kSA;
double j11SA;
double wuzhen10SA;
double yg9SA;
double f15SA;
double taiwanSA;
double ribenSA;
double hanguoSA;
double alibokeSA;
double hangmuSA;
double agzSA;
double tiangongSA;
double zhihuisuoSA;
double ea18gSA;
double gaopaoSA;
//----------------------------------------------------------------------//
// 蓝方装备要素个数
int hm;
int agz;
int f15;
int zhihuisuo;
int tiangong;
int taiwan;
int riben;
int hanguo;
int aliboke;
int ea18g;
int zhuangjiache;
int tank;
int gaopao;
// 能力缩放系数,干扰的能力不同意不设变量,防空系统和指挥所的探测不设变量
double wurenjiFactor;
double yourenjiFactor;
double shipFactor;
double zhihuisuoFactor;
double fighterdectectFactor;
double lanfangdetectFactor;
public:
struct Individual {
int kj500, j16d, j20, wg2, gj11, h6k, j11, yg9, wuzhen10,kj2000;
double fitness;
Individual() : kj500(0), j16d(0), j20(0), wg2(0), gj11(0), h6k(0), j11(0), yg9(0), wuzhen10(0), kj2000(0), fitness(0.0) {}
void calculateFitness(GeneticAlgorithm* ga);
};
private:
// 能力系数
struct AbilityFactor {
double DetectFactor;
double AttackFactor;
double InterfereFactor;
double SurFactor;
};
std::vector<AbilityFactor> Factor;
// 遗传算法超参数
int populationSize;
int generations;
double mutationRate;
int tournamentSize;
double logDef(double num);
double calculateEfficiency(int kj500, int j16d, int j20, int wg2, int gj11, int h6k, int j11, int yg9, int wuzhen10, int kj2000);
void 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);
Individual tournamentSelection(const std::vector<Individual>& population);
Individual crossover(const Individual& parent1, const Individual& parent2);
void mutate(Individual& individual);
};
#endif // GENETICALGORITHM_H