Skip to content

Commit

Permalink
Merge pull request #17 from MartinuzziFrancesco/fm/qol
Browse files Browse the repository at this point in the history
Quality of life improvements
  • Loading branch information
MartinuzziFrancesco authored Mar 6, 2024
2 parents c468df7 + b15f9e1 commit 6a26114
Show file tree
Hide file tree
Showing 19 changed files with 260 additions and 180 deletions.
9 changes: 9 additions & 0 deletions .JuliaFormatter.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
style = "blue"
whitespace_in_kwargs = false
always_use_return = true
margin = 92
indent = 4
format_docstrings = true
separate_kwargs_with_semicolon = true
always_for_in = true
annotate_untyped_fields_with_any = false
42 changes: 42 additions & 0 deletions .github/workflows/FormatCheck.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: format-check

on:
push:
branches:
- 'master'
- 'release-'
tags: '*'
pull_request:

jobs:
build:
runs-on: ${{ matrix.os }}
strategy:
matrix:
julia-version: [1]
julia-arch: [x86]
os: [ubuntu-latest]
steps:
- uses: julia-actions/setup-julia@latest
with:
version: ${{ matrix.julia-version }}

- uses: actions/checkout@v4
- name: Install JuliaFormatter and format
# This will use the latest version by default but you can set the version like so:
#
# julia -e 'using Pkg; Pkg.add(PackageSpec(name="JuliaFormatter", version="0.13.0"))'
run: |
julia -e 'using Pkg; Pkg.add(PackageSpec(name="JuliaFormatter"))'
julia -e 'using JuliaFormatter; format(".", verbose=true)'
- name: Format check
run: |
julia -e '
out = Cmd(`git diff --name-only`) |> read |> String
if out == ""
exit(0)
else
@error "Some files have not been formatted !!!"
write(stdout, out)
exit(1)
end'
8 changes: 4 additions & 4 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
name = "CellularAutomata"
uuid = "878138dc-5b27-11ea-1a71-cb95d38d6b29"
authors = ["Francesco Martinuzzi"]
version = "0.0.3"
version = "0.0.4"

[deps]


[compat]
julia = "1"

[extras]
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
SafeTestsets = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"

[targets]
test = ["Test", "SafeTestsets", "Random"]
test = ["Test", "SafeTestsets", "Random", "Aqua"]
12 changes: 7 additions & 5 deletions docs/make.jl
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
using Documenter, CellularAutomata
include("pages.jl")
using Documenter, CellularAutomata
include("pages.jl")

makedocs(;sitename="CellularAutomata.jl",
makedocs(;
sitename="CellularAutomata.jl",
modules=[CellularAutomata],
clean=true,
doctest=true,
linkcheck=true,
warnonly=[:missing_docs],
pages = pages)
pages=pages,
)

deploydocs(;
repo="github.com/MartinuzziFrancesco/CellularAutomata.jl.git", push_preview=true
)
)
6 changes: 3 additions & 3 deletions docs/pages.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ pages = [
"Examples" => [
"One dimensional CA" => "onedim/onedimensionca.md"
"Two dimensional CA" => "twodim/twodimensionca.md"
],
],
"API Documentation" => Any[
"General APIs" => "api/general.md"
"One Dimensional CA" => "api/onedim.md"
"Two Dimensial CA" => "api/twodim.md"
]
]
],
]
32 changes: 17 additions & 15 deletions src/CellularAutomata.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ struct CellularAutomaton{F,E} <: AbstractCA
evolution::E
end


"""
CellularAutomaton(rule::AbstractODRule, initial_conditions, generations)
CellularAutomaton(rule::AbstractTDRule, initial_conditions, generations)
Expand All @@ -21,35 +20,38 @@ with given initial conditions and number of generations. OD indicates one-diomen
indicates two-dimensiona cellular automata rules.
"""
function CellularAutomaton(rule::AbstractODRule, initial_conditions, generations)
evolution = zeros(
typeof(initial_conditions[2]), generations, length(initial_conditions)
)
evolution[1, :] = initial_conditions

