Расход топлива трактором (учебный датасет)
Автор
using DataFrames, CSV, XLSX, Random, Dates, Statistics
"""
generate_tractor_data(; n_days=10, n_runs_per_day=2, base_fuel=20.0,
base_speed=15.0, base_load=0.6, noise_std=0.5, failure_prob=0.3,
drift_rate=0.02, seed=42, outdir=@__DIR__)
Генерирует синтетический датасет телеметрии трактора с показаниями расхода топлива
и поломками измерителя (занижение, завышение, шум, дрейф).
Сохраняет tractor_fuel_data.csv и tractor_fuel_data.xlsx.
"""
function generate_tractor_data(;
n_days::Int=10,
n_runs_per_day::Int=2,
base_fuel::Float64=20.0,
base_speed::Float64=15.0,
base_load::Float64=0.6,
noise_std::Float64=0.5,
failure_prob::Float64=0.3,
drift_rate::Float64=0.02,
seed::Int=42,
outdir::AbstractString=@__DIR__
)
Random.seed!(seed)
all_trips = DataFrame[]
trip_id = 0
for day in 1:n_days
date = Date(2024, 1, 1) + Day(day - 1)
for run in 1:n_runs_per_day
trip_id += 1
duration = rand(4:8)
n_points = duration * 60
start_hour = rand(8:10)
start_minute = rand(0:59)
start_time = DateTime(Dates.year(date), Dates.month(date), Dates.day(date), start_hour, start_minute, 0)
time = collect(start_time:Minute(1):start_time + Minute(n_points - 1))
speed = base_speed .+ 2 .* sin.(2π .* 0.01 .* (1:n_points)) .+ 0.5 .* randn(n_points)
speed = max.(speed, 5.0)
load = base_load .+ 0.3 .* sin.(2π .* 0.005 .* (1:n_points)) .+ 0.1 .* randn(n_points)
load = clamp.(load, 0.1, 0.95)
fuel_true = base_fuel .+ 0.5 .* speed ./ 10 .+ 5 .* load .+ drift_rate .* (1:n_points)
fuel_true .+= 0.3 .* randn(n_points)
fuel_true = max.(fuel_true, 5.0)
has_failure = rand() < failure_prob
fuel_measured = copy(fuel_true)
failure_type = "none"
failure_start = 0
if has_failure
failure_type = rand(["under", "over", "noise", "drift"])
failure_start = rand(Int(floor(0.2*n_points)):Int(floor(0.8*n_points)))
for i in failure_start:n_points
progress = (i - failure_start) / (n_points - failure_start)
if failure_type == "under"
fuel_measured[i] *= (0.7 - 0.2*progress)
elseif failure_type == "over"
fuel_measured[i] *= (1.3 + 0.2*progress)
elseif failure_type == "noise"
fuel_measured[i] += 3*randn()
else
fuel_measured[i] += 0.5*progress * randn()
end
end
end
for idx in rand(1:n_points, 5)
fuel_measured[idx] = NaN
end
vibration = 0.1 .+ 0.02 .* randn(n_points)
if has_failure
for i in failure_start:n_points
progress = (i - failure_start) / (n_points - failure_start)
vibration[i] += 0.1 * progress
end
end
sensor_temp = 25 .+ 0.01 .* (1:n_points) .+ 0.5 .* randn(n_points)
if has_failure && failure_type == "drift"
sensor_temp .+= 0.02 .* (1:n_points)
end
trip_df = DataFrame(
trip_id = fill(trip_id, n_points),
time = time,
point = collect(1:n_points),
fuel_true = fuel_true,
fuel_measured = fuel_measured,
speed = speed,
load = load,
vibration = vibration,
sensor_temp = sensor_temp,
has_failure = fill(has_failure, n_points),
failure_type = fill(failure_type, n_points)
)
push!(all_trips, trip_df)
end
end
df = vcat(all_trips...)
CSV.write(joinpath(outdir, "tractor_fuel_data.csv"), df)
XLSX.writetable(joinpath(outdir, "tractor_fuel_data.xlsx"),
collect(eachcol(df)), names(df); sheetname="trips", overwrite=true)
println("Сохранён tractor_fuel_data: ", nrow(df), " точек, выездов: ", length(unique(df.trip_id)))
return df
end
if abspath(PROGRAM_FILE) == @__FILE__
generate_tractor_data()
end