From 22b7b1275170d32ac2983ea6ab9197d8ff83517f Mon Sep 17 00:00:00 2001 From: StarsAC Date: Wed, 26 Nov 2025 22:40:43 +0800 Subject: [PATCH] =?UTF-8?q?feat(ml=5Fpredict):=20=E5=90=AF=E7=94=A8=20ml?= =?UTF-8?q?=5Flog=5Fideal=5Fattempt=20=E6=97=A5=E5=BF=97=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 将原本被注释掉的 ml_log_ideal_attempt 函数启用,用于记录理想尝试的相关数据。 该函数会将每次尝试的时间戳、次数、范数、迹、核阶数、预测概率及成功标志写入 CSV 文件。 确保在项目根目录下生成 dataset 文件夹,并将日志持久化存储于 ideal_data.csv 中。 --- src/signature/ref/lvlx/ml_predict.c | 72 ++++++++++++++--------------- 1 file changed, 36 insertions(+), 36 deletions(-) diff --git a/src/signature/ref/lvlx/ml_predict.c b/src/signature/ref/lvlx/ml_predict.c index 6d0af71..195b5aa 100644 --- a/src/signature/ref/lvlx/ml_predict.c +++ b/src/signature/ref/lvlx/ml_predict.c @@ -78,47 +78,47 @@ double ml_predict_success(long norm_val, int trace_val, int kernel_order) { // 日志函数:记录一次理想尝试 // 记录到 CSV 文件,包含时间戳 -// void ml_log_ideal_attempt(int attempt, -// const quat_left_ideal_t *lideal_com, -// int kernel_order, -// int success_flag) -// { -// const char *dir_path = "./dataset"; -// struct stat st = {0}; +void ml_log_ideal_attempt(int attempt, + const quat_left_ideal_t *lideal_com, + int kernel_order, + int success_flag) +{ + const char *dir_path = "./dataset"; + struct stat st = {0}; -// if (stat(dir_path, &st) == -1) { -// mkdir(dir_path, 0755); -// } + if (stat(dir_path, &st) == -1) { + mkdir(dir_path, 0755); + } -// // 使用固定文件名,避免频繁创建文件 -// char csv_path[512]; -// snprintf(csv_path, sizeof(csv_path), "%s/ideal_data.csv", dir_path); + // 使用固定文件名,避免频繁创建文件 + char csv_path[512]; + snprintf(csv_path, sizeof(csv_path), "%s/ideal_data.csv", dir_path); -// // 使用追加模式,避免覆盖 -// FILE *logfile = fopen(csv_path, "a+"); -// if (!logfile) { -// perror("无法打开 ideal_data CSV 文件"); -// return; -// } + // 使用追加模式,避免覆盖 + FILE *logfile = fopen(csv_path, "a+"); + if (!logfile) { + perror("无法打开 ideal_data CSV 文件"); + return; + } -// // 如果是第一次写入,添加表头 -// if (ftell(logfile) == 0) { -// fprintf(logfile, "Timestamp,Attempt,Norm,Trace,KernelOrder,Prob,Success\n"); -// } + // 如果是第一次写入,添加表头 + if (ftell(logfile) == 0) { + fprintf(logfile, "Timestamp,Attempt,Norm,Trace,KernelOrder,Prob,Success\n"); + } -// // 提取特征和记录 -// long norm_val = ibz_to_long_safe(&lideal_com->norm); -// int trace_val = quat_ideal_trace(lideal_com); -// double prob = ml_predict_success(norm_val, trace_val, kernel_order); + // 提取特征和记录 + long norm_val = ibz_to_long_safe(&lideal_com->norm); + int trace_val = quat_ideal_trace(lideal_com); + double prob = ml_predict_success(norm_val, trace_val, kernel_order); -// time_t t = time(NULL); -// struct tm tm_info; -// localtime_r(&t, &tm_info); -// char time_str[32]; -// strftime(time_str, sizeof(time_str), "%Y-%m-%d-%H-%M-%S", &tm_info); + time_t t = time(NULL); + struct tm tm_info; + localtime_r(&t, &tm_info); + char time_str[32]; + strftime(time_str, sizeof(time_str), "%Y-%m-%d-%H-%M-%S", &tm_info); -// fprintf(logfile, "%s,%d,%ld,%d,%d,%.3f,%d\n", -// time_str, attempt, norm_val, trace_val, kernel_order, prob, success_flag); + fprintf(logfile, "%s,%d,%ld,%d,%d,%.3f,%d\n", + time_str, attempt, norm_val, trace_val, kernel_order, prob, success_flag); -// fclose(logfile); -// } \ No newline at end of file + fclose(logfile); +} \ No newline at end of file