How to resolve the algorithm Langton's ant step by step in the Haskell programming language

Published on 7 June 2024 03:52 AM

How to resolve the algorithm Langton's ant step by step in the Haskell programming language

Table of Contents

Problem Statement

Langton's ant is a cellular automaton that models an ant sitting on a plane of cells, all of which are white initially, the ant facing in one of four directions. Each cell can either be black or white. The ant moves according to the color of the cell it is currently sitting in, with the following rules: This rather simple ruleset leads to an initially chaotic movement pattern, and after about 10000 steps, a cycle appears where the ant moves steadily away from the starting location in a diagonal corridor about 10 cells wide.
Conceptually the ant can then walk infinitely far away.

Start the ant near the center of a 100x100 field of cells, which is about big enough to contain the initial chaotic part of the movement. Follow the movement rules for the ant, terminate when it moves out of the region, and show the cell colors it leaves behind.

The problem has received some analysis; for more details, please take a look at the Wikipedia article   (a link is below)..

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Langton's ant step by step in the Haskell programming language

Source Code Explanation

1. Importing Libraries

import Data.Set (member,insert,delete,Set)

This imports the Data.Set module for working with sets.

2. Functional Utilities

(>>>) = flip (.)
p ?>> (f, g) = \x -> if p x then f x else g x

These are two helper functions:

  • (>>>) is function composition with flipped order.
  • p ?>> (f, g) is a conditional selector function that applies f if p is True and g otherwise.

3. State Data Type

data State = State { antPosition :: Point
                  , antDirection :: Point
                  , getCells :: Set Point }

The State data type represents the state of the Langton's Ant simulation, including the ant's position, direction, and the set of visited cells.

  • Point is defined as a tuple of two floating-point numbers.

4. Simulation Step

step :: State -> State
step = isBlack ?>> (setWhite >>> turnRight,
                   setBlack >>> turnLeft) >>> move

The step function takes a State and applies the following operations, depending on whether the current cell is black or white:

  • If the cell is black, setWhite sets the cell to white and turnRight turns the ant right.
  • If the cell is white, setBlack sets the cell to black and turnLeft turns the ant left.
  • Finally, move moves the ant one step in the current direction.

5. task Function

task :: State -> State
task = iterate step 
  >>> dropWhile ((< 50) . distance . antPosition) 
  >>> getCells . head
  • iterate step continuously applies the step function to the state.
  • dropWhile ((< 50) . distance . antPosition) drops states where the ant is within a 50-unit radius of the origin.
  • getCells . head extracts the set of visited cells from the first state that meets the distance criterion.

6. main Function (Image Rendering)

main = display w white (draw (task initial))
  • This version of main creates a graphical window using the Gloss library to display the simulation.
  • w is the window configuration.
  • initial is the initial state of the ant.
  • draw is a function that converts the final set of visited cells into a graphical representation.

7. Alternative main Function (Interactive Simulation)

main = simulate w white 500 initial draw (\_ _ -> step)
  • This alternative version of main creates an interactive simulation.
  • w is the window configuration.
  • initial is the initial state of the ant.
  • draw is a function that converts the current state into a graphical representation.
  • (\_ _ -> step) is a function that applies the step function to the current state.

Source code in the haskell programming language

import Data.Set (member,insert,delete,Set)


-- functional sequence 
(>>>) = flip (.)

-- functional choice
p ?>> (f, g) = \x -> if p x then f x else g x


data State = State { antPosition :: Point
                   , antDirection :: Point
                   , getCells :: Set Point }

type Point = (Float, Float)


step :: State -> State
step = isBlack ?>> (setWhite >>> turnRight,
                    setBlack >>> turnLeft) >>> move
  where
    isBlack   (State p _     m) = member p m
    setBlack  (State p d     m) = State p d (insert p m)
    setWhite  (State p d     m) = State p d (delete p m)
    turnRight (State p (x,y) m) = State p (y,-x) m
    turnLeft  (State p (x,y) m) = State p (-y,x) m
    move (State (x,y) (dx,dy) m) = State (x+dx, y+dy) (dx, dy) m


task :: State -> State
task = iterate step 
   >>> dropWhile ((< 50) . distance . antPosition) 
   >>> getCells . head
  where distance (x,y) = max (abs x) (abs y)


import Graphics.Gloss

main = display w white (draw (task initial))
  where
    w = InWindow "Langton's Ant" (400,400) (0,0)
    initial = State (0,0) (1,0) mempty
    draw = foldMap drawCell
    drawCell (x,y) = Translate (10*x) (10*y) $ rectangleSolid 10 10


main = simulate w white 500 initial draw (\_ _ -> step)
  where
    w = InWindow "Langton's Ant" (400,400) (0,0)
    initial = State (0,0) (1,0) mempty
    draw (State p _ s) = pictures [foldMap drawCell s, color red $ drawCell p]
    drawCell (x,y) = Translate (10*x) (10*y) $ rectangleSolid 10 10


  

You may also check:How to resolve the algorithm Zeckendorf number representation step by step in the Ruby programming language
You may also check:How to resolve the algorithm Singly-linked list/Traversal step by step in the C programming language
You may also check:How to resolve the algorithm Hash from two arrays step by step in the Tcl programming language
You may also check:How to resolve the algorithm CSV data manipulation step by step in the TUSCRIPT programming language
You may also check:How to resolve the algorithm Ethiopian multiplication step by step in the PL/M programming language