Skip to content

Commit

Permalink
Updated file saving/loading
Browse files Browse the repository at this point in the history
  • Loading branch information
Niyaz-Mohamed committed Aug 30, 2024
1 parent d0263c2 commit f2b0a1f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
37 changes: 24 additions & 13 deletions js/automata.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
outlinePoints,
registerCanvasCallbacks,
} from "./inputs/userInput.js";
import { editor } from "./inputs/settings.js";

//! Intialize Canvas
const canvas = document.getElementById("cellGrid");
Expand Down Expand Up @@ -350,6 +351,7 @@ export class LifeLikeAutomata extends Automata {
// Parse sequences to obtain rules
this.birthRules = [...new Set(parseSequence(ruleList[0]))];
this.surviveRules = [...new Set(parseSequence(ruleList[1]))];
this.ruleString = ruleString;
} else {
setConsoleText("Invalid Rulestring!");
}
Expand Down Expand Up @@ -658,7 +660,7 @@ export class ElementaryCA extends Automata {
args: [this.ruleNumber],
grid: this.grid.map((arr) => Array.from(arr)),
};
downloadObjectAsJSON(automataData, "elementaryCA.json");
downloadObjectAsJSON(automataData, "elementary.json");
}
}

Expand Down Expand Up @@ -728,8 +730,10 @@ export class BriansBrain extends Automata {
// Parse rulestring
let ruleList = ruleString.split("/").map(Number);
this.birthRules = [...new Set(ruleList)];
this.ruleString = ruleString;
} else if (ruleString == "") {
this.birthRules = [0];
this.ruleString = ruleString;
} else {
setConsoleText("Invalid Rulestring!");
}
Expand Down Expand Up @@ -957,9 +961,9 @@ export class RPSGame extends Automata {
this.neighborhood = neighborhood;
// Minimum number of cells that beat current cells in the neighborhood for conversion to occur
this.winCondition = winCondition;

// Number of states played with, ranges between 3 and 5
this.stateCount = [3, 4, 5].includes(stateCount) ? 3 : stateCount;
console.log(stateCount, this.stateCount);
this.stateCount = [3, 4, 5].includes(stateCount) ? stateCount : 3;

// Implement GPU kernel to update grid
this.gridUpdateKernel = this.gpu
Expand Down Expand Up @@ -1147,7 +1151,7 @@ export class NeuralCA extends Automata {
[-0.9, -0.66, -0.9],
[0.68, -0.9, 0.68],
],
activation = (x) => -(1 / Math.pow(2, 0.6 * Math.pow(x, 2))) + 1 // Inverse Gaussian
activationString = "function activation(x) {\n\treturn -(1 / Math.pow(2, 0.6 * Math.pow(x, 2))) + 1;\n}" // Inverse Gaussian
) {
super();
// Set frameskips to true
Expand All @@ -1159,7 +1163,9 @@ export class NeuralCA extends Automata {
weights.flat().length == neighborhood.flat().length / 2
? weights
: reshape2DArray(weights, neighborhood.length, neighborhood[0].length);
this.activation = activation;
this.activationString = activationString;
editor.setValue(activationString);
this.activation = eval(`(${activationString})`);
// Switch up colors to make the sim more interesting
this.maskOptions = [
[1, 0, 0],
Expand Down Expand Up @@ -1273,16 +1279,21 @@ export class NeuralCA extends Automata {
// Override downloading the data
saveData() {
const automataData = {
name: "Neural CA",
args: [this.neighborhood, this.weights], // Ignore activation as it cannot be saved in JSON
name: "Neural",
args: [this.neighborhood, this.weights, this.activationString], // Ignore activation as it cannot be saved in JSON
grid: this.grid.map((arr) => Array.from(arr)),
};
downloadObjectAsJSON(automataData, "neural.json");
}
}

export class Huegene extends Automata {
constructor(neighborhood = mooreNeighborhood()) {
constructor(
randomFactor = 40,
fade = false,
psychedelic = false,
neighborhood = mooreNeighborhood()
) {
super();
this.neighborhood = neighborhood;

Expand All @@ -1295,13 +1306,13 @@ export class Huegene extends Automata {
this.penState = this.genRandomPenColor();

// Select hue offsets for each cell
this.randomFactor = 40;
this.randomFactor = randomFactor;
document.getElementById("huegene-random-input").value = this.randomFactor;
this.updateOffset();

// Extra Settings
this.fade = false;
this.psychedelic = false;
this.fade = fade;
this.psychedelic = psychedelic;
this.fadeKeepRate = 0.99;
this.psychedelicRate = 4;
document.getElementById("huegene-psychedelic-input").checked = false;
Expand Down Expand Up @@ -1501,7 +1512,7 @@ export class Huegene extends Automata {
saveData() {
const automataData = {
name: "Huegene",
args: [this.neighborhood],
args: [this.randomFactor, this.fade, this.psychedelic, this.neighborhood],
grid: this.grid.map((arr) => Array.from(arr)),
};
downloadObjectAsJSON(automataData, "huegene.json");
Expand Down Expand Up @@ -1587,7 +1598,7 @@ export function setAutomata(newAutomataName, args = [], grid = null) {
// Convert cells to 2 or below
automata.grid = oldGrid.map((row) =>
row.map((state) =>
[0, 1, 2].includes(Math.round(state)) ? Math.round(state) : 0
[0, 1, 2, 3, 4].includes(Math.round(state)) ? Math.round(state) : 0
)
);
setConsoleText("Changed automata to Rock, Paper, Scissors");
Expand Down
6 changes: 3 additions & 3 deletions js/inputs/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ document.getElementById("file-input").addEventListener(
automata.grid[0].length
);
// Update required info

updateAutomataSelect(data["name"]);
setAutomata(data["name"], data["args"], data["grid"]);
} catch (error) {
Expand Down Expand Up @@ -475,6 +476,7 @@ function setActivation(type) {

// Intialize activation func code editor
const editor = ace.edit("neural-code-editor");
export { editor }; // Make editor available in other files
editor.setTheme("ace/theme/monokai"); // Dark theme
editor.session.setMode("ace/mode/javascript"); // JavaScript syntax highlighting
editor.setOptions({
Expand All @@ -484,9 +486,6 @@ editor.setOptions({
});
editor.session.setUseWrapMode(true);
editor.session.setUseSoftTabs(true);
editor.setValue(
"function activation(x) {\n\treturn -(1 / Math.pow(2, 0.6 * Math.pow(x, 2))) + 1;\n}"
);
editor.session.on("change", function (_) {
// Set to custom activation
activationSelector.selectedIndex = activationSelector.options.length - 1;
Expand All @@ -499,6 +498,7 @@ editor.session.on("change", function (_) {
const testValues = [Math.random(), Math.random(), Math.random(), 0, 1];
testValues.forEach((value) => activation(value));
automata.activation = activation;
automata.activationString = code;
automata.resetAnimationRequests();
setConsoleText("Updated Activation Function!");
}
Expand Down

0 comments on commit f2b0a1f

Please sign in to comment.