-
Notifications
You must be signed in to change notification settings - Fork 1
Solene #51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
BatyLeo
merged 6 commits into
JuliaDecisionFocusedLearning:main
from
sdelannoypavy:solene
Jan 12, 2026
Merged
Solene #51
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6516cc6
fixed buggs
sdelannoypavy 0cf9022
Added maintenance benchmark
sdelannoypavy 9cfc83b
Completed documentation
sdelannoypavy 778e7ec
Corrected some bugs and added documentation
sdelannoypavy 8731113
Fix tests
BatyLeo 9894b43
coverage
BatyLeo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| # Maintenance problem with resource constraint | ||
|
|
||
| The Maintenance problem with resource constraint is a sequential decision-making benchmark where an agent must repeatedly decide which components to maintain over time. The goal is to minimize total expected cost while accounting for independent degradation of components and limited maintenance capacity. | ||
|
|
||
|
|
||
| ## Problem Description | ||
|
|
||
| ### Overview | ||
|
|
||
| In this benchmark, a system consists of $N$ identical components, each of which can degrade over $n$ discrete states. State $1$ means that the component is new, state $n$ means that the component is failed. At each time step, the agent can maintain up to $K$ components. | ||
|
|
||
| This forms an endogenous multistage stochastic optimization problem, where the agent must plan maintenance actions over the horizon. | ||
|
|
||
| ### Mathematical Formulation | ||
|
|
||
| The maintenance problem can be formulated as a finite-horizon Markov Decision Process (MDP) with the following components: | ||
|
|
||
| **State Space** $\mathcal{S}$: At time step $t$, the state $s_t \in [1:n]^N$ is the degradation state for each component. | ||
|
|
||
| **Action Space** $\mathcal{A}$: The action at time $t$ is the set of components that are maintained at time $t$: | ||
| ```math | ||
| a_t \subseteq \{1, 2, \ldots, N\} \text{ such that } |a_t| \leq K | ||
| ``` | ||
| ### Transition Dynamics | ||
|
|
||
| The state transitions depend on whether a component is maintained or not: | ||
|
|
||
| For each component \(i\) at time \(t\): | ||
|
|
||
| - **Maintained component** (\(i \in a_t\)): | ||
|
|
||
| \[ | ||
| s_{t+1}^i = 1 \quad \text{(perfect maintenance)} | ||
| \] | ||
|
|
||
| - **Unmaintained component** (\(i \notin a_t\)): | ||
|
|
||
| \[ | ||
| s_{t+1}^i = | ||
| \begin{cases} | ||
| \min(s_t^i + 1, n) & \text{with probability } p,\\ | ||
| s_t^i & \text{with probability } 1-p. | ||
| \end{cases} | ||
| \] | ||
|
|
||
| Here, \(p\) is the degradation probability, \(s_t^i\) is the current state of component \(i\), and \(n\) is the maximum (failed) state. | ||
|
|
||
| --- | ||
|
|
||
| ### Cost Function | ||
|
|
||
| The immediate cost at time \(t\) is: | ||
|
|
||
| $$ | ||
| c(s_t, a_t) = \Big( c_m \cdot |a_t| + c_f \cdot \#\{ i : s_t^i = n \} \Big) | ||
| $$ | ||
|
|
||
| Where: | ||
|
|
||
| - $c_m$ is the maintenance cost per component. | ||
| - $|a_t|$ is the number of components maintained. | ||
| - $c_f$ is the failure cost per failed component. | ||
| - $\#\{ i : s_t^i = n \}$ counts the number of components in the failed state. | ||
|
|
||
| This formulation captures the total cost for maintaining components and penalizing failures. | ||
|
|
||
| **Objective**: Find a policy $\pi: \mathcal{S} \to \mathcal{A}$ that minimizes the expected cumulative cost: | ||
| ```math | ||
| \min_\pi \mathbb{E}\left[\sum_{t=1}^T c(s_t, \pi(s_t)) \right] | ||
| ``` | ||
|
|
||
| **Terminal Condition**: The episode terminates after $T$ time steps, with no terminal reward. | ||
|
|
||
| ## Key Components | ||
|
|
||
| ### [`MaintenanceBenchmark`](@ref) | ||
|
|
||
| The main benchmark configuration with the following parameters: | ||
|
|
||
| - `N`: number of components (default: 2) | ||
| - `K`: maximum number of components that can be maintained simultaneously (default: 1) | ||
| - `n`: number of degradation states per component (default: 3) | ||
| - `p`: degradation probability (default: 0.2) | ||
| - `c_f`: failure cost (default: 10.0) | ||
| - `c_m`: maintenance cost (default: 3.0) | ||
| - `max_steps`: Number of time steps per episode (default: 80) | ||
|
|
||
| ### Instance Generation | ||
|
|
||
| Each problem instance includes: | ||
|
|
||
| - **Starting State**: Random starting degradation state in $[1,n]$ for each components. | ||
|
|
||
| ### Environment Dynamics | ||
|
|
||
| The environment tracks: | ||
| - Current time step | ||
| - Current degradation state. | ||
|
|
||
| **State Observation**: Agents observe a normalized feature vector containing the degradation state of each component. | ||
|
|
||
| ## Benchmark Policies | ||
|
|
||
| ### Greedy Policy | ||
|
|
||
| Greedy policy that maintains components in the last two degradation states, up to the maintenance capacity. This provides a simple baseline. | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| module Maintenance | ||
|
|
||
| using ..Utils | ||
|
|
||
| using DocStringExtensions: TYPEDEF, TYPEDFIELDS, TYPEDSIGNATURES, SIGNATURES | ||
| using Distributions: Uniform, Categorical | ||
| using Flux: Chain, Dense | ||
| using LinearAlgebra: dot | ||
| using Random: Random, AbstractRNG, MersenneTwister | ||
| using Statistics: mean | ||
|
|
||
| using Combinatorics: combinations | ||
|
|
||
| """ | ||
| $TYPEDEF | ||
|
|
||
| Benchmark for a standard maintenance problem with resource constraints. | ||
| Components are identical and degrade independently over time. | ||
| A high cost is incurred for each component that reaches the final degradation level. | ||
| A cost is also incurred for maintaining a component. | ||
| The number of simultaneous maintenance operations is limited by a maintenance capacity constraint. | ||
|
|
||
| # Fields | ||
| $TYPEDFIELDS | ||
|
|
||
| """ | ||
| struct MaintenanceBenchmark <: AbstractDynamicBenchmark{true} | ||
| "number of components" | ||
| N::Int | ||
| "maximum number of components that can be maintained simultaneously" | ||
| K::Int | ||
| "number of degradation states per component" | ||
| n::Int | ||
| "degradation probability" | ||
| p::Float64 | ||
| "failure cost" | ||
| c_f::Float64 | ||
| "maintenance cost" | ||
| c_m::Float64 | ||
| "number of steps per episode" | ||
| max_steps::Int | ||
|
sdelannoypavy marked this conversation as resolved.
|
||
|
|
||
| function MaintenanceBenchmark(N, K, n, p, c_f, c_m, max_steps) | ||
| @assert K <= N "number of maintained components $K > number of components $N" | ||
| @assert K >= 0 && N >= 0 "number of components should be positive" | ||
| @assert 0 <= p <= 1 "degradation probability $p is not in [0, 1]" | ||
| return new(N, K, n, p, c_f, c_m, max_steps) | ||
| end | ||
| end | ||
|
|
||
| """ | ||
| MaintenanceBenchmark(; | ||
| N=2, | ||
| K=1, | ||
| n=3, | ||
| p=0.2 | ||
| c_f=10.0, | ||
| c_m=3.0, | ||
| max_steps=80, | ||
| ) | ||
|
|
||
| Constructor for [`MaintenanceBenchmark`](@ref). | ||
| By default, the benchmark has 2 components, maintenance capacity 1, number of degradation levels 3, | ||
| degradation probability 0.2, failure cost 10.0, maintenance cost 3.0, 80 steps per episode, and is exogenous. | ||
| """ | ||
| function MaintenanceBenchmark(; N=2, K=1, n=3, p=0.2, c_f=10.0, c_m=3.0, max_steps=80) | ||
| return MaintenanceBenchmark(N, K, n, p, c_f, c_m, max_steps) | ||
| end | ||
|
|
||
| # Accessor functions | ||
| component_count(b::MaintenanceBenchmark) = b.N | ||
| maintenance_capacity(b::MaintenanceBenchmark) = b.K | ||
| degradation_levels(b::MaintenanceBenchmark) = b.n | ||
| degradation_probability(b::MaintenanceBenchmark) = b.p | ||
| failure_cost(b::MaintenanceBenchmark) = b.c_f | ||
| maintenance_cost(b::MaintenanceBenchmark) = b.c_m | ||
| max_steps(b::MaintenanceBenchmark) = b.max_steps | ||
|
|
||
| include("instance.jl") | ||
| include("environment.jl") | ||
| include("policies.jl") | ||
| include("maximizer.jl") | ||
|
|
||
| """ | ||
| $TYPEDSIGNATURES | ||
|
|
||
| Outputs a data sample containing an [`Instance`](@ref). | ||
| """ | ||
| function Utils.generate_sample(b::MaintenanceBenchmark, rng::AbstractRNG) | ||
| return DataSample(; instance=Instance(b, rng)) | ||
| end | ||
|
|
||
| """ | ||
| $TYPEDSIGNATURES | ||
|
|
||
| Generates a statistical model for the maintenance benchmark. | ||
| The model is a small neural network with one hidden layer no activation function. | ||
| """ | ||
| function Utils.generate_statistical_model(b::MaintenanceBenchmark; seed=nothing) | ||
| Random.seed!(seed) | ||
| N = component_count(b) | ||
| return Chain(Dense(N => N), Dense(N => N), vec) | ||
| end | ||
|
|
||
| """ | ||
| $TYPEDSIGNATURES | ||
|
|
||
| Outputs a top k maximizer, with k being the maintenance capacity of the benchmark. | ||
| """ | ||
| function Utils.generate_maximizer(b::MaintenanceBenchmark) | ||
| return TopKPositiveMaximizer(maintenance_capacity(b)) | ||
| end | ||
|
|
||
| """ | ||
| $TYPEDSIGNATURES | ||
|
|
||
| Creates an [`Environment`](@ref) from an [`Instance`](@ref) of the maintenance benchmark. | ||
| The seed of the environment is randomly generated using the provided random number generator. | ||
| """ | ||
| function Utils.generate_environment( | ||
| ::MaintenanceBenchmark, instance::Instance, rng::AbstractRNG; kwargs... | ||
| ) | ||
| seed = rand(rng, 1:typemax(Int)) | ||
| return Environment(instance; seed) | ||
| end | ||
|
|
||
| """ | ||
| $TYPEDSIGNATURES | ||
|
|
||
| Returns two policies for the dynamic assortment benchmark: | ||
| - `Greedy`: maintains components when they are in the last state before failure, up to the maintenance capacity | ||
| """ | ||
| function Utils.generate_policies(::MaintenanceBenchmark) | ||
| greedy = Policy( | ||
| "Greedy", | ||
| "policy that maintains components when they are in the last state before failure, up to the maintenance capacity", | ||
| greedy_policy, | ||
| ) | ||
| return (greedy,) | ||
| end | ||
|
|
||
| export MaintenanceBenchmark | ||
|
|
||
| end | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.