I am making a maze solver via Q Learning algorithm. I have a width X height maze that is generated randomly. Each cell of the maze is a div. I have CSS code for different type cells.
.wall {
background-color: red;
}
.currentPosition {
background-color: yellow;
}
I want to tell Q Learning briefly for those who don't know, basically our agent(or mouse, what do you call) changes its position very fastly until solve the maze.
I'm expecting a result like the gif above. I add 'currentPosition' to classlist of the cell where the agent is.
Here the my pseudo code:
trainAgent()
function trainAgent(){
for(let i = 0; i < generation number(like 1000000); i++){
while(until agent goes to the goal or walls){
// Do some calculations
// Update some numbers in q matrix
// Change current position of agent and add 'currentPosition' class to the new div
}
}
}
function setCurrentPoint(row, column){
if(currentPoint.div)
currentPoint.div.classList.remove("currentPoint")
currentPoint.row = row
currentPoint.column = column
currentPoint.div = mapMatrix[row][column]
currentPoint.div.classList.add('currentPoint')
}
My problem starts here. Until the loops finishes the browser freezes! So I can't see the instant position of agent. I tried add setTimeout()
but nothing has changed. If I was doing this project in Java I would do them in separate threads but I don't know what can I do in JavaScript.