Permalink
Browse files

initial commit. I already have a lot of working code.

  • Loading branch information...
0 parents commit 74f0ef199dfb8675f45e34f8c2816444f3d5fcdf uberj committed Jul 26, 2011
6 Makefile
@@ -0,0 +1,6 @@
+all:
+ gcc -g main.c -o run
+pic:
+ gcc -g main.c -o run
+ ./run data
+ python gen_pic.py data
0 automaton.c
No changes.
BIN automatondata.JPEG
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN automatontest.txt.JPEG
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN automatontest190.JPEG
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN core
Binary file not shown.
0 data
No changes.
24 gen_auto.txt
@@ -0,0 +1,24 @@
+00000000000000000000000000000000000000000000000000000
+11111111110101010101010001111111111111100000000000111
+11111111111111111111111111111111111111111111111111111
+00000000001010101010010101010101010101010101010101010
+00000000000000000000000001111111111111111111111111111
+11111111111111111111111110000000000000000000000000000
+00000000000000000000000000000000000000000000000000000
+11111111110101010101010001111111111111100000000000111
+11111111111111111111111111111111111111111111111111111
+00000000001010101010010101010101010101010101010101010
+00000000000000000000000001111111111111111111111111111
+11111111111111111111111110000000000000000000000000000
+00000000000000000000000000000000000000000000000000000
+11111111110101010101010001111111111111100000000000111
+11111111111111111111111111111111111111111111111111111
+00000000001010101010010101010101010101010101010101010
+00000000000000000000000001111111111111111111111111111
+11111111111111111111111110000000000000000000000000000
+00000000000000000000000000000000000000000000000000000
+11111111110101010101010001111111111111100000000000111
+11111111111111111111111111111111111111111111111111111
+00000000001010101010010101010101010101010101010101010
+00000000000000000000000001111111111111111111111111111
+11111111111111111111111110000000000000000000000000000
60 gen_pic.py
@@ -0,0 +1,60 @@
+import Image
+import ImageDraw
+import numpy
+import sys
+
+
+#Colors
+white = 255
+black = 0
+try:
+ auto_file = open(sys.argv[1])
+except Exception,e:
+ print "File not found"
+
+# Find out how many cells in a Latice and Num generations
+lines = auto_file.readlines()
+G = len(lines) # Number of generations
+N = len(lines[0].strip()) # Cells in a Lattice
+auto_file.seek(0) # Reset file pointer
+
+# A ball-park estimate
+width = 1000
+# Determine appropriate sizes for cells
+c_w = width/N # cell_width
+width = c_w*N # We lost some precision with integer division
+#c_h = height/G # cell_height
+c_h = c_w
+height = c_w*G # determine height
+
+
+img = Image.new('L',(width,height))
+draw = ImageDraw.Draw(img)
+
+print "Number of Generations: "+str(G)
+print "Latice size: "+str(N)
+print "Cell height: "+str(c_h)
+print "Cell width: "+str(c_w)
+print "Image Height: "+str(height)
+print "Image Width: "+str(width)
+
+g_c = c_c = 0 # Generation count = Cell_count = 0
+for line in auto_file.readlines():
+ for cell in line.strip():
+ if cell is '0':
+ x1,y1 = c_c*c_w, g_c*c_h
+ x2,y2 = (c_c + 1) * c_w, (g_c + 1)*c_h
+ draw.rectangle([(x1,y1) ,(x2,y2) ], black )
+ elif cell is '1':
+ x1,y1 = c_c*c_w, g_c*c_h
+ x2,y2 = (c_c + 1) * c_w, (g_c + 1)*c_h
+ draw.rectangle([(x1,y1) ,(x2,y2) ], white )
+ else:
+ print "Invalid Character"
+ print "exiting on Error...."
+ c_c = c_c + 1
+
+ c_c = 0
+ g_c = g_c + 1
+img.show()
+img.save("automaton"+str(sys.argv[1])+".JPEG")
BIN gen_pic.pyc
Binary file not shown.
31 gen_rules.sh
@@ -0,0 +1,31 @@
+#!/bin/bash
+# run <file> <radius> <latice_size> <steps> <lower_rule> <upper_rule>
+
+if [ $# -ne 6 ];then
+ echo "run <file> <radius> <latice_size> <steps> <lower_rule> <upper_rule>"
+ exit
+fi
+
+file=$1
+radius=$2
+latice_size=$3
+steps=$4
+lower_rule=$5
+upper_rule=$6
+
+c=$lower_rule
+while [ $c -lt $upper_rule ]
+do
+ ./run $file $radius $latice_size $steps $lower_rule $upper_rule
+ c=$((c+1))
+done
+
+c=$lower_rule
+while [ $c -lt $upper_rule ]
+do
+ echo "=========================="
+ echo
+ echo
+ python gen_pic.py $file$c
+ c=$((c+1))
+done
226 main.c
@@ -0,0 +1,226 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <math.h>
+/*
+ * WTF!!! Why! Is this not in my version of sdlib? Fucking Ubuntu...
+ */
+void reverse(char s[])
+{
+ int i, j;
+ char c;
+
+ for (i = 0, j = strlen(s)-1; i<j; i++, j--) {
+ c = s[i];
+ s[i] = s[j];
+ s[j] = c;
+ }
+}
+void itoa(int n, char s[])
+{
+ int i, sign;
+
+ if ((sign = n) < 0) /* record sign */
+ n = -n; /* make n positive */
+ i = 0;
+ do { /* generate digits in reverse order */
+ s[i++] = n % 10 + '0'; /* get next digit */
+ } while ((n /= 10) > 0); /* delete it */
+ if (sign < 0)
+ s[i++] = '-';
+ s[i] = '\0';
+ reverse(s);
+}
+
+struct program {
+ unsigned int radius; // 2^( 2*radius + 1 ) is neighborhood
+ unsigned int neighborhood;
+ unsigned int *function; // 2^(2^( 2*radius + 1 )) rules
+};
+
+struct automaton {
+ struct program prog;
+ char *state;
+ char *next_state;
+ char *swp;
+ int l_size; // Latice size
+ int steps; // How many steps to iterate
+};
+
+/*
+ * Initialize an automaton
+ */
+void init_automaton( struct automaton* autom, unsigned int radius, unsigned int latice_size, unsigned int steps ) {
+ int f_size, i;
+ for(f_size = 1, i = 0;i< 2*radius+1 ;i++) {
+ f_size *= 2;
+ }
+ printf("f_size: %i\n",f_size);
+ autom->prog.neighborhood = 2*radius + 1;
+ autom->prog.function = (unsigned int *) calloc(f_size, sizeof(unsigned int) );
+ autom->prog.radius = radius;
+ autom->l_size = latice_size;
+ autom->steps = steps;
+
+ autom->state = (char *)malloc( latice_size );
+ autom->next_state = (char *)malloc( latice_size );
+}
+
+/*
+ * Givin a Wolfram rule, generate the correct transformation function
+ */
+void generate_program( struct automaton* autom, unsigned int rule ) {
+ int state,i;
+ for( i=0, state = rule; rule > 0; rule = rule / 2, i++ ){
+ autom->prog.function[i] = rule % 2;
+ printf("i = %i, rule %i= %i\n",i,rule,rule%2 );
+ }
+ autom->prog.function[i] = rule % 2;
+ printf("i = %i, rule %i= %i\n",i,rule,rule%2 );
+}
+
+/*
+ * Write current state to file
+ */
+void save_state( struct automaton* autom, FILE *fp ) {
+ int i;
+ char new_line = '\n';
+ char state;
+ for(i = 0; i < autom->l_size; i++ ) {
+ state = autom->state[i]+'0';
+ fwrite( &state, 1, 1, fp );
+ }
+ fwrite( &new_line, 1, 1, fp );
+}
+
+/*
+ * Find number using radius and look up next state
+ */
+void apply_function( struct automaton* autom ) {
+ int i, j, k, cell_state, lucus;
+ unsigned int radius = autom->prog.radius;
+ // Do state while i < radius (we need to wrap around)
+ i = j = k = cell_state = 0;
+ while( i < autom->prog.radius ) {
+ k = cell_state = 0;
+ // Generate the rule that the current cell, i, is in.
+ for( j = i - radius; j <= i + (int) radius ; j++ ) {
+ cell_state *= 2; // Mult by 2
+ if( j < 0 ) // We need to wrap around
+ cell_state += autom->state[ autom->l_size - radius + i ];
+ else
+ cell_state += autom->state[j];
+ }
+ // Using the cell_state generated, look up the correct next state in the function table
+ autom->next_state[i] = autom->prog.function[cell_state];
+ i++;
+ }
+ // Do state until i < latice_size - radius
+ while( i < autom->l_size - radius ) {
+ k = cell_state = 0;
+ // Generate the rule that the current cell, i, is in.
+ for( j = i - radius; j <= i +radius; j++ ) {
+ cell_state *= 2; // Mult by 2
+ cell_state += autom->state[j];
+ }
+ // Using the cell_state generated, look up the correct next state in the function table
+ autom->next_state[i] = autom->prog.function[cell_state];
+ i++;
+ }
+ // Do state while i < latice_size
+ while( i < autom->l_size ) {
+ k = cell_state = 0;
+ // Generate the rule that the current cell, i, is in.
+ for( j = i - radius; j <= i +radius; j++ ) {
+ cell_state *= 2; // Mult by 2
+ if( j >= autom->l_size )
+ cell_state += autom->state[ j % autom->l_size ]; //Wrap around
+ else
+ cell_state += autom->state[ j ]; //Wrap around
+ }
+ // Using the cell_state generated, look up the correct next state in the function table
+ autom->next_state[i] = autom->prog.function[cell_state];
+ i++;
+ }
+
+}
+void get_initial_cond( struct automaton *autom ) {
+ //int i;
+ /*
+ for(i=0;i<autom->l_size ;i++){
+ autom->state[i] = 1;
+ autom->next_state[i] = 0;
+ }
+ */
+ autom->state[autom->l_size / 2] = 1;
+}
+
+/*
+ * Use this if we every use a memory scheme
+ */
+void next_state( struct automaton *autom ) {
+ autom->swp = autom->next_state;
+ autom->next_state = autom->state;
+ autom->state = autom->swp;
+}
+/*
+ * Do one cycle with an automaton
+ */
+void cycle_do_one( struct automaton *autom, int radius, int latice_size, \
+ int steps, int lower_rule, int upper_rule, char *fname )
+{
+ int rule,j;
+ FILE *fp;
+ char fname_num[30]; // fname with the rule appended
+ // Can you say buffer overflow?
+ char num[10];
+
+
+ for( rule = lower_rule; rule < upper_rule; rule++ ){
+ // Set up file
+ itoa(rule,num);
+ strcpy(fname_num, fname);
+ strcat(fname_num,num);
+ fp = fopen( fname_num, "w+" );
+
+ init_automaton( autom, radius, latice_size, steps );
+ // Get initial conditions
+ get_initial_cond( autom );
+ // Get Program
+ generate_program( autom, rule );
+ // Generate while N > 0
+ for( j = 0; j < autom->steps; j++ ){
+ // write to file
+ save_state( autom , fp );
+ apply_function( autom );
+ next_state( autom );
+ // Calculate next state
+ }
+ fclose( fp );
+ }
+
+}
+
+int main( int argc, char **argv ) {
+ struct automaton autom;
+ int i;
+ FILE *fp;
+ int radius, latice_size, steps, lower_rule, upper_rule;
+ char *fname;
+ if( argc != 7 ){
+ printf("./run <file> <radius> <latice_size> <steps> <lower_rule> <upper_rule>\n");
+ return 2;
+ }
+ fname = argv[1];
+ radius = atoi( argv[2] );
+ latice_size = atoi( argv[3] );
+ steps = atoi( argv[4] );
+ lower_rule = atoi( argv[5] );
+ upper_rule = atoi( argv[6] );
+
+ cycle_do_one( &autom, radius, latice_size, steps, lower_rule, upper_rule, (char *)fname );
+
+ return 0;
+}
+
+
BIN run
Binary file not shown.

0 comments on commit 74f0ef1

Please sign in to comment.