Add docstrings
This commit is contained in:
parent
bd97cffcc1
commit
d3513c008a
2 changed files with 16 additions and 0 deletions
|
@ -5,6 +5,7 @@ version = "0.1.0"
|
||||||
|
|
||||||
[deps]
|
[deps]
|
||||||
CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"
|
CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"
|
||||||
|
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
|
||||||
Logging = "56ddb016-857b-54e1-b83d-db4d58db5568"
|
Logging = "56ddb016-857b-54e1-b83d-db4d58db5568"
|
||||||
Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"
|
Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"
|
||||||
|
|
||||||
|
|
|
@ -1,11 +1,18 @@
|
||||||
module GardenOptim
|
module GardenOptim
|
||||||
|
|
||||||
|
using DocStringExtensions
|
||||||
using Logging
|
using Logging
|
||||||
using CSV
|
using CSV
|
||||||
using Tables
|
using Tables
|
||||||
|
|
||||||
export loadplants, loadgarden, loadcosts, update!, randomgardenevolution!, outputgarden
|
export loadplants, loadgarden, loadcosts, update!, randomgardenevolution!, outputgarden
|
||||||
|
|
||||||
|
@template (FUNCTIONS, METHODS, MACROS) =
|
||||||
|
"""
|
||||||
|
$(TYPEDSIGNATURES)
|
||||||
|
$(DOCSTRING)
|
||||||
|
"""
|
||||||
|
|
||||||
function loadplants()::Vector{String}
|
function loadplants()::Vector{String}
|
||||||
plants = readlines("data/plants.txt")
|
plants = readlines("data/plants.txt")
|
||||||
@info "loaded $(length(plants)) plants"
|
@info "loaded $(length(plants)) plants"
|
||||||
|
@ -32,6 +39,7 @@ function loadcosts()::Matrix{Float64}
|
||||||
costs = Float64.(max.(costs, permutedims(costs)))
|
costs = Float64.(max.(costs, permutedims(costs)))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
"Return a random index to be filled from the garden mask."
|
||||||
function randomindex(mask::Matrix{Bool})::Int
|
function randomindex(mask::Matrix{Bool})::Int
|
||||||
while true
|
while true
|
||||||
i = rand(1:length(mask))
|
i = rand(1:length(mask))
|
||||||
|
@ -41,6 +49,7 @@ function randomindex(mask::Matrix{Bool})::Int
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
"Swap to the elements corresponding to the two provided indices."
|
||||||
function swap!(garden::Matrix{Int}, i::Int, j::Int)
|
function swap!(garden::Matrix{Int}, i::Int, j::Int)
|
||||||
t = garden[i]
|
t = garden[i]
|
||||||
garden[i] = garden[j]
|
garden[i] = garden[j]
|
||||||
|
@ -48,6 +57,7 @@ function swap!(garden::Matrix{Int}, i::Int, j::Int)
|
||||||
garden
|
garden
|
||||||
end
|
end
|
||||||
|
|
||||||
|
"Return the neighbours to be filled of the cell at the given index."
|
||||||
function neighbours(garden::Matrix{Int}, idx::Int)::Vector{Int}
|
function neighbours(garden::Matrix{Int}, idx::Int)::Vector{Int}
|
||||||
m, n = size(garden)
|
m, n = size(garden)
|
||||||
j, i = divrem(idx - 1, m)
|
j, i = divrem(idx - 1, m)
|
||||||
|
@ -61,6 +71,7 @@ function neighbours(garden::Matrix{Int}, idx::Int)::Vector{Int}
|
||||||
]
|
]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
"Compute the cost difference when swapping the two provided indices."
|
||||||
function deltacost(garden::Matrix{Int}, costs::Matrix{Float64}, i::Int, j::Int)::Float64
|
function deltacost(garden::Matrix{Int}, costs::Matrix{Float64}, i::Int, j::Int)::Float64
|
||||||
cost = 0
|
cost = 0
|
||||||
for k in neighbours(garden, i)
|
for k in neighbours(garden, i)
|
||||||
|
@ -72,6 +83,7 @@ function deltacost(garden::Matrix{Int}, costs::Matrix{Float64}, i::Int, j::Int):
|
||||||
cost
|
cost
|
||||||
end
|
end
|
||||||
|
|
||||||
|
"Update the garden using Metropolis-Hastings, using the inverse temperature beta."
|
||||||
function update!(
|
function update!(
|
||||||
garden::Matrix{Int},
|
garden::Matrix{Int},
|
||||||
mask::Matrix{Bool},
|
mask::Matrix{Bool},
|
||||||
|
@ -93,11 +105,13 @@ function update!(
|
||||||
garden
|
garden
|
||||||
end
|
end
|
||||||
|
|
||||||
|
"Fill the garden randomly with a predefined number of plants."
|
||||||
function randomfillgarden!(garden::Matrix{Int}, mask::Matrix{Bool}, plantcount::Int)
|
function randomfillgarden!(garden::Matrix{Int}, mask::Matrix{Bool}, plantcount::Int)
|
||||||
garden[mask] = rand(1:plantcount, sum(mask))
|
garden[mask] = rand(1:plantcount, sum(mask))
|
||||||
garden
|
garden
|
||||||
end
|
end
|
||||||
|
|
||||||
|
"Update the garden for a given number of steps, starting from a random initialisation."
|
||||||
function randomgardenevolution!(
|
function randomgardenevolution!(
|
||||||
garden::Matrix{Int},
|
garden::Matrix{Int},
|
||||||
mask::Matrix{Bool},
|
mask::Matrix{Bool},
|
||||||
|
@ -112,6 +126,7 @@ function randomgardenevolution!(
|
||||||
garden
|
garden
|
||||||
end
|
end
|
||||||
|
|
||||||
|
"Save the garden to a CSV file."
|
||||||
function outputgarden(garden::Matrix{Int}, plants::Vector{String})
|
function outputgarden(garden::Matrix{Int}, plants::Vector{String})
|
||||||
output = vcat([""], plants)[garden .+ 1]
|
output = vcat([""], plants)[garden .+ 1]
|
||||||
CSV.write("output.csv", Tables.table(output), writeheader=false)
|
CSV.write("output.csv", Tables.table(output), writeheader=false)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue