Skip to content

Commit

Permalink
Added "add random mark" operation
Browse files Browse the repository at this point in the history
  • Loading branch information
adamblvck committed Aug 6, 2024
1 parent 354345f commit 279fc38
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 4 deletions.
7 changes: 6 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ <h1>Assembly Driven Laws of Form Playground</h1>
</div>

<div class="control-center">
<h2>Probably of<br>Operation / Step</h2>
<h2>Probability of<br>Operation / Step</h2>
<div class="sliders-section">
<div class="slider-container">
<p>[[ ]] = <span id="percent1">10%</span></p>
Expand All @@ -62,6 +62,10 @@ <h2>Probably of<br>Operation / Step</h2>
<p>[] = [][] <span id="percent4">10%</span></p>
<input type="range" min="0" max="10" value="1" class="slider" id="slider4">
</div>
<div class="slider-container">
<p>Random Mark <span id="percent5">10%</span></p>
<input type="range" min="0" max="10" value="0" class="slider" id="slider4">
</div>
</div>
</div>

Expand All @@ -73,6 +77,7 @@ <h2>Probably of<br>Operation / Step</h2>
<script type="text/javascript" src="lof_compensate.js"></script>
<script type="text/javascript" src="lof_condense.js"></script>
<script type="text/javascript" src="lof_confirm.js"></script>
<script type="text/javascript" src="lof_measure.js"></script>
<script src="./js/chart.js"></script>
<script type="text/javascript" src="script.js"></script>

Expand Down
1 change: 1 addition & 0 deletions lof.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// var _ = require('lodash');
// this files contains faulty transformations, but results in very interesting patterns

// Utility Function: Compare if two objects or arrays are equal
function areEqualObjects(obj1, obj2) {
Expand Down
87 changes: 87 additions & 0 deletions lof_measure.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
function LoFMeasure(nestedArray) {
// Helper function to find all arrays in the nested structure
function findAllArrays(arr, parentIndex = []) {
let arrays = [];
for (let i = 0; i < arr.length; i++) {
if (Array.isArray(arr[i])) {
let currentIndex = parentIndex.concat(i);
arrays.push(currentIndex);
arrays = arrays.concat(findAllArrays(arr[i], currentIndex));
}
}
return arrays;
}

// Helper function to find all decapsulatable arrays
function findDecapsulatableArrays(arr, parentIndex = []) {
let decapsulatableArrays = [];
for (let i = 0; i < arr.length; i++) {
if (Array.isArray(arr[i]) && arr[i].length === 1 && Array.isArray(arr[i][0])) {
let currentIndex = parentIndex.concat(i);
decapsulatableArrays.push(currentIndex);
decapsulatableArrays = decapsulatableArrays.concat(findDecapsulatableArrays(arr[i], currentIndex));
} else if (Array.isArray(arr[i])) {
decapsulatableArrays = decapsulatableArrays.concat(findDecapsulatableArrays(arr[i], parentIndex.concat(i)));
}
}
return decapsulatableArrays;
}

// Helper function to encapsulate an array at a given path
function encapsulateAtPath(arr, path) {
if (path.length === 1) {
arr[path[0]] = [arr[path[0]]];
} else {
encapsulateAtPath(arr[path[0]], path.slice(1));
}
}

// Helper function to decapsulate an array at a given path
function decapsulateAtPath(arr, path) {
if (path.length === 1) {
arr[path[0]] = arr[path[0]][0];
} else {
decapsulateAtPath(arr[path[0]], path.slice(1));
}
}

// Randomly decide to encapsulate or decapsulate
let operation = Math.random() < 0.5 ? 'encapsulate' : 'decapsulate';

if (operation === 'encapsulate') {
// Find all arrays in the nested structure
let allArrays = findAllArrays(nestedArray);
if (allArrays.length === 0) {
return nestedArray; // No arrays to encapsulate
}
// Select a random array to encapsulate
let randomArray = allArrays[Math.floor(Math.random() * allArrays.length)];
encapsulateAtPath(nestedArray, randomArray);
} else {
// Find all decapsulatable arrays in the nested structure
let decapsulatableArrays = findDecapsulatableArrays(nestedArray);
if (decapsulatableArrays.length === 0) {
return nestedArray; // No arrays to decapsulate
}
// Select a random array to decapsulate
let randomArray = decapsulatableArrays[Math.floor(Math.random() * decapsulatableArrays.length)];
decapsulateAtPath(nestedArray, randomArray);
}

return nestedArray;
}

// Test cases
let testCase1 = [[[]]];
let testCase2 = [[[]], [[]]];
let testCase3 = [[[], []], []];
let testCase4 = [[], [[]]];
let testCase5 = [[[[[]]]]];
let testCase6 = [[[[[]]]],[[[[], [[[[]]]]]]]];

console.log(JSON.stringify(LoFMeasure(testCase1)));
console.log(JSON.stringify(LoFMeasure(testCase2)));
console.log(JSON.stringify(LoFMeasure(testCase3)));
console.log(JSON.stringify(LoFMeasure(testCase4)));
console.log(JSON.stringify(LoFMeasure(testCase5)));
console.log(JSON.stringify(LoFMeasure(testCase6)));
7 changes: 4 additions & 3 deletions script.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
// const { calculateAssemblyIndex} = require('./assembley.js');

const lof_policy = {
lof_weights: [1, 1, 1, 1]
lof_weights: [1, 1, 1, 1, 0]
};

const sliders = document.querySelectorAll('.slider');
const percents = [
document.getElementById('percent1'),
document.getElementById('percent2'),
document.getElementById('percent3'),
document.getElementById('percent4')
document.getElementById('percent4'),
document.getElementById('percent5'),
];

sliders.forEach((slider, index) => {
Expand Down Expand Up @@ -249,7 +250,7 @@ document.getElementById('playButton').addEventListener('click', () => {
const frequency = document.getElementById('frequencySlider').value;
timer = setInterval(() => {

const lof = [LoFCancel, LofCompensate, LoFCondense, LoFConfirm]
const lof = [LoFCancel, LofCompensate, LoFCondense, LoFConfirm, LoFMeasure]
const execution_distribution = lof_policy.lof_weights; // [3, 1, 3, 3]

// Create an array to hold the weighted functions
Expand Down

0 comments on commit 279fc38

Please sign in to comment.