Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
#lang racket
;; Filter function: Generates possible coordinates that can be visited
(define (filter-coords coords visited-coords min max)
(filter
(lambda (coord)
(let ((x (car coord))
(y (cdr coord)))
(not (or (member coord visited-coords)
(< x min) (< y min) (> x max) (> y max)))))
coords))
;; Function will generate all possible moves of length one from the current position and return list of them
(define (gen-coords curr-coord)
(let ((x (car curr-coord))
(y (cdr curr-coord)))
(list (cons x (+ y 1))
(cons x (- y 1))
(cons (+ x 1) y)
(cons (- x 1) y))))
;; Random Coordinate function: Generate a random coordinate from the list of filtered coordinates
(define (random-coord coords)
(if (= (length coords) 0)
'()
(let ((random-val (random (length coords))))
(list-ref coords random-val))))
;;Calucluate the distance
(define (distance start end)
(let ((x (abs (- (car start) (car end))))
(y (abs (- (car end) (cdr end)))))
(+ x y)))
(define (minNmax coord1 coord2)
(define (minhelp x y)
(if (< x y) x
y))
(define (maxhelp x y)
(if (> x y) x
y))
(cons (minhelp (car coord1) coord2) (maxhelp (cdr coord1) coord2)))
;;Main function
(define (random-walk start-coord size max-steps)
(define (walk-helper coord visit-list steps size x-coord y-coord) ;Picks a move out of the filtered list
(let ((picked-move (random-coord (filter-coords (gen-coords coord)
visit-list (* -1 size) size))));Define local binding which picks random move from filtered list
(if (or (null? picked-move) (= steps max-steps))
(list steps (distance start-coord coord) (* (- (cdr x-coord) (car x-coord)) (- (cdr y-coord) (car y-coord))));If there are no moves return the number of steps
(walk-helper picked-move (cons coord visit-list) (+ 1 steps) size (minNmax x-coord (car coord)) (minNmax y-coord (cdr coord)))))) ;recursively call walker-helper to make next move
(walk-helper start-coord '() 0 size '(0 . 0) '(0 . 0))) ;function call to initiate random-walk
;;Run simiulation x times of size 100
(define (run-sim x size max-steps)
(define (run-sim-aux start end path-len)
(if (= start end)
'()
(cons path-len (run-sim-aux (+ 1 start) end (random-walk '(0 . 0) size max-steps)))))
(run-sim-aux 0 x (random-walk '(0 . 0) size max-steps)))
;;Calculate the average
(define (avg-list lst)
(let ((x (foldr + 0.0 lst)))
(/ x (length lst))))
;;Calculate the standard deviation
(define (std-list lst)
(define (pred x y)
(let ((mean (avg-list lst)))
(+ y (expt (- x mean) 2))))
(sqrt (/ (foldr pred 0 lst) (length lst))))
;;Calculate the median
(define (median-lst lst)
(let* ((sort-lst (sort lst <))
(len (length sort-lst)))
(if (odd? len)
(list-ref sort-lst (floor (/ len 2)))
(/ (+ (list-ref sort-lst (floor (/ len 2)))
(list-ref sort-lst (- (/ len 2) 1)))
2))))
;;Prints out the average, median, variance, standard deviation as a list if you run the simulation 10 times
(display "Simulation ran 100 times, 100x100 lattice\n")
(display "List of steps, distance, lattice size: \n")
(define y (run-sim 100 100 +inf.0))
(define steps (map car y))
(display "Average, Standard-deviation: ")
(list (avg-list steps) (std-list steps))
(display "Simulation ran 100 times, 1000x1000 lattice\n")
(define x (run-sim 100 1000 +inf.0))
(display "List of steps, distance, lattice size: \n")
;x
(display "Average, Standard-devation: ")
(define steps2 (map car x))
(list (avg-list steps2) (std-list steps2))
;;Part 2
(define a (run-sim 10 +inf.0 5000))
(display "Simulation ran 10 times, no bounds, 5000 steps\n")
;a
(define dist-lst (map cadr a))
(display "Distance: Average, Median, Variance, Standard-deviation: ")
(list (avg-list dist-lst) (median-lst dist-lst) (expt (std-list dist-lst) 2) (std-list dist-lst))
(define lattice-lst (map caddr a))
(display "Lattice: Average, Median, Variance, Standard-deviation: ")
(list (avg-list lattice-lst) (median-lst lattice-lst) (expt (std-list lattice-lst) 2) (std-list lattice-lst))
;;100 times
(define p (run-sim 100 +inf.0 5000))
(display "Simulation ran 100 times, no bounds, 5000 steps\n")
;p
(define dist-lst1 (map cadr p))
(display "Distance: Average, Median, Variance, Standard-deviation: ")
(list (avg-list dist-lst1) (median-lst dist-lst1) (expt (std-list dist-lst1) 2) (std-list dist-lst1))
(define lattice-lst1 (map caddr p))
(display "Lattice: Average, Median, Variance, Standard-deviation: ")
(list (avg-list lattice-lst1) (median-lst lattice-lst1) (expt (std-list lattice-lst1) 2) (std-list lattice-lst1))
;;1000 times
(define e (run-sim 1000 +inf.0 5000))
(display "Simulation ran 1000 times, no bounds, 5000 steps\n")
;;List of steps, distance lattice
;e
(define dist-lst2 (map cadr e))
(display "Distance: Average, Median, Variance, Standard-deviation: ")
(list (avg-list dist-lst2) (median-lst dist-lst2) (expt (std-list dist-lst2) 2) (std-list dist-lst2))
(define lattice-lst2 (map caddr e))
(display "Lattice: Average, Median, Variance, Standard-deviation: ")
(list (avg-list lattice-lst2) (median-lst lattice-lst2) (expt (std-list lattice-lst2) 2) (std-list lattice-lst2))