forked from s444420/AL-2020
a* implementacja
This commit is contained in:
parent
9f4de9afd4
commit
8a58a491b8
@ -13,6 +13,7 @@
|
||||
<div class="board" id="board">
|
||||
|
||||
</div>
|
||||
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
@ -94,22 +94,22 @@
|
||||
// document.getElementById(agent.getId()).style.backgroundImage = "url('Agent.jpg')";
|
||||
// }
|
||||
|
||||
// //Funkcja zmieniajaca polozenie agenta o 1 pole
|
||||
// function przemieszczenie(side, time, agent){
|
||||
// const lastPosition = agent.getId();
|
||||
// setTimeout(function(){
|
||||
// usunAgenta(agent)
|
||||
// if(side == "left")
|
||||
// agent.left();
|
||||
// if(side == "right")
|
||||
// agent.right();
|
||||
// if(side == "up")
|
||||
// agent.up();
|
||||
// if(side == "down")
|
||||
// agent.down();
|
||||
// umiescAgenta(agent);
|
||||
// }, time);
|
||||
// }
|
||||
//Funkcja zmieniajaca polozenie agenta o 1 pole
|
||||
function przemieszczenie(side, time, agent){
|
||||
const lastPosition = agent.getId();
|
||||
setTimeout(function(){
|
||||
usunAgenta(agent)
|
||||
if(side == "left")
|
||||
agent.left();
|
||||
if(side == "right")
|
||||
agent.right();
|
||||
if(side == "up")
|
||||
agent.up();
|
||||
if(side == "down")
|
||||
agent.down();
|
||||
umiescAgenta(agent);
|
||||
}, time);
|
||||
}
|
||||
|
||||
// //Funkcja ktora ustala droge agenta do przebycia
|
||||
// function droga(agent) {
|
||||
|
@ -18,21 +18,6 @@ class Agent{
|
||||
return params;
|
||||
}
|
||||
|
||||
goForward(){
|
||||
if(this.turn == 'Up'){
|
||||
this.positionY += 1;
|
||||
}
|
||||
else if(this.turn == 'Down'){
|
||||
this.positionY -= 1;
|
||||
}
|
||||
else if(this.turn == 'Left'){
|
||||
this.positionX -= 1;
|
||||
}
|
||||
else if(this.turn == 'Right'){
|
||||
this.positionX += 1;
|
||||
}
|
||||
}
|
||||
|
||||
turnLeft(){
|
||||
if(this.turn == 'Up'){
|
||||
this.turn = 'Left';
|
||||
@ -83,6 +68,21 @@ class Agent{
|
||||
document.getElementById(this.positionX + "-" + this.positionY).style.backgroundImage = "";
|
||||
}
|
||||
|
||||
goForward(){
|
||||
if(this.turn == 'Up'){
|
||||
this.positionY += 1;
|
||||
}
|
||||
else if(this.turn == 'Down'){
|
||||
this.positionY -= 1;
|
||||
}
|
||||
else if(this.turn == 'Left'){
|
||||
this.positionX -= 1;
|
||||
}
|
||||
else if(this.turn == 'Right'){
|
||||
this.positionX += 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@ -93,6 +93,11 @@ class Field{
|
||||
this.isShelf = isShelf;
|
||||
this.isOccupiedByAgent = isOccupiedByAgent;
|
||||
this.costOfTravel = costOfTravel;
|
||||
this.neighbors = [];
|
||||
this.gScore = 0;
|
||||
this.heuristic = 0;
|
||||
this.f = 0;
|
||||
this.previous = undefined;
|
||||
}
|
||||
|
||||
getParams(){
|
||||
@ -123,6 +128,12 @@ class Field{
|
||||
setCostOfTravel(costOfTravel){
|
||||
this.costOfTravel = costOfTravel;
|
||||
}
|
||||
|
||||
setNeighbors(field){
|
||||
if(field.isShelf == false){
|
||||
this.neighbors.push(field)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class Shelf{
|
||||
@ -179,7 +190,8 @@ class Candy{
|
||||
}
|
||||
|
||||
}
|
||||
//funckcje
|
||||
|
||||
//funkcje
|
||||
|
||||
function createBoard(rangeX, rangeY){
|
||||
const board = [];
|
||||
@ -332,11 +344,162 @@ function showBoard(board){
|
||||
}
|
||||
}
|
||||
|
||||
function getDistance(x1,y1,x2,y2){
|
||||
var a = x1 - x2;
|
||||
var b = y1 - y2;
|
||||
var c = Math.sqrt(a*a + b*b);
|
||||
return c
|
||||
function getDistance(field, goalField){
|
||||
// var a = field.xField - goalField.xField;
|
||||
// var b = field.yField - goalField.yField;
|
||||
// var c = Math.sqrt(a*a + b*b);
|
||||
var d = Math.abs(field.xField - goalField.xField) + Math.abs(field.yField - goalField.yField)
|
||||
return d
|
||||
}
|
||||
|
||||
function addNeighbors(board){
|
||||
for(var y = 0; y < 10; y++){
|
||||
for(var x = 0; x < 10; x++){
|
||||
var leftNeigbor, rightNeighbor, topNeighbor, bottomNeighbor;
|
||||
if(x > 0){
|
||||
leftNeigbor = board[y][x - 1];
|
||||
board[y][x].setNeighbors(leftNeigbor);
|
||||
}
|
||||
if(x < board[y].length - 1){
|
||||
rightNeighbor = board[y][x + 1];
|
||||
board[y][x].setNeighbors(rightNeighbor);
|
||||
}
|
||||
if(y > 0){
|
||||
bottomNeighbor = board[y - 1][x];
|
||||
board[y][x].setNeighbors(bottomNeighbor);
|
||||
}
|
||||
if(y < board.length - 1){
|
||||
topNeighbor = board[y + 1][x];
|
||||
board[y][x].setNeighbors(topNeighbor);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function colorGreen(field){
|
||||
document.getElementById(field.xField + "-" + field.yField).style.backgroundColor = "green";
|
||||
}
|
||||
|
||||
function colorRed(field){
|
||||
document.getElementById(field.xField + "-" + field.yField).style.backgroundColor = "red";
|
||||
}
|
||||
|
||||
function colorYellow(field){
|
||||
document.getElementById(field.xField + "-" + field.yField).style.backgroundColor = "yellow";
|
||||
}
|
||||
|
||||
function addNeighborsToOpenSet(set, neigbors){
|
||||
for(x = 0; x < neigbors.length; x++){
|
||||
set.push(neigbors[x]);
|
||||
colorGreen(neigbors[x]);
|
||||
}
|
||||
}
|
||||
|
||||
function addToClosedSet(set, field){
|
||||
set.push(field);
|
||||
colorRed(field);
|
||||
}
|
||||
|
||||
|
||||
function addToOpenSet(set, field){
|
||||
set.push(field);
|
||||
colorGreen(field);
|
||||
}
|
||||
|
||||
function calculateFScore(field, goalField){
|
||||
let fscore, distanceToGoal, costOfTravel;
|
||||
distanceToGoal = getDistance(field, goalField);
|
||||
costOfTravel = field.costOfTravel;
|
||||
fscore = costOfTravel + distanceToGoal;
|
||||
return fscore;
|
||||
}
|
||||
|
||||
function findLowestFScore(openSet, goalField){
|
||||
let bestField = openSet[0];
|
||||
for(var x = 1; x < openSet.length; x++){
|
||||
if(calculateFScore(openSet[x], goalField) < calculateFScore(bestField, goalField)){
|
||||
bestField = openSet[x];
|
||||
}
|
||||
}
|
||||
return bestField;
|
||||
}
|
||||
|
||||
function removeFromSet(openSet, promisingField){
|
||||
for(var x = openSet.length - 1; x >= 0; x--){
|
||||
if(openSet[x] == promisingField){
|
||||
openSet.splice(x,1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function sleep(milliseconds) {
|
||||
const date = Date.now();
|
||||
let currentDate = null;
|
||||
do {
|
||||
currentDate = Date.now();
|
||||
} while (currentDate - date < milliseconds);
|
||||
}
|
||||
|
||||
function aStar(startField, goalField){
|
||||
let closedSet = [];
|
||||
let openSet = [];
|
||||
var path;
|
||||
|
||||
addToOpenSet(openSet, startField)
|
||||
|
||||
while(openSet.length > 0){
|
||||
|
||||
let current = findLowestFScore(openSet, goalField);
|
||||
|
||||
if(current === goalField){
|
||||
path = []
|
||||
var temp = current;
|
||||
path.push(temp);
|
||||
while(temp.previous){
|
||||
path.push(temp.previous);
|
||||
temp = temp.previous
|
||||
}
|
||||
console.log("A* COMPLETED");
|
||||
|
||||
for(var i = 0; i < path.length; i++){
|
||||
colorYellow(path[i]);
|
||||
}
|
||||
return path
|
||||
}
|
||||
|
||||
removeFromSet(openSet, current);
|
||||
addToClosedSet(closedSet, current);
|
||||
|
||||
var neighbors = current.neighbors;
|
||||
|
||||
for(var i = 0; i < neighbors.length; i++){
|
||||
|
||||
var neighbor = neighbors[i];
|
||||
|
||||
if(!closedSet.includes(neighbor)){
|
||||
var tempG = current.gScore + neighbor.costOfTravel;
|
||||
|
||||
if(openSet.includes(neighbor)){
|
||||
if(tempG < neighbor.gScore){
|
||||
neighbor.gScore = tempG;
|
||||
}
|
||||
} else {
|
||||
neighbor.gScore = tempG;
|
||||
addToOpenSet(openSet, neighbor);
|
||||
}
|
||||
|
||||
neighbor.heuristic = getDistance(neighbor, goalField);
|
||||
neighbor.f = neighbor.gScore + neighbor.heuristic;
|
||||
neighbor.previous = current;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function executePath(agent, path){
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@ -345,12 +508,17 @@ board = createShelfOnBoard(board);
|
||||
board = createCostofField(board, 10, 10);
|
||||
let candy = new Candy('zelek', 'truskawkowy', 'Jumbo', 541);
|
||||
let shelfs = createShelf();
|
||||
|
||||
let agent = new Agent(0,0, 'Right');
|
||||
var button = document.getElementById("testButton");
|
||||
addNeighbors(board);
|
||||
|
||||
|
||||
function start(){
|
||||
showBoard(board);
|
||||
console.log(board);
|
||||
|
||||
let path = aStar(board[0][0], board[7][7]);
|
||||
//console.log(path);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
@ -83,3 +83,21 @@ body {
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
#buttonContainer{
|
||||
width: 100%;
|
||||
height: 150px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
|
||||
}
|
||||
|
||||
|
||||
#testButton{
|
||||
height:100px;
|
||||
width:200px;
|
||||
margin: -20px -50px;
|
||||
position:relative;
|
||||
top:30px;
|
||||
left:48%;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user