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
88 changes: 23 additions & 65 deletions tempControl.html
Original file line number Diff line number Diff line change
Expand Up @@ -1432,7 +1432,6 @@ <h3>📋 系统日志</h3>
episodeRewards: []
};

this.stepSinceLastDraw = 0;
}

setupCharts() {
Expand Down Expand Up @@ -1817,14 +1816,7 @@ <h3>📋 系统日志</h3>
this.trainCurveChart.data.datasets[1].data = tempTrace;
this.trainCurveChart.data.datasets[2].data = rewardTrace;

const CHART_UPDATE_INTERVAL = 1000; // 每 20 步才 redraw

this.stepSinceLastDraw++;
if (this.stepSinceLastDraw >= CHART_UPDATE_INTERVAL || step === episodeEnd) {
this.stepSinceLastDraw = 0;
this.trainCurveChart.update('none');

}
this.trainCurveChart.update('none');

}

Expand Down Expand Up @@ -1879,12 +1871,8 @@ <h3>📋 系统日志</h3>
deltaT: parseFloat(document.getElementById('deltaT').value)
};

const rewardTrace = [];

this.stepSinceLastDraw = 0;

// 禁止训练期间使用固定 targetTemp
config.targetTemp = null;
// 禁止训练期间使用固定 targetTemp
config.targetTemp = null;

// 动态计算合理的目标温度范围,基于环境温度和最大功率
const minTemp = Math.max(25, config.roomTemp + 5); // 至少高于室温5度
Expand Down Expand Up @@ -1977,35 +1965,7 @@ <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);

// 改进的记忆函数,传入更多温度信息
this.ddpgAgent.remember(state, heatingPower, reward, nextState, false, waterTemp, episodeTarget, nextWaterTemp);
Expand Down Expand Up @@ -2093,14 +2053,14 @@ <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');
}

// 检查训练稳定性并可能重置探索
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 +2088,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