evolution = zeros(typeof(initial_conditions[2]), generations, length(initial_conditions))
evolution[1,:] = initial_conditions

for i=2:generations
evolution[i,:] = rule(evolution[i-1,:])
for i in 2:generations
evolution[i, :] = rule(evolution[i - 1, :])
end

CellularAutomaton(generations, rule, evolution)

return CellularAutomaton(generations, rule, evolution)
end

function CellularAutomaton(rule::AbstractTDRule, initial_conditions, generations)

evolution = zeros(typeof(initial_conditions[2]), size(initial_conditions, 1), size(initial_conditions, 2), generations)
evolution = zeros(
typeof(initial_conditions[2]),
size(initial_conditions, 1),
size(initial_conditions, 2),
generations,
)
evolution[:, :, 1] = initial_conditions

for i=2:generations
evolution[:, :, i] = rule(evolution[:, :, i-1])
for i in 2:generations
evolution[:, :, i] = rule(evolution[:, :, i - 1])
end

CellularAutomaton(generations, rule, evolution)

return CellularAutomaton(generations, rule, evolution)
end

export CellularAutomaton

include("dca.jl")
export DCA, ECA
export DCA
include("cca.jl")
export CCA
include("tca.jl")
Expand Down
54 changes: 29 additions & 25 deletions src/cca.jl
Original file line number Diff line number Diff line change
@@ -1,51 +1,55 @@
abstract type AbstractCCARule <: AbstractODRule end
abstract type AbstractCCARule <: AbstractODRule end

struct CCA{T} <: AbstractCCARule
rule::T
radius::Int
end

"""
"""
TCA(code; radius=1)
Returns a ```CCA``` object given a specific code and radius.
Returns a `CCA` object given a specific code and radius.
"""
function CCA(rule; radius=1)
CCA(rule, radius)
return CCA(rule, radius)
end

function (cca::CCA)(starting_array)

nextgen = evolution(starting_array, cca.rule, cca.radius)
return nextgen = evolution(starting_array, cca.rule, cca.radius)
end

function c_state_reader(neighborhood, radius)

sum(neighborhood)/length(neighborhood)
return sum(neighborhood) / length(neighborhood)
end

function evolution(cell, rule, radius::Number)

neighborhood_size = radius*2+1
neighborhood_size = radius * 2 + 1
output = zeros(length(cell))
cell = vcat(cell[end-neighborhood_size÷2+1:end], cell, cell[1:neighborhood_size÷2])

for i=1:length(cell)-neighborhood_size+1
output[i] = modf(c_state_reader(cell[i:i+neighborhood_size-1], radius)+rule)[1]
cell = vcat(
cell[(end - neighborhood_size ÷ 2 + 1):end], cell, cell[1:(neighborhood_size ÷ 2)]
)

for i in 1:(length(cell) - neighborhood_size + 1)
output[i] = modf(
c_state_reader(cell[i:(i + neighborhood_size - 1)], radius) + rule
)[1]
end
output

return output
end

function evolution(cell, rule, radius::Tuple)

neighborhood_size = sum(radius)+1
neighborhood_size = sum(radius) + 1
output = zeros(length(cell))
cell = vcat(cell[end-neighborhood_size÷2+1:end], cell, cell[1:neighborhood_size÷2])

for i=1:length(cell)-neighborhood_size+1
output[i] = modf(c_state_reader(cell[i:i+neighborhood_size-1], radius)+rule)[1]
cell = vcat(
cell[(end - neighborhood_size ÷ 2 + 1):end], cell, cell[1:(neighborhood_size ÷ 2)]
)

for i in 1:(length(cell) - neighborhood_size + 1)
output[i] = modf(
c_state_reader(cell[i:(i + neighborhood_size - 1)], radius) + rule
)[1]
end
output
end

return output
end
71 changes: 32 additions & 39 deletions src/dca.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
abstract type AbstractDCARule <: AbstractODRule end
abstract type AbstractDCARule <: AbstractODRule end

struct DCA{B,R,T} <: AbstractDCARule
rule::B
Expand All @@ -7,68 +7,61 @@ struct DCA{B,R,T} <: AbstractDCARule
radius::T
end

"""
"""
DCA(rule; states=2, radius=1)
Returns a ```DCA``` object given a specific rule, number of states and radius. The ruleset for the rule is computed and
Returns a `DCA` object given a specific rule, number of states and radius. The ruleset for the rule is computed and
stored in the struct as well.
"""
function DCA(rule;
states=2,
radius=1)

