|
| 1 | +module Maintenance |
| 2 | + |
| 3 | +using ..Utils |
| 4 | + |
| 5 | +using DocStringExtensions: TYPEDEF, TYPEDFIELDS, TYPEDSIGNATURES, SIGNATURES |
| 6 | +using Distributions: Uniform, Categorical |
| 7 | +using Flux: Chain, Dense |
| 8 | +using LinearAlgebra: dot |
| 9 | +using Random: Random, AbstractRNG, MersenneTwister |
| 10 | +using Statistics: mean |
| 11 | + |
| 12 | +using Combinatorics: combinations |
| 13 | + |
| 14 | +""" |
| 15 | +$TYPEDEF |
| 16 | +
|
| 17 | +Benchmark for a standard maintenance problem with resource constraints. |
| 18 | +Components are identical and degrade independently over time. |
| 19 | +A high cost is incurred for each component that reaches the final degradation level. |
| 20 | +A cost is also incurred for maintaining a component. |
| 21 | +The number of simultaneous maintenance operations is limited by a maintenance capacity constraint. |
| 22 | +
|
| 23 | +# Fields |
| 24 | +$TYPEDFIELDS |
| 25 | +
|
| 26 | +""" |
| 27 | +struct MaintenanceBenchmark <: AbstractDynamicBenchmark{true} |
| 28 | + "number of components" |
| 29 | + N::Int |
| 30 | + "maximum number of components that can be maintained simultaneously" |
| 31 | + K::Int |
| 32 | + "number of degradation states per component" |
| 33 | + n::Int |
| 34 | + "degradation probability" |
| 35 | + p::Float64 |
| 36 | + "failure cost" |
| 37 | + c_f::Float64 |
| 38 | + "maintenance cost" |
| 39 | + c_m::Float64 |
| 40 | + "number of steps per episode" |
| 41 | + max_steps::Int |
| 42 | + |
| 43 | + function MaintenanceBenchmark(N, K, n, p, c_f, c_m, max_steps) |
| 44 | + @assert K <= N "number of maintained components $K > number of components $N" |
| 45 | + @assert K >= 0 && N >= 0 "number of components should be positive" |
| 46 | + @assert 0 <= p <= 1 "degradation probability $p is not in [0, 1]" |
| 47 | + return new(N, K, n, p, c_f, c_m, max_steps) |
| 48 | + end |
| 49 | +end |
| 50 | + |
| 51 | +""" |
| 52 | + MaintenanceBenchmark(; |
| 53 | + N=2, |
| 54 | + K=1, |
| 55 | + n=3, |
| 56 | + p=0.2 |
| 57 | + c_f=10.0, |
| 58 | + c_m=3.0, |
| 59 | + max_steps=80, |
| 60 | + ) |
| 61 | +
|
| 62 | +Constructor for [`MaintenanceBenchmark`](@ref). |
| 63 | +By default, the benchmark has 2 components, maintenance capacity 1, number of degradation levels 3, |
| 64 | +degradation probability 0.2, failure cost 10.0, maintenance cost 3.0, 80 steps per episode, and is exogenous. |
| 65 | +""" |
| 66 | +function MaintenanceBenchmark(; N=2, K=1, n=3, p=0.2, c_f=10.0, c_m=3.0, max_steps=80) |
| 67 | + return MaintenanceBenchmark(N, K, n, p, c_f, c_m, max_steps) |
| 68 | +end |
| 69 | + |
| 70 | +# Accessor functions |
| 71 | +component_count(b::MaintenanceBenchmark) = b.N |
| 72 | +maintenance_capacity(b::MaintenanceBenchmark) = b.K |
| 73 | +degradation_levels(b::MaintenanceBenchmark) = b.n |
| 74 | +degradation_probability(b::MaintenanceBenchmark) = b.p |
| 75 | +failure_cost(b::MaintenanceBenchmark) = b.c_f |
| 76 | +maintenance_cost(b::MaintenanceBenchmark) = b.c_m |
| 77 | +max_steps(b::MaintenanceBenchmark) = b.max_steps |
| 78 | + |
| 79 | +include("instance.jl") |
| 80 | +include("environment.jl") |
| 81 | +include("policies.jl") |
| 82 | +include("maximizer.jl") |
| 83 | + |
| 84 | +""" |
| 85 | +$TYPEDSIGNATURES |
| 86 | +
|
| 87 | +Outputs a data sample containing an [`Instance`](@ref). |
| 88 | +""" |
| 89 | +function Utils.generate_sample(b::MaintenanceBenchmark, rng::AbstractRNG) |
| 90 | + return DataSample(; instance=Instance(b, rng)) |
| 91 | +end |
| 92 | + |
| 93 | +""" |
| 94 | +$TYPEDSIGNATURES |
| 95 | +
|
| 96 | +Generates a statistical model for the maintenance benchmark. |
| 97 | +The model is a small neural network with one hidden layer no activation function. |
| 98 | +""" |
| 99 | +function Utils.generate_statistical_model(b::MaintenanceBenchmark; seed=nothing) |
| 100 | + Random.seed!(seed) |
| 101 | + N = component_count(b) |
| 102 | + return Chain(Dense(N => N), Dense(N => N), vec) |
| 103 | +end |
| 104 | + |
| 105 | +""" |
| 106 | +$TYPEDSIGNATURES |
| 107 | +
|
| 108 | +Outputs a top k maximizer, with k being the maintenance capacity of the benchmark. |
| 109 | +""" |
| 110 | +function Utils.generate_maximizer(b::MaintenanceBenchmark) |
| 111 | + return TopKPositiveMaximizer(maintenance_capacity(b)) |
| 112 | +end |
| 113 | + |
| 114 | +""" |
| 115 | +$TYPEDSIGNATURES |
| 116 | +
|
| 117 | +Creates an [`Environment`](@ref) from an [`Instance`](@ref) of the maintenance benchmark. |
| 118 | +The seed of the environment is randomly generated using the provided random number generator. |
| 119 | +""" |
| 120 | +function Utils.generate_environment( |
| 121 | + ::MaintenanceBenchmark, instance::Instance, rng::AbstractRNG; kwargs... |
| 122 | +) |
| 123 | + seed = rand(rng, 1:typemax(Int)) |
| 124 | + return Environment(instance; seed) |
| 125 | +end |
| 126 | + |
| 127 | +""" |
| 128 | +$TYPEDSIGNATURES |
| 129 | +
|
| 130 | +Returns two policies for the dynamic assortment benchmark: |
| 131 | +- `Greedy`: maintains components when they are in the last state before failure, up to the maintenance capacity |
| 132 | +""" |
| 133 | +function Utils.generate_policies(::MaintenanceBenchmark) |
| 134 | + greedy = Policy( |
| 135 | + "Greedy", |
| 136 | + "policy that maintains components when they are in the last state before failure, up to the maintenance capacity", |
| 137 | + greedy_policy, |
| 138 | + ) |
| 139 | + return (greedy,) |
| 140 | +end |
| 141 | + |
| 142 | +export MaintenanceBenchmark |
| 143 | + |
| 144 | +end |
0 commit comments