How to resolve the algorithm Percolation/Mean cluster density step by step in the EchoLisp programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Percolation/Mean cluster density step by step in the EchoLisp programming language

Table of Contents

Problem Statement

Let

c

{\displaystyle c}

be a 2D boolean square matrix of

n × n

{\displaystyle n\times n}

values of either 1 or 0 where the probability of any value being 1 is

p

{\displaystyle p}

, (and of 0 is therefore

1 − p

{\displaystyle 1-p}

). We define a cluster of 1's as being a group of 1's connected vertically or horizontally (i.e., using the Von Neumann neighborhood rule) and bounded by either

0

{\displaystyle 0}

or by the limits of the matrix. Let the number of such clusters in such a randomly constructed matrix be

C

n

{\displaystyle C_{n}}

. Percolation theory states that

K ( p )

{\displaystyle K(p)}

(the mean cluster density) will satisfy

K ( p )

C

n

/

n

2

{\displaystyle K(p)=C_{n}/n^{2}}

as

n

{\displaystyle n}

tends to infinity. For

p

0.5

{\displaystyle p=0.5}

,

K ( p )

{\displaystyle K(p)}

is found numerically to approximate

0.065770

{\displaystyle 0.065770}

... Show the effect of varying

n

{\displaystyle n}

on the accuracy of simulated

K ( p )

{\displaystyle K(p)}

for

p

0.5

{\displaystyle p=0.5}

and for values of

n

{\displaystyle n}

up to at least

1000

{\displaystyle 1000}

. Any calculation of

C

n

{\displaystyle C_{n}}

for finite

n

{\displaystyle n}

is subject to randomness, so an approximation should be computed as the average of

t

{\displaystyle t}

runs, where

t

{\displaystyle t}

5

{\displaystyle 5}

. For extra credit, graphically show clusters in a

15 × 15

{\displaystyle 15\times 15}

,

p

0.5

{\displaystyle p=0.5}

grid. Show your output here.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Percolation/Mean cluster density step by step in the EchoLisp programming language

Source code in the echolisp programming language

(define-constant BLACK  (rgb 0 0 0.6))
(define-constant WHITE -1)
;; sets pixels to clusterize to WHITE
;; returns bit-map vector
(define (init-C n p )
    (plot-size n n)
    (define C (pixels->int32-vector )) ;; get canvas bit-map
    (pixels-map (lambda (x y) (if (< (random) p) WHITE BLACK )) C)
    C )
    
;; random color for new cluster
(define (new-color)
    (hsv->rgb (random) 0.9 0.9))
    
;; make-region predicate
(define (in-cluster C x y)
    (= (pixel-ref C x y) WHITE))
    
;; paint all adjacents to (x0,y0) with new color
(define (make-cluster C x0 y0)
                (pixel-set! C x0 y0 (new-color))
                (make-region in-cluster C x0 y0))
        
;; task
(define (make-clusters (n 400) (p 0.5))
    (define Cn 0)
    (define C null)
        (for ((t 5)) ;; 5 iterations
        (plot-clear)
        (set!  C (init-C n p))
        (for* ((x0 n) (y0 n))
            #:when  (= (pixel-ref C x0 y0) WHITE) 
            (set! Cn (1+ Cn))
         (make-cluster C x0 y0)))

    (writeln 'n n  'Cn Cn  'density  (// Cn (* n n) 5) )
    (vector->pixels C)) ;; to screen


  

You may also check:How to resolve the algorithm Emirp primes step by step in the Ring programming language
You may also check:How to resolve the algorithm Sum multiples of 3 and 5 step by step in the 360 Assembly programming language
You may also check:How to resolve the algorithm String prepend step by step in the Ruby programming language
You may also check:How to resolve the algorithm Sudoku step by step in the Scala programming language
You may also check:How to resolve the algorithm Guess the number/With feedback step by step in the Elena programming language