Commit 618d8082 authored by 刘乐's avatar 刘乐

1, 优化调度接口修改和优化

parent 8b64e676
......@@ -41,15 +41,18 @@ void CivConQuaTest::test(char* uri)
// 追踪分析
void CivConTrackTest::test(char* uri)
{
char result[1024 * 512];
char sn[64];
strcpy_s(sn, "5fbaaf9e-dfcf-4e97-9fb1-31d03293f654");
strcpy_s(sn, "5c8afd3e-b361-4458-b5ca-6891ec80ae8c");
int succ = trackingSimulation(uri, sn, 6, result);
memset(result, 0, sizeof(result) / sizeof(char));
int succ = trackingSimulation(uri, sn, 1, result);
std::cout << result << std::endl;
std::cout << "测试结果:" << succ << std::endl;
}
void CivUpstreamTrackingTest::test(char* uri)
......
......@@ -92,7 +92,7 @@ double FirstOptScheduling::objectiveFunction(float cost, const map<string, doubl
continue;
map<string, double>::iterator maxIter = mMaxMonitorVals.find(key);
if (maxIter == mMaxMonitorVals.end())
if(maxIter == mMaxMonitorVals.end())
continue;
float minVal = minIter->second;
......@@ -101,11 +101,11 @@ double FirstOptScheduling::objectiveFunction(float cost, const map<string, doubl
if (val < minVal)
{
sum += ((minVal - val) / maxMinDiff);
sum += ((val - minVal ) / maxMinDiff);
}
else if (val > maxVal)
{
sum += ((val - maxVal) / maxMinDiff);
sum += ((maxVal- val ) / maxMinDiff);
}
}
......
......@@ -43,4 +43,6 @@ public:
virtual double waterProductionCost(const vector<double>& Q, const vector<double>& S3);
virtual double waterSupplyAndDemand(const vector<double>& Q, const double& Qd);
};
......@@ -44,6 +44,7 @@ double OptScheduling::waterSupplyAndDemand(const std::vector<double>& Q, const d
void OptScheduling::setMaxMonitorVals(const map<string, double>& maxMonitors)
{
mMaxMonitorVals = maxMonitors;
}
void OptScheduling::setMinMonitorsVals(const map<string, double>& minMonitors)
......
......@@ -84,6 +84,10 @@ protected:
// 设置的监测点的调度最大值
map<string, double> mMaxMonitorVals;
double mMaxVals; // 监测点最大值
double mMinVals; // 监测点最小是
// 设置的监测点的调度最小值
map<string, double> mMinMonitorVals;
};
\ No newline at end of file
......@@ -6,9 +6,9 @@
CivHydrCompute::CivHydrCompute()
{
mInFile = CivCommonUtils::getExePath()+ "\\test.inp";
mRptFile = CivCommonUtils::getExePath() + "\\test.rpt";
mBinFile = CivCommonUtils::getExePath() + "\\test.bin";
mInFile = "test.inp";
mRptFile = "test.rpt";
mBinFile = "test.bin";
}
bool CivHydrCompute::qualityCompute()
......@@ -51,6 +51,7 @@ bool CivHydrCompute::qualityCompute()
return false;
}
mResultCache.clear();
do
{
......@@ -117,6 +118,11 @@ bool CivHydrCompute::qualityCompute()
return true;
}
void CivHydrCompute::clearCache()
{
mResultCache.clear();
}
bool CivHydrCompute::hdyrCompute()
{
short hydCode = 1;
......@@ -136,6 +142,7 @@ bool CivHydrCompute::hdyrCompute()
errcode = ENopen(inpFile, rptFile, binFile);
if (errcode > 0)
{
CivSysLog::getInstance()->error("ENopen 失败", "CivHydrCompute", __FUNCTION__);
......@@ -233,7 +240,6 @@ bool CivHydrCompute::chlorineCompute()
return false;
}
do
{
int nLinkCount;
......@@ -299,29 +305,15 @@ void CivHydrCompute::dateAndTime(int time,std::string& nowDate,std::string& nowT
{
CivDate civDate;
nowDate = civDate.getDate();
int baseTime = 0;// CivCommonUtils::currentHour();
int a = baseTime + time;
int modulus = a % 24;
int yu = a / 24;
if (yu > 0)
{
civDate.addDate();
nowDate = civDate.getDate();
a = modulus;
}
if (modulus < 10)
nowTime = ("0" + std::to_string(a) + ":00:00");
else
nowTime = (std::to_string(a) + ":00:00");
nowTime = nowDate + " " +nowTime;
nowTime = nowDate + " " +to_string(time)+":00:00" ;
}
void CivHydrCompute::getNodeResult(short time)
{
if (time <= 0)
return;
time -= 1;
// 计算节点
int nNodeCount;
ENgetcount(EN_NODECOUNT, &nNodeCount);
......@@ -366,6 +358,10 @@ void CivHydrCompute::getNodeResult(short time)
void CivHydrCompute::getLinkResult(short time)
{
if (time <= 0)
return;
time -= 1;
int nLinkCount;
ENgetcount(EN_LINKCOUNT, &nLinkCount);
int* typeCode = (int*)malloc(sizeof(int));
......@@ -422,6 +418,10 @@ void CivHydrCompute::getLinkResult(short time)
void CivHydrCompute::getNodeQuality(short time)
{
if (time <= 0)
return;
time -= 1;
int nNodeCount;
ENgetcount(EN_NODECOUNT, &nNodeCount);
......@@ -444,6 +444,10 @@ void CivHydrCompute::getNodeQuality(short time)
void CivHydrCompute::getLinkQuality(short time)
{
if (time <= 0)
return;
time -= 1;
int nLinkCount;
ENgetcount(EN_LINKCOUNT, &nLinkCount);
int* typeCode = (int*)malloc(sizeof(int));
......@@ -465,6 +469,11 @@ void CivHydrCompute::getLinkQuality(short time)
void CivHydrCompute::getNodeChlorine(short time)
{
if (time <= 0)
return;
time -= 1;
int nNodeCount;
ENgetcount(EN_NODECOUNT, &nNodeCount);
int* typeCode = (int*)malloc(sizeof(int));
......@@ -489,6 +498,11 @@ void CivHydrCompute::getNodeChlorine(short time)
void CivHydrCompute::getLinkChlorine(short time)
{
if (time <= 0)
return;
time -= 1;
int nLinkCount;
ENgetcount(EN_LINKCOUNT, &nLinkCount);
int* typeCode = (int*)malloc(sizeof(int));
......@@ -540,7 +554,10 @@ void CivHydrCompute::getTrackingResult(const std::string& uri,std::string& jsonR
for (int j = 0; j < total; j++)
{
std::string sn = pipeS[j];
PipeCoord pipe = pipeCoords.find(sn)->second;
auto iter = pipeCoords.find(sn);
if (iter != pipeCoords.end())
{
PipeCoord pipe = iter->second;
jsonResult.append("[[");
jsonResult.append(pipe.startX);
......@@ -552,6 +569,7 @@ void CivHydrCompute::getTrackingResult(const std::string& uri,std::string& jsonR
jsonResult.append(pipe.endY);
jsonResult.append("]],");
}
}
jsonResult = jsonResult.substr(0, jsonResult.length() - 1);
jsonResult.append("],");
......
......@@ -37,6 +37,8 @@ public:
void getNodeItemByInterval(int time, NodeResultItems& nodeItem);
void getPipeItemByInterval(int time, LinkResultItems& linkItem);
void clearCache();
private:
void getNodeResult(short time);
void getLinkResult(short time);
......
......@@ -32,6 +32,9 @@ bool PANDANALYSIS_API trackingSimulation(char* uri, char* sN, int hours, char* r
std::string uriStr = uri;
std::string sNStr = sN;
// 先清空掉输出空间的
memset(result, 0, sizeof(result) / sizeof(char));
if (uriStr.empty() || sNStr.empty())
return false;
......
......@@ -89,7 +89,7 @@ bool CivHydrSimulation::qualitySimulation()
return true;
}
bool CivHydrSimulation::trackingSimulation(char* snNode, int hours,std::string& jsonResult)
bool CivHydrSimulation::trackingSimulation(char* snNode, int hours, std::string& jsonResult)
{
CivInpConvertor convertor(mUri);
......@@ -101,6 +101,8 @@ bool CivHydrSimulation::trackingSimulation(char* snNode, int hours,std::string&
if (inpFileS.empty())
return false;
// 先清除一次已经计算的缓存数据
mHydrCompute.clearCache();
if (!mHydrCompute.qualityCompute())
{
return false;
......
......@@ -9,7 +9,8 @@
CivInpConvertor::CivInpConvertor(const std::string& uri)
:mUri(uri)
{
mInpFileName = "test.inp";
mProjInpFileName = "ptest.inp";
}
CivInpConvertor::~CivInpConvertor()
......@@ -41,7 +42,7 @@ std::string CivInpConvertor::convertPorjInp(const std::string& projCode, const s
director.setBuilder(builder);
director.create();
std::string inpFile = "ptest.inp";
std::string inpFile = mProjInpFileName;
if (!builder->getNewInp(inpFile))
{
delete builder;
......@@ -59,7 +60,7 @@ std::string CivInpConvertor::convertBaseInp()
director.setBuilder(builder);
director.create();
std::string inpFile = "test.inp";
std::string inpFile = mInpFileName;
if (!builder->getNewInp(inpFile))
{
delete builder;
......@@ -87,7 +88,7 @@ std::string CivInpConvertor::convertTrackInp(const std::string& sn, const std::s
director.setBuilder(builder);
director.create();
std::string inpFile = "test.inp";
std::string inpFile = mInpFileName;
if (!builder->getNewInp(inpFile))
{
delete builder;
......@@ -107,7 +108,7 @@ std::string CivInpConvertor::convertAnalysisInp()
director.setBuilder(builder);
director.create();
std::string inpFile = "test.inp";
std::string inpFile = mInpFileName;
if (!builder->getNewInp(inpFile))
{
delete builder;
......@@ -130,7 +131,7 @@ std::string CivInpConvertor::convertResidualInp()
director.setBuilder(builder);
director.create();
std::string inpFile = "test.inp";
std::string inpFile = mInpFileName;
if (!builder->getNewInp(inpFile))
{
delete builder;
......@@ -154,7 +155,7 @@ std::string CivInpConvertor::convertNotPorjInp(const std::string& simulTime)
director.setBuilder(builder);
director.create();
std::string inpFile = "test.inp";
std::string inpFile = mInpFileName;
if (!builder->getNewInp(inpFile))
{
delete builder;
......
......@@ -47,4 +47,7 @@ public:
private:
std::string mUri;
std::string mInpFileName;
std::string mProjInpFileName;
};
\ No newline at end of file
......@@ -18,7 +18,11 @@ CivOptSchedEngine::CivOptSchedEngine(const string& uri)
bool CivOptSchedEngine::optimalScheduling(int time)
{
// 计算初始化
beginCompute();
if (!beginCompute())
{
return false;
}
// 开始遗传算法
std::shared_ptr<OptScheduling> optScheduling(new FirstOptScheduling());
......@@ -29,14 +33,14 @@ bool CivOptSchedEngine::optimalScheduling(int time)
genAlg.setPumpVec(mPumpVec);
// 种群初始化
popsize = 20;
popsize = 10;
genAlg.init(popsize, mMutRate, mCrossRate, mGenLength, 0, 1);
// 开始迭代计算
int index = 0;
// 迭代次数
int generation = 100;
int generation = 20;
mSchedulingCompute->clearLastVals();
while (index++ < generation)
......@@ -79,7 +83,7 @@ bool CivOptSchedEngine::optimalScheduling(int time)
// 计算个体自适度
double fitness = genAlg.fitnessfunction(cost, monitorCalcMapValues);
if (fitness > 1)
if (fitness >= 1)
fitness = 0;
logStr.append(" 适应度值:" + to_string(fitness));
......@@ -113,9 +117,10 @@ bool CivOptSchedEngine::optimalScheduling(int time)
string infoStr = "最优水泵组合值:";
for (int i = 0; i < totalGenVec; i++)
{
infoStr.append(mPumpVec[i] +":" + to_string(genVec[i]));
infoStr.append(mPumpVec[i] +": " + to_string(genVec[i]) +" ");
mPumpBestSpeed.insert(pair<string, double>(mPumpVec[i], genVec[i]));
}
infoStr.append(" 最佳适应度:" + to_string(genome.fitness));
CivSysLog::getInstance()->error(infoStr, "CivOptSchedEngine", __FUNCTION__);
mSchedulingCompute->clearLastVals();
......@@ -195,6 +200,12 @@ bool CivOptSchedEngine::beginCompute()
// 获取水泵个数,根据水泵个数确定基因编码长度
CivPumpHelper pumpHelper(mUri);
mGenLength = pumpHelper.getPumpNumbers();
if (mGenLength <= 0)
{
CivSysLog::getInstance()->error("CivOptSchedEngine","获取不到水泵",__FUNCTION__);
return false;
}
// 获取本点号和code的映射
map<string, string> snToCodeMap;
......@@ -214,7 +225,11 @@ bool CivOptSchedEngine::beginCompute()
// 构造水力计算对象
mSchedulingCompute->setMonitors(mMonitorsVec);
mSchedulingCompute->openFile(inpFile);
if (!mSchedulingCompute->openFile(inpFile))
{
CivSysLog::getInstance()->info(inpFile,"CivOptSchedEngine",__FUNCTION__);
return false;
}
// 原始管网计算一次水力,保留调度前的水力计算结果
if (!mSchedulingCompute->calculate())
......
......@@ -7,14 +7,15 @@
CivSchedulingCompute::CivSchedulingCompute(SchdulingType schedulingType)
:mChdulingType(schedulingType)
{
mRptFile = CivCommonUtils::getExePath() + "\\test.rpt";
mBinFile = CivCommonUtils::getExePath() + "\\test.bin";
mRptFile = "test.rpt";
mBinFile = "test.bin";
mInpFile = "test.inp";
}
bool CivSchedulingCompute::openFile(const string& inpFILE)
{
char inpFile[256];
strcpy_s(inpFile, inpFILE.c_str());
strcpy_s(inpFile, mInpFile.c_str());
char rptFile[128];
strcpy_s(rptFile, mRptFile.c_str());
......
......@@ -64,6 +64,7 @@ private:
private:
std::string mRptFile;
std::string mBinFile;
std::string mInpFile;
// 水泵编号和code的映射
vector<string> mMonitors;
......
......@@ -14,6 +14,15 @@ void CivSimulResultCache::addNodeQuality(float quality, int interval, const std:
mNodeItemsMap[interval][sNo].dQuality = quality;
}
CivSimulResultCache::~CivSimulResultCache()
{
mNodeItemsMap.clear();
mLinkItemsMap.clear();
mTankItemsMap.clear();
mNodeCodeSnMap.clear();
mLineCodeSnMap.clear();
}
void CivSimulResultCache::addLinkQuality(float quality, int interval,const std::string& sNo)
{
if (mLinkItemsMap.find(interval) == mLinkItemsMap.end())
......@@ -115,6 +124,9 @@ void CivSimulResultCache::getPipeItemByInterval(int time, LinkResultItems& linkI
void CivSimulResultCache::getCodeToSnMap(const std::string& uri)
{
if (mNodeCodeSnMap.size() > 0 || mLineCodeSnMap.size() > 0)
return;
CivSimulResDbHelper helper(uri);
helper.getNodeCodeSnMap(mNodeCodeSnMap);
helper.getLinCodeSnMap(mLineCodeSnMap);
......
......@@ -13,6 +13,7 @@ using namespace std;
class CivSimulResultCache
{
public:
~CivSimulResultCache();
/**
*@brief 缓存节点水质
*@param quality:存储的值
......
......@@ -340,7 +340,10 @@ void CivInpDbHelper::handleValve()
CivPipe::Table pipeStruct;
pipeStruct.ID = pipe.find("编号")->second;
if(pipe.find("code") != pipe.end())
pipeStruct.code = pipe.find("code")->second;
pipeStruct.Node1 = pipe.find("起始节点")->second;
pipeStruct.Node2 = pipe.find("终止节点")->second;
pipeStruct.Length = pipe.find("管长")->second;
......@@ -368,14 +371,33 @@ void CivInpDbHelper::handleValve()
std::map<std::string, std::string> pipe = downPipes[i];
CivPipe::Table pipeStruct;
auto iterEnd = pipe.end();
if(pipe.find("编号") != iterEnd)
pipeStruct.ID = pipe.find("编号")->second;
if(pipe.find("code")!= iterEnd)
pipeStruct.code = pipe.find("code")->second;
if(pipe.find("起始节点")!= iterEnd)
pipeStruct.Node1 = pipe.find("起始节点")->second;
if (pipe.find("终止节点") != iterEnd)
pipeStruct.Node2 = pipe.find("终止节点")->second;
if (pipe.find("管长") != iterEnd)
pipeStruct.Length = pipe.find("管长")->second;
if (pipe.find("管径") != iterEnd)
pipeStruct.Diameter = pipe.find("管径")->second;
if (pipe.find("摩阻系数") != iterEnd)
pipeStruct.Roughness = pipe.find("摩阻系数")->second;
pipeStruct.Status = "Open";
if (pipe.find("局损系数") != iterEnd)
pipeStruct.MinorLoss = pipe.find("局损系数")->second;
mPipesVec.push_back(pipeStruct);
......
......@@ -4,6 +4,16 @@
#include<vector>
#include "pandaLog.h"
#ifdef _WIN32
#define SEP "\\\\"
#else
#define SEP "/"
#endif // _WIN32
// 字符串转编码
#define TransUTFCoding(name) CivCommonUtils::string_To_UTF8(name)
#define UTFTransCoding(name) CivCommonUtils::UTF8_To_string(name)
......
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