Certainly! Below is a single HTML file that accomplishes the task you described. This file includes the necessary HTML, CSS, and JavaScript to create the grid, plot the dots and apples, and use TensorFlow.js to perform gradient descent.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Grid with Dots and Apples</title>
<style>
#grid {
display: grid;
grid-template-columns: repeat(10, 1fr);
grid-template-rows: repeat(10, 1fr);
width: 500px;
height: 500px;
border: 1px solid black;
}
.dot {
width: 10px;
height: 10px;
background-color: blue;
position: absolute;
border-radius: 50%;
}
.apple {
font-size: 24px;
position: absolute;
}
</style>
</head>
<body>
<div id="grid"></div>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>
<script>
const gridSize = 10;
const numDots = 100;
const numApples = 4;
let dots = [];
let apples = [];
let dotOwners = new Array(numDots).fill(0).map(() => Math.floor(Math.random() * numApples));
function createDots() {
for (let i = 0; i < numDots; i++) {
const dot = document.createElement('div');
dot.className = 'dot';
dot.style.top = `${Math.random() * 490}px`;
dot.style.left = `${Math.random() * 490}px`;
document.getElementById('grid').appendChild(dot);
dots.push(dot);
}
}
function createApples() {
for (let i = 0; i < numApples; i++) {
const apple = document.createElement('div');
apple.className = 'apple';
apple.innerText = '🍎';
apple.style.top = `${Math.random() * 490}px`;
apple.style.left = `${Math.random() * 490}px`;
document.getElementById('grid').appendChild(apple);
apples.push(apple);
}
}
function calculateLoss() {
let loss = 0;
for (let i = 0; i < numDots; i++) {
const dot = dots[i];
const owner = dotOwners[i];
const apple = apples[owner];
const dx = parseFloat(dot.style.left) - parseFloat(apple.style.left);
const dy = parseFloat(dot.style.top) - parseFloat(apple.style.top);
loss += Math.sqrt(dx * dx + dy * dy);
}
return loss / numDots;
}
async function gradientDescent() {
const learningRate = 0.01;
for (let step = 0; step < 100; step++) {
const gradients = tf.variable(tf.zeros([numApples, 2]));
for (let i = 0; i < numDots; i++) {
const dot = dots[i];
const owner = dotOwners[i];
const apple = apples[owner];
const dx = parseFloat(dot.style.left) - parseFloat(apple.style.left);
const dy = parseFloat(dot.style.top) - parseFloat(apple.style.top);
gradients.dataSync()[owner * 2] -= dx;
gradients.dataSync()[owner * 2 + 1] -= dy;
}
gradients.div(numDots).print();
for (let i = 0; i < numApples; i++) {
const apple = apples[i];
apple.style.left = `${parseFloat(apple.style.left) + gradients.dataSync()[i * 2] * learningRate}px`;
apple.style.top = `${parseFloat(apple.style.top) + gradients.dataSync()[i * 2 + 1] * learningRate}px`;
}
// Check for owner swaps
for (let i = 0; i < numDots; i++) {
for (let j = 0; j < numApples; j++) {
if (j !== dotOwners[i]) {
dotOwners[i] = j;
const newLoss = calculateLoss();
if (newLoss < calculateLoss()) {
break;
} else {
dotOwners[i] = j;
}
}
}
}
await tf.nextFrame();
}
}
createDots();
createApples();
gradientDescent();
</script>
</body>
</html>
Explanation:
-
HTML and CSS:
- The
#grid div is styled to create a 10x10 grid.
- Dots and apples are styled with CSS classes.
-
JavaScript:
- Dots and Apples Creation: Functions
createDots and createApples randomly place dots and apples within the grid.
- Loss Calculation: The
calculateLoss function calculates the average distance between each apple and its assigned dots.
- Gradient Descent: The
gradientDescent function uses TensorFlow.js to perform gradient descent, moving the apples to minimize the loss. It also checks for possible owner swaps to further reduce the loss.
This script will create a 10x10 grid with 100 dots and 4 apples, and then use gradient descent to optimize the positions of the apples.