Could someone help me to change the random walk to self-avoiding random walk? I tried to do that but without success. Below is the code for the random walk, that works:
using StatsBase, LinearAlgebra
# Here I defined the position in the NxN matrix
function position(vertical, horizontal, N)
ud = BitArray(a == vertical for a = 1:N ) # Goes up or down
lr = transpose(BitArray(a == horizontal for a = 1:N )) # Goes left or right
return ud .* lr
end
function SAW(N, Iterations=3) # SAW, Self Avoiding Walk
x = ceil(N/2) # x,y are the starting positions
y = ceil(N/2)
Mat = zeros(Int8, N , N) # this is the lattice
pos = zeros(Int8, N , N) # this is our position
for i in 1:Iterations
RandomOnes = sample(-1:2:1, 2, replace = false)[1] #gives the values -1 or 1
ZeroOne = sample(0:1, 1, replace = false)[1] #gives the values 0 or 1
#Check where a free space is and create a list accordingly
# In order to avoid moving diagonally, only one of the two variables (x,y)
# can have the values 1 or -1, the other one has to be 0 (e.g. if x = 1 then y = 0)
if ZeroOne == 1
x += RandomOnes
y += 0
else
x += 0
y += RandomOnes
end
Mat += position(y,x,N)
pos = position(y,x,N)
end
for i in 1:N
for j in 1:N
if Mat[i,j] == 1
Mat[i,j] += 0
end
end
end
return Mat, pos
end
I know that I have to save the last position of my walker, and see if he was already there but I have no idea how to implement it in my code