-
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 3 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
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,150 @@ | ||
| 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 idependently over time. | ||
|
sdelannoypavy marked this conversation as resolved.
Outdated
|
||
| 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.
|
||
| end | ||
|
|
||
| """ | ||
| MaintenanceBenchmark(; | ||
| N=2, | ||
| K=1, | ||
| n=3, | ||
| p=0.2 | ||
| c_f=10.0, | ||
| c_m=3.0, | ||
| max_steps=10, | ||
| ) | ||
|
|
||
| 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, 10 steps per episode, and is exogenous. | ||
| """ | ||
|
|
||
|
sdelannoypavy marked this conversation as resolved.
Outdated
|
||
| 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=MersenneTwister(0) | ||
| ) | ||
|
sdelannoypavy marked this conversation as resolved.
Outdated
|
||
| 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 | ||
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,167 @@ | ||
| """ | ||
| $TYPEDEF | ||
|
|
||
| Environment for the maintenance problem. | ||
|
|
||
| # Fields | ||
| $TYPEDFIELDS | ||
| """ | ||
| @kwdef mutable struct Environment{I<:Instance,R<:AbstractRNG,S<:Union{Nothing,Int}} <: | ||
| Utils.AbstractEnvironment | ||
| "associated instance" | ||
| instance::I | ||
| "current step" | ||
| step::Int | ||
| "degradation state" | ||
| degradation_state::Vector{Int} | ||
| "rng" | ||
| rng::R | ||
| "seed for RNG" | ||
| seed::S | ||
| end | ||
|
|
||
| """ | ||
| $TYPEDSIGNATURES | ||
|
|
||
| Creates an [`Environment`](@ref) from an [`Instance`](@ref) of the maintenance benchmark. | ||
| """ | ||
| function Environment(instance::Instance; seed=0, rng::AbstractRNG=MersenneTwister(seed)) | ||
| degradation_state = starting_state(instance) | ||
|
sdelannoypavy marked this conversation as resolved.
Outdated
|
||
| env = Environment(; | ||
| instance, | ||
| step=1, | ||
| degradation_state, | ||
| rng=rng, | ||
| seed=seed, | ||
| ) | ||
| Utils.reset!(env; reset_rng=true) | ||
| return env | ||
| end | ||
|
|
||
| component_count(env::Environment) = component_count(env.instance) | ||
| maintenance_capacity(env::Environment) = maintenance_capacity(env.instance) | ||
| degradation_levels(env::Environment) = degradation_levels(env.instance) | ||
| degradation_probability(env::Environment) = degradation_probability(env.instance) | ||
| failure_cost(env::Environment) = failure_cost(env.instance) | ||
| maintenance_cost(env::Environment) = maintenance_cost(env.instance) | ||
| max_steps(env::Environment) = max_steps(env.instance) | ||
| starting_state(env::Environment) = starting_state(env.instance) | ||
|
|
||
|
|
||
| """ | ||
| $TYPEDSIGNATURES | ||
| Draw random degradations for all components. | ||
| """ | ||
|
|
||
|
sdelannoypavy marked this conversation as resolved.
Outdated
|
||
| function degrad!(env::Environment) | ||
| N = component_count(env) | ||
| n = degradation_levels(env) | ||
| p = degradation_probability(env) | ||
|
|
||
| for i in 1:N | ||
| if env.degradation_state[i] < n && rand() < p | ||
|
sdelannoypavy marked this conversation as resolved.
Outdated
|
||
| env.degradation_state[i] += 1 | ||
| end | ||
| end | ||
|
|
||
| return env.degradation_state | ||
| end | ||
|
|
||
| """ | ||
| $TYPEDSIGNATURES | ||
| Maintain components. | ||
| """ | ||
|
|
||
|
sdelannoypavy marked this conversation as resolved.
Outdated
|
||
| function maintain!(env::Environment, maintenance::BitVector) | ||
| N = component_count(env) | ||
|
|
||
| for i in 1:N | ||
| if maintenance[i] | ||
| env.degradation_state[i] = 1 | ||
| end | ||
| end | ||
|
|
||
| return env.degradation_state | ||
| end | ||
|
|
||
| """ | ||
| $TYPEDSIGNATURES | ||
|
|
||
| Compute maintenance cost. | ||
| """ | ||
| function maintenance_cost(env::Environment, maintenance::BitVector) | ||
| return maintenance_cost(env) * sum(maintenance) | ||
| end | ||
|
|
||
| """ | ||
| $TYPEDSIGNATURES | ||
|
|
||
| Compute degradation cost. | ||
| """ | ||
| function degradation_cost(env::Environment) | ||
| N = component_count(env) | ||
|
sdelannoypavy marked this conversation as resolved.
Outdated
|
||
| n = degradation_levels(env) | ||
| return failure_cost(env) * count(==(n), env.degradation_state) | ||
| end | ||
|
|
||
|
|
||
| """ | ||
| $TYPEDSIGNATURES | ||
|
|
||
| Outputs the seed of the environment. | ||
| """ | ||
| Utils.get_seed(env::Environment) = env.seed | ||
|
|
||
| """ | ||
| $TYPEDSIGNATURES | ||
|
|
||
| Resets the environment to the initial state: | ||
| - reset the rng if `reset_rng` is true | ||
| - reset the step to 1 | ||
| - reset the degradation state to the starting state | ||
| """ | ||
| function Utils.reset!(env::Environment; reset_rng=false, seed=env.seed) | ||
| reset_rng && Random.seed!(env.rng, seed) | ||
| env.step = 1 | ||
| env.degradation_state .= starting_state(env) | ||
| return nothing | ||
| end | ||
|
|
||
| """ | ||
| $TYPEDSIGNATURES | ||
|
|
||
| Checks if the environment has reached the maximum number of steps. | ||
| """ | ||
| function Utils.is_terminated(env::Environment) | ||
| return env.step > max_steps(env) | ||
| end | ||
|
|
||
| """ | ||
| $TYPEDSIGNATURES | ||
|
|
||
| Returns features, state tuple. | ||
| The features observed by the agent at current step are the degradation states of all components. | ||
| It is also the internal state, so we return the same thing twice. | ||
|
|
||
| """ | ||
| function Utils.observe(env::Environment) | ||
| state = env.degradation_state | ||
| return state, state | ||
| end | ||
|
|
||
| """ | ||
| $TYPEDSIGNATURES | ||
|
|
||
| Performs one step in the environment given a maintenance. | ||
| Draw random degradations for components that are not maintained. | ||
| """ | ||
| function Utils.step!(env::Environment, maintenance::BitVector) | ||
| @assert !Utils.is_terminated(env) "Environment is terminated, cannot act!" | ||
| cost = maintenance_cost(env, maintenance) + degradation_cost(env) | ||
| degrad!(env) | ||
| maintain!(env, maintenance) | ||
| env.step += 1 | ||
| return cost | ||
| end | ||
|
|
||
|
|
||
Oops, something went wrong.
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.