Jump to content

Recommended Posts

Posted

I'm new to Opusmodus and Lisp programming. It will be awhile before I can implement some of the ideas that I have. In the meantime I've discovered that the Opusmodus GPT can do some programming for me. Here are two functions that it generated for me. A one dimensional totalistc CA with an arbitrary number of states, and a function to generate random rule sets. It took some prompting to get them to this stage. Totalistic CA rules are symmetrical, so if your initial state is a palindrome all generations will be.

 

(defun totalistic-cellular-automaton (states rules initial-state generations)
  "Generates a multi-state totalistic cellular automaton.
   STATES: the number of possible states (e.g., 0, 1, 2, ...).
   RULES: a list of rules that map the sum of neighborhood states to a new state.
   INITIAL-STATE: a list representing the initial state of the automaton.
   GENERATIONS: the number of generations to evolve.
   Returns a list of all generations."
  (let* ((length (length initial-state))  ; Calculate the length from the initial-state
         (current-state initial-state)
         (next-state (make-list length))
         (all-generations (list current-state))) ; Initialize list with initial state
    (loop for gen from 1 to (1- generations)
          do (progn
               (loop for i from 0 below length
                     do (let* ((left (nth (mod (- i 1) length) current-state))  ; Left neighbor (wrap-around)
                               (center (nth i current-state))                 ; Current cell
                               (right (nth (mod (+ i 1) length) current-state)) ; Right neighbor (wrap-around)
                               (neighborhood-sum (+ left center right)))      ; Calculate the sum of neighbors
                          ;; Apply the totalistic rule based on the neighborhood sum
                          (setf (nth i next-state) (nth neighborhood-sum rules))))
               ;; Append the new state to the list of all generations
               (push (copy-list next-state) all-generations)
               ;; Move to the next state
               (setf current-state (copy-list next-state))))
    ;; Return the list of all generations
    (reverse all-generations)))  ; Return in the correct order

(defun generate-random-rule-set (states &key seed)
  "Generates a random rule set for a totalistic cellular automaton with the given number of STATES.
   The rule set maps sums (from 0 to 3 * (states - 1)) to a random state.
   The SEED parameter allows reproducibility of random output."
  (let ((max-sum (* 3 (1- states))))
    ;; Generate the entire rule set in one go using rndn with the seed parameter
    (rndn (1+ max-sum) 0 (1- states) :seed seed)))  ;; Generate integers between 0 and states-1

;; usage
(setf states 8)
(setf rule (generate-random-rule-set states :seed 12))
(totalistic-cellular-automaton states rule '(0 1 2 7 7 2 1 0) 4)
=> ((0 1 2 7 7 2 1 0)
    (7 3 6 2 2 6 3 7)
    (2 2 6 6 6 6 2 2)
    (3 6 7 4 4 7 6 3))

 

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

Terms of Use Privacy Policy