Система теплоснабжения в ЖКХ (учебный датасет)
Author
using DataFrames, CSV, Dates, Random, Statistics, XLSX
function generate_utility_data(;
n_days=30,
readings_per_day=24,
seed=42,
outdir=@__DIR__
)
Random.seed!(seed)
start_date = DateTime(2024, 1, 1)
n_points = n_days * readings_per_day
timestamps = [DateTime(start_date + Day(d)) + Hour(h)
for d in 0:n_days-1 for h in 0:readings_per_day-1]
# --- 1. ПОГОДНЫЕ УСЛОВИЯ ---
outside_temp = -5 .+ 10*sin.(2π * (1:n_points)/readings_per_day) .+
3*cos.(2π * (1:n_points)/(n_points/2)) .+ 2*randn(n_points)
hour_of_day = [Dates.hour(t) for t in timestamps]
# Солнечная радиация (только днём, пик в полдень)
solar_radiation = zeros(n_points)
for i in 1:n_points
h = hour_of_day[i]
if 6 <= h <= 20
solar_radiation[i] = max(0, 300 * (1 - ((h - 13)/7)^2)) + 20*randn()
end
end
solar_radiation = max.(0, solar_radiation)
# --- 2. ТЕПЛОВАЯ НАГРУЗКА (правильная физика) ---
# Теплопотери при 22°C комфорта
heat_demand = max.(0, 22 .- outside_temp)
# Базовый расход: растёт с теплопотерями, суточный цикл (утренний/вечерний пик)
base_flow = 30 .+ 3.0 * heat_demand .+
5*sin.(2π * (hour_of_day .+ 6) / 24) .+ # Пик утром и вечером
3*randn(n_points)
flow_rate = max.(15, base_flow)
# --- 3. ДАВЛЕНИЕ (квадратичная характеристика сети) ---
# P ≈ k * Q² + потери на трение
base_pressure = 2.0 .+ 0.005 * flow_rate .^ 2 .+ 0.2*randn(n_points)
# --- 4. АНОМАЛИИ (утечки) ---
leak_days = rand(0:n_days-1, 3)
leak_magnitude = rand(3) .* 0.5 .+ 0.5 # Разная интенсивность утечек
anomaly_flow = zeros(n_points)
anomaly_pressure = zeros(n_points)
anomaly_flow_mark = zeros(Int, n_points) # Метка для анализа
for (idx, d) in enumerate(leak_days)
start_idx = d*readings_per_day + 1
end_idx = (d+1)*readings_per_day
# Утечка: расход растёт, давление падает
magnitude = leak_magnitude[idx]
anomaly_flow[start_idx:end_idx] .= 15 .* magnitude .* rand(readings_per_day) .+ 5*magnitude
anomaly_pressure[start_idx:end_idx] .= -0.8 .* magnitude .* rand(readings_per_day) .- 0.5*magnitude
anomaly_flow_mark[start_idx:end_idx] .= idx
end
flow_rate = flow_rate .+ anomaly_flow
pressure = base_pressure .+ anomaly_pressure .+ 0.3*randn(n_points)
pressure = max.(2.0, pressure)
# --- 5. ТОК НАСОСА (гидравлическая мощность) ---
# P_эл = Q * ΔP / (η_насоса * η_двигателя)
# Для учебных целей: I ≈ k * Q * P / U
base_current = 5 .+ 0.025 * flow_rate .* pressure .+ 2*randn(n_points)
current = max.(3, base_current)
# --- 6. ВНУТРЕННЯЯ ТЕМПЕРАТУРА НА ЭТАЖАХ ---
floor_temps = Dict{Int, Vector{Float64}}()
for floor in 1:8
# Базовое распределение: теплее на верхних этажах
floor_offset = (9 - floor) * 0.3
# Суточные колебания с задержкой на этажах
daily_cycle = 2*sin.(2π * hour_of_day / 24 .+ floor*0.2)
# Солнечный нагрев: больше на верхних этажах
solar_effect = solar_radiation * (0.0005 + 0.0003 * floor)
# Отопление: больше тепла на нижних этажах (ближе к источнику)
heating_effect = heat_demand * (0.5 - 0.04 * floor)
# Температура
temp = 18 .+ floor_offset .+ daily_cycle .+
0.02*solar_effect .+ 0.04*heating_effect .+ 0.5*randn(n_points)
# Аномалии при утечках: падение температуры
for (idx, d) in enumerate(leak_days)
start_idx = d*readings_per_day + 1
end_idx = (d+1)*readings_per_day
if rand() < 0.5 # Не все утечки влияют на все этажи
magnitude = leak_magnitude[idx]
temp[start_idx:end_idx] .-= (1.5 .+ 1.5*magnitude .* rand(readings_per_day))
end
end
floor_temps[floor] = max.(14, min.(28, temp))
end
# --- 7. ДОПОЛНИТЕЛЬНЫЕ ПРИЗНАКИ ДЛЯ АНАЛИЗА ---
# День недели (выходной vs рабочий)
day_of_week = [Dates.dayofweek(t) for t in timestamps]
is_weekend = [dow in [6, 7] ? 1 : 0 for dow in day_of_week]
# Ночной режим (меньше потребление)
is_night = [h in 0:5 || h in 22:23 ? 1 : 0 for h in hour_of_day]
# --- 8. СБОРКА DATAFRAME ---
df = DataFrame(
timestamp = timestamps,
outside_temp = outside_temp,
solar_radiation = solar_radiation,
flow_rate = flow_rate,
pressure = pressure,
current = current,
hour = hour_of_day,
day = [d for d in 0:n_days-1 for _ in 0:readings_per_day-1],
day_of_week = day_of_week,
is_weekend = is_weekend,
is_night = is_night,
is_leak_day = [d in leak_days ? 1 : 0 for d in 0:n_days-1 for _ in 0:readings_per_day-1],
leak_id = anomaly_flow_mark # 0 = нет утечки, 1,2,3 = номер утечки
)
for floor in 1:8
df[!, "temp_floor_floor"] = floor_temps[floor]
end
CSV.write(joinpath(outdir, "utility_data.csv"), df)
XLSX.writetable(joinpath(outdir, "utility_data.xlsx"), collect(eachcol(df)), names(df); sheetname="data", overwrite=true)
return df
end