Skip to content

Commit

Permalink
FINISHING THE FIRST STAGE OF THE PROJECT
Browse files Browse the repository at this point in the history
  • Loading branch information
THEKINGSTAR committed Dec 11, 2023
1 parent ab18803 commit 133cf4d
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 8 deletions.
22 changes: 21 additions & 1 deletion main_project/ann.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,27 @@ class NeuralNetwork{

return outputs;
}


static mutate(network, amount = 1){
network.level.array.forEach(level => {
for (let i = 0; i < level.biases.length; i++) {
level.biases[i] = lerp(
level.biases[i],
Math.random() * 2 - 1,
amount
)
}
for (let i = 0; i < level.weights.length; i++) {
for (let j = 0; j < level.weights.length; j++) {
level.weights[i][j] = lerp(
level.weights[i][j],
Math.random() * 2 - 1,
amount
)
}
}
});
}
}


Expand Down
33 changes: 27 additions & 6 deletions main_project/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,46 @@ const road= new Road(carCanvas.width / 2, carCanvas.width * 0.9);


//Initiate the Car traffics
const N = 100;
const N = 1000;
const cars = generateCars(N);


let bestCar = cars[0];
if(localStorage.getItem("bestBrain")){
for (let i = 0; i < cars.length; i++) {
cars[i].brain = JSON.parse(
localStorage.getItem("bestBrain"));
if (i != 0) {
NeuralNetwork.mutate(cars[i].brain, 0.1);
}
}
}
//Create traffic
const traffic = [
new Car(road.getLaneCenter(1), -100, 30, 50, "DUMMY", 2)
new Car(road.getLaneCenter(1), -100, 30, 50, "DUMMY", 2),
new Car(road.getLaneCenter(0), -300, 30, 50, "DUMMY", 2),
new Car(road.getLaneCenter(2), -300, 30, 50, "DUMMY", 2),
new Car(road.getLaneCenter(0), -500, 30, 50, "DUMMY", 2),
new Car(road.getLaneCenter(1), -500, 30, 50, "DUMMY", 2),
new Car(road.getLaneCenter(1), -700, 30, 50, "DUMMY", 2),
new Car(road.getLaneCenter(2), -700, 30, 50, "DUMMY", 2)
];


animate();

function save(){
localStorage.setItem("bestBrain",
JSON.stringify(bestCar.brain));
}
function discard(){
localStorage.removeItem("bestBrain");
}

function generateCars(N){
const cars = [];
for (let i = 1; i <= N; i++)
{
cars.push(new Car(road.getLaneCenter(1), 100, 30, 50, "AI"))
}

return cars;
}

Expand All @@ -54,7 +75,7 @@ function animate(time){
cars[i].update(road.borders, traffic);
}

const bestCar = cars.find(
bestCar = cars.find(
c=>c.y == Math.min(...cars.map(c=>c.y))
);

Expand Down
4 changes: 4 additions & 0 deletions main_project/self_driving_car.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

<body>
<canvas id="carCanvas"></canvas>
<div id="verticalButtons">
<button onclick="save()">💾🚗🚓🚕</button>
<button onclick="discard()">🗑️🦽</button>
</div>
<canvas id="networkCanvas"></canvas>
<script src="visualizer.js"></script>
<script src="ann.js"></script>
Expand Down
23 changes: 22 additions & 1 deletion main_project/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,33 @@ body{
margin:0;
background: darkgray;
overflow: hidden;
text-align: center;
display:flex;
justify-content: center;
align-items: center;

}

#verticalButtons{
display: flex;
flex-direction: column;
}

button{
border: none;
border-radius: 5px;
padding: 5px 5px 7px 5px;
margin: 2px;
cursor: pointer;
}

button:hover{
background: blue;
}

#networkCanvas{
background: black;
}

#carCanvas{
background: lightgray;
}

0 comments on commit 133cf4d

Please sign in to comment.