Commit ea5b7910 by lvshi.gong

修改随机森林的训练逻辑

parent b4f5c5a2
......@@ -5,7 +5,8 @@
//发送给流式的消息
RamdomForestCalculate::RamdomForestCalculate(QObject *parent)
{
SPDLOG_LOGGER_DEBUG(spdlog::get("logger"),"Hello RamdomForestCalculate ");
QString a = "算法模块开始运行";
SPDLOG_LOGGER_DEBUG(spdlog::get("logger"),"{}",a.toStdString());
qRegisterMetaType<Dc_SacData>("Dc_SacData");
......@@ -22,25 +23,33 @@ RamdomForestCalculate::RamdomForestCalculate(QObject *parent)
QString dbPath = QString("/home/pi/SigerTMS/stream/SAC/%1.db").arg("RamdomForestFeatureValue");
QList<SACRecord> record = queryRecordsByLabel(dbPath,0);
if(record.size() != 0)
{
IfHaveModel = true;
std::vector<std::vector<float>> FearValues;
std::vector<int> TypeValues;
DealRecord(record,FearValues,TypeValues);
Classifier->train(FearValues, TypeValues, 0.2f, 100);
}
//连接信号和槽函数
QObject::connect(m_tcpServerManager, &TcpServerManager::getdc_sacdata,
[=](const Dc_SacData &sac_data) {
qDebug() << "收到 dc_sacdata 信号";
qDebug() << "Source:" << sac_data.source;
qDebug() << "SacFile:" << sac_data.sacFile;
qDebug() << "Target:" << sac_data.target;
SPDLOG_LOGGER_DEBUG(spdlog::get("logger"),"收到 dc_sacdata 信号");
SPDLOG_LOGGER_DEBUG(spdlog::get("logger"),"Source:{}",sac_data.source.toStdString());
SPDLOG_LOGGER_DEBUG(spdlog::get("logger"),"SacFile:{}",sac_data.sacFile.toStdString());
SPDLOG_LOGGER_DEBUG(spdlog::get("logger"),"Target:{}",sac_data.target.toStdString());
Save_SACDATA(sac_data); // 现在可以识别了
});
//连接TMS信号和槽函数
QObject::connect(m_tcpServerManager, &TcpServerManager::TMSTrain,
[=]() {
qDebug() << "收到 TMS 信号";
SPDLOG_LOGGER_DEBUG(spdlog::get("logger"),"收到 TMS 信号");
IfHaveModel = true;
Retrain();
//重新训练
});
......@@ -53,10 +62,10 @@ void RamdomForestCalculate::SendStreamAlarminfo(const StreamInfo &alarmInfo)
// 统一的清理函数
auto cleanup = [client]() {
static bool cleaned = false; // 静态变量确保只清理一次
static bool cleaned = false; // 静态变量确保只清理一次收到 TMS 信号
if (!cleaned) {
cleaned = true;
qDebug() << "清理客户端资源";
//qDebug() << "清理客户端资源";
client->disconnectFromServer();
QTimer::singleShot(0, client, &QObject::deleteLater); // 延迟删除确保安全
}
......@@ -74,7 +83,7 @@ void RamdomForestCalculate::SendStreamAlarminfo(const StreamInfo &alarmInfo)
message["endIds"] = alarmInfo.endIds;
client->sendMessage(message);
qDebug() << "消息发送完成";
SPDLOG_LOGGER_DEBUG(spdlog::get("logger"),"发送给流式告警信息成功");
// 发送完成后立即开始清理
cleanup();
......@@ -173,6 +182,9 @@ int RamdomForestCalculate::Save_SACDATA(const Dc_SacData &sacdata)
//step2: 预测报警
//std::string data;
qDebug()<<"x "<<x;
if(IfHaveModel)
{
std::vector<float> test_sample(10,0);
//用逗号分割
QStringList list = x.split(',');
......@@ -191,6 +203,7 @@ int RamdomForestCalculate::Save_SACDATA(const Dc_SacData &sacdata)
cout << "测试样本预测类别: " << pred << endl;
StreamInfo(pred,sacdata.target,sacdata.startIds,sacdata.endIds);
}
//step3: 存入数据库
std::string data = QString(
......
......@@ -86,6 +86,7 @@ private:
TcpServerManager *m_tcpServerManager;
RandomForestClassifier* Classifier;
bool IfHaveModel = false;
};
......
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