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

1, 代码优化

parent 4828200d
......@@ -4,11 +4,11 @@
#include <stdio.h>
#include <random>
#include "OptScheduling.h"
#include "FirstOptScheduling.h"
GenAlg::GenAlg(OptScheduling* optScheduling,GenType genType)
:mOptScheduling(std::unique_ptr<OptScheduling>(optScheduling)),mGenType(genType)
GenAlg::GenAlg(GenType genType):mGenType(genType)
{
mOptScheduling = std::make_unique<FirstOptScheduling>();
}
double GenAlg::random(int min , int max)
......@@ -253,9 +253,10 @@ double GenAlg::averageFitness()
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)
......
......@@ -28,7 +28,7 @@ public:
};
//构造函数
GenAlg(OptScheduling* optScheduling, GenType genType);
explicit GenAlg(GenType genType);
//初始化变量
void reset();
......@@ -42,7 +42,6 @@ public:
// 轮盘赌选择函数
Genome chromoRoulette();
// 基因变异函数(实值编码)
void mutate(vector<double>& chromo);
......@@ -64,8 +63,8 @@ public:
// 选择运算,前群体中适应度较高的个体按某种规则或模型遗传到下一代群体中
void select();
// 计算个体自适应度
void fitnessfunction();
// 根据监测点计算值,计算个体自适应度
void fitnessfunction(map<string,double>& monitorValues);
/**
* @brief 编码, 将水泵开关状态编码成二进制状态:0表示管,1表示开
......
......@@ -10,7 +10,6 @@ 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) {}
......
......@@ -143,7 +143,11 @@
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
<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>
</ItemDefinitionGroup>
<ItemGroup>
......
#include "CivOptSchedEngine.h"
#include "CivInpConvertor.h"
#include "GenAlg.h"
#include "FirstOptScheduling.h"
CivOptSchedEngine::CivOptSchedEngine(const string& uri):mUri(uri)
{
......@@ -13,7 +17,12 @@ bool CivOptSchedEngine::optimalScheduling()
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:
// 调度结束时段
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()
int linkType;
ENgetlinktype(i, &linkType);
if (linkType != EN_PUMP)
continue;
......@@ -89,6 +88,8 @@ bool CivSchedulingCompute::calculate()
ENnextH(&tstep);
// 保存
saveResult(iTime);
iTime++;
} while (tstep > 0);
......@@ -110,5 +111,35 @@ void CivSchedulingCompute::updatePumpStatus(const map<string, char>& pumpStatus)
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 @@
#include <string>
using namespace std;
/**
优化调度计算类
优化调度计算类
*/
class CivSchedulingCompute
{
public:
CivSchedulingCompute();
// 设置监测点
void setMonitors(const vector<string>& monitor);
// 读入inp文件
bool openFile(string& inpFILE);
// 水力计算
bool calculate();
// 结束计算,关闭文件句柄
void close();
// 更新水泵开关状态
void updatePumpStatus(const map<string, char>& pumpStatus);
// 获取监测点计算出的值
void getMonitorsValue(vector<map<string, double>>& monitorMap);
private:
// 保存计算的值
void saveResult(int time);
private:
std::string mRptFile;
std::string mBinFile;
// 水泵编号和code的映射
vector<string> mMonitors;
// 错误吗
int mErrcode = 0;
......@@ -33,5 +48,5 @@ private:
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>
<ClCompile Include="CivInpProject.cpp" />
<ClCompile Include="CivNewInp.cpp" />
<ClCompile Include="CivOptSchedEngine.cpp" />
<ClCompile Include="CivOptSchedPumpEngine.cpp" />
<ClCompile Include="CivProjInpBuilder.cpp" />
<ClCompile Include="CivProjManager.cpp" />
<ClCompile Include="CivProjSimulation.cpp" />
......@@ -193,7 +192,6 @@ copy pandaAnalysis.h $(OutDir)..\include /y</Command>
<ClInclude Include="CivInpProject.h" />
<ClInclude Include="CivNewInp.h" />
<ClInclude Include="CivOptSchedEngine.h" />
<ClInclude Include="CivOptSchedPumpEngine.h" />
<ClInclude Include="CivProjInpBuilder.h" />
<ClInclude Include="CivProjManager.h" />
<ClInclude Include="CivProjSimulation.h" />
......
......@@ -75,9 +75,6 @@
<ClCompile Include="CivOptSchedEngine.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="CivOptSchedPumpEngine.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="CivSchedulingCompute.cpp">
<Filter>源文件</Filter>
</ClCompile>
......@@ -149,9 +146,6 @@
<ClInclude Include="CivOptSchedEngine.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="CivOptSchedPumpEngine.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="CivSchedulingCompute.h">
<Filter>头文件</Filter>
</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;
class PANDADBMANAGER_API CivSimulResHelperAbs
{
public:
explicit CivSimulResHelperAbs();
CivSimulResHelperAbs();
virtual ~CivSimulResHelperAbs();
virtual bool insertNodes(const NodeResultItems& nodeitems);
......
......@@ -161,6 +161,7 @@ copy CivSimulResHelperAbs.h $(OutDir)..\include /y
copy CivSimulResDbHelper.h $(OutDir)..\include /y
copy CivHydrTableHelper.h $(OutDir)..\include /y
copy CIvProjSimulResHelper.h $(OutDir)..\include /y
copy CivPumpHelper.h $(OutDir)..\include /y
</Command>
</PostBuildEvent>
</ItemDefinitionGroup>
......@@ -173,6 +174,7 @@ copy CIvProjSimulResHelper.h $(OutDir)..\include /y
<ClInclude Include="CivPgDbConnection.h" />
<ClInclude Include="CivProjInpDbHelper.h" />
<ClInclude Include="CIvProjSimulResHelper.h" />
<ClInclude Include="CivPumpHelper.h" />
<ClInclude Include="CivSimulResDbHelper.h" />
<ClInclude Include="CivSimulResHelperAbs.h" />
<ClInclude Include="CivSimuResStruct.h" />
......@@ -189,6 +191,7 @@ copy CIvProjSimulResHelper.h $(OutDir)..\include /y
<ClCompile Include="CivPgDbConnection.cpp" />
<ClCompile Include="CivProjInpDbHelper.cpp" />
<ClCompile Include="CIvProjSimulResHelper.cpp" />
<ClCompile Include="CivPumpHelper.cpp" />
<ClCompile Include="CivSimulResDbHelper.cpp" />
<ClCompile Include="CivSimulResHelperAbs.cpp" />
</ItemGroup>
......
......@@ -57,6 +57,9 @@
<ClInclude Include="pandaDbManager.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="CivPumpHelper.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="CivAssembly.cpp">
......@@ -89,5 +92,8 @@
<ClCompile Include="CIvProjSimulResHelper.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="CivPumpHelper.cpp">
<Filter>源文件</Filter>
</ClCompile>
</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