Commit 4805137f authored by 刘乐's avatar 刘乐

1,算法修改

parent 561c5284
......@@ -9,15 +9,26 @@
#include "CivHydrTest.h"
#include <vector>
#include <string>
#include <stdlib.h>
#include <windows.h>
#include <consoleapi2.h>
using namespace std;
float random()
{
return rand() / (RAND_MAX + 1.0);
}
int main(int argc, char* argv[])
{
srand((unsigned)time(NULL)); /*这一句dao很重要,zhi如果没有的话,每dao次产生内的数据都是不变的。*/
for (int i = 0; i < 10; i++)
{
printf("%d",rand()%2);/*产生10个0到容1的随机数*/
}
while (true)
{
const char* uri = "host=192.168.19.100 port=5432 dbname=JinXian user=postgres password=admin";
......
......@@ -2,24 +2,30 @@
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <random>
#include "OptScheduling.h"
GenAlg::GenAlg(OptScheduling* optScheduling):mOptScheduling(std::unique_ptr<OptScheduling>(optScheduling))
GenAlg::GenAlg(OptScheduling* optScheduling,GenType genType)
:mOptScheduling(std::unique_ptr<OptScheduling>(optScheduling)),mGenType(genType)
{
}
int GenAlg::random()
double GenAlg::random(int min , int max)
{
srand((unsigned)time(NULL));
return rand();
double m1 = (double)(rand() % 101) / 101; // 计算 0,1之间的随机小数,得到的值域近似为(0,1)
min++; //将 区间变为(min+1,max),
double m2 = (double)((rand() % (max - min + 1)) + min); //计算 min+1,max 之间的随机整数,得到的值域为[min+1,max]
m2 = m2 - 1; //令值域为[min,max-1]
return m1 + m2; //返回值域为(min,max),为所求随机浮点
}
void GenAlg::init(int popsize, double MutRate, double CrossRate, int GenLenght, double LeftPoint, double RightPoint)
void GenAlg::init(int popsize, double MutRate, double CrossRate, int GenLength, double LeftPoint, double RightPoint)
{
mPopSize = popsize;
mMutationRate = MutRate;
mCrossoverRate = CrossRate;
mChromoLength = GenLenght;
mChromoLength = GenLength;
mTotalFitness = 0;
mGeneration = 0;
......@@ -41,7 +47,18 @@ void GenAlg::init(int popsize, double MutRate, double CrossRate, int GenLenght,
// 把所有的基因编码初始化为函数区间内的随机数。
for (int j = 0; j < mChromoLength; j++)
{
vecPop[i].vecGenome.push_back(random() * (mRightPoint - mLeftPoint) + mLeftPoint);
switch (mGenType)
{
case RealValueCoding:
vecPop[i].vecGenome.push_back(random(0,MAX_RANDOM) * (mRightPoint - mLeftPoint) + mLeftPoint);
break;
case BinaryCoding:
vecPop[i].mBinaryGenVec.push_back((int)random() % 2);
break;
default:
break;
}
}
}
}
......@@ -52,7 +69,7 @@ void GenAlg::mutate(vector<double>& chromo)
for (int i = 0; i < chromo.size(); ++i)
{
// 如果发生突变的话
if (random() < mMutationRate)
if (random(0,1) < mMutationRate)
{
// 使该权值增加或者减少一个很小的随机数值
chromo[i] += ((random() - 0.5) * mMaxPerturbation);
......@@ -70,10 +87,90 @@ void GenAlg::mutate(vector<double>& chromo)
}
}
void GenAlg::mutate(vector<char>& chromo)
{
// 遵循预定的突变概率,对基因进行突变
for (int i = 0; i < chromo.size(); ++i)
{
// 如果发生突变的话
if (random(0,1) < mMutationRate)
{
chromo[i] =( chromo[i]&0x01) ^ 0x01;
}
}
}
void GenAlg::select()
{
}
int GenAlg::nextRandom(int total, vector<int> visited)
{
int index = random(0, total);
std::vector<int>::iterator iter = std::find(visited.begin(), visited.end(), index);
if (iter != visited.end())
return nextRandom(total, visited);
visited.push_back(index);
return index;
}
void GenAlg::corssver()
{
// 染色体个数
int popSize = vecPop.size();
// 种群数组的索引
std::vector<int> randIndexVec;
for (int i = 0; i < popSize; i++)
randIndexVec.push_back(i);
// 随机排序,为了随机配对染色体
std::random_shuffle(randIndexVec.begin(), randIndexVec.end());
int left = 0;
int right = popSize-1;
while (left < right)
{
int indexLeft = randIndexVec[left];
int indexRight = randIndexVec[right];
// 两个染色体互换
crossover(vecPop[indexLeft].mBinaryGenVec, vecPop[indexRight].mBinaryGenVec);
left++;
right--;
}
}
void GenAlg::crossover(vector<char>& chromo1, vector<char>& chromo2)
{
if (chromo1.size() != chromo2.size())
return;
int total = chromo1.size();
// 基因交叉重组
for (int i = 0; i < total; i++)
{
// 满足交叉概率
if (random(0, 1) < mCrossoverRate)
{
int index = random(0, total);
for (int i = index; i < total; i++)
{
swap(chromo1[i], chromo2[i]);
}
}
}
}
Genome GenAlg::chromoRoulette()
{
// 产生一个 0 到种群总适应性评分总和之间的随机数. mTotalFitness记录了整个种群的适应性分数总和
double slice = (random()) * mTotalFitness;
double slice = (random(0,1)) * mTotalFitness;
// 这个基因将承载转盘所选出来的那个个体.
Genome theChosenOne;
......@@ -87,7 +184,7 @@ Genome GenAlg::chromoRoulette()
//累计适应性分数.
fitnessSoFar += vecPop[i].fitness;
//如果累计分数大于随机数,就选择此时的基因.
// 如果累计分数大于随机数,就选择此时的基因.
if (fitnessSoFar >= slice)
{
theChosenOne = vecPop[i];
......@@ -100,12 +197,46 @@ Genome GenAlg::chromoRoulette()
void GenAlg::calculateBestWorstAvTot()
{
int total = vecPop.size();
for (int i = 0; i < total; i++)
{
double fitNessTemp = vecPop[i].fitness;
// 计算总适应度值
mTotalFitness += fitNessTemp;
// 查找最好适应度值
if (mBestFitness < fitNessTemp)
mBestFitness = fitNessTemp;
// 查找最差适应度值
if (mWorstFitness > fitNessTemp)
mWorstFitness = fitNessTemp;
}
// 计算平均适应度
mAverageFitness = mTotalFitness / (double)total;
}
void GenAlg::reset()
{
mPopSize = 0;
mMutationRate = 0;
mCrossoverRate =0;
mChromoLength = 0;
mTotalFitness = 0;
mGeneration = 0;
mBestFitness = 0.0;
mWorstFitness = 99999999;
mAverageFitness = 0;
mMaxPerturbation = 0.001;
mLeftPoint =0;
mRightPoint = 0;
// 清空种群容器,以初始化
vecPop.clear();
}
void GenAlg::epoch(vector<Genome>& vecNewPop)
......@@ -115,12 +246,36 @@ void GenAlg::epoch(vector<Genome>& vecNewPop)
Genome GenAlg::bestFitness()
{
Genome genome;
return genome;
int maxIndex = -1;
for (int i = 0; i < vecPop.size(); i++)
{
if (mBestFitness == vecPop[i].fitness)
maxIndex = i;
}
// 没找到构造一个空的基因型对象
if (maxIndex == -1)
return Genome();
return vecPop[maxIndex];
}
double GenAlg::averageFitness()
{
return 0;
return mAverageFitness;
}
void GenAlg::fitnessfunction()
{
}
void GenAlg::encoding()
{
}
void GenAlg::decoding()
{
}
\ No newline at end of file
......@@ -2,20 +2,31 @@
#include "pandaAlgorithm.h"
#include "genome.h"
#include <vector>
#include <memory>
#include <vector>
#include <time.h>
#include<stdlib.h>
using namespace std;
class OptScheduling;
#define MAX_RANDOM 2147483647.0
class OptScheduling;
/**
遗传算法引擎
*/
class GenAlg
class PANDAALGORITHM_API GenAlg
{
public:
// 基因编码类型
enum GenType
{
RealValueCoding,
BinaryCoding
};
//构造函数
GenAlg(OptScheduling* optScheduling);
GenAlg(OptScheduling* optScheduling, GenType genType);
//初始化变量
void reset();
......@@ -29,9 +40,13 @@ public:
// 轮盘赌选择函数
Genome chromoRoulette();
// 基因变异函数
// 基因变异函数(实值编码)
void mutate(vector<double>& chromo);
// 基因变异函数(二进制编码)
void mutate(vector<char>& chromo);
//这函数产生新一代基因
void epoch(vector<Genome>& vecNewPop);
......@@ -41,13 +56,36 @@ public:
// 获取平均适应度
double averageFitness();
// 产生随机数
int random();
// 所有基因重组
void corssver();
// 选择运算,前群体中适应度较高的个体按某种规则或模型遗传到下一代群体中
void select();
// 计算个体自适应度
void fitnessfunction();
// 编码,
void encoding();
// 解码
void decoding();
private:
// 产生[a,b)之间的浮点数
double random(int a=0, int b= RAND_MAX);
int nextRandom(int total, vector<int> visited);
// 两个染色体交叉运算
void crossover(vector<char>& chromo1, vector<char>& chromo2);
private:
// 这个容器将储存每一个个体的染色体
vector <Genome> vecPop;
// 基因编码类型
GenType mGenType;
// 种群数量
int mPopSize;
......
......@@ -2,18 +2,22 @@
#include <vector>
/**
染色体
染色体,实值编码
*/
class Genome
{
public:
friend class GenAlg;
friend class GenEngine;
Genome() :fitness(0) {}
Genome(std::vector <double> vec, double f) : vecGenome(vec), fitness(f) {} //类的带参数初始化参数。
Genome(std::vector <char> vec, double f) :mBinaryGenVec(vec), fitness(f) {}
private:
std::vector<double> vecGenome; // 装载基因的容器
std::vector<char> mBinaryGenVec; // 二进制编码
double fitness; // 适应度
};
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment