;; AI Final Project ;; Module: ALM Specifics - UI ;; CSC 466 - Graci ;; Jacob Peck - 20110427 ; write to file (JFugueDaemon listens in directory temp/) (defmethod write-jfugue-input-file ((comp composition) &aux filename output) (setf filename (concatenate 'string "temp/" (composition-title comp))) (setf output (list-to-string (composition-data comp))) (with-open-file (outfile filename :direction :output) (format outfile output) ) NIL ) ; choose highest weighted cell (defmethod choose-weighted-cell ((auto ca) &aux cells max choices choice) (setf cells (ca-cells auto)) (setf max 0) (setf choices '()) (dolist (element cells NIL) (if (> (count 'X (cell-history element)) max) (setf max (count 'X (cell-history element)))) ) (dolist (element cells NIL) (if (= max (count 'X (cell-history element))) (setf choices (append choices (list element))) ) ) (setf choice (pick choices)) (format t "Choosing rule ~A (weight ~A)...~%Rules: ~A~%~%" (lcell-rulenum choice) (count 'X (cell-history choice)) (lsys-rules (lcell-lsystem choice))) choice ) ; the UI (defmethod alm (&aux desiredfitness cellcount name pop ca composition lsyslist iterate firsttime generation) ; obligatory version header (format t "ALM v. 20110447, by Jacob Peck~%") ; intro and get initial information (format t "Welcome to ALM!~%Please input an alphabet to use as a list of symbols:~%~%") (setf *alphabet* (read)) (format t "~%Please enter the desired fitness (in the range [0...1]):~%~%") (setf desiredfitness (read)) (format t "~%Please enter the desired number of cells (at least 3, around 50-100 is a decent amount):~%~%") (setf cellcount (read)) (format t "~%Please enter a title for this composition:~%~%") (setf name (read)) (format t "~%Alright, we're ready to get started. Just to review, here is the information you have provided me:~%Alphabet: ~A~%Desired fitness: ~A~%Number of cells: ~A~%Name: ~A~%~%" (list-to-string *alphabet*) desiredfitness cellcount name) ; create population, evolve until average fitness >= desired fitness (format t "Generating initial population...~%") (setf pop (generate-random-population *alphabet* cellcount #'fitness-balancing-output)) (format t "Evolving population.") (loop do (setf pop (run-genetic-algorithm pop #'fitness-balancing-output)) (format t ".") until (>= (calculate-fitness pop) desiredfitness)) (format t "~%") (population-average-fitness pop) (format t "~%") ; create CA (format t "Creating cellular automata...~%~%") (setf lsyslist '()) (dolist (element (population-individuals pop) NIL) (setf lsyslist (append lsyslist (list (individual-data element)))) ) (setf ca (make-ca-lcells cellcount '(X -) #'rule-86 lsyslist)) (visualize-ca-lcells ca) (format t "~%") ;; loop! (setf iterate t) (setf firsttime t) (setf generation 0) (loop do ; choose rule (iterate-ca ca) (visualize-ca-lcells ca) (format t "~%") (setf choice (choose-weighted-cell ca)) ; rewrite + update composition (format t "Rewriting...~%~%") (if firsttime (progn (setf composition (make-composition (write-to-string name) (lsys-data (lcell-lsystem choice)))) (setf firsttime NIL) ) ) (setf (lsys-data (lcell-lsystem choice)) (composition-data composition)) (iterate-lsys (lcell-lsystem choice)) (setf (composition-data composition) (lsys-data (lcell-lsystem choice))) (setf generation (+ 1 generation)) (setf (composition-title composition) (concatenate 'string (write-to-string name) "-" (write-to-string generation))) ; output results (format t "Generation ~A: ~A~%" generation (list-to-string (composition-data composition))) (write-jfugue-input-file composition) ; continue iterating? (format t "Continue iterating? ") (setf iterate (read)) ;; end loop until (null iterate)) (format t "Check in the output/ directory for .midi files of the various stages of this composition.~%Thanks for using ALM!~%") NIL )