wstępny raport

This commit is contained in:
Tomek 2020-04-28 23:27:45 +02:00
parent 35518f0f86
commit ae99fb977c
2 changed files with 157 additions and 47 deletions

View File

@ -69,6 +69,7 @@ class Agent{
}
goForward(){
this.hideAgent()
if(this.turn == 'Up'){
this.positionY += 1;
}
@ -81,6 +82,26 @@ class Agent{
else if(this.turn == 'Right'){
this.positionX += 1;
}
this.showAgent()
}
goToField(field){
if(field.yField > this.positionY){
//pole jest nad agentem
}
if(field.yField < this.positionY){
//pole jest pod agentem
}
if(field.xField > this.positionX){
//pole jest na prawo
}
if(field.xField < this.positionX){
//pole jest na lewo
}
}
@ -94,8 +115,8 @@ class Field{
this.isOccupiedByAgent = isOccupiedByAgent;
this.costOfTravel = costOfTravel;
this.neighbors = [];
this.gScore = 0;
this.heuristic = 0;
this.g = 0;
this.h = 0;
this.f = 0;
this.previous = undefined;
}
@ -376,24 +397,37 @@ function addNeighbors(board){
}
}
function colorGreen(field){
document.getElementById(field.xField + "-" + field.yField).style.backgroundColor = "green";
// 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";
// }
const colorFactory = color => (field, animationFrame) => {
const delay = animationFrame * 50
console.log(delay)
setTimeout(() => {
document.getElementById(field.xField + "-" + field.yField).style.backgroundColor = color;
}, delay);
return ++animationFrame
}
function colorRed(field){
document.getElementById(field.xField + "-" + field.yField).style.backgroundColor = "red";
}
const colorYellow = colorFactory('yellow')
const colorGreen = colorFactory('green')
const colorRed = colorFactory('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 addNeighborsToOpenSet(set, neigbors){
// for(x = 0; x < neigbors.length; x++){
// set.push(neigbors[x]);
// colorGreen(neigbors[x]);
// }
// }
function addToClosedSet(set, field){
set.push(field);
@ -432,24 +466,27 @@ function removeFromSet(openSet, promisingField){
}
}
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;
let animationFrame = 0
addToOpenSet(openSet, startField)
// addToOpenSet(openSet, startField)
openSet.push(startField);
colorGreen(startField, animationFrame);
while(openSet.length > 0){
let current = findLowestFScore(openSet, goalField);
// let current = findLowestFScore(openSet, goalField);
let winner = 0;
for(let i = 0; i < openSet.length; i++){
if (openSet[i].f < openSet[winner].f){
winner = i
}
}
let current = openSet[winner];
if(current === goalField){
path = []
@ -462,44 +499,45 @@ function aStar(startField, goalField){
console.log("A* COMPLETED");
for(var i = 0; i < path.length; i++){
colorYellow(path[i]);
animationFrame = colorYellow(path[i], animationFrame);
}
return path
return path;
}
removeFromSet(openSet, current);
addToClosedSet(closedSet, current);
// addToClosedSet(closedSet, current);
closedSet.push(current);
animationFrame = colorRed(current, animationFrame);
var neighbors = current.neighbors;
for(var i = 0; i < neighbors.length; i++){
for(var i = 0; i < neighbors.length; i++){
var neighbor = neighbors[i];
if(!closedSet.includes(neighbor)){
var tempG = current.gScore + neighbor.costOfTravel;
var tempG = current.g + neighbor.costOfTravel;
if(openSet.includes(neighbor)){
if(tempG < neighbor.gScore){
neighbor.gScore = tempG;
if(tempG < neighbor.g){
neighbor.g = tempG;
}
} else {
neighbor.gScore = tempG;
addToOpenSet(openSet, neighbor);
neighbor.g = tempG;
// addToOpenSet(openSet, neighbor);
openSet.push(neighbor);
animationFrame = colorGreen(neighbor,animationFrame);
}
neighbor.heuristic = getDistance(neighbor, goalField);
neighbor.f = neighbor.gScore + neighbor.heuristic;
neighbor.previous = current;
neighbor.h = getDistance(neighbor, goalField);
neighbor.f = neighbor.g + neighbor.h;
neighbor.previous = current;
}
}
}
}
function executePath(agent, path){
}
@ -509,15 +547,22 @@ 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 delayedFunction(func, index, time) {
setTimeout(() => func(), time * index);
}
function start(){
showBoard(board);
let path = aStar(board[0][0], board[7][7]);
//console.log(path);
// for (let i = 0; i<9; i++) {
// delayedStep(agent, i);
// }
const path = aStar(board[0][0], board[9][9]);
// agent.showAgent();
console.log(path.reverse())
}

65
route-planning.md Normal file
View File

@ -0,0 +1,65 @@
# Raport 2
### Definicja pętli głownej strategi przeszukiwania
Naszą główną funkcją jest funkcja `aStar(startField, goalField)`, przyjmuje ona dwa argumenty:
`startField` - pole z którego wyrusza agent
`goalField` - cel drogi agenta
Zwraca ona ścieszkę z `startField` do `goalField`.
`closedSet` - lista zawierająca sprawdzane i sprawdzone pola z listy `openSet`
`openSet` - lista zawierająca pola do sprawdzenia
`path` - lista zawierająca pola tworzące ścieszkę do wybranego celu
Dodajemy pierwszy element który będziemy sprawdzać do listy openSet
`addToOpenSet(openSet, startField)`
Dopuki lista `openSet` nie bedzie pusta sprawdzamy jej elementy
`while(openSet.length > 0)`
Wybieramy najbardziej obiecujący element z zbioru `openSet`
`let current = findLowestFScore(openSet, goalField)`
Jeśli pole `current` okaże się naszym celem tworzymy ścieszkę z punktu startowego do naszego celu.
```javascript
if(current === goalField){
path = []
var temp = current;
path.push(temp);
while(temp.previous){
path.push(temp.previous);
temp = temp.previous
}
```
Następnie kolorujemy tą ścieszkę i kończymy funkcję zwracając ścieszkę `path`
```javascript
for(var i = 0; i < path.length; i++){
colorYellow(path[i]);
}
return path
```
Jeśli pole `current` nie jest naszym celem, to usuwamy je z listy `openSet` i dodajemy do listy `closedSet`
`removeFromSet(openSet, current);`
`addToClosedSet(closedSet, current);`
Pobieramy sąsiadów pola `current`
`var neighbors = current.neighbors;`
Dla każdego sąsiada obliczamy koszt dotarcia do niego z punktu początkowego najlepszą ścieszką
```javascript
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);
}
```
Po przypisaniu kosztu do sąsiada dodajemy jego odległość do celu
`neighbor.heuristic = getDistance(neighbor, goalField);`,
??????????
`neighbor.f = neighbor.gScore + neighbor.heuristic;`
oraz jego poprzednika
`neighbor.previous = current;`
### Definicja funkcji następnika
### Definicja przyjętej heurystyki