Commit 565e3aa3 authored by 刘乐's avatar 刘乐

1,优化调度模型接口定义封装

parent d32ecb9c
......@@ -41,7 +41,7 @@ double FirstOptScheduling::pumpPower(const PumpStruct& pumpParam)
for (int j = 0; j < columns; j++)
{
temp +=( (a*pumpParam.mNS[i][j]*pumpParam.mQS[i][j] \
*pumpParam.mHS[i][j])/(pumpParam.mK[i][j]*pumpParam.mY[i][j]));
*pumpParam.mHS[i][j])/(pumpParam.mX[i][j]*pumpParam.mY[i][j]));
}
temp *= pumpParam.mS2[i];
result += temp;
......
#include "GenAlg.h"
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
GenAlg::GenAlg(OptScheduling* optScheduling):mOptScheduling(std::unique_ptr<OptScheduling>(optScheduling))
{
}
int GenAlg::random()
{
srand((unsigned)time(NULL));
return rand();
}
void GenAlg::init(int popsize, double MutRate, double CrossRate, int GenLenght, double LeftPoint, double RightPoint)
{
mPopSize = popsize;
mMutationRate = MutRate;
mCrossoverRate = CrossRate;
mChromoLength = GenLenght;
mTotalFitness = 0;
mGeneration = 0;
mBestFitness = 0.0;
mWorstFitness = 99999999;
mAverageFitness = 0;
mMaxPerturbation = 0.001;
mLeftPoint = LeftPoint;
mRightPoint = RightPoint;
// 清空种群容器,以初始化
vecPop.clear();
for (int i = 0; i < mPopSize; i++)
{
// 类的构造函数已经把适应性评分初始化为0
vecPop.push_back(Genome());
// 把所有的基因编码初始化为函数区间内的随机数。
for (int j = 0; j < mChromoLength; j++)
{
vecPop[i].vecGenome.push_back(random() * (mRightPoint - mLeftPoint) + mLeftPoint);
}
}
}
void GenAlg::mutate(vector<double>& chromo)
{
// 遵循预定的突变概率,对基因进行突变
for (int i = 0; i < chromo.size(); ++i)
{
// 如果发生突变的话
if (random() < mMutationRate)
{
// 使该权值增加或者减少一个很小的随机数值
chromo[i] += ((random() - 0.5) * mMaxPerturbation);
// 左右边界
if (chromo[i] < mLeftPoint)
{
chromo[i] = mRightPoint;
}
else if (chromo[i] > mRightPoint)
{
chromo[i] = mLeftPoint;
}
}
}
}
Genome GenAlg::chromoRoulette()
{
// 产生一个 0 到种群总适应性评分总和之间的随机数. mTotalFitness记录了整个种群的适应性分数总和
double slice = (random()) * mTotalFitness;
// 这个基因将承载转盘所选出来的那个个体.
Genome theChosenOne;
// 累计适应性分数的和.
double fitnessSoFar = 0;
// 遍历总种群里面的每一条染色体。
for (int i = 0; i < mPopSize; ++i)
{
//累计适应性分数.
fitnessSoFar += vecPop[i].fitness;
//如果累计分数大于随机数,就选择此时的基因.
if (fitnessSoFar >= slice)
{
theChosenOne = vecPop[i];
break;
}
}
//返回转盘选出来的个体基因
return theChosenOne;
}
void GenAlg::calculateBestWorstAvTot()
{
}
void GenAlg::reset()
{
}
void GenAlg::epoch(vector<Genome>& vecNewPop)
{
}
Genome GenAlg::bestFitness()
{
Genome genome;
return genome;
}
double GenAlg::averageFitness()
{
return 0;
}
#pragma once
#include "pandaAlgorithm.h"
#include "genome.h"
#include <vector>
#include <memory>
using namespace std;
class OptScheduling;
/**
遗传算法引擎
*/
class GenAlg
{
public:
//构造函数
GenAlg(OptScheduling* optScheduling);
//初始化变量
void reset();
//初始化函数
void init(int popsize, double MutRate, double CrossRate, int GenLenght, double LeftPoint, double RightPoint);
// 计算mTotalFitness, mBestFitness, mWorstFitness, mAverageFitness等变量
void calculateBestWorstAvTot();
// 轮盘赌选择函数
Genome chromoRoulette();
// 基因变异函数
void mutate(vector<double>& chromo);
//这函数产生新一代基因
void epoch(vector<Genome>& vecNewPop);
// 获取最好适应度的基因组
Genome bestFitness();
// 获取平均适应度
double averageFitness();
// 产生随机数
int random();
private:
// 这个容器将储存每一个个体的染色体
vector <Genome> vecPop;
// 种群数量
int mPopSize;
// 每一条染色体的基因的总数目
int mChromoLength;
// 所有个体对应的适应性评分的总和
double mTotalFitness;
//在所有个体当中最适应的个体的适应性评分
double mBestFitness;
// 所有个体的适应性评分的平均值
double mAverageFitness;
// 在所有个体当中最不适应的个体的适应性评分
double mWorstFitness;
// 最适应的个体在m_vecPop容器里面的索引号
Genome mFittestGenome;
// 基因突变的概率,一般介于0.05和0.3之间
double mMutationRate;
// 基因交叉的概率一般设为0.7
double mCrossoverRate;
// 代数的记数器
int mGeneration;
// 最大变异步长
double mMaxPerturbation;
// 左边界
double mLeftPoint;
// 右边界
double mRightPoint;
// 优化调度函数
std::unique_ptr<OptScheduling> mOptScheduling;
};
#include "GenEngine.h"
GenEngine::GenEngine()
{
}
\ No newline at end of file
#pragma once
/**
Żģ
¿Í»§¶Ë
*/
class CivOptSchedSimulation
class GenEngine
{
public:
CivOptSchedSimulation();
bool optimal();
explicit GenEngine();
private:
......
#pragma once
#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) {} //类的带参数初始化参数。
private:
std::vector<double> vecGenome; // 装载基因的容器
double fitness; // 适应度
};
......@@ -10,6 +10,12 @@ OptScheduling::~OptScheduling()
}
double OptScheduling::objectiveFunction()
{
double res = 0;
return res;
}
double OptScheduling::pressureDifference(const std::vector<double>& HC, const std::vector<double>& HF)
{
double res = 0;
......
......@@ -9,7 +9,6 @@ public:
explicit OptScheduling();
virtual ~OptScheduling();
/**
水泵参数
*/
......@@ -27,6 +26,8 @@ public:
std::vector<int> mP; //第i泵站中调速泵的台数
};
virtual double objectiveFunction();
/**
* @brief 供需压差函数
* @param [in] HC[i]: 测压点i预测压力值
......
#include "OptSchedulingGenetic.h"
OptSchedulingGenetic::OptSchedulingGenetic(std::shared_ptr<OptScheduling*>& optPtr)
:mOptPtr(optPtr)
{
}
OptSchedulingGenetic::~OptSchedulingGenetic()
{
}
void OptSchedulingGenetic::initPop()
{
}
void OptSchedulingGenetic::fitNess()
{
}
void OptSchedulingGenetic::select()
{
}
void OptSchedulingGenetic::coding()
{
}
void OptSchedulingGenetic::decoding()
{
}
void OptSchedulingGenetic::crossover()
{
}
void OptSchedulingGenetic::variation()
{
}
void OptSchedulingGenetic::update()
{
}
#pragma once
#include "genetic.h"
#include "OptScheduling.h"
#include "pandaAlgorithm.h"
#include<memory>
/**
* 具体的遗传优化算法
*/
class PANDAALGORITHM_API OptSchedulingGenetic :public Genetic
{
public:
explicit OptSchedulingGenetic(std::shared_ptr<OptScheduling*> &optPtr);
~OptSchedulingGenetic();
/**
*@brief 初始化种群
*/
virtual void initPop();
/**
*@brief 计算种群自适应度
*/
virtual void fitNess();
/**
*@brief 选择
*/
virtual void select();
/**
*@brief 编码
*/
virtual void coding();
/**
*@brief 解码
*/
virtual void decoding();
/**
*@brief 交叉
*/
virtual void crossover();
/**
*@brief 选择
*/
virtual void variation();
/**
*@brief 更新种
*
*/
virtual void update();
private:
std::shared_ptr<OptScheduling*> mOptPtr;
};
#pragma once
/**
Żȱվ
*/
class SecondOptScheduling
{
public:
SecondOptScheduling();
};
#include "genetic.h"
Genetic::Genetic()
{
}
Genetic::~Genetic()
{
}
void Genetic::initPop()
{
}
void Genetic::fitNess()
{
}
void Genetic::select()
{
}
void Genetic::coding()
{
}
void Genetic::decoding()
{
}
void Genetic::crossover()
{
}
void Genetic::variation()
{
}
void Genetic::update()
{
}
\ No newline at end of file
#pragma once
#include "pandaAlgorithm.h"
/**
遗传算法接口
*/
class PANDAALGORITHM_API Genetic
{
public:
Genetic();
virtual ~Genetic();
/**
*@brief 初始化种群
*/
virtual void initPop();
/**
*@brief 计算种群自适应度
*/
virtual void fitNess();
/**
*@brief 选择
*/
virtual void select();
/**
*@brief 编码
*/
virtual void coding();
/**
*@brief 解码
*/
virtual void decoding();
/**
*@brief 交叉
*/
virtual void crossover();
/**
*@brief 选择
*/
virtual void variation();
/**
*@brief 更新种群
*/
virtual void update();
void setNumPop(int numPop) { mNumPop = numPop; }
int numPop() const { return mNumPop; }
void rseRtangeL(int range) { mIrangeL = range; }
int rangeL() const { return mIrangeL; }
void setRangeR(int range) { mIrangeR = range; }
int rangeR() const { return mIrangeR; }
// 设置编码长度
void setLength(int len) { mLength = len; }
int length() const { return mLength; }
void setIteration(int iterator) { mIteration = iterator; }
int iteration() const { return mIteration; }
// 杂焦虑
double crossoverRate() const { return mCrossoverRate; }
void setCrossverRate(double verRate) { mCrossoverRate = verRate; }
double selectRate() const { return mSelectRate; }
void setSelectRate(double selRate) { mSelectRate = selRate; }
double variationRate() const { return mVariationRate; }
void setVariationRate(double rate) { mVariationRate = rate; }
private:
int mNumPop = 100; // 初始种群大小
int mIrangeL = -1; // 问题解区间
int mIrangeR = 2;
int mLength = 22; // 二进制编码长度
int mIteration = 10000; // 迭代次数
double mCrossoverRate = 0.7; // 杂交率
double mSelectRate = 0.5; // 选择率
double mVariationRate = 0.001; //变异率
};
......@@ -143,22 +143,23 @@
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
<PostBuildEvent>
<Command>copy pandaAlgorithm.h $(OutDir)..\include /y
copy genetic.h $(OutDir)..\include /y</Command>
<Command>copy pandaAlgorithm.h $(OutDir)..\include /y</Command>
</PostBuildEvent>
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="FirstOptScheduling.h" />
<ClInclude Include="genetic.h" />
<ClInclude Include="GenAlg.h" />
<ClInclude Include="GenEngine.h" />
<ClInclude Include="Genome.h" />
<ClInclude Include="OptScheduling.h" />
<ClInclude Include="OptSchedulingGenetic.h" />
<ClInclude Include="pandaAlgorithm.h" />
<ClInclude Include="SecondOptScheduling.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="FirstOptScheduling.cpp" />
<ClCompile Include="genetic.cpp" />
<ClCompile Include="GenAlg.cpp" />
<ClCompile Include="GenEngine.cpp" />
<ClCompile Include="OptScheduling.cpp" />
<ClCompile Include="OptSchedulingGenetic.cpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
......
......@@ -15,9 +15,6 @@
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="genetic.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="pandaAlgorithm.h">
<Filter>头文件</Filter>
</ClInclude>
......@@ -27,21 +24,30 @@
<ClInclude Include="FirstOptScheduling.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="OptSchedulingGenetic.h">
<ClInclude Include="GenAlg.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="GenEngine.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="Genome.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="SecondOptScheduling.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="genetic.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="OptScheduling.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="FirstOptScheduling.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="OptSchedulingGenetic.cpp">
<ClCompile Include="GenAlg.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="GenEngine.cpp">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup>
......
#include "CivOptSchedEngine.h"
CivOptSchedEngine::CivOptSchedEngine(const string& uri):mUri(uri)
{
}
bool CivOptSchedEngine::optimal()
{
}
\ No newline at end of file
#pragma once
#include <string>
#include <vector>
#include <map>
using namespace std;
/**
优化调度模拟客户端
*/
class CivOptSchedEngine
{
public:
explicit CivOptSchedEngine(const string& uri);
bool optimal();
private:
// 水泵编码与水泵状态的映射
map<string, int> mPumpsStatus;
// 数据连接地址
string mUri;
};
#include "CivOptSchedPumpEngine.h"
CivOptSchedPumpEngine::CivOptSchedPumpEngine(const std::string& uri)
:CivOptSchedEngine(uri)
{
}
\ No newline at end of file
#pragma once
#include "CivOptSchedEngine.h"
/**
Ż
*/
class CivOptSchedPumpEngine: public CivOptSchedEngine
{
public:
explicit CivOptSchedPumpEngine(const std::string& uri);
private:
};
#include "CivOptSchedSimulation.h"
CivOptSchedSimulation::CivOptSchedSimulation()
{
}
bool CivOptSchedSimulation::optimal()
{
}
\ No newline at end of file
......@@ -167,7 +167,8 @@ copy pandaAnalysis.h $(OutDir)..\include /y</Command>
<ClCompile Include="CivInpDirector.cpp" />
<ClCompile Include="CivInpProject.cpp" />
<ClCompile Include="CivNewInp.cpp" />
<ClCompile Include="CivOptSchedSimulation.cpp" />
<ClCompile Include="CivOptSchedEngine.cpp" />
<ClCompile Include="CivOptSchedPumpEngine.cpp" />
<ClCompile Include="CivProjInpBuilder.cpp" />
<ClCompile Include="CivProjManager.cpp" />
<ClCompile Include="CivProjSimulation.cpp" />
......@@ -190,7 +191,8 @@ copy pandaAnalysis.h $(OutDir)..\include /y</Command>
<ClInclude Include="CivInpDirector.h" />
<ClInclude Include="CivInpProject.h" />
<ClInclude Include="CivNewInp.h" />
<ClInclude Include="CivOptSchedSimulation.h" />
<ClInclude Include="CivOptSchedEngine.h" />
<ClInclude Include="CivOptSchedPumpEngine.h" />
<ClInclude Include="CivProjInpBuilder.h" />
<ClInclude Include="CivProjManager.h" />
<ClInclude Include="CivProjSimulation.h" />
......
......@@ -51,9 +51,6 @@
<ClCompile Include="CivHydrCompute.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="CivOptSchedSimulation.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="CivBaseInpBuilder.cpp">
<Filter>源文件</Filter>
</ClCompile>
......@@ -75,6 +72,12 @@
<ClCompile Include="CivProjSmulResultCache.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="CivOptSchedEngine.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="CivOptSchedPumpEngine.cpp">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="CivHydrFuncInter.h">
......@@ -116,9 +119,6 @@
<ClInclude Include="CivHydrCompute.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="CivOptSchedSimulation.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="CivInpBuilder.h">
<Filter>头文件</Filter>
</ClInclude>
......@@ -143,5 +143,11 @@
<ClInclude Include="pandaAnalysis.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="CivOptSchedEngine.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="CivOptSchedPumpEngine.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup>
</Project>
\ No newline at end of file
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