Added CLI flags to WolframAutomata allowing user to specify starting population.
authorAaron Taylor <ataylor@subgeniuskitty.com>
Mon, 15 Mar 2021 11:39:52 +0000 (04:39 -0700)
committerAaron Taylor <ataylor@subgeniuskitty.com>
Mon, 15 Mar 2021 11:39:52 +0000 (04:39 -0700)
hacks/WolframAutomata/WolframAutomata.c

index 33182e1..4f882a2 100644 (file)
 //              -overlay
 //        which ruleset number to use? Or random? Or random from small set of hand-selected interesting examples?
 //              In order of precedence:
 //              -overlay
 //        which ruleset number to use? Or random? Or random from small set of hand-selected interesting examples?
 //              In order of precedence:
-//                  -random (select a random rule on each run)
+//                  -rule-random (select a random rule on each run)
 //                  -rule N (always simulate Rule N on each run)
 //                  (if neither of the above two are specified, then a random CURATED rule is selected on each run)
 //                  -rule N (always simulate Rule N on each run)
 //                  (if neither of the above two are specified, then a random CURATED rule is selected on each run)
-//        which starting population to use? Or random? Or one bit in middle? Or one bit on edge?  (For random: Can I allow specifying a density like 25%, 50%, 75%?)
-//              Options (with precedence): -population STRING   (string is a comma separated list of cell IDs to populate, starting from 0)
-//                                         -population-curated
-//                                         -population-random
+//        which starting population to use, random or one bit? (for random: allow specifying a density)
+//              In order of precedence:
+//                  -population-single
+//                  -population-random DENSITY
+//                  (the two options above only apply to the simulation under the -rule-random or -rule N options. in curated mode, starting population is defined in the curation array)
+//                  TODO: In the future, add the option for user to pass list of cell IDs to turn ON.
 //        size of pixel square (e.g. 1x1, 2x2, 3x3, etc)
 //              -pixel-size N
 
 //        size of pixel square (e.g. 1x1, 2x2, 3x3, etc)
 //              -pixel-size N
 
