From 89cc431fd2e31d6f4cebb2e231c1fc6c9e51193c Mon Sep 17 00:00:00 2001 From: Mo8it Date: Tue, 8 Feb 2022 21:20:18 +0100 Subject: [PATCH] Fix typos --- .gitignore | 5 ++- .../radial_distribution_function.jl | 12 ++--- graphics/center_of_mass.jl | 2 +- graphics/periodic_boundary_conditions.jl | 16 +++---- src/RL/Envs/LocalCOMEnv.jl | 26 +++++------ .../LocalCOMWithAdditionalShapeRewardEnv.jl | 26 +++++------ .../LocalCOMWithAdditionalShapeRewardEnv2.jl | 26 +++++------ ...tNeighbourEnv.jl => NearestNeighborEnv.jl} | 44 +++++++++---------- src/RL/RL.jl | 8 ++-- src/ReCo.jl | 2 +- src/Shape.jl | 2 +- src/run.jl | 2 +- 12 files changed, 86 insertions(+), 85 deletions(-) rename src/RL/Envs/{NearestNeighbourEnv.jl => NearestNeighborEnv.jl} (69%) diff --git a/.gitignore b/.gitignore index 3e5431f..4c165c4 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,7 @@ exports *.ipynb* # Profiling -*.mem \ No newline at end of file +*.mem + +# VSCodium +.vscode \ No newline at end of file diff --git a/analysis/radial_distribution_function/radial_distribution_function.jl b/analysis/radial_distribution_function/radial_distribution_function.jl index beadcbd..feea156 100644 --- a/analysis/radial_distribution_function/radial_distribution_function.jl +++ b/analysis/radial_distribution_function/radial_distribution_function.jl @@ -72,22 +72,22 @@ function radial_distribution(; bundle_paths = ReCo.sorted_bundle_paths(sim_dir; rev=true) - snapshot_conunter = 0 + snapshot_counter = 0 break_bundle_path_loop = false for bundle_path in bundle_paths bundle::ReCo.Bundle = JLD2.load_object(bundle_path) for snapshot_ind in (bundle.n_snapshots):-1:1 - snapshot_conunter += 1 + snapshot_counter += 1 @simd for particle_ind in 1:n_particles - cs[particle_ind, snapshot_conunter] = bundle.c[ + cs[particle_ind, snapshot_counter] = bundle.c[ particle_ind, snapshot_ind ] end - if snapshot_conunter == n_last_snapshots + if snapshot_counter == n_last_snapshots break_bundle_path_loop = true break end @@ -98,8 +98,8 @@ function radial_distribution(; end end - if snapshot_conunter != n_last_snapshots - error("snapshot_conunter != n_last_snapshots") + if snapshot_counter != n_last_snapshots + error("snapshot_counter != n_last_snapshots") end g = zeros(Float64, n_radii) diff --git a/graphics/center_of_mass.jl b/graphics/center_of_mass.jl index 4c93626..3475cf0 100644 --- a/graphics/center_of_mass.jl +++ b/graphics/center_of_mass.jl @@ -82,7 +82,7 @@ function gen_COM_graphics() ## - Drawing(box_length, box_height, "$graphics_export_dir/circular_projectiong.pdf") + Drawing(box_length, box_height, "$graphics_export_dir/circular_projection.pdf") origin() fontsize(5) diff --git a/graphics/periodic_boundary_conditions.jl b/graphics/periodic_boundary_conditions.jl index 2a0e764..34d489c 100644 --- a/graphics/periodic_boundary_conditions.jl +++ b/graphics/periodic_boundary_conditions.jl @@ -7,20 +7,20 @@ using ReCo: ReCo function gen_periodic_boundary_conditions_graphics() Random.seed!(23) - drwaing_box_length = 300 + drawing_box_length = 300 graphics_export_dir = "exports/graphics" mkpath(graphics_export_dir) Drawing( - drwaing_box_length, - drwaing_box_length, + drawing_box_length, + drawing_box_length, "$graphics_export_dir/periodic_boundary_conditions.pdf", ) origin() particle_radius = 10 - box_len = drwaing_box_length / 3 + box_len = drawing_box_length / 3 half_box_len = box_len / 2 N_in_one_box = 5 @@ -79,13 +79,13 @@ function gen_periodic_boundary_conditions_graphics() setline(1.0) for line_factor in (-3, -1, 1, 3) line( - Point(line_factor * drwaing_box_length / 6, drwaing_box_length / 2), - Point(line_factor * drwaing_box_length / 6, -drwaing_box_length / 2), + Point(line_factor * drawing_box_length / 6, drawing_box_length / 2), + Point(line_factor * drawing_box_length / 6, -drawing_box_length / 2), :stroke, ) line( - Point(drwaing_box_length / 2, line_factor * drwaing_box_length / 6), - Point(-drwaing_box_length / 2, line_factor * drwaing_box_length / 6), + Point(drawing_box_length / 2, line_factor * drawing_box_length / 6), + Point(-drawing_box_length / 2, line_factor * drawing_box_length / 6), :stroke, ) end diff --git a/src/RL/Envs/LocalCOMEnv.jl b/src/RL/Envs/LocalCOMEnv.jl index f45adfa..fd13813 100644 --- a/src/RL/Envs/LocalCOMEnv.jl +++ b/src/RL/Envs/LocalCOMEnv.jl @@ -39,8 +39,8 @@ end mutable struct LocalCOMEnvHelper <: EnvHelper shared::EnvHelperSharedProps - vec_to_neighbour_sums::Vector{SVector{2,Float64}} - n_neighbours::Vector{Int64} + vec_to_neighbor_sums::Vector{SVector{2,Float64}} + n_neighbors::Vector{Int64} distances_to_local_center_of_mass::Vector{Float64} max_distance_to_local_center_of_mass::Float64 @@ -69,8 +69,8 @@ end function pre_integration_hook!(env_helper::LocalCOMEnvHelper) @simd for id in 1:(env_helper.shared.n_particles) - env_helper.vec_to_neighbour_sums[id] = SVector(0.0, 0.0) - env_helper.n_neighbours[id] = 0 + env_helper.vec_to_neighbor_sums[id] = SVector(0.0, 0.0) + env_helper.n_neighbors[id] = 0 end return nothing @@ -83,11 +83,11 @@ function state_update_helper_hook!( r⃗₁₂::SVector{2,Float64}, distance²::Float64, ) - env_helper.vec_to_neighbour_sums[id1] += r⃗₁₂ - env_helper.vec_to_neighbour_sums[id2] -= r⃗₁₂ + env_helper.vec_to_neighbor_sums[id1] += r⃗₁₂ + env_helper.vec_to_neighbor_sums[id2] -= r⃗₁₂ - env_helper.n_neighbours[id1] += 1 - env_helper.n_neighbours[id2] += 1 + env_helper.n_neighbors[id1] += 1 + env_helper.n_neighbors[id2] += 1 return nothing end @@ -98,13 +98,13 @@ function state_update_hook!(env_helper::LocalCOMEnvHelper, particles::Vector{ReC env = env_helper.shared.env for particle_id in 1:n_particles - n_neighbours = env_helper.n_neighbours[particle_id] + n_neighbors = env_helper.n_neighbors[particle_id] - if n_neighbours == 0 + if n_neighbors == 0 state_id = env.shared.n_states else vec_to_local_center_of_mass = - env_helper.vec_to_neighbour_sums[particle_id] / n_neighbours + env_helper.vec_to_neighbor_sums[particle_id] / n_neighbors distance = ReCo.norm2d(vec_to_local_center_of_mass) env_helper.distances_to_local_center_of_mass[particle_id] = distance distance_state_ind = find_state_ind(distance, env.distance_state_space) @@ -127,9 +127,9 @@ end function update_reward!( env::LocalCOMEnv, env_helper::LocalCOMEnvHelper, particle::ReCo.Particle ) - n_neighbours = env_helper.n_neighbours[particle.id] + n_neighbors = env_helper.n_neighbors[particle.id] - if n_neighbours == 0 + if n_neighbors == 0 env.shared.reward = 0.0 else reward = minimizing_reward( diff --git a/src/RL/Envs/LocalCOMWithAdditionalShapeRewardEnv.jl b/src/RL/Envs/LocalCOMWithAdditionalShapeRewardEnv.jl index b450095..e4fc279 100644 --- a/src/RL/Envs/LocalCOMWithAdditionalShapeRewardEnv.jl +++ b/src/RL/Envs/LocalCOMWithAdditionalShapeRewardEnv.jl @@ -39,8 +39,8 @@ end mutable struct LocalCOMWithAdditionalShapeRewardEnvHelper <: EnvHelper shared::EnvHelperSharedProps - vec_to_neighbour_sums::Vector{SVector{2,Float64}} - n_neighbours::Vector{Int64} + vec_to_neighbor_sums::Vector{SVector{2,Float64}} + n_neighbors::Vector{Int64} distances_to_local_center_of_mass::Vector{Float64} max_distance_to_local_center_of_mass::Float64 @@ -89,8 +89,8 @@ end function pre_integration_hook!(env_helper::LocalCOMWithAdditionalShapeRewardEnvHelper) @simd for id in 1:(env_helper.shared.n_particles) - env_helper.vec_to_neighbour_sums[id] = SVector(0.0, 0.0) - env_helper.n_neighbours[id] = 0 + env_helper.vec_to_neighbor_sums[id] = SVector(0.0, 0.0) + env_helper.n_neighbors[id] = 0 end return nothing @@ -103,11 +103,11 @@ function state_update_helper_hook!( r⃗₁₂::SVector{2,Float64}, distance²::Float64, ) - env_helper.vec_to_neighbour_sums[id1] += r⃗₁₂ - env_helper.vec_to_neighbour_sums[id2] -= r⃗₁₂ + env_helper.vec_to_neighbor_sums[id1] += r⃗₁₂ + env_helper.vec_to_neighbor_sums[id2] -= r⃗₁₂ - env_helper.n_neighbours[id1] += 1 - env_helper.n_neighbours[id2] += 1 + env_helper.n_neighbors[id1] += 1 + env_helper.n_neighbors[id2] += 1 return nothing end @@ -122,16 +122,16 @@ function state_update_hook!( distance_to_local_center_of_mass_sum = 0.0 for particle_id in 1:n_particles - n_neighbours = env_helper.n_neighbours[particle_id] + n_neighbors = env_helper.n_neighbors[particle_id] - if n_neighbours == 0 + if n_neighbors == 0 state_id = env.shared.n_states distance_to_local_center_of_mass_sum += env_helper.max_distance_to_local_center_of_mass else vec_to_local_center_of_mass = - env_helper.vec_to_neighbour_sums[particle_id] / n_neighbours + env_helper.vec_to_neighbor_sums[particle_id] / n_neighbors distance = ReCo.norm2d(vec_to_local_center_of_mass) env_helper.distances_to_local_center_of_mass[particle_id] = distance distance_to_local_center_of_mass_sum += distance @@ -172,9 +172,9 @@ function update_reward!( env_helper::LocalCOMWithAdditionalShapeRewardEnvHelper, particle::ReCo.Particle, ) - n_neighbours = env_helper.n_neighbours[particle.id] + n_neighbors = env_helper.n_neighbors[particle.id] - if n_neighbours == 0 + if n_neighbors == 0 env.shared.reward = 0.0 else reward = minimizing_reward( diff --git a/src/RL/Envs/LocalCOMWithAdditionalShapeRewardEnv2.jl b/src/RL/Envs/LocalCOMWithAdditionalShapeRewardEnv2.jl index 4372bce..dc9b429 100644 --- a/src/RL/Envs/LocalCOMWithAdditionalShapeRewardEnv2.jl +++ b/src/RL/Envs/LocalCOMWithAdditionalShapeRewardEnv2.jl @@ -39,8 +39,8 @@ end mutable struct LocalCOMWithAdditionalShapeRewardEnv2Helper <: EnvHelper shared::EnvHelperSharedProps - vec_to_neighbour_sums::Vector{SVector{2,Float64}} - n_neighbours::Vector{Int64} + vec_to_neighbor_sums::Vector{SVector{2,Float64}} + n_neighbors::Vector{Int64} distances_to_local_center_of_mass::Vector{Float64} max_distance_to_local_center_of_mass::Float64 @@ -86,8 +86,8 @@ end function pre_integration_hook!(env_helper::LocalCOMWithAdditionalShapeRewardEnv2Helper) @simd for id in 1:(env_helper.shared.n_particles) - env_helper.vec_to_neighbour_sums[id] = SVector(0.0, 0.0) - env_helper.n_neighbours[id] = 0 + env_helper.vec_to_neighbor_sums[id] = SVector(0.0, 0.0) + env_helper.n_neighbors[id] = 0 end return nothing @@ -100,11 +100,11 @@ function state_update_helper_hook!( r⃗₁₂::SVector{2,Float64}, distance²::Float64, ) - env_helper.vec_to_neighbour_sums[id1] += r⃗₁₂ - env_helper.vec_to_neighbour_sums[id2] -= r⃗₁₂ + env_helper.vec_to_neighbor_sums[id1] += r⃗₁₂ + env_helper.vec_to_neighbor_sums[id2] -= r⃗₁₂ - env_helper.n_neighbours[id1] += 1 - env_helper.n_neighbours[id2] += 1 + env_helper.n_neighbors[id1] += 1 + env_helper.n_neighbors[id2] += 1 return nothing end @@ -120,16 +120,16 @@ function state_update_hook!( distance_to_local_center_of_mass_sum = 0.0 for particle_id in 1:n_particles - n_neighbours = env_helper.n_neighbours[particle_id] + n_neighbors = env_helper.n_neighbors[particle_id] - if n_neighbours == 0 + if n_neighbors == 0 state_id = env.shared.n_states distance_to_local_center_of_mass_sum += env_helper.max_distance_to_local_center_of_mass else vec_to_local_center_of_mass = - env_helper.vec_to_neighbour_sums[particle_id] / n_neighbours + env_helper.vec_to_neighbor_sums[particle_id] / n_neighbors distance = ReCo.norm2d(vec_to_local_center_of_mass) env_helper.distances_to_local_center_of_mass[particle_id] = distance distance_to_local_center_of_mass_sum += distance @@ -161,9 +161,9 @@ function update_reward!( env_helper::LocalCOMWithAdditionalShapeRewardEnv2Helper, particle::ReCo.Particle, ) - n_neighbours = env_helper.n_neighbours[particle.id] + n_neighbors = env_helper.n_neighbors[particle.id] - if n_neighbours == 0 + if n_neighbors == 0 env.shared.reward = 0.0 else reward = minimizing_reward( diff --git a/src/RL/Envs/NearestNeighbourEnv.jl b/src/RL/Envs/NearestNeighborEnv.jl similarity index 69% rename from src/RL/Envs/NearestNeighbourEnv.jl rename to src/RL/Envs/NearestNeighborEnv.jl index 3a931af..c48cfc6 100644 --- a/src/RL/Envs/NearestNeighbourEnv.jl +++ b/src/RL/Envs/NearestNeighborEnv.jl @@ -1,12 +1,12 @@ using ..ReCo: ReCo -struct NearestNeighbourEnv <: Env +struct NearestNeighborEnv <: Env shared::EnvSharedProps distance_state_space::Vector{Interval} direction_angle_state_space::Vector{Interval} - function NearestNeighbourEnv(; + function NearestNeighborEnv(; n_distance_states::Int64=3, n_direction_angle_states::Int64=3, args ) @assert n_distance_states > 1 @@ -36,11 +36,11 @@ struct NearestNeighbourEnv <: Env end end -mutable struct NearestNeighbourEnvHelper <: EnvHelper +mutable struct NearestNeighborEnvHelper <: EnvHelper shared::EnvHelperSharedProps - vecs_to_neighbour::Vector{SVector{2,Float64}} - sq_distances_to_neighbour::Vector{Float64} + vecs_to_neighbor::Vector{SVector{2,Float64}} + sq_distances_to_neighbor::Vector{Float64} current_κ::Float64 goal_κ::Float64 @@ -48,7 +48,7 @@ mutable struct NearestNeighbourEnvHelper <: EnvHelper half_box_len::Float64 - function NearestNeighbourEnvHelper(shared::EnvHelperSharedProps, half_box_len::Float64) + function NearestNeighborEnvHelper(shared::EnvHelperSharedProps, half_box_len::Float64) goal_κ = 0.4 max_distance_to_goal_κ = max(1 - goal_κ, goal_κ) @@ -64,49 +64,47 @@ mutable struct NearestNeighbourEnvHelper <: EnvHelper end end -function gen_env_helper( - ::NearestNeighbourEnv, env_helper_shared::EnvHelperSharedProps; args -) - return NearestNeighbourEnvHelper(env_helper_shared, args.half_box_len) +function gen_env_helper(::NearestNeighborEnv, env_helper_shared::EnvHelperSharedProps; args) + return NearestNeighborEnvHelper(env_helper_shared, args.half_box_len) end -function pre_integration_hook!(env_helper::NearestNeighbourEnvHelper) +function pre_integration_hook!(env_helper::NearestNeighborEnvHelper) @simd for particle_id in 1:(env_helper.shared.n_particles) - env_helper.sq_distances_to_neighbour[particle_id] = Inf64 + env_helper.sq_distances_to_neighbor[particle_id] = Inf64 end return nothing end function state_update_helper_hook!( - env_helper::NearestNeighbourEnvHelper, + env_helper::NearestNeighborEnvHelper, id1::Int64, id2::Int64, r⃗₁₂::SVector{2,Float64}, distance²::Float64, ) - if distance² < env_helper.sq_distances_to_neighbour[id1] - env_helper.vecs_to_neighbour[id1] = r⃗₁₂ - env_helper.sq_distances_to_neighbour[id1] = distance² + if distance² < env_helper.sq_distances_to_neighbor[id1] + env_helper.vecs_to_neighbor[id1] = r⃗₁₂ + env_helper.sq_distances_to_neighbor[id1] = distance² end - if distance² < env_helper.sq_distances_to_neighbour[id2] - env_helper.vecs_to_neighbour[id2] = -r⃗₁₂ - env_helper.sq_distances_to_neighbour[id2] = distance² + if distance² < env_helper.sq_distances_to_neighbor[id2] + env_helper.vecs_to_neighbor[id2] = -r⃗₁₂ + env_helper.sq_distances_to_neighbor[id2] = distance² end return nothing end function state_update_hook!( - env_helper::NearestNeighbourEnvHelper, particles::Vector{ReCo.Particle} + env_helper::NearestNeighborEnvHelper, particles::Vector{ReCo.Particle} ) n_particles = env_helper.shared.n_particles env = env_helper.shared.env for particle_id in 1:n_particles - sq_distance = env_helper.sq_distances_to_neighbour[particle_id] + sq_distance = env_helper.sq_distances_to_neighbor[particle_id] if sq_distance == Inf64 state_id = env.shared.n_states @@ -116,7 +114,7 @@ function state_update_hook!( si, co = sincos(particles[particle_id].φ) direction_angle = ReCo.angle2( - SVector(co, si), env_helper.vecs_to_neighbour[particle_id] + SVector(co, si), env_helper.vecs_to_neighbor[particle_id] ) direction_state_ind = find_state_ind( direction_angle, env.direction_angle_state_space @@ -136,7 +134,7 @@ function state_update_hook!( end function update_reward!( - env::NearestNeighbourEnv, env_helper::NearestNeighbourEnvHelper, particle::ReCo.Particle + env::NearestNeighborEnv, env_helper::NearestNeighborEnvHelper, particle::ReCo.Particle ) reward = minimizing_reward( abs(env_helper.current_κ - env_helper.goal_κ), env_helper.max_distance_to_goal_κ diff --git a/src/RL/RL.jl b/src/RL/RL.jl index 5490488..64bd451 100644 --- a/src/RL/RL.jl +++ b/src/RL/RL.jl @@ -4,7 +4,7 @@ export run_rl, LocalCOMWithAdditionalShapeRewardEnv, LocalCOMWithAdditionalShapeRewardEnv2, OriginEnv, - NearestNeighbourEnv, + NearestNeighborEnv, LocalCOMEnv, OriginCompassEnv, COMCompassEnv @@ -77,7 +77,7 @@ function gen_agent( end """ - run_rl(EnvType; ) + run_rl(EnvType::Type{<:Env}; ) Run a reinforcement learning process and return the tuple (`env_helper`, `rl_dir`). `env_helper` is the environment helper and `rl_dir` is the path of the process directory relative to the directory `ReCo.jl`. @@ -178,7 +178,7 @@ function run_rl( # Reset reset!(env) - # Pre espisode + # Pre episode hook(PRE_EPISODE_STAGE, agent, env) agent(PRE_EPISODE_STAGE, env) @@ -234,7 +234,7 @@ end include("Envs/LocalCOMWithAdditionalShapeRewardEnv.jl") include("Envs/LocalCOMWithAdditionalShapeRewardEnv2.jl") include("Envs/OriginEnv.jl") -include("Envs/NearestNeighbourEnv.jl") +include("Envs/NearestNeighborEnv.jl") include("Envs/LocalCOMEnv.jl") include("Envs/OriginCompassEnv.jl") include("Envs/COMCompassEnv.jl") diff --git a/src/ReCo.jl b/src/ReCo.jl index 64a2f8c..cbe4ed5 100644 --- a/src/ReCo.jl +++ b/src/ReCo.jl @@ -9,7 +9,7 @@ export init_sim, LocalCOMWithAdditionalShapeRewardEnv, LocalCOMWithAdditionalShapeRewardEnv2, OriginEnv, - NearestNeighbourEnv, + NearestNeighborEnv, LocalCOMEnv, OriginCompassEnv, COMCompassEnv diff --git a/src/Shape.jl b/src/Shape.jl index 912252f..dc98111 100644 --- a/src/Shape.jl +++ b/src/Shape.jl @@ -28,7 +28,7 @@ function center_of_mass_from_proj_sums( digits = 5 # No need for 1/n_particles with atan - # If proj is (0, 0) then COM is 0 or L or -L. Here, 0 is choosen with θ = π + # If proj is (0, 0) then COM is 0 or L or -L. Here, 0 is chosen with θ = π if round(x_proj_sum[1]; digits=digits) == round(x_proj_sum[2]; digits=digits) == 0 x_θ = π else diff --git a/src/run.jl b/src/run.jl index 8456582..8ded43d 100644 --- a/src/run.jl +++ b/src/run.jl @@ -25,7 +25,7 @@ Some of the last snapshots might be lost if the simulations is stopped (see the - `duration::Float64`: Duration of the simulation. - `snapshot_at::Float64=$DEFAULT_SNAPSHOT_AT`: Snapshot time interval. - `seed::Int64=$DEFAULT_SEED`: Random number generator seed. -- `n_bundle_snapshots::Int64=$DEFAULT_N_BUNDLE_SNAPSHOTS`: Number of snapshots in a bundle. This number is relevant for long simulations that can be stopped while running. A simulation can be continued from the last bundle of snapshots. If the number of snapshots in a bundle is too high and the simulation is stopped, many of the last snapshots can be lost. A low number results in high IO since snapshots are then bundled and stored more often. For example, setting this number to 1 results in saving every snapshot immidiately without bundeling it with other snapshots which would be more efficient. Setting the number to 1000 could mean loosing 999 snapshots in the worst case if the simulation is stopped before having 1000 snapshots to bundle and save. +- `n_bundle_snapshots::Int64=$DEFAULT_N_BUNDLE_SNAPSHOTS`: Number of snapshots in a bundle. This number is relevant for long simulations that can be stopped while running. A simulation can be continued from the last bundle of snapshots. If the number of snapshots in a bundle is too high and the simulation is stopped, many of the last snapshots can be lost. A low number results in high IO since snapshots are then bundled and stored more often. For example, setting this number to 1 results in saving every snapshot immediately without bundeling it with other snapshots which would be more efficient. Setting the number to 1000 could mean loosing 999 snapshots in the worst case if the simulation is stopped before having 1000 snapshots to bundle and save. - `env_helper::Union{RL.EnvHelper,Nothing}=nothing`: Environment helper. It should be left as the default `nothing` unless this method is used internally for reinforcement learning. - `show_progress::Bool=$DEFAULT_SHOW_PROGRESS`: Show simulation progress bar. """