|
| 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 idependently 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 | +end |
| 43 | + |
| 44 | +""" |
| 45 | + MaintenanceBenchmark(; |
| 46 | + N=2, |
| 47 | + K=1, |
| 48 | + n=3, |
| 49 | + p=0.2 |
| 50 | + c_f=10.0, |
| 51 | + c_m=3.0, |
| 52 | + max_steps=10, |
| 53 | + ) |
| 54 | +
|
| 55 | +Constructor for [`MaintenanceBenchmark`](@ref). |
| 56 | +By default, the benchmark has 2 components, maintenance capacity 1, number of degradation levels 3, |
| 57 | +degradation probability 0.2, failure cost 10.0, maintenance cost 3.0, 10 steps per episode, and is exogenous. |
| 58 | +""" |
| 59 | + |
| 60 | +function MaintenanceBenchmark(; |
| 61 | + N=2, |
| 62 | + K=1, |
| 63 | + n=3, |
| 64 | + p=0.2, |
| 65 | + c_f=10.0, |
| 66 | + c_m=3.0, |
| 67 | + max_steps=10, |
| 68 | +) |
| 69 | + return MaintenanceBenchmark( |
| 70 | + N, K, n, p, c_f, c_m, max_steps |
| 71 | + ) |
| 72 | +end |
| 73 | + |
| 74 | +# Accessor functions |
| 75 | +component_count(b::MaintenanceBenchmark) = b.N |
| 76 | +maintenance_capacity(b::MaintenanceBenchmark) = b.K |
| 77 | +degradation_levels(b::MaintenanceBenchmark) = b.n |
| 78 | +degradation_probability(b::MaintenanceBenchmark) = b.p |
| 79 | +failure_cost(b::MaintenanceBenchmark) = b.c_f |
| 80 | +maintenance_cost(b::MaintenanceBenchmark) = b.c_m |
| 81 | +max_steps(b::MaintenanceBenchmark) = b.max_steps |
| 82 | + |
| 83 | +include("instance.jl") |
| 84 | +include("environment.jl") |
| 85 | +include("policies.jl") |
| 86 | +include("maximizer.jl") |
| 87 | + |
| 88 | +""" |
| 89 | +$TYPEDSIGNATURES |
| 90 | +
|
| 91 | +Outputs a data sample containing an [`Instance`](@ref). |
| 92 | +""" |
| 93 | +function Utils.generate_sample( |
| 94 | + b::MaintenanceBenchmark, rng::AbstractRNG=MersenneTwister(0) |
| 95 | +) |
| 96 | + return DataSample(; instance=Instance(b, rng)) |
| 97 | +end |
| 98 | + |
| 99 | +""" |
| 100 | +$TYPEDSIGNATURES |
| 101 | +
|
| 102 | +Generates a statistical model for the maintenance benchmark. |
| 103 | +The model is a small neural network with one hidden layer no activation function. |
| 104 | +""" |
| 105 | +function Utils.generate_statistical_model(b::MaintenanceBenchmark; seed=nothing) |
| 106 | + Random.seed!(seed) |
| 107 | + N = component_count(b) |
| 108 | + return Chain(Dense(N => N), Dense(N => N), vec) |
| 109 | +end |
| 110 | + |
| 111 | +""" |
| 112 | +$TYPEDSIGNATURES |
| 113 | +
|
| 114 | +Outputs a top k maximizer, with k being the maintenance capacity of the benchmark. |
| 115 | +""" |
| 116 | +function Utils.generate_maximizer(b::MaintenanceBenchmark) |
| 117 | + return TopKPositiveMaximizer(maintenance_capacity(b)) |
| 118 | +end |
| 119 | + |
| 120 | +""" |
| 121 | +$TYPEDSIGNATURES |
| 122 | +
|
| 123 | +Creates an [`Environment`](@ref) from an [`Instance`](@ref) of the maintenance benchmark. |
| 124 | +The seed of the environment is randomly generated using the provided random number generator. |
| 125 | +""" |
| 126 | +function Utils.generate_environment( |
| 127 | + ::MaintenanceBenchmark, instance::Instance, rng::AbstractRNG; kwargs... |
| 128 | +) |
| 129 | + seed = rand(rng, 1:typemax(Int)) |
| 130 | + return Environment(instance; seed) |
| 131 | +end |
| 132 | + |
| 133 | +""" |
| 134 | +$TYPEDSIGNATURES |
| 135 | +
|
| 136 | +Returns two policies for the dynamic assortment benchmark: |
| 137 | +- `Greedy`: maintains components when they are in the last state before failure, up to the maintenance capacity |
| 138 | +""" |
| 139 | +function Utils.generate_policies(::MaintenanceBenchmark) |
| 140 | + greedy = Policy( |
| 141 | + "Greedy", |
| 142 | + "policy that maintains components when they are in the last state before failure, up to the maintenance capacity", |
| 143 | + greedy_policy, |
| 144 | + ) |
| 145 | + return (greedy) |
| 146 | +end |
| 147 | + |
| 148 | +export MaintenanceBenchmark |
| 149 | + |
| 150 | +end |
0 commit comments