+/* -------------------------------------------------------------------------- */
+/* Data Structures                                                            */
+/* -------------------------------------------------------------------------- */
+
 struct state {
     /* Various X resources */
     Display * dpy;
 struct state {
     /* Various X resources */
     Display * dpy;
@@ -61,6 +67,10 @@ struct state {
     uint8_t rule_requested; // Note: Repurposing Rule 0 as a null value.
     Bool rule_random;
 
     uint8_t rule_requested; // Note: Repurposing Rule 0 as a null value.
     Bool rule_random;
 
+    // TODO: Describe these.
+    int population_density;
+    Bool population_single;
+
     /* Misc Commandline Options */
     int pixel_size; /* Size of CA cell in pixels (e.g. pixel_size=3 means 3x3 pixels per cell). */
     int delay_microsec; /* Requested delay to screenhack framework before next call to WolframAutomata_draw(). */
     /* Misc Commandline Options */
     int pixel_size; /* Size of CA cell in pixels (e.g. pixel_size=3 means 3x3 pixels per cell). */
     int delay_microsec; /* Requested delay to screenhack framework before next call to WolframAutomata_draw(). */
@@ -70,22 +80,97 @@ struct state {
     size_t number_of_cells;
 };
 
     size_t number_of_cells;
 };
 
+// TODO: Decorations
+enum seed_population {
+    left_only,
+    middle_only,
+    right_only,
+    random_seed
+};
+
+// TODO: Decorations
+struct curated_ruleset {
+    uint8_t              rule;
+    enum seed_population seed;
+};
+
 // TODO: Check the full set of 256 CAs for visually interesting examples.
 // TODO: Check the full set of 256 CAs for visually interesting examples.
-static const uint8_t curated_rule_list[] = {
-    22,
-    30,
-    45,
-    57,
-    73,
-    86
+// TODO: Add comments explaining why each ruleset is interesting.
+static const struct curated_ruleset curated_ruleset_list[] = {
+    {110, random_seed}
 };
 
 };
 
+/* -------------------------------------------------------------------------- */
+/* Helper Functions                                                           */
+/* -------------------------------------------------------------------------- */
+
+// TODO: decorations? inline?
+void
+generate_random_seed(struct state * state)
+{
+    int i;
+    for (i = 0; i < state->number_of_cells; i++) {
+        state->current_generation[i] = ((random() % 100) < state->population_density) ? True : False;
+    }
+}
+
+// TODO: function decorations?
+// TODO: Explain why this santizes the index for accessing current_generation (i.e. it creates a circular topology).
+size_t
+sindex(struct state * state, int index)
+{
+    while (index < 0) {
+        index += state->number_of_cells;
+    }
+    while (index >= state->number_of_cells) {
+        index -= state->number_of_cells;
+    }
+    return (size_t) index;
+}
+
+// TODO: function decorations?
+// TODO: At least give a one-sentence explanation of the algorithm since this function is the core of the simulation.
+Bool
+calculate_cell(struct state * state, int cell_id)
+{
+    uint8_t cell_pattern = 0;
+    int i;
+    for (i = -1; i < 2; i++) {
+        cell_pattern = cell_pattern << 1;
+        if (state->current_generation[sindex(state, cell_id+i)] == True) {
+            cell_pattern |= 1;
+        }
+    }
+    if ((state->rule_number >> cell_pattern) & 1) {
+        return True;
+    } else {
+        return False;
+    }
+}
+
+// TODO: function decorations?
+void
+render_current_generation(struct state * state)
+{
+    size_t xpos;
+    for (xpos = 0; xpos < state->number_of_cells; xpos++) {
+        if (state->current_generation[xpos] == True) {
+            XFillRectangle(state->dpy, state->evolution_history, state->gc, xpos*state->pixel_size, state->ypos, state->pixel_size, state->pixel_size);
+        }
+    }
+}
+
+/* -------------------------------------------------------------------------- */
+/* Screenhack API Functions                                                   */
+/* -------------------------------------------------------------------------- */
+
 static void *
 WolframAutomata_init(Display * dpy, Window win)
 {
     struct state * state = calloc(1, sizeof(*state)); // TODO: Check calloc() call
     XGCValues gcv;
     XWindowAttributes xgwa;
 static void *
 WolframAutomata_init(Display * dpy, Window win)
 {
     struct state * state = calloc(1, sizeof(*state)); // TODO: Check calloc() call
     XGCValues gcv;
     XWindowAttributes xgwa;
+    const struct curated_ruleset * curated_ruleset = NULL;
 
     state->dpy = dpy;
     state->win = win;
 
     state->dpy = dpy;
     state->win = win;
@@ -107,6 +192,7 @@ WolframAutomata_init(Display * dpy, Window win)
     if (state->pixel_size > state->xlim) state->pixel_size = state->xlim;
 
     state->number_of_cells = state->xlim / state->pixel_size;
     if (state->pixel_size > state->xlim) state->pixel_size = state->xlim;
 
     state->number_of_cells = state->xlim / state->pixel_size;
+    // TODO: Do we want to enforce that number_of_cells > 0?
 
     /* The minimum number of generations is 2 since we must allocate enough   */
     /* space to hold the seed generation and at least one pass through        */
 
     /* The minimum number of generations is 2 since we must allocate enough   */
     /* space to hold the seed generation and at least one pass through        */
@@ -134,75 +220,56 @@ WolframAutomata_init(Display * dpy, Window win)
     } else {
         /* No command-line options were specified, so select rules randomly   */
         /* from a curated list.                                               */
     } else {
         /* No command-line options were specified, so select rules randomly   */
         /* from a curated list.                                               */
-        size_t number_of_array_elements = sizeof(curated_rule_list)/sizeof(curated_rule_list[0]);
-        state->rule_number = curated_rule_list[random() % number_of_array_elements];
+        size_t number_of_array_elements = sizeof(curated_ruleset_list)/sizeof(curated_ruleset_list[0]);
+        curated_ruleset = &curated_ruleset_list[random() % number_of_array_elements];
+        state->rule_number = curated_ruleset->rule;
+    }
+
+    /* Time to construct the seed generation for this simulation.             */
+    state->population_single = get_boolean_resource(state->dpy, "population-single", "Boolean");
+    state->population_density = get_integer_resource(state->dpy, "population-density", "Integer");
+    if (state->population_density < 0 || state->population_density > 100) state->population_density = 50;
+    state->current_generation = calloc(1, sizeof(*state->current_generation)*state->number_of_cells);
+    if (!state->current_generation) {
+        fprintf(stderr, "ERROR: Failed to calloc() in WolframAutomata_init().\n");
+        exit(EXIT_FAILURE);
+    }
+    if (curated_ruleset) {
+        /* If we're using a curated ruleset, ignore any CLI flags related to  */
+        /* setting the seed generation, instead drawing that information from */
+        /* the curated ruleset.                                               */
+        switch (curated_ruleset->seed) {
+            case random_seed: generate_random_seed(state);                                break;
+            case left_only:   state->current_generation[0] = True;                        break;
+            case right_only:  state->current_generation[state->number_of_cells-1] = True; break;
+            case middle_only: state->current_generation[state->number_of_cells/2] = True; break;
+        }
+    } else {
+        /* If we're not using a curated ruleset, process any relevant flags   */
+        /* from the user, falling back to a random seed generation if nothing */
+        /* else is specified.                                                 */
+        if (state->population_single) {
+            state->current_generation[0] = True;
+        } else {
+            generate_random_seed(state);
+        }
     }
 
     // TODO: These should be command-line options, but I need to learn how the get_integer_resource() and similar functions work first.
     state->display_info = True;
 
     }
 
     // TODO: These should be command-line options, but I need to learn how the get_integer_resource() and similar functions work first.
     state->display_info = True;
 
-    state->current_generation = calloc(1, (sizeof(*(state->current_generation))*state->number_of_cells)); // TODO: Check calloc() call TODO: Can't recall precedence; can I eliminate any parenthesis?
-    // TODO: Make the starting state a user-configurable option. At least give the user some options like 'random', 'one-middle', 'one edge', etc.
-    //       Ideally accept something like a list of integers representing starting pixels to be "on".
-    state->current_generation[0] = True;
-
     state->evolution_history = XCreatePixmap(state->dpy, state->win, state->xlim, state->num_generations*state->pixel_size, xgwa.depth);
     // Pixmap contents are undefined after creation. Explicitly set a black
     // background by drawing a black rectangle over the entire pixmap.
     XSetForeground(state->dpy, state->gc, state->bg);
     XFillRectangle(state->dpy, state->evolution_history, state->gc, 0, 0, state->xlim, state->num_generations*state->pixel_size);
     XSetForeground(state->dpy, state->gc, state->fg);
     state->evolution_history = XCreatePixmap(state->dpy, state->win, state->xlim, state->num_generations*state->pixel_size, xgwa.depth);
     // Pixmap contents are undefined after creation. Explicitly set a black
     // background by drawing a black rectangle over the entire pixmap.
     XSetForeground(state->dpy, state->gc, state->bg);
     XFillRectangle(state->dpy, state->evolution_history, state->gc, 0, 0, state->xlim, state->num_generations*state->pixel_size);
     XSetForeground(state->dpy, state->gc, state->fg);
-    // TODO: Need to draw starting generation on pixmap and increment state->ypos.
+    render_current_generation(state);
+    state->ypos += state->pixel_size;
 
     return state;
 }
 
 
     return state;
 }
 
