Commit 6cb113e7 authored by 刘乐's avatar 刘乐

1, 代码优化

parent 4828200d
...@@ -4,11 +4,11 @@ ...@@ -4,11 +4,11 @@
#include <stdio.h> #include <stdio.h>
#include <random> #include <random>
#include "OptScheduling.h" #include "OptScheduling.h"
#include "FirstOptScheduling.h"
GenAlg::GenAlg(OptScheduling* optScheduling,GenType genType) GenAlg::GenAlg(GenType genType):mGenType(genType)
:mOptScheduling(std::unique_ptr<OptScheduling>(optScheduling)),mGenType(genType)
{ {
mOptScheduling = std::make_unique<FirstOptScheduling>();
} }
double GenAlg::random(int min , int max) double GenAlg::random(int min , int max)
...@@ -253,9 +253,10 @@ double GenAlg::averageFitness() ...@@ -253,9 +253,10 @@ double GenAlg::averageFitness()
return mAverageFitness; return mAverageFitness;
} }
void GenAlg::fitnessfunction() void GenAlg::fitnessfunction(map<string, double>& monitorValues)
{ {
// 计算适应
double objVal = mOptScheduling->objectiveFunction();
} }
void GenAlg::encoding(map<string, int>& phenotype, vector<char>& chromo) void GenAlg::encoding(map<string, int>& phenotype, vector<char>& chromo)
......
...@@ -28,7 +28,7 @@ public: ...@@ -28,7 +28,7 @@ public:
}; };
//构造函数 //构造函数
GenAlg(OptScheduling* optScheduling, GenType genType); explicit GenAlg(GenType genType);
//初始化变量 //初始化变量
void reset(); void reset();
...@@ -42,7 +42,6 @@ public: ...@@ -42,7 +42,6 @@ public:
// 轮盘赌选择函数 // 轮盘赌选择函数
Genome chromoRoulette(); Genome chromoRoulette();
// 基因变异函数(实值编码) // 基因变异函数(实值编码)
void mutate(vector<double>& chromo); void mutate(vector<double>& chromo);
...@@ -64,8 +63,8 @@ public: ...@@ -64,8 +63,8 @@ public:
// 选择运算,前群体中适应度较高的个体按某种规则或模型遗传到下一代群体中 // 选择运算,前群体中适应度较高的个体按某种规则或模型遗传到下一代群体中
void select(); void select();
// 计算个体自适应度 // 根据监测点计算值,计算个体自适应度
void fitnessfunction(); void fitnessfunction(map<string,double>& monitorValues);
/** /**
* @brief 编码, 将水泵开关状态编码成二进制状态:0表示管,1表示开 * @brief 编码, 将水泵开关状态编码成二进制状态:0表示管,1表示开
......
...@@ -10,7 +10,6 @@ public: ...@@ -10,7 +10,6 @@ public:
friend class GenAlg; friend class GenAlg;
friend class GenEngine; friend class GenEngine;
Genome() :fitness(0) {} Genome() :fitness(0) {}
Genome(std::vector <double> vec, double f) : vecGenome(vec), fitness(f) {} //类的带参数初始化参数。 Genome(std::vector <double> vec, double f) : vecGenome(vec), fitness(f) {} //类的带参数初始化参数。
Genome(std::vector <char> vec, double f) :mBinaryGenVec(vec), fitness(f) {} Genome(std::vector <char> vec, double f) :mBinaryGenVec(vec), fitness(f) {}
......
...@@ -143,7 +143,11 @@ ...@@ -143,7 +143,11 @@
<GenerateDebugInformation>true</GenerateDebugInformation> <GenerateDebugInformation>true</GenerateDebugInformation>
</Link> </Link>
<PostBuildEvent> <PostBuildEvent>
<Command>copy pandaAlgorithm.h $(OutDir)..\include /y</Command> <Command>copy pandaAlgorithm.h $(OutDir)..\include /y
copy GenAlg.h $(OutDir)..\include /y
copy Genome.h $(OutDir)..\include /y
copy FirstOptScheduling.h $(OutDir)..\include /y
copy OptScheduling.h $(OutDir)..\include /y</Command>
</PostBuildEvent> </PostBuildEvent>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemGroup> <ItemGroup>
......
#include "CivOptSchedEngine.h" #include "CivOptSchedEngine.h"
#include "CivInpConvertor.h" #include "CivInpConvertor.h"
#include "GenAlg.h"
#include "FirstOptScheduling.h"
CivOptSchedEngine::CivOptSchedEngine(const string& uri):mUri(uri) CivOptSchedEngine::CivOptSchedEngine(const string& uri):mUri(uri)
{ {
...@@ -13,7 +17,12 @@ bool CivOptSchedEngine::optimalScheduling() ...@@ -13,7 +17,12 @@ bool CivOptSchedEngine::optimalScheduling()
string inpFile = convertor.convertBaseInp(); string inpFile = convertor.convertBaseInp();
// //
GenAlg genAlg(GenAlg::BinaryCoding);
// Ⱥʼ
popsize = 100;
genAlg.init(popsize, mMutRate, mCrossRate, mGenLength, 0, 0);
// genAlg.epoch();
} }
......
...@@ -52,4 +52,20 @@ private: ...@@ -52,4 +52,20 @@ private:
// 调度结束时段 // 调度结束时段
string mEndTime; string mEndTime;
// 初始中群大小
int popsize;
// 变异率
double mMutRate;
// 交叉率
double mCrossRate;
//基因编码长度
int mGenLength;
// 针对浮点数编码
double mLeftPoint;
double mRightPoint;
}; };
#include "CivOptSchedPumpEngine.h"
CivOptSchedPumpEngine::CivOptSchedPumpEngine(const std::string& uri)
:CivOptSchedEngine(uri)
{
}
\ No newline at end of file
#pragma once
#include "CivOptSchedEngine.h"
#include <string>
#include <map>
#include <vector>
using namespace std;
/**
վŻ
*/
class CivOptSchedPumpEngine: public CivOptSchedEngine
{
public:
explicit CivOptSchedPumpEngine(const std::string& uri);
private:
// ֮ǰ
vector<map<string, double>> mBeforeScheduling;
// ֮
vector<map<string, double>> mSchduling;
// ˮӳ
map<string, string> mPumpMap;
};
...@@ -66,7 +66,6 @@ bool CivSchedulingCompute::calculate() ...@@ -66,7 +66,6 @@ bool CivSchedulingCompute::calculate()
int linkType; int linkType;
ENgetlinktype(i, &linkType); ENgetlinktype(i, &linkType);
if (linkType != EN_PUMP) if (linkType != EN_PUMP)
continue; continue;
...@@ -89,6 +88,8 @@ bool CivSchedulingCompute::calculate() ...@@ -89,6 +88,8 @@ bool CivSchedulingCompute::calculate()
ENnextH(&tstep); ENnextH(&tstep);
// 保存
saveResult(iTime);
iTime++; iTime++;
} while (tstep > 0); } while (tstep > 0);
...@@ -110,5 +111,35 @@ void CivSchedulingCompute::updatePumpStatus(const map<string, char>& pumpStatus) ...@@ -110,5 +111,35 @@ void CivSchedulingCompute::updatePumpStatus(const map<string, char>& pumpStatus)
void CivSchedulingCompute::getMonitorsValue(vector<map<string, double>>& monitorMap) void CivSchedulingCompute::getMonitorsValue(vector<map<string, double>>& monitorMap)
{ {
monitorMap = mMonitors; monitorMap = mMonitorsValue;
}
void CivSchedulingCompute::setMonitors(const vector<string>& monitor)
{
mMonitors = monitor;
}
void CivSchedulingCompute::saveResult(int time)
{
// 计算节点
int nNodeCount;
ENgetcount(EN_NODECOUNT, &nNodeCount);
map<string, double> pressureMap;
for (int i = 1; i <= nNodeCount; i++)
{
char szNo[256];
ENgetnodeid(i, szNo);// 编号
// 判断是不是监测点
auto iter = std::find(mMonitors.begin(), mMonitors.end(), szNo);
if (iter == mMonitors.end())
continue;
float pressure;
ENgetnodevalue(i, EN_PRESSURE, &pressure); // 压力
pressureMap.insert(pair<string, double>(szNo, pressure));
}
mMonitorsValue[time] = pressureMap;
} }
\ No newline at end of file
...@@ -3,29 +3,44 @@ ...@@ -3,29 +3,44 @@
#include <string> #include <string>
using namespace std; using namespace std;
/** /**
优化调度计算类 优化调度计算类
*/ */
class CivSchedulingCompute class CivSchedulingCompute
{ {
public: public:
CivSchedulingCompute(); CivSchedulingCompute();
// 设置监测点
void setMonitors(const vector<string>& monitor);
// 读入inp文件
bool openFile(string& inpFILE); bool openFile(string& inpFILE);
// 水力计算
bool calculate(); bool calculate();
// 结束计算,关闭文件句柄
void close(); void close();
// 更新水泵开关状态
void updatePumpStatus(const map<string, char>& pumpStatus); void updatePumpStatus(const map<string, char>& pumpStatus);
// 获取监测点计算出的值
void getMonitorsValue(vector<map<string, double>>& monitorMap); void getMonitorsValue(vector<map<string, double>>& monitorMap);
private:
// 保存计算的值
void saveResult(int time);
private: private:
std::string mRptFile; std::string mRptFile;
std::string mBinFile; std::string mBinFile;
// 水泵编号和code的映射
vector<string> mMonitors;
// 错误吗 // 错误吗
int mErrcode = 0; int mErrcode = 0;
...@@ -33,5 +48,5 @@ private: ...@@ -33,5 +48,5 @@ private:
map<string, char> mPumpStatus; map<string, char> mPumpStatus;
// 存储测点的计算值 // 存储测点的计算值
vector<map<string, double>> mMonitors; vector<map<string, double>> mMonitorsValue;
}; };
...@@ -168,7 +168,6 @@ copy pandaAnalysis.h $(OutDir)..\include /y</Command> ...@@ -168,7 +168,6 @@ copy pandaAnalysis.h $(OutDir)..\include /y</Command>
<ClCompile Include="CivInpProject.cpp" /> <ClCompile Include="CivInpProject.cpp" />
<ClCompile Include="CivNewInp.cpp" /> <ClCompile Include="CivNewInp.cpp" />
<ClCompile Include="CivOptSchedEngine.cpp" /> <ClCompile Include="CivOptSchedEngine.cpp" />
<ClCompile Include="CivOptSchedPumpEngine.cpp" />
<ClCompile Include="CivProjInpBuilder.cpp" /> <ClCompile Include="CivProjInpBuilder.cpp" />
<ClCompile Include="CivProjManager.cpp" /> <ClCompile Include="CivProjManager.cpp" />
<ClCompile Include="CivProjSimulation.cpp" /> <ClCompile Include="CivProjSimulation.cpp" />
...@@ -193,7 +192,6 @@ copy pandaAnalysis.h $(OutDir)..\include /y</Command> ...@@ -193,7 +192,6 @@ copy pandaAnalysis.h $(OutDir)..\include /y</Command>
<ClInclude Include="CivInpProject.h" /> <ClInclude Include="CivInpProject.h" />
<ClInclude Include="CivNewInp.h" /> <ClInclude Include="CivNewInp.h" />
<ClInclude Include="CivOptSchedEngine.h" /> <ClInclude Include="CivOptSchedEngine.h" />
<ClInclude Include="CivOptSchedPumpEngine.h" />
<ClInclude Include="CivProjInpBuilder.h" /> <ClInclude Include="CivProjInpBuilder.h" />
<ClInclude Include="CivProjManager.h" /> <ClInclude Include="CivProjManager.h" />
<ClInclude Include="CivProjSimulation.h" /> <ClInclude Include="CivProjSimulation.h" />
......
...@@ -75,9 +75,6 @@ ...@@ -75,9 +75,6 @@
<ClCompile Include="CivOptSchedEngine.cpp"> <ClCompile Include="CivOptSchedEngine.cpp">
<Filter>源文件</Filter> <Filter>源文件</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="CivOptSchedPumpEngine.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="CivSchedulingCompute.cpp"> <ClCompile Include="CivSchedulingCompute.cpp">
<Filter>源文件</Filter> <Filter>源文件</Filter>
</ClCompile> </ClCompile>
...@@ -149,9 +146,6 @@ ...@@ -149,9 +146,6 @@
<ClInclude Include="CivOptSchedEngine.h"> <ClInclude Include="CivOptSchedEngine.h">
<Filter>头文件</Filter> <Filter>头文件</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="CivOptSchedPumpEngine.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="CivSchedulingCompute.h"> <ClInclude Include="CivSchedulingCompute.h">
<Filter>头文件</Filter> <Filter>头文件</Filter>
</ClInclude> </ClInclude>
......
#include "CivPumpHelper.h"
#include "CivPgDbConnection.h"
CivPumpHelper::CivPumpHelper(const string& uri)
{
mConn = new CivPgDbConnection();
mConn->connect(uri);
}
CivPumpHelper::~CivPumpHelper()
{
mConn->disconnect();
delete mConn;
}
void CivPumpHelper::getMap(const string& key, const string& val, map<string, string>& res)
{
vector<map<string, string>> vecMap;
if (!mConn->query(mTable, { key ,val }, vecMap))
return;
if (vecMap.size() <= 0)
return;
size_t total = vecMap.size();
for (int i = 0; i < total; i++)
{
map<string, string> tempMap = vecMap[i];
string keyVal = tempMap.find(key)->second;
string valVal = tempMap.find(val)->second;
res.insert(pair<string, string>(keyVal, valVal));
}
}
\ No newline at end of file
#pragma once
#include<string>
#include<map>
#include "pandaDbManager.h"
using namespace std;
class CivConnection;
/**
水泵查询接口
*/
class PANDADBMANAGER_API CivPumpHelper
{
public:
explicit CivPumpHelper(const string& uri);
~CivPumpHelper();
// 获取指定属性字段键值对映射
void getMap(const string& key, const string& val, map<string, string>& res);
private:
CivConnection* mConn = nullptr;
const string mTable = "水泵";
};
...@@ -13,7 +13,7 @@ class CivConnection; ...@@ -13,7 +13,7 @@ class CivConnection;
class PANDADBMANAGER_API CivSimulResHelperAbs class PANDADBMANAGER_API CivSimulResHelperAbs
{ {
public: public:
explicit CivSimulResHelperAbs(); CivSimulResHelperAbs();
virtual ~CivSimulResHelperAbs(); virtual ~CivSimulResHelperAbs();
virtual bool insertNodes(const NodeResultItems& nodeitems); virtual bool insertNodes(const NodeResultItems& nodeitems);
......
...@@ -161,6 +161,7 @@ copy CivSimulResHelperAbs.h $(OutDir)..\include /y ...@@ -161,6 +161,7 @@ copy CivSimulResHelperAbs.h $(OutDir)..\include /y
copy CivSimulResDbHelper.h $(OutDir)..\include /y copy CivSimulResDbHelper.h $(OutDir)..\include /y
copy CivHydrTableHelper.h $(OutDir)..\include /y copy CivHydrTableHelper.h $(OutDir)..\include /y
copy CIvProjSimulResHelper.h $(OutDir)..\include /y copy CIvProjSimulResHelper.h $(OutDir)..\include /y
copy CivPumpHelper.h $(OutDir)..\include /y
</Command> </Command>
</PostBuildEvent> </PostBuildEvent>
</ItemDefinitionGroup> </ItemDefinitionGroup>
...@@ -173,6 +174,7 @@ copy CIvProjSimulResHelper.h $(OutDir)..\include /y ...@@ -173,6 +174,7 @@ copy CIvProjSimulResHelper.h $(OutDir)..\include /y
<ClInclude Include="CivPgDbConnection.h" /> <ClInclude Include="CivPgDbConnection.h" />
<ClInclude Include="CivProjInpDbHelper.h" /> <ClInclude Include="CivProjInpDbHelper.h" />
<ClInclude Include="CIvProjSimulResHelper.h" /> <ClInclude Include="CIvProjSimulResHelper.h" />
<ClInclude Include="CivPumpHelper.h" />
<ClInclude Include="CivSimulResDbHelper.h" /> <ClInclude Include="CivSimulResDbHelper.h" />
<ClInclude Include="CivSimulResHelperAbs.h" /> <ClInclude Include="CivSimulResHelperAbs.h" />
<ClInclude Include="CivSimuResStruct.h" /> <ClInclude Include="CivSimuResStruct.h" />
...@@ -189,6 +191,7 @@ copy CIvProjSimulResHelper.h $(OutDir)..\include /y ...@@ -189,6 +191,7 @@ copy CIvProjSimulResHelper.h $(OutDir)..\include /y
<ClCompile Include="CivPgDbConnection.cpp" /> <ClCompile Include="CivPgDbConnection.cpp" />
<ClCompile Include="CivProjInpDbHelper.cpp" /> <ClCompile Include="CivProjInpDbHelper.cpp" />
<ClCompile Include="CIvProjSimulResHelper.cpp" /> <ClCompile Include="CIvProjSimulResHelper.cpp" />
<ClCompile Include="CivPumpHelper.cpp" />
<ClCompile Include="CivSimulResDbHelper.cpp" /> <ClCompile Include="CivSimulResDbHelper.cpp" />
<ClCompile Include="CivSimulResHelperAbs.cpp" /> <ClCompile Include="CivSimulResHelperAbs.cpp" />
</ItemGroup> </ItemGroup>
......
...@@ -57,6 +57,9 @@ ...@@ -57,6 +57,9 @@
<ClInclude Include="pandaDbManager.h"> <ClInclude Include="pandaDbManager.h">
<Filter>头文件</Filter> <Filter>头文件</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="CivPumpHelper.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="CivAssembly.cpp"> <ClCompile Include="CivAssembly.cpp">
...@@ -89,5 +92,8 @@ ...@@ -89,5 +92,8 @@
<ClCompile Include="CIvProjSimulResHelper.cpp"> <ClCompile Include="CIvProjSimulResHelper.cpp">
<Filter>源文件</Filter> <Filter>源文件</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="CivPumpHelper.cpp">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
</Project> </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