Skip to content

Small world (Watts-Strogatz) initializer #269

Closed

Description

@MartinuzziFrancesco

Description

Given that the standard initialization of the reservoir follows Erdős–Rényi it makes sense to try different topologies. In Kawai2019 the authors provide a very detailed analysis of the small world topology, and it could be used as base for the implementation

Possible implementation

Of course an implementation is already available at Graphs.jl but that seems a big dependency to call for just this one function.

using Random

# watts_strogatz or small_world?
function small_world(rng::AbstractRNG, ::Type{T}, dims::Integer...;
        num_neighbors::Integer, rewiring_probability::Float64, radius::AbstractFloat=1.0) where T
    num_nodes = if length(dims) == 1
        dims[1]
    elseif length(dims) == 2
        if dims[1] != dims[2]
            error("The provided dimensions must form a square matrix.")
        else
            dims[1]
        end
    else
        error("dims must be one integer or two equal integers for a square matrix.")
    end
    if num_neighbors % 2 != 0 || num_neighbors >= num_nodes
        error("num_neighbors must be an even number and less than the number of nodes.")
    end
    reservoir_matrix = zeros(T, num_nodes, num_nodes)
    half_neighbors = div(num_neighbors, 2)
    for current_node in 1:num_nodes
        for offset in 1:half_neighbors
            neighbor_node = mod1(current_node + offset, num_nodes)
            reservoir_matrix[current_node, neighbor_node] = one(T)
            reservoir_matrix[neighbor_node, current_node] = one(T)
        end
    end
    for current_node in 1:num_nodes
        for offset in 1:half_neighbors
            neighbor_node = mod1(current_node + offset, num_nodes)
            if rand(rng) < rewiring_probability
                potential_new_neighbors = [candidate for candidate in 1:num_nodes if candidate != current_node && reservoir_matrix[current_node, candidate] == 0]
                if !isempty(potential_new_neighbors)
                    new_neighbor_node = rand(rng, potential_new_neighbors)
                    reservoir_matrix[current_node, neighbor_node] = zero(T)
                    reservoir_matrix[neighbor_node, current_node] = zero(T)
                    reservoir_matrix[current_node, new_neighbor_node] = one(T)
                    reservoir_matrix[new_neighbor_node, current_node] = one(T)
                end
            end
        end
    end
    scale_radius!(reservoir_matrix, radius)
    return reservoir_matrix
end

Activity

  1. aviralgarg05 commented on Nov 11, 2026

    @aviralgarg05
    Contributor

    Hi, I’d like to work on this if it’s still available. I checked the current initializer API and the topology work added since this issue was opened.

    My plan is to add a small_world initializer following the existing RNG, element-type, dimensions, spectral-radius, and sparse-output conventions. I’d also add validation for square dimensions, the neighbour count, and rewiring probability, along with seeded tests covering determinism, symmetry, edge count, the zero/full rewiring cases, element types, radius scaling, and invalid inputs. I’ll include the export and user-facing API documentation as required by the contribution guide.

    Before I start, should the initial connections keep the binary weights from the issue sketch, or should they be sampled like the newer topology initializers? Also, would you like return_sparse supported in the first version?

  2. MartinuzziFrancesco commented on Nov 11, 2026

    @MartinuzziFrancesco
    CollaboratorAuthor

    I think it's better if they are sampled already, it should also be easier to link in the existing infrastructure. The return_sparse can be added after the initial proof of concept if you'd like, but it will need to be in the initializer for the PR!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    good first issueGood for newcomersinitsinitializers for internal matrices

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions