scrap versions --- scrap 1 // paste in here: // https://p5js.org/examples/interaction-reach-1.html let fruit; let upperArmAngle = 0; let lowerArmAngle = 0; let grabbed = false; // Whether the fruit is grabbed let moves = 0; // Track the number of moves function setup() { createCanvas(400, 400); fruit = createVector(220 + random(-10, 10), 200 + random(-10, 10)); // Adjust position and add randomness let btn = createButton('Click Me to Start'); // Position the button btn.position(0, 0); // Assign a function to call when the button is clicked btn.mousePressed(onButtonPressed); } function onButtonPressed() { console.log("Button was pressed."); let epochs = 5; let currentEpoch = 0; // Function to reset to starting conditions function reset() { upperArmAngle = 0; lowerArmAngle = 0; grabbed = false; moves = 0; fruit = createVector(220 + random(-10, 10), 200 + random(-10, 10)); } // Function to perform a random movement function randomMove() { const moves = ['q', 'w', 'a', 's']; const randomIndex = floor(random(0, moves.length)); const move = moves[randomIndex]; if (move === 'q') { upperArmAngle -= PI / 36; } if (move === 'w') { upperArmAngle += PI / 36; } if (move === 'a') { lowerArmAngle -= PI / 36; } if (move === 's') { lowerArmAngle += PI / 36; } moves++; } // Function to run an epoch function runEpoch() { // Add random movements here to demonstrate the arm moving randomMove(); // Check and log the distance to head for demonstration const headX = 200; const headY = 100; const distanceToHead = dist(fruit.x, fruit.y, headX, headY); console.log(`Distance to head: ${distanceToHead}`); // Reset if epoch is finished if (currentEpoch < epochs) { reset(); currentEpoch++; console.log(`Epoch ${currentEpoch}/${epochs}`); } else { console.log("Training complete"); clearInterval(interval); } } // Start the interval to run epochs const interval = setInterval(runEpoch, 1000); // Run an epoch every second } function draw() { background(220); // Head ellipse(200, 100, 50, 50); // Head // Torso line(200, 125, 200, 250); // Torso // Left arm (static) line(200, 150, 150, 180); // Upper arm line(150, 180, 110, 160); // Forearm // Left leg (static) line(200, 250, 170, 300); // Thigh line(170, 300, 170, 350); // Shin // Right leg (static) line(200, 250, 230, 300); // Thigh line(230, 300, 230, 350); // Shin // Right arm (movable) let upperArmEndX = 200 + 50 * cos(upperArmAngle); let upperArmEndY = 150 + 50 * sin(upperArmAngle); line(200, 150, upperArmEndX, upperArmEndY); // Upper arm let lowerArmEndX = upperArmEndX + 40 * cos(lowerArmAngle + upperArmAngle); let lowerArmEndY = upperArmEndY + 40 * sin(lowerArmAngle + upperArmAngle); line(upperArmEndX, upperArmEndY, lowerArmEndX, lowerArmEndY); // Forearm // Draw the fruit fill(255, 0, 0); if (grabbed) { fruit.x = lowerArmEndX; fruit.y = lowerArmEndY; } ellipse(fruit.x, fruit.y, 20, 20); // Victory condition let headX = 200; // X-coordinate of the head's center let headY = 100; // Y-coordinate of the head's center let distanceToHead = dist(fruit.x, fruit.y, headX, headY); if (distanceToHead <= 25) { // Close enough to the head fill(0, 255, 0); textSize(32); text("Victory!", 100, 200); noLoop(); // Stop drawing console.log(`Victory in ${moves} moves!`); } // Display distances and instructions displayInfo(); } function keyPressed() { const angleIncrement = PI / 36; if (key === 'q') { upperArmAngle -= angleIncrement; moves++; } if (key === 'w') { upperArmAngle += angleIncrement; moves++; } if (key === 'a') { lowerArmAngle -= angleIncrement; moves++; } if (key === 's') { lowerArmAngle += angleIncrement; moves++; } if (key === 'g') grab(); // Grab the fruit if (key === 'r') release(); // Release the fruit } function grab() { let handTipX = 200 + 50 * cos(upperArmAngle) + 40 * cos(lowerArmAngle + upperArmAngle); let handTipY = 150 + 50 * sin(upperArmAngle) + 40 * sin(lowerArmAngle + upperArmAngle); let distanceToFruit = dist(handTipX, handTipY, fruit.x, fruit.y); if (distanceToFruit < 10) { // Close enough to grab grabbed = true; moves++; } } function release() { grabbed = false; moves++; } function displayInfo() { fill(0); textSize(16); text("Use q/w to rotate upper (stage) right arm", 10, 340) text("a/s to rotate lower (stage) right arm", 10, 360) text("g to grab, r to release", 10, 380); text(`Moves: ${moves}`, 10, 40); } ---- screap 2: // paste in here: // https://p5js.org/examples/interaction-reach-1.html let fruit; let upperArmAngle = 0; let lowerArmAngle = 0; let grabbed = false; // Whether the fruit is grabbed let moves = 0; // Track the number of moves function setup() { createCanvas(400, 400); fruit = createVector(220 + random(-10, 10), 200 + random(-10, 10)); // Adjust position and add randomness let btn = createButton('Click Me to Start'); // Position the button btn.position(0, 0); // Assign a function to call when the button is clicked btn.mousePressed(onButtonPressed); } function onButtonPressed() { console.log("Button was pressed."); let epochs = 5; let currentEpoch = 0; // Function to reset to starting conditions function reset() { upperArmAngle = 0; lowerArmAngle = 0; grabbed = false; moves = 0; fruit = createVector(220 + random(-10, 10), 200 + random(-10, 10)); } // Function to perform a random movement function randomMove() { const moveChoices = ['q', 'w', 'a', 's']; // Renamed from 'moves' to 'moveChoices' to avoid conflict const randomIndex = floor(random(0, moveChoices.length)); const move = moveChoices[randomIndex]; if (move === 'q') { upperArmAngle -= PI / 36; } if (move === 'w') { upperArmAngle += PI / 36; } if (move === 'a') { lowerArmAngle -= PI / 36; } if (move === 's') { lowerArmAngle += PI / 36; } moves++; // This now correctly refers to the global 'moves' variable } // Function to run an epoch function runEpoch() { // Add random movements here to demonstrate the arm moving randomMove(); // Check and log the distance to head for demonstration const headX = 200; const headY = 100; const distanceToHead = dist(fruit.x, fruit.y, headX, headY); console.log(`Distance to head: ${distanceToHead}`); // Reset if epoch is finished if (currentEpoch < epochs) { reset(); currentEpoch++; console.log(`Epoch ${currentEpoch}/${epochs}`); } else { console.log("Training complete"); clearInterval(interval); } } // Start the interval to run epochs const interval = setInterval(runEpoch, 1000); // Run an epoch every second } function draw() { background(220); // Head ellipse(200, 100, 50, 50); // Head // Torso line(200, 125, 200, 250); // Torso // Left arm (static) line(200, 150, 150, 180); // Upper arm line(150, 180, 110, 160); // Forearm // Left leg (static) line(200, 250, 170, 300); // Thigh line(170, 300, 170, 350); // Shin // Right leg (static) line(200, 250, 230, 300); // Thigh line(230, 300, 230, 350); // Shin // Right arm (movable) let upperArmEndX = 200 + 50 * cos(upperArmAngle); let upperArmEndY = 150 + 50 * sin(upperArmAngle); line(200, 150, upperArmEndX, upperArmEndY); // Upper arm let lowerArmEndX = upperArmEndX + 40 * cos(lowerArmAngle + upperArmAngle); let lowerArmEndY = upperArmEndY + 40 * sin(lowerArmAngle + upperArmAngle); line(upperArmEndX, upperArmEndY, lowerArmEndX, lowerArmEndY); // Forearm // Draw the fruit fill(255, 0, 0); if (grabbed) { fruit.x = lowerArmEndX; fruit.y = lowerArmEndY; } ellipse(fruit.x, fruit.y, 20, 20); // Victory condition let headX = 200; // X-coordinate of the head's center let headY = 100; // Y-coordinate of the head's center let distanceToHead = dist(fruit.x, fruit.y, headX, headY); if (distanceToHead <= 25) { // Close enough to the head fill(0, 255, 0); textSize(32); text("Victory!", 100, 200); noLoop(); // Stop drawing console.log(`Victory in ${moves} moves!`); } // Display distances and instructions displayInfo(); } function keyPressed() { const angleIncrement = PI / 36; if (key === 'q') { upperArmAngle -= angleIncrement; moves++; } if (key === 'w') { upperArmAngle += angleIncrement; moves++; } if (key === 'a') { lowerArmAngle -= angleIncrement; moves++; } if (key === 's') { lowerArmAngle += angleIncrement; moves++; } if (key === 'g') grab(); // Grab the fruit if (key === 'r') release(); // Release the fruit } function grab() { let handTipX = 200 + 50 * cos(upperArmAngle) + 40 * cos(lowerArmAngle + upperArmAngle); let handTipY = 150 + 50 * sin(upperArmAngle) + 40 * sin(lowerArmAngle + upperArmAngle); let distanceToFruit = dist(handTipX, handTipY, fruit.x, fruit.y); if (distanceToFruit < 10) { // Close enough to grab grabbed = true; moves++; } } function release() { grabbed = false; moves++; } function displayInfo() { fill(0); textSize(16); text("Use q/w to rotate upper (stage) right arm", 10, 340) text("a/s to rotate lower (stage) right arm", 10, 360) text("g to grab, r to release", 10, 380); text(`Moves: ${moves}`, 10, 40); } ----- scrap 3: // paste in here: // https://p5js.org/examples/interaction-reach-1.html let fruit; let upperArmAngle = 0; let lowerArmAngle = 0; let grabbed = false; // Whether the fruit is grabbed let moves = 0; // Track the number of moves function setup() { createCanvas(400, 400); fruit = createVector(220 + random(-10, 10), 200 + random(-10, 10)); // Adjust position and add randomness let btn = createButton('Click Me to Start'); // Position the button btn.position(0, 0); // Assign a function to call when the button is clicked btn.mousePressed(onButtonPressed); } function onButtonPressed() { console.log("Button was pressed."); let epochs = 5; let currentEpoch = 0; // Function to reset to starting conditions function reset() { upperArmAngle = 0; lowerArmAngle = 0; grabbed = false; moves = 0; fruit = createVector(220 + random(-10, 10), 200 + random(-10, 10)); } // Function to perform a random movement function randomMove() { const moveChoices = ['q', 'w', 'a', 's']; // Renamed from 'moves' to 'moveChoices' to avoid conflict const randomIndex = floor(random(0, moveChoices.length)); const move = moveChoices[randomIndex]; if (move === 'q') { upperArmAngle -= PI / 36; } if (move === 'w') { upperArmAngle += PI / 36; } if (move === 'a') { lowerArmAngle -= PI / 36; } if (move === 's') { lowerArmAngle += PI / 36; } moves++; // This now correctly refers to the global 'moves' variable } // Function to run an epoch function runEpoch() { // Add random movements here to demonstrate the arm moving randomMove(); // Check and log the distance to head for demonstration const headX = 200; const headY = 100; const distanceToHead = dist(fruit.x, fruit.y, headX, headY); console.log(`Distance to head: ${distanceToHead}`); // Reset if epoch is finished if (currentEpoch < epochs) { reset(); currentEpoch++; console.log(`Epoch ${currentEpoch}/${epochs}`); } else { console.log("Training complete"); clearInterval(interval); } } // Start the interval to run epochs const interval = setInterval(runEpoch, 1000); // Run an epoch every second } function draw() { background(220); // Head ellipse(200, 100, 50, 50); // Head // Torso line(200, 125, 200, 250); // Torso // Left arm (static) line(200, 150, 150, 180); // Upper arm line(150, 180, 110, 160); // Forearm // Left leg (static) line(200, 250, 170, 300); // Thigh line(170, 300, 170, 350); // Shin // Right leg (static) line(200, 250, 230, 300); // Thigh line(230, 300, 230, 350); // Shin // Right arm (movable) let upperArmEndX = 200 + 50 * cos(upperArmAngle); let upperArmEndY = 150 + 50 * sin(upperArmAngle); line(200, 150, upperArmEndX, upperArmEndY); // Upper arm let lowerArmEndX = upperArmEndX + 40 * cos(lowerArmAngle + upperArmAngle); let lowerArmEndY = upperArmEndY + 40 * sin(lowerArmAngle + upperArmAngle); line(upperArmEndX, upperArmEndY, lowerArmEndX, lowerArmEndY); // Forearm // Draw the fruit fill(255, 0, 0); if (grabbed) { fruit.x = lowerArmEndX; fruit.y = lowerArmEndY; } ellipse(fruit.x, fruit.y, 20, 20); // Victory condition let headX = 200; // X-coordinate of the head's center let headY = 100; // Y-coordinate of the head's center let distanceToHead = dist(fruit.x, fruit.y, headX, headY); if (distanceToHead <= 25) { // Close enough to the head fill(0, 255, 0); textSize(32); text("Victory!", 100, 200); noLoop(); // Stop drawing console.log(`Victory in ${moves} moves!`); } // Display distances and instructions displayInfo(); } function keyPressed() { const angleIncrement = PI / 36; if (key === 'q') { upperArmAngle -= angleIncrement; moves++; } if (key === 'w') { upperArmAngle += angleIncrement; moves++; } if (key === 'a') { lowerArmAngle -= angleIncrement; moves++; } if (key === 's') { lowerArmAngle += angleIncrement; moves++; } if (key === 'g') grab(); // Grab the fruit if (key === 'r') release(); // Release the fruit } function grab() { let handTipX = 200 + 50 * cos(upperArmAngle) + 40 * cos(lowerArmAngle + upperArmAngle); let handTipY = 150 + 50 * sin(upperArmAngle) + 40 * sin(lowerArmAngle + upperArmAngle); let distanceToFruit = dist(handTipX, handTipY, fruit.x, fruit.y); if (distanceToFruit < 10) { // Close enough to grab grabbed = true; moves++; } } function release() { grabbed = false; moves++; } function displayInfo() { fill(0); textSize(16); text("Use q/w to rotate upper (stage) right arm", 10, 340) text("a/s to rotate lower (stage) right arm", 10, 360) text("g to grab, r to release", 10, 380); text(`Moves: ${moves}`, 10, 40); } ------ scrap 4: REAL NEURAL NETWORK TRAINING DATA: // paste in here: // https://p5js.org/examples/interaction-reach-1.html let fruit; let upperArmAngle = 0; let lowerArmAngle = 0; let grabbed = false; // Whether the fruit is grabbed let moves = 0; // Track the number of moves function setup() { createCanvas(400, 400); fruit = createVector(220 + random(-10, 10), 200 + random(-10, 10)); // Adjust position and add randomness let btn = createButton('Click Me to Start'); // Position the button btn.position(0, 0); // Assign a function to call when the button is clicked btn.mousePressed(onButtonPressed); } function onButtonPressed() { console.log("Button was pressed."); // Simplified Neural Network weights let inputToHiddenWeights = [Array(2).fill().map(() => Math.random() - 0.5), Array(2).fill().map(() => Math.random() - 0.5)]; let hiddenToOutputWeights = [Math.random() - 0.5, Math.random() - 0.5]; let epochs = 5; let currentEpoch = 0; function reset() { upperArmAngle = 0; lowerArmAngle = 0; grabbed = false; moves = 0; fruit = createVector(220 + random(-10, 10), 200 + random(-10, 10)); } // Apply neural network to generate arm movement function applyNN() { let input = [fruit.x, fruit.y]; let hidden = inputToHiddenWeights.map(weights => weights[0] * input[0] + weights[1] * input[1]); let output = hidden.map(h => h * hiddenToOutputWeights[0] + h * hiddenToOutputWeights[1]); // Use outputs to adjust arm angles upperArmAngle += output[0]; lowerArmAngle += output[1]; moves++; } function runEpoch() { reset(); for (let step = 0; step < 100; step++) { // Simulate steps within an epoch applyNN(); } currentEpoch++; if (currentEpoch >= epochs) { console.log("Training complete"); clearInterval(interval); } } const interval = setInterval(runEpoch, 1000); // Run an epoch every second } function draw() { background(220); // Head ellipse(200, 100, 50, 50); // Head // Torso line(200, 125, 200, 250); // Torso // Left arm (static) line(200, 150, 150, 180); // Upper arm line(150, 180, 110, 160); // Forearm // Left leg (static) line(200, 250, 170, 300); // Thigh line(170, 300, 170, 350); // Shin // Right leg (static) line(200, 250, 230, 300); // Thigh line(230, 300, 230, 350); // Shin // Right arm (movable) let upperArmEndX = 200 + 50 * cos(upperArmAngle); let upperArmEndY = 150 + 50 * sin(upperArmAngle); line(200, 150, upperArmEndX, upperArmEndY); // Upper arm let lowerArmEndX = upperArmEndX + 40 * cos(lowerArmAngle + upperArmAngle); let lowerArmEndY = upperArmEndY + 40 * sin(lowerArmAngle + upperArmAngle); line(upperArmEndX, upperArmEndY, lowerArmEndX, lowerArmEndY); // Forearm // Draw the fruit fill(255, 0, 0); if (grabbed) { fruit.x = lowerArmEndX; fruit.y = lowerArmEndY; } ellipse(fruit.x, fruit.y, 20, 20); // Victory condition let headX = 200; // X-coordinate of the head's center let headY = 100; // Y-coordinate of the head's center let distanceToHead = dist(fruit.x, fruit.y, headX, headY); if (distanceToHead <= 25) { // Close enough to the head fill(0, 255, 0); textSize(32); text("Victory!", 100, 200); noLoop(); // Stop drawing console.log(`Victory in ${moves} moves!`); } // Display distances and instructions displayInfo(); } function keyPressed() { const angleIncrement = PI / 36; if (key === 'q') { upperArmAngle -= angleIncrement; moves++; } if (key === 'w') { upperArmAngle += angleIncrement; moves++; } if (key === 'a') { lowerArmAngle -= angleIncrement; moves++; } if (key === 's') { lowerArmAngle += angleIncrement; moves++; } if (key === 'g') grab(); // Grab the fruit if (key === 'r') release(); // Release the fruit } function grab() { let handTipX = 200 + 50 * cos(upperArmAngle) + 40 * cos(lowerArmAngle + upperArmAngle); let handTipY = 150 + 50 * sin(upperArmAngle) + 40 * sin(lowerArmAngle + upperArmAngle); let distanceToFruit = dist(handTipX, handTipY, fruit.x, fruit.y); if (distanceToFruit < 10) { // Close enough to grab grabbed = true; moves++; } } function release() { grabbed = false; moves++; } function displayInfo() { fill(0); textSize(16); text("Use q/w to rotate upper (stage) right arm", 10, 340) text("a/s to rotate lower (stage) right arm", 10, 360) text("g to grab, r to release", 10, 380); text(`Moves: ${moves}`, 10, 40); } ------ scrap 5: not sure what this one did: // paste in here: // https://p5js.org/examples/interaction-reach-1.html let fruit; let upperArmAngle = 0; let lowerArmAngle = 0; let grabbed = false; // Whether the fruit is grabbed let moves = 0; // Track the number of moves function setup() { createCanvas(400, 400); fruit = createVector(220 + random(-10, 10), 200 + random(-10, 10)); // Adjust position and add randomness let btn = createButton('Click Me to Start'); // Position the button btn.position(0, 0); // Assign a function to call when the button is clicked btn.mousePressed(onButtonPressed); } function onButtonPressed() { console.log("Button was pressed."); // Simplified Neural Network weights let inputToHiddenWeights = [Array(2).fill().map(() => Math.random() - 0.5), Array(2).fill().map(() => Math.random() - 0.5)]; let hiddenToOutputWeights = [Math.random() - 0.5, Math.random() - 0.5]; let epochs = 5; let currentEpoch = 0; function reset() { upperArmAngle = 0; lowerArmAngle = 0; grabbed = false; moves = 0; fruit = createVector(220 + random(-10, 10), 200 + random(-10, 10)); } // Apply neural network to generate arm movement function applyNN() { let input = [fruit.x, fruit.y]; let hidden = inputToHiddenWeights.map(weights => weights[0] * input[0] + weights[1] * input[1]); let output = hidden.map(h => h * hiddenToOutputWeights[0] + h * hiddenToOutputWeights[1]); // Use outputs to adjust arm angles upperArmAngle += output[0]; lowerArmAngle += output[1]; moves++; } function runEpoch() { reset(); for (let step = 0; step < 100; step++) { // Simulate steps within an epoch applyNN(); } currentEpoch++; if (currentEpoch >= epochs) { console.log("Training complete"); clearInterval(interval); } } const interval = setInterval(runEpoch, 1000); // Run an epoch every second } function draw() { background(220); // Head ellipse(200, 100, 50, 50); // Head // Torso line(200, 125, 200, 250); // Torso // Left arm (static) line(200, 150, 150, 180); // Upper arm line(150, 180, 110, 160); // Forearm // Left leg (static) line(200, 250, 170, 300); // Thigh line(170, 300, 170, 350); // Shin // Right leg (static) line(200, 250, 230, 300); // Thigh line(230, 300, 230, 350); // Shin // Right arm (movable) let upperArmEndX = 200 + 50 * cos(upperArmAngle); let upperArmEndY = 150 + 50 * sin(upperArmAngle); line(200, 150, upperArmEndX, upperArmEndY); // Upper arm let lowerArmEndX = upperArmEndX + 40 * cos(lowerArmAngle + upperArmAngle); let lowerArmEndY = upperArmEndY + 40 * sin(lowerArmAngle + upperArmAngle); line(upperArmEndX, upperArmEndY, lowerArmEndX, lowerArmEndY); // Forearm // Draw the fruit fill(255, 0, 0); if (grabbed) { fruit.x = lowerArmEndX; fruit.y = lowerArmEndY; } ellipse(fruit.x, fruit.y, 20, 20); // Victory condition let headX = 200; // X-coordinate of the head's center let headY = 100; // Y-coordinate of the head's center let distanceToHead = dist(fruit.x, fruit.y, headX, headY); if (distanceToHead <= 25) { // Close enough to the head fill(0, 255, 0); textSize(32); text("Victory!", 100, 200); noLoop(); // Stop drawing console.log(`Victory in ${moves} moves!`); } // Display distances and instructions displayInfo(); } function keyPressed() { const angleIncrement = PI / 36; if (key === 'q') { upperArmAngle -= angleIncrement; moves++; } if (key === 'w') { upperArmAngle += angleIncrement; moves++; } if (key === 'a') { lowerArmAngle -= angleIncrement; moves++; } if (key === 's') { lowerArmAngle += angleIncrement; moves++; } if (key === 'g') grab(); // Grab the fruit if (key === 'r') release(); // Release the fruit } function grab() { let handTipX = 200 + 50 * cos(upperArmAngle) + 40 * cos(lowerArmAngle + upperArmAngle); let handTipY = 150 + 50 * sin(upperArmAngle) + 40 * sin(lowerArmAngle + upperArmAngle); let distanceToFruit = dist(handTipX, handTipY, fruit.x, fruit.y); if (distanceToFruit < 10) { // Close enough to grab grabbed = true; moves++; } } function release() { grabbed = false; moves++; } function displayInfo() { fill(0); textSize(16); text("Use q/w to rotate upper (stage) right arm", 10, 340) text("a/s to rotate lower (stage) right arm", 10, 360) text("g to grab, r to release", 10, 380); text(`Moves: ${moves}`, 10, 40); } ---- scrap 6 // paste in here: // https://p5js.org/examples/interaction-reach-1.html let fruit; let upperArmAngle = 0; let lowerArmAngle = 0; let grabbed = false; // Whether the fruit is grabbed let moves = 0; // Track the number of moves function setup() { createCanvas(400, 400); fruit = createVector(220 + random(-10, 10), 200 + random(-10, 10)); // Adjust position and add randomness let btn = createButton('Click Me to Start'); // Position the button btn.position(0, 0); // Assign a function to call when the button is clicked btn.mousePressed(onButtonPressed); } function onButtonPressed() { console.log("Button was pressed."); // Simplified Neural Network weights let inputToHiddenWeights = [Array(2).fill().map(() => Math.random() - 0.5), Array(2).fill().map(() => Math.random() - 0.5)]; let hiddenToOutputWeights = [Math.random() - 0.5, Math.random() - 0.5]; let epochs = 5; let currentEpoch = 0; function reset() { upperArmAngle = 0; lowerArmAngle = 0; grabbed = false; moves = 0; fruit = createVector(220 + random(-10, 10), 200 + random(-10, 10)); } // Apply neural network to generate arm movement function applyNN() { let input = [fruit.x, fruit.y]; let hidden = inputToHiddenWeights.map(weights => weights[0] * input[0] + weights[1] * input[1]); let output = hidden.map(h => h * hiddenToOutputWeights[0] + h * hiddenToOutputWeights[1]); // Use outputs to adjust arm angles upperArmAngle += output[0]; lowerArmAngle += output[1]; moves++; } function runEpoch() { reset(); for (let step = 0; step < 100; step++) { // Simulate steps within an epoch applyNN(); } currentEpoch++; if (currentEpoch >= epochs) { console.log("Training complete"); clearInterval(interval); } } const interval = setInterval(runEpoch, 1000); // Run an epoch every second } function draw() { background(220); // Head ellipse(200, 100, 50, 50); // Head // Torso line(200, 125, 200, 250); // Torso // Left arm (static) line(200, 150, 150, 180); // Upper arm line(150, 180, 110, 160); // Forearm // Left leg (static) line(200, 250, 170, 300); // Thigh line(170, 300, 170, 350); // Shin // Right leg (static) line(200, 250, 230, 300); // Thigh line(230, 300, 230, 350); // Shin // Right arm (movable) let upperArmEndX = 200 + 50 * cos(upperArmAngle); let upperArmEndY = 150 + 50 * sin(upperArmAngle); line(200, 150, upperArmEndX, upperArmEndY); // Upper arm let lowerArmEndX = upperArmEndX + 40 * cos(lowerArmAngle + upperArmAngle); let lowerArmEndY = upperArmEndY + 40 * sin(lowerArmAngle + upperArmAngle); line(upperArmEndX, upperArmEndY, lowerArmEndX, lowerArmEndY); // Forearm // Draw the fruit fill(255, 0, 0); if (grabbed) { fruit.x = lowerArmEndX; fruit.y = lowerArmEndY; } ellipse(fruit.x, fruit.y, 20, 20); // Victory condition let headX = 200; // X-coordinate of the head's center let headY = 100; // Y-coordinate of the head's center let distanceToHead = dist(fruit.x, fruit.y, headX, headY); if (distanceToHead <= 25) { // Close enough to the head fill(0, 255, 0); textSize(32); text("Victory!", 100, 200); noLoop(); // Stop drawing console.log(`Victory in ${moves} moves!`); } // Display distances and instructions displayInfo(); } function keyPressed() { const angleIncrement = PI / 36; if (key === 'q') { upperArmAngle -= angleIncrement; moves++; } if (key === 'w') { upperArmAngle += angleIncrement; moves++; } if (key === 'a') { lowerArmAngle -= angleIncrement; moves++; } if (key === 's') { lowerArmAngle += angleIncrement; moves++; } if (key === 'g') grab(); // Grab the fruit if (key === 'r') release(); // Release the fruit } function grab() { let handTipX = 200 + 50 * cos(upperArmAngle) + 40 * cos(lowerArmAngle + upperArmAngle); let handTipY = 150 + 50 * sin(upperArmAngle) + 40 * sin(lowerArmAngle + upperArmAngle); let distanceToFruit = dist(handTipX, handTipY, fruit.x, fruit.y); if (distanceToFruit < 10) { // Close enough to grab grabbed = true; moves++; } } function release() { grabbed = false; moves++; } function displayInfo() { fill(0); textSize(16); text("Use q/w to rotate upper (stage) right arm", 10, 340) text("a/s to rotate lower (stage) right arm", 10, 360) text("g to grab, r to release", 10, 380); text(`Moves: ${moves}`, 10, 40); } ----- scrap 7: // paste in here: // https://p5js.org/examples/interaction-reach-1.html let fruit; let upperArmAngle = 0; let lowerArmAngle = 0; let grabbed = false; // Whether the fruit is grabbed let moves = 0; // Track the number of moves function setup() { createCanvas(400, 400); fruit = createVector(220 + random(-10, 10), 200 + random(-10, 10)); // Adjust position and add randomness let btn = createButton('Click Me to Start'); // Position the button btn.position(0, 0); // Assign a function to call when the button is clicked btn.mousePressed(onButtonPressed); } function onButtonPressed() { console.log("Button was pressed."); // Simplified Neural Network weights let inputToHiddenWeights = [Array(2).fill().map(() => Math.random() - 0.5), Array(2).fill().map(() => Math.random() - 0.5)]; let hiddenToOutputWeights = [Math.random() - 0.5, Math.random() - 0.5]; let epochs = 5; let currentEpoch = 0; function reset() { upperArmAngle = 0; lowerArmAngle = 0; grabbed = false; moves = 0; fruit = createVector(220 + random(-10, 10), 200 + random(-10, 10)); } // Apply neural network to generate arm movement function applyNN() { let input = [fruit.x, fruit.y]; let hidden = inputToHiddenWeights.map(weights => weights[0] * input[0] + weights[1] * input[1]); let output = hidden.map(h => h * hiddenToOutputWeights[0] + h * hiddenToOutputWeights[1]); // Use outputs to adjust arm angles upperArmAngle += output[0]; lowerArmAngle += output[1]; moves++; } function runEpoch() { reset(); for (let step = 0; step < 100; step++) { // Simulate steps within an epoch applyNN(); } currentEpoch++; if (currentEpoch >= epochs) { console.log("Training complete"); // Convert weights to a string representation const weightsStr = JSON.stringify({inputToHiddenWeights, hiddenToOutputWeights}); // Copy the stringified weights to the clipboard console.log(weightsStr); clearInterval(interval); } } const interval = setInterval(runEpoch, 1000); // Run an epoch every second } function draw() { background(220); // Head ellipse(200, 100, 50, 50); // Head // Torso line(200, 125, 200, 250); // Torso // Left arm (static) line(200, 150, 150, 180); // Upper arm line(150, 180, 110, 160); // Forearm // Left leg (static) line(200, 250, 170, 300); // Thigh line(170, 300, 170, 350); // Shin // Right leg (static) line(200, 250, 230, 300); // Thigh line(230, 300, 230, 350); // Shin // Right arm (movable) let upperArmEndX = 200 + 50 * cos(upperArmAngle); let upperArmEndY = 150 + 50 * sin(upperArmAngle); line(200, 150, upperArmEndX, upperArmEndY); // Upper arm let lowerArmEndX = upperArmEndX + 40 * cos(lowerArmAngle + upperArmAngle); let lowerArmEndY = upperArmEndY + 40 * sin(lowerArmAngle + upperArmAngle); line(upperArmEndX, upperArmEndY, lowerArmEndX, lowerArmEndY); // Forearm // Draw the fruit fill(255, 0, 0); if (grabbed) { fruit.x = lowerArmEndX; fruit.y = lowerArmEndY; } ellipse(fruit.x, fruit.y, 20, 20); // Victory condition let headX = 200; // X-coordinate of the head's center let headY = 100; // Y-coordinate of the head's center let distanceToHead = dist(fruit.x, fruit.y, headX, headY); if (distanceToHead <= 25) { // Close enough to the head fill(0, 255, 0); textSize(32); text("Victory!", 100, 200); noLoop(); // Stop drawing console.log(`Victory in ${moves} moves!`); } // Display distances and instructions displayInfo(); } function keyPressed() { const angleIncrement = PI / 36; if (key === 'q') { upperArmAngle -= angleIncrement; moves++; } if (key === 'w') { upperArmAngle += angleIncrement; moves++; } if (key === 'a') { lowerArmAngle -= angleIncrement; moves++; } if (key === 's') { lowerArmAngle += angleIncrement; moves++; } if (key === 'g') grab(); // Grab the fruit if (key === 'r') release(); // Release the fruit } function grab() { let handTipX = 200 + 50 * cos(upperArmAngle) + 40 * cos(lowerArmAngle + upperArmAngle); let handTipY = 150 + 50 * sin(upperArmAngle) + 40 * sin(lowerArmAngle + upperArmAngle); let distanceToFruit = dist(handTipX, handTipY, fruit.x, fruit.y); if (distanceToFruit < 10) { // Close enough to grab grabbed = true; moves++; } } function release() { grabbed = false; moves++; } function displayInfo() { fill(0); textSize(16); text("Use q/w to rotate upper (stage) right arm", 10, 340) text("a/s to rotate lower (stage) right arm", 10, 360) text("g to grab, r to release", 10, 380); text(`Moves: ${moves}`, 10, 40); } ----- scrap 8: // paste in here: // https://p5js.org/examples/interaction-reach-1.html let fruit; let upperArmAngle = 0; let lowerArmAngle = 0; let grabbed = false; // Whether the fruit is grabbed let moves = 0; // Track the number of moves function setup() { createCanvas(400, 400); fruit = createVector(220 + random(-10, 10), 200 + random(-10, 10)); // Adjust position and add randomness let btn = createButton('Click Me to Start'); // Position the button btn.position(0, 0); // Assign a function to call when the button is clicked btn.mousePressed(onButtonPressed); } function onButtonPressed() { console.log("Button was pressed."); // Simplified Neural Network weights let inputToHiddenWeights = [Array(2).fill().map(() => Math.random() - 0.5), Array(2).fill().map(() => Math.random() - 0.5)]; let hiddenToOutputWeights = [Math.random() - 0.5, Math.random() - 0.5]; let epochs = 5; let currentEpoch = 0; function reset() { upperArmAngle = 0; lowerArmAngle = 0; grabbed = false; moves = 0; fruit = createVector(220 + random(-10, 10), 200 + random(-10, 10)); } // Apply neural network to generate arm movement function applyNN() { let input = [fruit.x, fruit.y]; let hidden = inputToHiddenWeights.map(weights => weights[0] * input[0] + weights[1] * input[1]); let output = hidden.map(h => h * hiddenToOutputWeights[0] + h * hiddenToOutputWeights[1]); // Use outputs to adjust arm angles upperArmAngle += output[0]; lowerArmAngle += output[1]; moves++; } function runEpoch() { reset(); for (let step = 0; step < 100; step++) { // Simulate steps within an epoch applyNN(); } currentEpoch++; if (currentEpoch >= epochs) { console.log("Training complete"); // Convert weights to a string representation const weightsStr = JSON.stringify({inputToHiddenWeights, hiddenToOutputWeights}); // Copy the stringified weights to the clipboard console.log(weightsStr); clearInterval(interval); } } const interval = setInterval(runEpoch, 1000); // Run an epoch every second } function draw() { background(220); // Head ellipse(200, 100, 50, 50); // Head // Torso line(200, 125, 200, 250); // Torso // Left arm (static) line(200, 150, 150, 180); // Upper arm line(150, 180, 110, 160); // Forearm // Left leg (static) line(200, 250, 170, 300); // Thigh line(170, 300, 170, 350); // Shin // Right leg (static) line(200, 250, 230, 300); // Thigh line(230, 300, 230, 350); // Shin // Right arm (movable) let upperArmEndX = 200 + 50 * cos(upperArmAngle); let upperArmEndY = 150 + 50 * sin(upperArmAngle); line(200, 150, upperArmEndX, upperArmEndY); // Upper arm let lowerArmEndX = upperArmEndX + 40 * cos(lowerArmAngle + upperArmAngle); let lowerArmEndY = upperArmEndY + 40 * sin(lowerArmAngle + upperArmAngle); line(upperArmEndX, upperArmEndY, lowerArmEndX, lowerArmEndY); // Forearm // Draw the fruit fill(255, 0, 0); if (grabbed) { fruit.x = lowerArmEndX; fruit.y = lowerArmEndY; } ellipse(fruit.x, fruit.y, 20, 20); // Victory condition let headX = 200; // X-coordinate of the head's center let headY = 100; // Y-coordinate of the head's center let distanceToHead = dist(fruit.x, fruit.y, headX, headY); if (distanceToHead <= 25) { // Close enough to the head fill(0, 255, 0); textSize(32); text("Victory!", 100, 200); noLoop(); // Stop drawing console.log(`Victory in ${moves} moves!`); } // Display distances and instructions displayInfo(); } function keyPressed() { const angleIncrement = PI / 36; if (key === 'q') { upperArmAngle -= angleIncrement; moves++; } if (key === 'w') { upperArmAngle += angleIncrement; moves++; } if (key === 'a') { lowerArmAngle -= angleIncrement; moves++; } if (key === 's') { lowerArmAngle += angleIncrement; moves++; } if (key === 'g') grab(); // Grab the fruit if (key === 'r') release(); // Release the fruit } function grab() { let handTipX = 200 + 50 * cos(upperArmAngle) + 40 * cos(lowerArmAngle + upperArmAngle); let handTipY = 150 + 50 * sin(upperArmAngle) + 40 * sin(lowerArmAngle + upperArmAngle); let distanceToFruit = dist(handTipX, handTipY, fruit.x, fruit.y); if (distanceToFruit < 10) { // Close enough to grab grabbed = true; moves++; } } function release() { grabbed = false; moves++; } function displayInfo() { fill(0); textSize(16); text("Use q/w to rotate upper (stage) right arm", 10, 340) text("a/s to rotate lower (stage) right arm", 10, 360) text("g to grab, r to release", 10, 380); text(`Moves: ${moves}`, 10, 40); } ----- large number of epochs and steps: // paste in here: // https://p5js.org/examples/interaction-reach-1.html let fruit; let upperArmAngle = 0; let lowerArmAngle = 0; let grabbed = false; // Whether the fruit is grabbed let moves = 0; // Track the number of moves function setup() { createCanvas(400, 400); fruit = createVector(220 + random(-10, 10), 200 + random(-10, 10)); // Adjust position and add randomness let btn = createButton('Click Me to Start'); // Position the button btn.position(0, 0); // Assign a function to call when the button is clicked btn.mousePressed(onButtonPressed); } function onButtonPressed() { let num_steps = 10000; console.log("Button was pressed."); // Simplified Neural Network weights let inputToHiddenWeights = [Array(2).fill().map(() => Math.random() - 0.5), Array(2).fill().map(() => Math.random() - 0.5)]; let hiddenToOutputWeights = [Math.random() - 0.5, Math.random() - 0.5]; let epochs = 10000; let currentEpoch = 0; function reset() { upperArmAngle = 0; lowerArmAngle = 0; grabbed = false; moves = 0; fruit = createVector(220 + random(-10, 10), 200 + random(-10, 10)); } // Apply neural network to generate arm movement function applyNN() { let input = [fruit.x, fruit.y]; let hidden = inputToHiddenWeights.map(weights => weights[0] * input[0] + weights[1] * input[1]); let output = hidden.map(h => h * hiddenToOutputWeights[0] + h * hiddenToOutputWeights[1]); // Use outputs to adjust arm angles upperArmAngle += output[0]; lowerArmAngle += output[1]; moves++; } function runEpoch() { reset(); for (let step = 0; step < num_steps; step++) { // Simulate steps within an epoch applyNN(); } currentEpoch++; console.log("Running epoch", currentEpoch, " of ", epochs) if (currentEpoch >= epochs) { console.log("Training complete"); // Convert weights to a string representation const weightsStr = JSON.stringify({inputToHiddenWeights, hiddenToOutputWeights}); // Copy the stringified weights to the clipboard console.log(weightsStr); clearInterval(interval); } } const interval = setInterval(runEpoch, 3000); // Run an epoch every three seconds } function draw() { background(220); // Head ellipse(200, 100, 50, 50); // Head // Torso line(200, 125, 200, 250); // Torso // Left arm (static) line(200, 150, 150, 180); // Upper arm line(150, 180, 110, 160); // Forearm // Left leg (static) line(200, 250, 170, 300); // Thigh line(170, 300, 170, 350); // Shin // Right leg (static) line(200, 250, 230, 300); // Thigh line(230, 300, 230, 350); // Shin // Right arm (movable) let upperArmEndX = 200 + 50 * cos(upperArmAngle); let upperArmEndY = 150 + 50 * sin(upperArmAngle); line(200, 150, upperArmEndX, upperArmEndY); // Upper arm let lowerArmEndX = upperArmEndX + 40 * cos(lowerArmAngle + upperArmAngle); let lowerArmEndY = upperArmEndY + 40 * sin(lowerArmAngle + upperArmAngle); line(upperArmEndX, upperArmEndY, lowerArmEndX, lowerArmEndY); // Forearm // Draw the fruit fill(255, 0, 0); if (grabbed) { fruit.x = lowerArmEndX; fruit.y = lowerArmEndY; } ellipse(fruit.x, fruit.y, 20, 20); // Victory condition let headX = 200; // X-coordinate of the head's center let headY = 100; // Y-coordinate of the head's center let distanceToHead = dist(fruit.x, fruit.y, headX, headY); if (distanceToHead <= 25) { // Close enough to the head fill(0, 255, 0); textSize(32); text("Victory!", 100, 200); noLoop(); // Stop drawing console.log(`Victory in ${moves} moves!`); } // Display distances and instructions displayInfo(); } function keyPressed() { const angleIncrement = PI / 36; if (key === 'q') { upperArmAngle -= angleIncrement; moves++; } if (key === 'w') { upperArmAngle += angleIncrement; moves++; } if (key === 'a') { lowerArmAngle -= angleIncrement; moves++; } if (key === 's') { lowerArmAngle += angleIncrement; moves++; } if (key === 'g') grab(); // Grab the fruit if (key === 'r') release(); // Release the fruit } function grab() { let handTipX = 200 + 50 * cos(upperArmAngle) + 40 * cos(lowerArmAngle + upperArmAngle); let handTipY = 150 + 50 * sin(upperArmAngle) + 40 * sin(lowerArmAngle + upperArmAngle); let distanceToFruit = dist(handTipX, handTipY, fruit.x, fruit.y); if (distanceToFruit < 10) { // Close enough to grab grabbed = true; moves++; } } function release() { grabbed = false; moves++; } function displayInfo() { fill(0); textSize(16); text("Use q/w to rotate upper (stage) right arm", 10, 340) text("a/s to rotate lower (stage) right arm", 10, 360) text("g to grab, r to release", 10, 380); text(`Moves: ${moves}`, 10, 40); } ----- 500 epoch training, but no code to grab the fruit: // paste in here: // https://p5js.org/examples/interaction-reach-1.html let fruit; let upperArmAngle = 0; let lowerArmAngle = 0; let grabbed = false; // Whether the fruit is grabbed let moves = 0; // Track the number of moves function setup() { createCanvas(400, 400); fruit = createVector(220 + random(-10, 10), 200 + random(-10, 10)); // Adjust position and add randomness let btn = createButton('Click Me to Start'); // Position the button btn.position(0, 0); // Assign a function to call when the button is clicked btn.mousePressed(onButtonPressed); } function onButtonPressed() { let num_steps = 10000; console.log("Button was pressed."); // Simplified Neural Network weights let inputToHiddenWeights = [Array(2).fill().map(() => Math.random() - 0.5), Array(2).fill().map(() => Math.random() - 0.5)]; let hiddenToOutputWeights = [Math.random() - 0.5, Math.random() - 0.5]; let epochs = 500; let currentEpoch = 0; function reset() { upperArmAngle = 0; lowerArmAngle = 0; grabbed = false; moves = 0; fruit = createVector(220 + random(-10, 10), 200 + random(-10, 10)); } // Apply neural network to generate arm movement function applyNN() { let input = [fruit.x, fruit.y]; let hidden = inputToHiddenWeights.map(weights => weights[0] * input[0] + weights[1] * input[1]); let output = hidden.map(h => h * hiddenToOutputWeights[0] + h * hiddenToOutputWeights[1]); // Use outputs to adjust arm angles upperArmAngle += output[0]; lowerArmAngle += output[1]; moves++; } function runEpoch() { reset(); for (let step = 0; step < num_steps; step++) { // Simulate steps within an epoch applyNN(); } currentEpoch++; console.log("Running epoch", currentEpoch, " of ", epochs) if (currentEpoch >= epochs) { console.log("Training complete"); // Convert weights to a string representation const weightsStr = JSON.stringify({inputToHiddenWeights, hiddenToOutputWeights}); // Copy the stringified weights to the clipboard console.log(weightsStr); clearInterval(interval); } } const interval = setInterval(runEpoch, 3000); // Run an epoch every three seconds } function draw() { background(220); // Head ellipse(200, 100, 50, 50); // Head // Torso line(200, 125, 200, 250); // Torso // Left arm (static) line(200, 150, 150, 180); // Upper arm line(150, 180, 110, 160); // Forearm // Left leg (static) line(200, 250, 170, 300); // Thigh line(170, 300, 170, 350); // Shin // Right leg (static) line(200, 250, 230, 300); // Thigh line(230, 300, 230, 350); // Shin // Right arm (movable) let upperArmEndX = 200 + 50 * cos(upperArmAngle); let upperArmEndY = 150 + 50 * sin(upperArmAngle); line(200, 150, upperArmEndX, upperArmEndY); // Upper arm let lowerArmEndX = upperArmEndX + 40 * cos(lowerArmAngle + upperArmAngle); let lowerArmEndY = upperArmEndY + 40 * sin(lowerArmAngle + upperArmAngle); line(upperArmEndX, upperArmEndY, lowerArmEndX, lowerArmEndY); // Forearm // Draw the fruit fill(255, 0, 0); if (grabbed) { fruit.x = lowerArmEndX; fruit.y = lowerArmEndY; } ellipse(fruit.x, fruit.y, 20, 20); // Victory condition let headX = 200; // X-coordinate of the head's center let headY = 100; // Y-coordinate of the head's center let distanceToHead = dist(fruit.x, fruit.y, headX, headY); if (distanceToHead <= 25) { // Close enough to the head fill(0, 255, 0); textSize(32); text("Victory!", 100, 200); noLoop(); // Stop drawing console.log(`Victory in ${moves} moves!`); } // Display distances and instructions displayInfo(); } function keyPressed() { const angleIncrement = PI / 36; if (key === 'q') { upperArmAngle -= angleIncrement; moves++; } if (key === 'w') { upperArmAngle += angleIncrement; moves++; } if (key === 'a') { lowerArmAngle -= angleIncrement; moves++; } if (key === 's') { lowerArmAngle += angleIncrement; moves++; } if (key === 'g') grab(); // Grab the fruit if (key === 'r') release(); // Release the fruit } function grab() { let handTipX = 200 + 50 * cos(upperArmAngle) + 40 * cos(lowerArmAngle + upperArmAngle); let handTipY = 150 + 50 * sin(upperArmAngle) + 40 * sin(lowerArmAngle + upperArmAngle); let distanceToFruit = dist(handTipX, handTipY, fruit.x, fruit.y); if (distanceToFruit < 10) { // Close enough to grab grabbed = true; moves++; } } function release() { grabbed = false; moves++; } function displayInfo() { fill(0); textSize(16); text("Use q/w to rotate upper (stage) right arm", 10, 340) text("a/s to rotate lower (stage) right arm", 10, 360) text("g to grab, r to release", 10, 380); text(`Moves: ${moves}`, 10, 40); } ----- 100 training epochs: // paste in here: // https://p5js.org/examples/interaction-reach-1.html let fruit; let upperArmAngle = 0; let lowerArmAngle = 0; let grabbed = false; // Whether the fruit is grabbed let moves = 0; // Track the number of moves function setup() { createCanvas(400, 400); fruit = createVector(220 + random(-10, 10), 200 + random(-10, 10)); // Adjust position and add randomness let btn = createButton('Click Me to Start'); // Position the button btn.position(0, 0); // Assign a function to call when the button is clicked btn.mousePressed(onButtonPressed); } function onButtonPressed() { let num_steps = 10000; console.log("Button was pressed."); // Simplified Neural Network weights let inputToHiddenWeights = [Array(2).fill().map(() => Math.random() - 0.5), Array(2).fill().map(() => Math.random() - 0.5)]; let hiddenToOutputWeights = [Math.random() - 0.5, Math.random() - 0.5]; let epochs = 100; let currentEpoch = 0; function reset() { upperArmAngle = 0; lowerArmAngle = 0; grabbed = false; moves = 0; fruit = createVector(220 + random(-10, 10), 200 + random(-10, 10)); } // Apply neural network to generate arm movement function applyNN() { let input = [fruit.x, fruit.y]; let hidden = inputToHiddenWeights.map(weights => weights[0] * input[0] + weights[1] * input[1]); let output = hidden.map(h => h * hiddenToOutputWeights[0] + h * hiddenToOutputWeights[1]); // Use outputs to adjust arm angles upperArmAngle += output[0]; lowerArmAngle += output[1]; moves++; } function runEpoch() { reset(); for (let step = 0; step < num_steps; step++) { // Simulate steps within an epoch applyNN(); } currentEpoch++; console.log("Running epoch", currentEpoch, " of ", epochs) if (currentEpoch >= epochs) { console.log("Training complete"); // Convert weights to a string representation const weightsStr = JSON.stringify({inputToHiddenWeights, hiddenToOutputWeights}); // Copy the stringified weights to the clipboard console.log(weightsStr); clearInterval(interval); } } const interval = setInterval(runEpoch, 3000); // Run an epoch every three seconds } function draw() { background(220); // Head ellipse(200, 100, 50, 50); // Head // Torso line(200, 125, 200, 250); // Torso // Left arm (static) line(200, 150, 150, 180); // Upper arm line(150, 180, 110, 160); // Forearm // Left leg (static) line(200, 250, 170, 300); // Thigh line(170, 300, 170, 350); // Shin // Right leg (static) line(200, 250, 230, 300); // Thigh line(230, 300, 230, 350); // Shin // Right arm (movable) let upperArmEndX = 200 + 50 * cos(upperArmAngle); let upperArmEndY = 150 + 50 * sin(upperArmAngle); line(200, 150, upperArmEndX, upperArmEndY); // Upper arm let lowerArmEndX = upperArmEndX + 40 * cos(lowerArmAngle + upperArmAngle); let lowerArmEndY = upperArmEndY + 40 * sin(lowerArmAngle + upperArmAngle); line(upperArmEndX, upperArmEndY, lowerArmEndX, lowerArmEndY); // Forearm // Draw the fruit fill(255, 0, 0); if (grabbed) { fruit.x = lowerArmEndX; fruit.y = lowerArmEndY; } ellipse(fruit.x, fruit.y, 20, 20); // Victory condition let headX = 200; // X-coordinate of the head's center let headY = 100; // Y-coordinate of the head's center let distanceToHead = dist(fruit.x, fruit.y, headX, headY); if (distanceToHead <= 25) { // Close enough to the head fill(0, 255, 0); textSize(32); text("Victory!", 100, 200); noLoop(); // Stop drawing console.log(`Victory in ${moves} moves!`); } // Display distances and instructions displayInfo(); } function keyPressed() { const angleIncrement = PI / 36; if (key === 'q') { upperArmAngle -= angleIncrement; moves++; } if (key === 'w') { upperArmAngle += angleIncrement; moves++; } if (key === 'a') { lowerArmAngle -= angleIncrement; moves++; } if (key === 's') { lowerArmAngle += angleIncrement; moves++; } if (key === 'g') grab(); // Grab the fruit if (key === 'r') release(); // Release the fruit } function grab() { let handTipX = 200 + 50 * cos(upperArmAngle) + 40 * cos(lowerArmAngle + upperArmAngle); let handTipY = 150 + 50 * sin(upperArmAngle) + 40 * sin(lowerArmAngle + upperArmAngle); let distanceToFruit = dist(handTipX, handTipY, fruit.x, fruit.y); if (distanceToFruit < 10) { // Close enough to grab grabbed = true; moves++; } } function release() { grabbed = false; moves++; } function displayInfo() { fill(0); textSize(16); text("Use q/w to rotate upper (stage) right arm", 10, 340) text("a/s to rotate lower (stage) right arm", 10, 360) text("g to grab, r to release", 10, 380); text(`Moves: ${moves}`, 10, 40); } ----- 10,000 steps and 100 epochs but no grab possibility: // paste in here: // https://p5js.org/examples/interaction-reach-1.html let fruit; let upperArmAngle = 0; let lowerArmAngle = 0; let grabbed = false; // Whether the fruit is grabbed let moves = 0; // Track the number of moves function setup() { createCanvas(400, 400); fruit = createVector(220 + random(-10, 10), 200 + random(-10, 10)); // Adjust position and add randomness let btn = createButton('Click Me to Start'); // Position the button btn.position(0, 0); // Assign a function to call when the button is clicked btn.mousePressed(onButtonPressed); } function onButtonPressed() { let num_steps = 10000; console.log("Button was pressed."); // Simplified Neural Network weights let inputToHiddenWeights = [Array(2).fill().map(() => Math.random() - 0.5), Array(2).fill().map(() => Math.random() - 0.5)]; let hiddenToOutputWeights = [Math.random() - 0.5, Math.random() - 0.5]; let epochs = 100; let currentEpoch = 0; function reset() { upperArmAngle = 0; lowerArmAngle = 0; grabbed = false; moves = 0; fruit = createVector(220 + random(-10, 10), 200 + random(-10, 10)); } // Apply neural network to generate arm movement function applyNN() { let input = [fruit.x, fruit.y]; let hidden = inputToHiddenWeights.map(weights => weights[0] * input[0] + weights[1] * input[1]); let output = hidden.map(h => h * hiddenToOutputWeights[0] + h * hiddenToOutputWeights[1]); // Use outputs to adjust arm angles upperArmAngle += output[0]; lowerArmAngle += output[1]; moves++; } function runEpoch() { reset(); for (let step = 0; step < num_steps; step++) { // Simulate steps within an epoch applyNN(); } currentEpoch++; console.log("Running epoch", currentEpoch, " of ", epochs) if (currentEpoch >= epochs) { console.log("Training complete"); // Convert weights to a string representation const weightsStr = JSON.stringify({inputToHiddenWeights, hiddenToOutputWeights}); // Copy the stringified weights to the clipboard console.log(weightsStr); clearInterval(interval); } } const interval = setInterval(runEpoch, 3000); // Run an epoch every three seconds } function draw() { background(220); // Head ellipse(200, 100, 50, 50); // Head // Torso line(200, 125, 200, 250); // Torso // Left arm (static) line(200, 150, 150, 180); // Upper arm line(150, 180, 110, 160); // Forearm // Left leg (static) line(200, 250, 170, 300); // Thigh line(170, 300, 170, 350); // Shin // Right leg (static) line(200, 250, 230, 300); // Thigh line(230, 300, 230, 350); // Shin // Right arm (movable) let upperArmEndX = 200 + 50 * cos(upperArmAngle); let upperArmEndY = 150 + 50 * sin(upperArmAngle); line(200, 150, upperArmEndX, upperArmEndY); // Upper arm let lowerArmEndX = upperArmEndX + 40 * cos(lowerArmAngle + upperArmAngle); let lowerArmEndY = upperArmEndY + 40 * sin(lowerArmAngle + upperArmAngle); line(upperArmEndX, upperArmEndY, lowerArmEndX, lowerArmEndY); // Forearm // Draw the fruit fill(255, 0, 0); if (grabbed) { fruit.x = lowerArmEndX; fruit.y = lowerArmEndY; } ellipse(fruit.x, fruit.y, 20, 20); // Victory condition let headX = 200; // X-coordinate of the head's center let headY = 100; // Y-coordinate of the head's center let distanceToHead = dist(fruit.x, fruit.y, headX, headY); if (distanceToHead <= 25) { // Close enough to the head fill(0, 255, 0); textSize(32); text("Victory!", 100, 200); noLoop(); // Stop drawing console.log(`Victory in ${moves} moves!`); } // Display distances and instructions displayInfo(); } function keyPressed() { const angleIncrement = PI / 36; if (key === 'q') { upperArmAngle -= angleIncrement; moves++; } if (key === 'w') { upperArmAngle += angleIncrement; moves++; } if (key === 'a') { lowerArmAngle -= angleIncrement; moves++; } if (key === 's') { lowerArmAngle += angleIncrement; moves++; } if (key === 'g') grab(); // Grab the fruit if (key === 'r') release(); // Release the fruit } function grab() { let handTipX = 200 + 50 * cos(upperArmAngle) + 40 * cos(lowerArmAngle + upperArmAngle); let handTipY = 150 + 50 * sin(upperArmAngle) + 40 * sin(lowerArmAngle + upperArmAngle); let distanceToFruit = dist(handTipX, handTipY, fruit.x, fruit.y); if (distanceToFruit < 10) { // Close enough to grab grabbed = true; moves++; } } function release() { grabbed = false; moves++; } function displayInfo() { fill(0); textSize(16); text("Use q/w to rotate upper (stage) right arm", 10, 340) text("a/s to rotate lower (stage) right arm", 10, 360) text("g to grab, r to release", 10, 380); text(`Moves: ${moves}`, 10, 40); } --- another version of the same thing: // paste in here: // https://p5js.org/examples/interaction-reach-1.html let fruit; let upperArmAngle = 0; let lowerArmAngle = 0; let grabbed = false; // Whether the fruit is grabbed let moves = 0; // Track the number of moves function setup() { createCanvas(400, 400); fruit = createVector(220 + random(-10, 10), 200 + random(-10, 10)); // Adjust position and add randomness let btn = createButton('Click Me to Start'); // Position the button btn.position(0, 0); // Assign a function to call when the button is clicked btn.mousePressed(onButtonPressed); } function onButtonPressed() { let num_steps = 10000; console.log("Button was pressed."); // Simplified Neural Network weights let inputToHiddenWeights = [Array(2).fill().map(() => Math.random() - 0.5), Array(2).fill().map(() => Math.random() - 0.5)]; let hiddenToOutputWeights = [Math.random() - 0.5, Math.random() - 0.5]; let epochs = 100; let currentEpoch = 0; function reset() { upperArmAngle = 0; lowerArmAngle = 0; grabbed = false; moves = 0; fruit = createVector(220 + random(-10, 10), 200 + random(-10, 10)); } // Apply neural network to generate arm movement function applyNN() { let input = [fruit.x, fruit.y]; let hidden = inputToHiddenWeights.map(weights => weights[0] * input[0] + weights[1] * input[1]); let output = hidden.map(h => h * hiddenToOutputWeights[0] + h * hiddenToOutputWeights[1]); // Use outputs to adjust arm angles upperArmAngle += output[0]; lowerArmAngle += output[1]; moves++; } function runEpoch() { reset(); for (let step = 0; step < num_steps; step++) { // Simulate steps within an epoch applyNN(); } currentEpoch++; console.log("Running epoch", currentEpoch, " of ", epochs) if (currentEpoch >= epochs) { console.log("Training complete"); // Convert weights to a string representation const weightsStr = JSON.stringify({inputToHiddenWeights, hiddenToOutputWeights}); // Copy the stringified weights to the clipboard console.log(weightsStr); clearInterval(interval); } } const interval = setInterval(runEpoch, 3000); // Run an epoch every three seconds } function draw() { background(220); // Head ellipse(200, 100, 50, 50); // Head // Torso line(200, 125, 200, 250); // Torso // Left arm (static) line(200, 150, 150, 180); // Upper arm line(150, 180, 110, 160); // Forearm // Left leg (static) line(200, 250, 170, 300); // Thigh line(170, 300, 170, 350); // Shin // Right leg (static) line(200, 250, 230, 300); // Thigh line(230, 300, 230, 350); // Shin // Right arm (movable) let upperArmEndX = 200 + 50 * cos(upperArmAngle); let upperArmEndY = 150 + 50 * sin(upperArmAngle); line(200, 150, upperArmEndX, upperArmEndY); // Upper arm let lowerArmEndX = upperArmEndX + 40 * cos(lowerArmAngle + upperArmAngle); let lowerArmEndY = upperArmEndY + 40 * sin(lowerArmAngle + upperArmAngle); line(upperArmEndX, upperArmEndY, lowerArmEndX, lowerArmEndY); // Forearm // Draw the fruit fill(255, 0, 0); if (grabbed) { fruit.x = lowerArmEndX; fruit.y = lowerArmEndY; } ellipse(fruit.x, fruit.y, 20, 20); // Victory condition let headX = 200; // X-coordinate of the head's center let headY = 100; // Y-coordinate of the head's center let distanceToHead = dist(fruit.x, fruit.y, headX, headY); if (distanceToHead <= 25) { // Close enough to the head fill(0, 255, 0); textSize(32); text("Victory!", 100, 200); noLoop(); // Stop drawing console.log(`Victory in ${moves} moves!`); } // Display distances and instructions displayInfo(); } function keyPressed() { const angleIncrement = PI / 36; if (key === 'q') { upperArmAngle -= angleIncrement; moves++; } if (key === 'w') { upperArmAngle += angleIncrement; moves++; } if (key === 'a') { lowerArmAngle -= angleIncrement; moves++; } if (key === 's') { lowerArmAngle += angleIncrement; moves++; } if (key === 'g') grab(); // Grab the fruit if (key === 'r') release(); // Release the fruit } function grab() { let handTipX = 200 + 50 * cos(upperArmAngle) + 40 * cos(lowerArmAngle + upperArmAngle); let handTipY = 150 + 50 * sin(upperArmAngle) + 40 * sin(lowerArmAngle + upperArmAngle); let distanceToFruit = dist(handTipX, handTipY, fruit.x, fruit.y); if (distanceToFruit < 10) { // Close enough to grab grabbed = true; moves++; } } function release() { grabbed = false; moves++; } function displayInfo() { fill(0); textSize(16); text("Use q/w to rotate upper (stage) right arm", 10, 340) text("a/s to rotate lower (stage) right arm", 10, 360) text("g to grab, r to release", 10, 380); text(`Moves: ${moves}`, 10, 40); } ----- another version with no possibility to grab, but a random list of choices: // paste in here: // https://p5js.org/examples/interaction-reach-1.html let fruit; let upperArmAngle = 0; let lowerArmAngle = 0; let grabbed = false; // Whether the fruit is grabbed let moves = 0; // Track the number of moves function setup() { createCanvas(400, 400); fruit = createVector(220 + random(-10, 10), 200 + random(-10, 10)); // Adjust position and add randomness let btn = createButton('Click Me to Start'); // Position the button btn.position(0, 0); // Assign a function to call when the button is clicked btn.mousePressed(onButtonPressed); } function onButtonPressed() { console.log("Button was pressed."); let epochs = 5; let currentEpoch = 0; // Pre-trained weights, placeholders for now let inputToHiddenWeights = [[0, 0], [0, 0]]; let hiddenToOutputWeights = [0, 0]; function reset() { upperArmAngle = 0; lowerArmAngle = 0; grabbed = false; moves = 0; fruit = createVector(220 + random(-10, 10), 200 + random(-10, 10)); } function randomMove() { const moveChoices = ['q', 'w', 'a', 's']; const randomIndex = floor(random(0, moveChoices.length)); const move = moveChoices[randomIndex]; if (move === 'q') { upperArmAngle -= PI / 36; } if (move === 'w') { upperArmAngle += PI / 36; } if (move === 'a') { lowerArmAngle -= PI / 36; } if (move === 's') { lowerArmAngle += PI / 36; } moves++; } // Simple NN application for demonstration, placeholders for actual logic function applyNN() { // This function would apply the neural network's logic to decide on movements // Placeholder for demonstration } function runEpoch() { reset(); for (let step = 0; step < 100; step++) { // Simulate steps within an epoch applyNN(); // Apply the neural network (or in this case, random movements) randomMove(); } console.log(`Distance to head after epoch ${currentEpoch}:`, dist(fruit.x, fruit.y, 200, 100)); currentEpoch++; if (currentEpoch >= epochs) { console.log("Training complete"); clearInterval(interval); // Here, you would ideally output or save your trained model's weights } } const interval = setInterval(runEpoch, 1000); // Run an epoch every second } function draw() { background(220); // Head ellipse(200, 100, 50, 50); // Head // Torso line(200, 125, 200, 250); // Torso // Left arm (static) line(200, 150, 150, 180); // Upper arm line(150, 180, 110, 160); // Forearm // Left leg (static) line(200, 250, 170, 300); // Thigh line(170, 300, 170, 350); // Shin // Right leg (static) line(200, 250, 230, 300); // Thigh line(230, 300, 230, 350); // Shin // Right arm (movable) let upperArmEndX = 200 + 50 * cos(upperArmAngle); let upperArmEndY = 150 + 50 * sin(upperArmAngle); line(200, 150, upperArmEndX, upperArmEndY); // Upper arm let lowerArmEndX = upperArmEndX + 40 * cos(lowerArmAngle + upperArmAngle); let lowerArmEndY = upperArmEndY + 40 * sin(lowerArmAngle + upperArmAngle); line(upperArmEndX, upperArmEndY, lowerArmEndX, lowerArmEndY); // Forearm // Draw the fruit fill(255, 0, 0); if (grabbed) { fruit.x = lowerArmEndX; fruit.y = lowerArmEndY; } ellipse(fruit.x, fruit.y, 20, 20); // Victory condition let headX = 200; // X-coordinate of the head's center let headY = 100; // Y-coordinate of the head's center let distanceToHead = dist(fruit.x, fruit.y, headX, headY); if (distanceToHead <= 25) { // Close enough to the head fill(0, 255, 0); textSize(32); text("Victory!", 100, 200); noLoop(); // Stop drawing console.log(`Victory in ${moves} moves!`); } // Display distances and instructions displayInfo(); } function keyPressed() { const angleIncrement = PI / 36; if (key === 'q') { upperArmAngle -= angleIncrement; moves++; } if (key === 'w') { upperArmAngle += angleIncrement; moves++; } if (key === 'a') { lowerArmAngle -= angleIncrement; moves++; } if (key === 's') { lowerArmAngle += angleIncrement; moves++; } if (key === 'g') grab(); // Grab the fruit if (key === 'r') release(); // Release the fruit } function grab() { let handTipX = 200 + 50 * cos(upperArmAngle) + 40 * cos(lowerArmAngle + upperArmAngle); let handTipY = 150 + 50 * sin(upperArmAngle) + 40 * sin(lowerArmAngle + upperArmAngle); let distanceToFruit = dist(handTipX, handTipY, fruit.x, fruit.y); if (distanceToFruit < 10) { // Close enough to grab grabbed = true; moves++; } } function release() { grabbed = false; moves++; } function displayInfo() { fill(0); textSize(16); text("Use q/w to rotate upper (stage) right arm", 10, 340) text("a/s to rotate lower (stage) right arm", 10, 360) text("g to grab, r to release", 10, 380); text(`Moves: ${moves}`, 10, 40); } ---- scrap with some grabbing codee: // paste in here: // https://p5js.org/examples/interaction-reach-1.html let fruit; let upperArmAngle = 0; let lowerArmAngle = 0; let grabbed = false; // Whether the fruit is grabbed let moves = 0; // Track the number of moves function setup() { createCanvas(400, 400); fruit = createVector(220 + random(-10, 10), 200 + random(-10, 10)); // Adjust position and add randomness let btn = createButton('Click Me to Start'); // Position the button btn.position(0, 0); // Assign a function to call when the button is clicked btn.mousePressed(onButtonPressed); } function onButtonPressed() { console.log("Button was pressed."); let epochs = 5; let currentEpoch = 0; // Pre-trained weights, placeholders for now let inputToHiddenWeights = [[0, 0], [0, 0]]; let hiddenToOutputWeights = [0, 0]; function reset() { upperArmAngle = 0; lowerArmAngle = 0; grabbed = false; moves = 0; fruit = createVector(220 + random(-10, 10), 200 + random(-10, 10)); } function randomMove() { const moveChoices = ['q', 'w', 'a', 's', 'g']; const randomIndex = floor(random(0, moveChoices.length)); const move = moveChoices[randomIndex]; if (move === 'q') { upperArmAngle -= PI / 36; } if (move === 'w') { upperArmAngle += PI / 36; } if (move === 'a') { lowerArmAngle -= PI / 36; } if (move === 's') { lowerArmAngle += PI / 36; } if (move === 'g') { let handTipX = 200 + 50 * cos(upperArmAngle) + 40 * cos(lowerArmAngle + upperArmAngle); let handTipY = 150 + 50 * sin(upperArmAngle) + 40 * sin(lowerArmAngle + upperArmAngle); let distanceToFruit = dist(handTipX, handTipY, fruit.x, fruit.y); if (distanceToFruit < 10) { // Close enough to grab grabbed = true; } } moves++; } // Simple NN application for demonstration, placeholders for actual logic function applyNN() { // This function would apply the neural network's logic to decide on movements // Placeholder for demonstration } function runEpoch() { reset(); for (let step = 0; step < 100; step++) { // Simulate steps within an epoch applyNN(); // Apply the neural network (or in this case, random movements) randomMove(); } console.log(`Distance to head after epoch ${currentEpoch}:`, dist(fruit.x, fruit.y, 200, 100)); currentEpoch++; if (currentEpoch >= epochs) { console.log("Training complete"); clearInterval(interval); // Here, you would ideally output or save your trained model's weights } } const interval = setInterval(runEpoch, 1000); // Run an epoch every second } function draw() { background(220); // Head ellipse(200, 100, 50, 50); // Head // Torso line(200, 125, 200, 250); // Torso // Left arm (static) line(200, 150, 150, 180); // Upper arm line(150, 180, 110, 160); // Forearm // Left leg (static) line(200, 250, 170, 300); // Thigh line(170, 300, 170, 350); // Shin // Right leg (static) line(200, 250, 230, 300); // Thigh line(230, 300, 230, 350); // Shin // Right arm (movable) let upperArmEndX = 200 + 50 * cos(upperArmAngle); let upperArmEndY = 150 + 50 * sin(upperArmAngle); line(200, 150, upperArmEndX, upperArmEndY); // Upper arm let lowerArmEndX = upperArmEndX + 40 * cos(lowerArmAngle + upperArmAngle); let lowerArmEndY = upperArmEndY + 40 * sin(lowerArmAngle + upperArmAngle); line(upperArmEndX, upperArmEndY, lowerArmEndX, lowerArmEndY); // Forearm // Draw the fruit fill(255, 0, 0); if (grabbed) { fruit.x = lowerArmEndX; fruit.y = lowerArmEndY; } ellipse(fruit.x, fruit.y, 20, 20); // Victory condition let headX = 200; // X-coordinate of the head's center let headY = 100; // Y-coordinate of the head's center let distanceToHead = dist(fruit.x, fruit.y, headX, headY); if (distanceToHead <= 25) { // Close enough to the head fill(0, 255, 0); textSize(32); text("Victory!", 100, 200); noLoop(); // Stop drawing console.log(`Victory in ${moves} moves!`); } // Display distances and instructions displayInfo(); } function keyPressed() { const angleIncrement = PI / 36; if (key === 'q') { upperArmAngle -= angleIncrement; moves++; } if (key === 'w') { upperArmAngle += angleIncrement; moves++; } if (key === 'a') { lowerArmAngle -= angleIncrement; moves++; } if (key === 's') { lowerArmAngle += angleIncrement; moves++; } if (key === 'g') grab(); // Grab the fruit if (key === 'r') release(); // Release the fruit } function grab() { let handTipX = 200 + 50 * cos(upperArmAngle) + 40 * cos(lowerArmAngle + upperArmAngle); let handTipY = 150 + 50 * sin(upperArmAngle) + 40 * sin(lowerArmAngle + upperArmAngle); let distanceToFruit = dist(handTipX, handTipY, fruit.x, fruit.y); if (distanceToFruit < 10) { // Close enough to grab grabbed = true; moves++; } } function release() { grabbed = false; moves++; } function displayInfo() { fill(0); textSize(16); text("Use q/w to rotate upper (stage) right arm", 10, 340) text("a/s to rotate lower (stage) right arm", 10, 360) text("g to grab, r to release", 10, 380); text(`Moves: ${moves}`, 10, 40); } ----- last version I tried: // paste in here: // https://p5js.org/examples/interaction-reach-1.html let fruit; let upperArmAngle = 0; let lowerArmAngle = 0; let grabbed = false; // Whether the fruit is grabbed let moves = 0; // Track the number of moves function setup() { createCanvas(400, 400); fruit = createVector(220 + random(-10, 10), 200 + random(-10, 10)); // Adjust position and add randomness let btn = createButton('Click Me to Start'); // Position the button btn.position(0, 0); // Assign a function to call when the button is clicked btn.mousePressed(onButtonPressed); } function onButtonPressed() { console.log("Button was pressed."); let epochs = 5; let currentEpoch = 0; // Using the pre-trained weights provided let inputToHiddenWeights = [[0.03538708334821572, 0.030067037042046607], [-0.20361980545058334, -0.051275998961280544]]; let hiddenToOutputWeights = [0.08436437537780384, -0.050039818292926475]; function reset() { upperArmAngle = 0; lowerArmAngle = 0; grabbed = false; moves = 0; fruit = createVector(220 + random(-10, 10), 200 + random(-10, 10)); } function randomMove() { const moveChoices = ['q', 'w', 'a', 's']; const randomIndex = floor(random(0, moveChoices.length)); const move = moveChoices[randomIndex]; if (move === 'q') { upperArmAngle -= PI / 36; } if (move === 'w') { upperArmAngle += PI / 36; } if (move === 'a') { lowerArmAngle -= PI / 36; } if (move === 's') { lowerArmAngle += PI / 36; } moves++; } function applyNN() { // Normalize inputs based on canvas size for better generalization let normalizedInput = [ (fruit.x - 200) / 200, // Normalizing fruit.x (fruit.y - 200) / 200 // Normalizing fruit.y ]; // Calculating outputs from the input layer to the hidden layer let hiddenLayerOutputs = inputToHiddenWeights.map(weights => Math.max(0, weights[0] * normalizedInput[0] + weights[1] * normalizedInput[1])); // ReLU activation // Calculating the final output from the hidden layer to the output layer let upperArmAdjustment = hiddenToOutputWeights[0] * hiddenLayerOutputs[0]; let lowerArmAdjustment = hiddenToOutputWeights[1] * hiddenLayerOutputs[1]; // Applying the adjustments upperArmAngle += upperArmAdjustment; lowerArmAngle += lowerArmAdjustment; } function runEpoch() { reset(); for (let step = 0; step < 100; step++) { // Simulate steps within an epoch applyNN(); } currentEpoch++; console.log(`Epoch ${currentEpoch}: Distance to head =`, dist(fruit.x, fruit.y, 200, 100)); if (currentEpoch >= epochs) { console.log("Training complete"); clearInterval(interval); } } const interval = setInterval(runEpoch, 1000); // Run an epoch every second } function draw() { background(220); // Head ellipse(200, 100, 50, 50); // Head // Torso line(200, 125, 200, 250); // Torso // Left arm (static) line(200, 150, 150, 180); // Upper arm line(150, 180, 110, 160); // Forearm // Left leg (static) line(200, 250, 170, 300); // Thigh line(170, 300, 170, 350); // Shin // Right leg (static) line(200, 250, 230, 300); // Thigh line(230, 300, 230, 350); // Shin // Right arm (movable) let upperArmEndX = 200 + 50 * cos(upperArmAngle); let upperArmEndY = 150 + 50 * sin(upperArmAngle); line(200, 150, upperArmEndX, upperArmEndY); // Upper arm let lowerArmEndX = upperArmEndX + 40 * cos(lowerArmAngle + upperArmAngle); let lowerArmEndY = upperArmEndY + 40 * sin(lowerArmAngle + upperArmAngle); line(upperArmEndX, upperArmEndY, lowerArmEndX, lowerArmEndY); // Forearm // Draw the fruit fill(255, 0, 0); if (grabbed) { fruit.x = lowerArmEndX; fruit.y = lowerArmEndY; } ellipse(fruit.x, fruit.y, 20, 20); // Victory condition let headX = 200; // X-coordinate of the head's center let headY = 100; // Y-coordinate of the head's center let distanceToHead = dist(fruit.x, fruit.y, headX, headY); if (distanceToHead <= 25) { // Close enough to the head fill(0, 255, 0); textSize(32); text("Victory!", 100, 200); noLoop(); // Stop drawing console.log(`Victory in ${moves} moves!`); } // Display distances and instructions displayInfo(); } function keyPressed() { const angleIncrement = PI / 36; if (key === 'q') { upperArmAngle -= angleIncrement; moves++; } if (key === 'w') { upperArmAngle += angleIncrement; moves++; } if (key === 'a') { lowerArmAngle -= angleIncrement; moves++; } if (key === 's') { lowerArmAngle += angleIncrement; moves++; } if (key === 'g') grab(); // Grab the fruit if (key === 'r') release(); // Release the fruit } function grab() { let handTipX = 200 + 50 * cos(upperArmAngle) + 40 * cos(lowerArmAngle + upperArmAngle); let handTipY = 150 + 50 * sin(upperArmAngle) + 40 * sin(lowerArmAngle + upperArmAngle); let distanceToFruit = dist(handTipX, handTipY, fruit.x, fruit.y); if (distanceToFruit < 10) { // Close enough to grab grabbed = true; moves++; } } function release() { grabbed = false; moves++; } function displayInfo() { fill(0); textSize(16); text("Use q/w to rotate upper (stage) right arm", 10, 340) text("a/s to rotate lower (stage) right arm", 10, 360) text("g to grab, r to release", 10, 380); text(`Moves: ${moves}`, 10, 40); }