function DCA(rule; states=2, radius=1)
ruleset = conversion(rule, states, radius)
DCA(rule, ruleset, states, radius)
return DCA(rule, ruleset, states, radius)
end

function (dca::DCA)(starting_array)

nextgen = evolution(starting_array, dca.ruleset, dca.states, dca.radius)
return nextgen = evolution(starting_array, dca.ruleset, dca.states, dca.radius)
end

function conversion(rule, states, radius::Int)

rule_len = states^(2*radius+1)
rule_bin = parse.(Int, split(string(rule, base=states), ""))
rule_bin = vcat(zeros(typeof(rule_bin[1]), rule_len-length(rule_bin)), rule_bin)
reverse!(rule_bin)
rule_len = states^(2 * radius + 1)
rule_bin = parse.(Int, split(string(rule; base=states), ""))
rule_bin = vcat(zeros(typeof(rule_bin[1]), rule_len - length(rule_bin)), rule_bin)
return reverse!(rule_bin)
end

function conversion(rule, states, radius::Tuple)

rule_len = states^(sum(radius)+1)
rule_bin = parse.(Int, split(string(rule, base=states), ""))
rule_bin = vcat(zeros(typeof(rule_bin[1]), rule_len-length(rule_bin)), rule_bin)
reverse!(rule_bin)
rule_len = states^(sum(radius) + 1)
rule_bin = parse.(Int, split(string(rule; base=states), ""))
rule_bin = vcat(zeros(typeof(rule_bin[1]), rule_len - length(rule_bin)), rule_bin)
return reverse!(rule_bin)
end

function state_reader(neighborhood, states)

parse(Int,join(convert(Array{Int},neighborhood)), base=states)+1 #ugly
return parse(Int, join(convert(Array{Int}, neighborhood)); base=states) + 1 #ugly
end

function evolution(cell, ruleset, states, radius::Int)

neighborhood_size = radius*2+1
neighborhood_size = radius * 2 + 1
output = zeros(length(cell))
cell = vcat(cell[end-neighborhood_size÷2+1:end], cell, cell[1:neighborhood_size÷2])

for i=1:length(cell)-neighborhood_size+1
output[i] = ruleset[state_reader(cell[i:i+neighborhood_size-1], states)]
cell = vcat(
cell[(end - neighborhood_size ÷ 2 + 1):end], cell, cell[1:(neighborhood_size ÷ 2)]
)

for i in 1:(length(cell) - neighborhood_size + 1)
output[i] = ruleset[state_reader(cell[i:(i + neighborhood_size - 1)], states)]
end
output

return output
end

function evolution(cell, ruleset, states, radius::Tuple)

neighborhood_size = sum(radius)+1
neighborhood_size = sum(radius) + 1
output = zeros(length(cell))#da qui in poi da modificare
cell = vcat(cell[end-radius[1]+1:end], cell, cell[1:radius[2]])
for i=1:length(cell)-neighborhood_size+1
output[i] = ruleset[state_reader(cell[i:i+neighborhood_size-1], states)]
cell = vcat(cell[(end - radius[1] + 1):end], cell, cell[1:radius[2]])

for i in 1:(length(cell) - neighborhood_size + 1)
output[i] = ruleset[state_reader(cell[i:(i + neighborhood_size - 1)], states)]
end
output
end

return output
end
Loading

2 comments on commit 6a26114

@MartinuzziFrancesco
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/102370

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.0.4 -m "<description of version>" 6a2611455b272c1771867862aed8b6feedcb0003
git push origin v0.0.4

Please sign in to comment.