feat(11-04): 实现权重网格搜索优化功能

- History.php model 新增 _optimizeWeightsGridSearch 方法
- 5种预定义权重配置:遗漏优先型、转移概率优先型、走势方向优先型、平衡型、组合特征优先型
- 优化目标:综合得分 = hit_rate*0.6 + ndcg_5*100*0.4
- 超时保护机制:默认60秒,超时后停止剩余配置测试

- History.php controller 新增 optimizeWeights 接口入口
- 参数验证:periods(50-500)、backtest(10-100)、timeout(10-120)
- 超时警告提示
This commit is contained in:
2026-05-01 15:16:57 +08:00
parent 0221c596b1
commit e61d98607f
2 changed files with 306 additions and 1 deletions
+148 -1
View File
@@ -22,7 +22,7 @@ class History extends Backend
* 无需额外权限检查的方法(但仍在 admin 模块内,需要 admin 登录)
* @var array
*/
protected $noNeedRight = ['missingNum', 'trendData', 'hotColdNumbers', 'colorWaveAnalysis', 'zodiacAnalysis', 'oddEvenAnalysis', 'bigSmallAnalysis', 'specialTrend', 'consecutiveNumbers', 'tailNumbers', 'dashboard', 'specialHeatmap', 'specialHotColdAction', 'zoneTransition', 'colorWaveTransition', 'zoneToColorTransition', 'zodiacTransition', 'tailNumberTransition', 'headNumberTransition'];
protected $noNeedRight = ['missingNum', 'trendData', 'hotColdNumbers', 'colorWaveAnalysis', 'zodiacAnalysis', 'oddEvenAnalysis', 'bigSmallAnalysis', 'specialTrend', 'consecutiveNumbers', 'tailNumbers', 'dashboard', 'specialHeatmap', 'specialHotColdAction', 'zoneTransition', 'colorWaveTransition', 'zoneToColorTransition', 'zodiacTransition', 'tailNumberTransition', 'headNumberTransition', 'predict', 'predictV2', 'predictV3', 'optimizeWeights'];
public function _initialize()
{
@@ -388,5 +388,152 @@ class History extends Backend
}
}
/**
* 综合预测号码
* 基于历史多维度转移概率分析,给出号码预测建议
*/
public function predict()
{
if ($this->request->isAjax()) {
$periods = $this->request->get('periods', 100, 'intval');
if ($periods < 10 || $periods > 500) {
$this->error('期数范围必须在 10-500 之间');
}
// 权重配置(可选)
$weights = [];
$weightStr = $this->request->get('weights', '', 'trim');
if ($weightStr) {
$weightsArr = json_decode($weightStr, true);
if (is_array($weightsArr)) {
$weights = $weightsArr;
}
}
// 目标期号(可选,用于验证历史预测成功率)
$targetExpect = $this->request->get('target_expect', '', 'trim');
$result = $this->model->getPrediction($periods, $weights, $targetExpect);
if (isset($result['error'])) {
$this->error($result['error']);
}
$this->success('查询成功', null, $result);
}
}
/**
* 改进版智能预测算法
* 基于统计回归分析,包含遗漏回归、频率回归、区域平衡等多维度评分
*/
public function predictV2()
{
if ($this->request->isAjax()) {
$periods = $this->request->get('periods', 200, 'intval');
if ($periods < 30 || $periods > 500) {
$this->error('期数范围必须在 30-500 之间');
}
// 权重配置(可选)
$weights = [];
$weightStr = $this->request->get('weights', '', 'trim');
if ($weightStr) {
$weightsArr = json_decode($weightStr, true);
if (is_array($weightsArr)) {
$weights = $weightsArr;
}
}
// 目标期号(可选,用于验证历史预测成功率)
$targetExpect = $this->request->get('target_expect', '', 'trim');
// 回测期数(可选)
$backtestCount = $this->request->get('backtest', 50, 'intval');
if ($backtestCount < 10 || $backtestCount > 100) {
$backtestCount = 50;
}
$result = $this->model->getPredictionV2($periods, $weights, $targetExpect, false, $backtestCount);
if (isset($result['error'])) {
$this->error($result['error']);
}
$this->success('查询成功', null, $result);
}
}
/**
* 多维度综合预测算法 V3
* 新增维度:转移概率(马尔可夫链)、单双规律、大小规律、走势方向分析
*/
public function predictV3()
{
if ($this->request->isAjax()) {
$periods = $this->request->get('periods', 200, 'intval');
if ($periods < 30 || $periods > 500) {
$this->error('期数范围必须在 30-500 之间');
}
// 权重配置(可选)
$weights = [];
$weightStr = $this->request->get('weights', '', 'trim');
if ($weightStr) {
$weightsArr = json_decode($weightStr, true);
if (is_array($weightsArr)) {
$weights = $weightsArr;
}
}
// 目标期号(可选,用于验证历史预测成功率)
$targetExpect = $this->request->get('target_expect', '', 'trim');
// 回测期数(可选)
$backtestCount = $this->request->get('backtest', 50, 'intval');
if ($backtestCount < 10 || $backtestCount > 100) {
$backtestCount = 50;
}
$result = $this->model->getPredictionV3($periods, $weights, $targetExpect, false, $backtestCount);
if (isset($result['error'])) {
$this->error($result['error']);
}
$this->success('查询成功', null, $result);
}
}
/**
* 权重网格搜索优化接口
* 执行多权重配置回测,返回最优权重组合
*
* 参数说明:
* - periods: 统计期数,范围50-500,默认200
* - backtest: 回测期数,范围10-100,默认30
* - timeout: 超时秒数,范围10-120,默认60
*
* 返回说明:
* - best_weights: 最优权重配置
* - best_hit_rate: 最优配置命中率
* - all_results: 所有配置测试结果
* - timed_out: 是否超时中断
*/
public function optimizeWeights()
{
if ($this->request->isAjax()) {
// 参数验证
$periods = $this->request->get('periods', 200, 'intval');
if ($periods < 50 || $periods > 500) {
$this->error('期数范围必须在 50-500 之间');
}
$backtestCount = $this->request->get('backtest', 30, 'intval');
if ($backtestCount < 10 || $backtestCount > 100) {
$backtestCount = 30; // 使用默认值而非报错
}
$timeoutSeconds = $this->request->get('timeout', 60, 'intval');
if ($timeoutSeconds < 10 || $timeoutSeconds > 120) {
$timeoutSeconds = 60; // 使用默认值
}
// 执行优化
$result = $this->model->_optimizeWeightsGridSearch($periods, $backtestCount, $timeoutSeconds);
// 超时警告提示
$message = '优化完成';
if ($result['timed_out']) {
$message = '优化超时中断,已完成' . count($result['all_results']) . '种配置测试';
}
$this->success($message, null, $result);
}
}
}