-// TODO: function decorations?
-// TODO: Explain why this santizes the index for accessing current_generation (i.e. it creates a circular topology).
-size_t
-sindex(struct state * state, int index)
-{
-    while (index < 0) {
-        index += state->number_of_cells;
-    }
-    while (index >= state->number_of_cells) {
-        index -= state->number_of_cells;
-    }
-    return (size_t) index;
-}
-
-// TODO: function decorations?
-// TODO: At least give a one-sentence explanation of the algorithm since this function is the core of the simulation.
-Bool
-calculate_cell(struct state * state, int cell_id)
-{
-    uint8_t cell_pattern = 0;
-    int i;
-    for (i = -1; i < 2; i++) {
-        cell_pattern = cell_pattern << 1;
-        if (state->current_generation[sindex(state, cell_id+i)] == True) {
-            cell_pattern |= 1;
-        }
-    }
-    if ((state->rule_number >> cell_pattern) & 1) {
-        return True;
-    } else {
-        return False;
-    }
-}
-
-// TODO: function decorations?
-void
-render_current_generation(struct state * state)
-{
-    size_t xpos;
-    for (xpos = 0; xpos < state->number_of_cells; xpos++) {
-        if (state->current_generation[xpos] == True) {
-            XFillRectangle(state->dpy, state->evolution_history, state->gc, xpos*state->pixel_size, state->ypos, state->pixel_size, state->pixel_size);
-        }
-    }
-}
-
 static unsigned long
 WolframAutomata_draw(Display * dpy, Window win, void * closure)
 {
 static unsigned long
 WolframAutomata_draw(Display * dpy, Window win, void * closure)
 {
@@ -265,6 +332,8 @@ static const char * WolframAutomata_defaults[] = {
     "*num-generations:    5000",
     "*rule-requested:    0",
     "*rule-random:    False",
     "*num-generations:    5000",
     "*rule-requested:    0",
     "*rule-random:    False",
+    "*population-density:    50",
+    "*population-single:    False",
     0
 };
 
     0
 };
 
@@ -275,6 +344,8 @@ static XrmOptionDescRec WolframAutomata_options[] = {
     { "-num-generations",    ".num-generations",    XrmoptionSepArg, 0 },
     { "-rule",    ".rule-requested",    XrmoptionSepArg, 0 },
     { "-rule-random",    ".rule-random",    XrmoptionNoArg, "True" },
     { "-num-generations",    ".num-generations",    XrmoptionSepArg, 0 },
     { "-rule",    ".rule-requested",    XrmoptionSepArg, 0 },
     { "-rule-random",    ".rule-random",    XrmoptionNoArg, "True" },
+    { "-population-density",    ".population-density",    XrmoptionSepArg, 0 },
+    { "-population-single",    ".population-single",    XrmoptionNoArg, "True" },
     { 0, 0, 0, 0 }
 };
 
     { 0, 0, 0, 0 }
 };