Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 51 additions & 50 deletions tempControl.html
Original file line number Diff line number Diff line change
Expand Up @@ -1977,35 +1977,9 @@ <h3>📋 系统日志</h3>
];

// 改进的奖励函数 - 接收水温和目标温度
const reward = this.ddpgAgent.calculateReward(nextWaterTemp, episodeTarget, heatingPower, lastWaterTemp, step);
rewardTrace.push(reward);
this.log(`📈 [第${episode + 1}轮, 第${step + 1}步] 奖励: ${reward.toFixed(2)}`);

if (this.trainCurveChart.data.datasets.length < 3) {
this.trainCurveChart.data.datasets.push({
label: '每步奖励',
data: rewardTrace,
borderColor: '#E91E63',
backgroundColor: 'rgba(233, 30, 99, 0.1)',
fill: false,
tension: 0.1,
yAxisID: 'reward',
borderWidth: 1,
pointRadius: 0
});
} else {
this.trainCurveChart.data.datasets[2].data = rewardTrace;
}
if (!this.trainCurveChart.options.scales.reward) {
this.trainCurveChart.options.scales.reward = {
type: 'linear',
position: 'right',
title: { display: true, text: '奖励值' },
beginAtZero: false,
grid: { drawOnChartArea: false }
};
}
this.trainCurveChart.update('none');
const reward = this.ddpgAgent.calculateReward(nextWaterTemp, episodeTarget, heatingPower, lastWaterTemp, step);
// 收集奖励,但不在每步更新图表
rewardTrace.push(reward);

// 改进的记忆函数,传入更多温度信息
this.ddpgAgent.remember(state, heatingPower, reward, nextState, false, waterTemp, episodeTarget, nextWaterTemp);
Expand Down Expand Up @@ -2093,14 +2067,43 @@ <h3>📋 系统日志</h3>
this.lossChart.update('none');
}

if (this.tdLossChart) {
this.tdLossChart.data.labels.push(episode + 1);
this.tdLossChart.data.datasets[0].data.push(Math.abs(this.ddpgAgent.actorLoss || 0));
this.tdLossChart.data.datasets[1].data.push(this.ddpgAgent.criticLoss || 0);
this.tdLossChart.update('none');
}

// 检查训练稳定性并可能重置探索
if (this.tdLossChart) {
this.tdLossChart.data.labels.push(episode + 1);
this.tdLossChart.data.datasets[0].data.push(Math.abs(this.ddpgAgent.actorLoss || 0));
this.tdLossChart.data.datasets[1].data.push(this.ddpgAgent.criticLoss || 0);
this.tdLossChart.update('none');
}

// 回合结束后更新奖励曲线
if (this.trainCurveChart) {
if (this.trainCurveChart.data.datasets.length < 3) {
this.trainCurveChart.data.datasets.push({
label: '每步奖励',
data: rewardTrace,
borderColor: '#E91E63',
backgroundColor: 'rgba(233, 30, 99, 0.1)',
fill: false,
tension: 0.1,
yAxisID: 'reward',
borderWidth: 1,
pointRadius: 0
});
} else {
this.trainCurveChart.data.datasets[2].data = rewardTrace;
}
if (!this.trainCurveChart.options.scales.reward) {
this.trainCurveChart.options.scales.reward = {
type: 'linear',
position: 'right',
title: { display: true, text: '奖励值' },
beginAtZero: false,
grid: { drawOnChartArea: false }
};
}
this.trainCurveChart.update('none');
}

// 检查训练稳定性并可能重置探索
const resetExploration = this.ddpgAgent.checkTrainingStability(totalReward);
if (resetExploration) {
this.log(`🔄 第${episode + 1}轮: 重置探索策略,增大噪声至 ${(this.ddpgAgent.noiseScale * 100).toFixed(1)}%`, 'warning');
Expand Down Expand Up @@ -2128,19 +2131,17 @@ <h3>📋 系统日志</h3>
`平均: ${avgReward.toFixed(1)}`
);

if (episode % 5 === 0 || episode === nEpisodes - 1) {
// 计算温度误差指标
const avgError = tempErrors.reduce((a, b) => a + b, 0) / tempErrors.length;
const maxError = Math.max(...tempErrors);

this.log(
`📈 第${episode + 1}轮: 奖励=${totalReward.toFixed(1)}, ` +
`目标温度=${episodeTarget.toFixed(1)}°C, ` +
`噪声=${(this.ddpgAgent.noiseScale * 100).toFixed(1)}%, ` +
`平均误差=${avgError.toFixed(2)}°C, ` +
`到达目标用时=${stepsToTarget !== null ? stepsToTarget : '未达'}步`
);
}
// 计算温度误差指标
const avgError = tempErrors.reduce((a, b) => a + b, 0) / tempErrors.length;
const maxError = Math.max(...tempErrors);

this.log(
`📈 第${episode + 1}轮: 奖励=${totalReward.toFixed(1)}, ` +
`目标温度=${episodeTarget.toFixed(1)}°C, ` +
`噪声=${(this.ddpgAgent.noiseScale * 100).toFixed(1)}%, ` +
`平均误差=${avgError.toFixed(2)}°C, ` +
`到达目标用时=${stepsToTarget !== null ? stepsToTarget : '未达'}步`
);

// 每 5 轮或最后一轮,快速评估一次升温曲线
if (episode % 5 === 0 || episode === nEpisodes - 1) {
Expand Down