diff --git a/.idea/misc.xml b/.idea/misc.xml index 8656114..715ed69 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -3,5 +3,5 @@ - + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml index e8d76cf..9bdf8d2 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,10 +2,19 @@ +<<<<<<< HEAD + + + + + + +======= +>>>>>>> upstream/master diff --git a/Archiwum/frontend/js/main.js b/Archiwum/frontend/js/main.js index a0a8c6c..82a2d44 100644 --- a/Archiwum/frontend/js/main.js +++ b/Archiwum/frontend/js/main.js @@ -1,569 +1,569 @@ -//Klasy -class Agent{ - constructor(positionX, positionY, turn){ - this.positionX = positionX; - this.positionY = positionY; - this.turn = turn; - } - - getParams(){ - let params = {}; - - positionX = this.positionX; - positionY = this.positionY; - turn = this.turn; - - params = {positionX, positionY, turn}; - - return params; - } - - turnLeft(){ - if(this.turn == 'Up'){ - this.turn = 'Left'; - } - else if(this.turn == 'Down'){ - this.turn = 'Right'; - } - else if(this.turn == 'Left'){ - this.turn = 'Down'; - } - else if(this.turn == 'Right'){ - this.turn = 'Up' - } - } - - turnRight(){ - if(this.turn == 'Up'){ - this.turn = 'Right'; - } - else if(this.turn == 'Down'){ - this.turn = 'Left'; - } - else if(this.turn == 'Left'){ - this.turn = 'Up'; - } - else if(this.turn == 'Right'){ - this.turn = 'Down' - } - } - - showAgent(){ - if(this.turn === "Up"){ - document.getElementById(this.positionX + "-" + this.positionY).style.backgroundImage = "url('img/Up.png')"; - } - else if(this.turn === "Down"){ - document.getElementById(this.positionX + "-" + this.positionY).style.backgroundImage = "url('img/Down.png')"; - } - else if(this.turn === "Right"){ - document.getElementById(this.positionX + "-" + this.positionY).style.backgroundImage = "url('img/Right.png')"; - } - else if(this.turn === "Left"){ - document.getElementById(this.positionX + "-" + this.positionY).style.backgroundImage = "url('img/Left.png')"; - } - - } - - hideAgent(){ - document.getElementById(this.positionX + "-" + this.positionY).style.backgroundImage = ""; - } - - goForward(){ - this.hideAgent() - 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; - } - 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 - - } - } - - -} - -class Field{ - constructor(x, y, isShelf, isOccupiedByAgent, costOfTravel){ - this.xField = x; - this.yField = y; - this.isShelf = isShelf; - this.isOccupiedByAgent = isOccupiedByAgent; - this.costOfTravel = costOfTravel; - this.neighbors = []; - this.g = 0; - this.h = 0; - this.f = 0; - this.previous = undefined; - } - - getParams(){ - let params = {}; - - xField = this.xField; - yField = this.yField; - isShelf = this.isShelf; - isOccupiedByAgent = this.isOccupiedByAgent; - costOfTravel = this.costOfTravel; - params = {xField, yField, isShelf, isOccupiedByAgent, costOfTravel} - - return params; - } - - setIsShelf(isShelf){ - this.isShelf = isShelf; - } - - getIsShelf(){ - return this.isShelf; - } - - setIsOccupiedByAgent(isOccupiedByAgent){ - this.isOccupiedByAgent = isOccupiedByAgent; - } - - setCostOfTravel(costOfTravel){ - this.costOfTravel = costOfTravel; - } - - setNeighbors(field){ - if(field.isShelf == false){ - this.neighbors.push(field) - } - } -} - -class Shelf{ - constructor(accessY, accessX, havePlace, box1, box2, box3){ - this.accessX = accessX; - this.accessY = accessY; - this.havePlace = true; - this.box1 = box1; - this.box2 = box2; - this.box3 = box3; - } - - getParams(){ - let params = {}; - - xField = this.xField; - yField = this.yField; - havePlace = this.havePlace; - box1 = this.box1; - box2 = this.box2; - box3 = this.box3; - params = {xField, yField, havePlace, box1, box2, box3} - - return params; - } - - setbox1(box1){ - this.box1 = box1; - } - - setbox2(box1){ - this.box2 = box2; - } - - setbox3(box1){ - this.box3 = box3; - } - - setHavePlace(havePlace){ - this.havePlace = havePlace; - } -} - -class Candy{ - constructor(type, taste, mark, id){ - this.type = type; - this.taste = taste; - this.mark = mark; - this.id = id; - } - - getId(){ - return this.id; - } - -} - -//funkcje - -function createBoard(rangeX, rangeY){ - const board = []; - - for(let y = 0; y < rangeY; y++){ - const row = []; - for(let x = 0; x < rangeX; x++){ - let field = new Field(x, y, false, false, 1); - row.push(field) - } - board.push(row) - } - return board -} - -function createShelfOnBoard(board){ - //pierwszy poziom - board[2][2].setIsShelf(true); - board[3][2].setIsShelf(true); - - board[2][4].setIsShelf(true); - board[3][4].setIsShelf(true); - - board[2][6].setIsShelf(true); - board[3][6].setIsShelf(true); - - board[2][8].setIsShelf(true); - board[3][8].setIsShelf(true); - - //drugi poziom - board[5][2].setIsShelf(true); - board[5][3].setIsShelf(true); - - board[5][7].setIsShelf(true); - board[5][8].setIsShelf(true); - - //trzeci poziom - board[7][2].setIsShelf(true); - board[8][2].setIsShelf(true); - - board[7][4].setIsShelf(true); - board[8][4].setIsShelf(true); - - board[7][6].setIsShelf(true); - board[8][6].setIsShelf(true); - - board[7][8].setIsShelf(true); - board[8][8].setIsShelf(true); - return board -} - -function createCostofField(board, rangeX, rangeY){ - let cost; - let number; - for(let y = 0; y < rangeY; y++){ - for(let x = 0; x < rangeX; x++){ - number = 0; - if(y<9 && board[y+1][x].getIsShelf()){ - number = number + 1; - } - if(y>0 && board[y-1][x].getIsShelf()){ - number = number + 1; - } - if(x<9 && board[y][x+1].getIsShelf()){ - number = number + 1; - } - if(x>0 && board[y][x-1].getIsShelf()){ - number = number + 1; - } - cost = number*2; - if(cost==0){ - cost = 1; - } - board[y][x].setCostOfTravel(cost); - } - } - return board; -} - -function createShelf(){ - const listOfShelf = []; - - //pierwszy poziom - let shelf = new Shelf(2, 1, true, 211, 212, 0); - listOfShelf.push(shelf) - shelf = new Shelf(3, 1, true, 311, 0, 0); - listOfShelf.push(shelf) - - shelf = new Shelf(4, 4, false, 441, 442, 443); - listOfShelf.push(shelf) - shelf = new Shelf(1, 4, true, 0, 0, 0); - listOfShelf.push(shelf) - - shelf = new Shelf(4, 6, true, 461, 462, 0); - listOfShelf.push(shelf) - shelf = new Shelf(1, 6, true, 161, 0, 0); - listOfShelf.push(shelf) - - shelf = new Shelf(3, 9, true, 391, 0, 0); - listOfShelf.push(shelf) - shelf = new Shelf(2, 9, true, 291, 0, 0); - listOfShelf.push(shelf) - - //drugi poziom - shelf = new Shelf(5, 1, true, 511, 0, 0); - listOfShelf.push(shelf) - shelf = new Shelf(5, 4, true, 541, 0, 0); - listOfShelf.push(shelf) - - shelf = new Shelf(5, 6, true, 561, 0, 0); - listOfShelf.push(shelf) - shelf = new Shelf(5, 9, true, 591, 0, 0); - listOfShelf.push(shelf) - - //trzeci poziom - shelf = new Shelf(7, 1, true, 711, 0, 0); - listOfShelf.push(shelf) - shelf = new Shelf(8, 1, true, 811, 0, 0); - listOfShelf.push(shelf) - - shelf = new Shelf(6, 4, true, 641, 0, 0); - listOfShelf.push(shelf) - shelf = new Shelf(9, 4, true, 941, 0, 0); - listOfShelf.push(shelf) - - shelf = new Shelf(6, 6, true, 661, 0, 0); - listOfShelf.push(shelf) - shelf = new Shelf(9, 6, true, 961, 0, 0); - listOfShelf.push(shelf) - - shelf = new Shelf(7, 9, true, 791, 0, 0); - listOfShelf.push(shelf) - shelf = new Shelf(8, 9, true, 891, 0, 0); - listOfShelf.push(shelf) - - - return listOfShelf; -} - -function showBoard(board){ - for(let y = board.length - 1; y >= 0 ; y--){ - document.getElementById("board").innerHTML += "
"; - - for(let x = 0; x < board[y].length; x++){ - if(board[y][x].getIsShelf()) - document.getElementById("row-" + y).innerHTML += "
" - else - document.getElementById("row-" + y).innerHTML += "
"+ board[y][x].costOfTravel + "
" - } - } -} - -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"; +// //Klasy +// class Agent{ +// constructor(positionX, positionY, turn){ +// this.positionX = positionX; +// this.positionY = positionY; +// this.turn = turn; +// } +// +// getParams(){ +// let params = {}; +// +// positionX = this.positionX; +// positionY = this.positionY; +// turn = this.turn; +// +// params = {positionX, positionY, turn}; +// +// return params; +// } +// +// turnLeft(){ +// if(this.turn == 'Up'){ +// this.turn = 'Left'; +// } +// else if(this.turn == 'Down'){ +// this.turn = 'Right'; +// } +// else if(this.turn == 'Left'){ +// this.turn = 'Down'; +// } +// else if(this.turn == 'Right'){ +// this.turn = 'Up' +// } +// } +// +// turnRight(){ +// if(this.turn == 'Up'){ +// this.turn = 'Right'; +// } +// else if(this.turn == 'Down'){ +// this.turn = 'Left'; +// } +// else if(this.turn == 'Left'){ +// this.turn = 'Up'; +// } +// else if(this.turn == 'Right'){ +// this.turn = 'Down' +// } +// } +// +// showAgent(){ +// if(this.turn === "Up"){ +// document.getElementById(this.positionX + "-" + this.positionY).style.backgroundImage = "url('img/Up.png')"; +// } +// else if(this.turn === "Down"){ +// document.getElementById(this.positionX + "-" + this.positionY).style.backgroundImage = "url('img/Down.png')"; +// } +// else if(this.turn === "Right"){ +// document.getElementById(this.positionX + "-" + this.positionY).style.backgroundImage = "url('img/Right.png')"; +// } +// else if(this.turn === "Left"){ +// document.getElementById(this.positionX + "-" + this.positionY).style.backgroundImage = "url('img/Left.png')"; +// } +// +// } +// +// hideAgent(){ +// document.getElementById(this.positionX + "-" + this.positionY).style.backgroundImage = ""; +// } +// +// goForward(){ +// this.hideAgent() +// 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; +// } +// 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 +// +// } +// } +// +// // } - -// 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 -} - -const colorYellow = colorFactory('yellow') -const colorGreen = colorFactory('green') -const colorRed = colorFactory('red') - -// function addNeighborsToOpenSet(set, neigbors){ -// for(x = 0; x < neigbors.length; x++){ -// set.push(neigbors[x]); -// colorGreen(neigbors[x]); +// +// class Field{ +// constructor(x, y, isShelf, isOccupiedByAgent, costOfTravel){ +// this.xField = x; +// this.yField = y; +// this.isShelf = isShelf; +// this.isOccupiedByAgent = isOccupiedByAgent; +// this.costOfTravel = costOfTravel; +// this.neighbors = []; +// this.g = 0; +// this.h = 0; +// this.f = 0; +// this.previous = undefined; +// } +// +// getParams(){ +// let params = {}; +// +// xField = this.xField; +// yField = this.yField; +// isShelf = this.isShelf; +// isOccupiedByAgent = this.isOccupiedByAgent; +// costOfTravel = this.costOfTravel; +// params = {xField, yField, isShelf, isOccupiedByAgent, costOfTravel} +// +// return params; +// } +// +// setIsShelf(isShelf){ +// this.isShelf = isShelf; +// } +// +// getIsShelf(){ +// return this.isShelf; +// } +// +// setIsOccupiedByAgent(isOccupiedByAgent){ +// this.isOccupiedByAgent = isOccupiedByAgent; +// } +// +// setCostOfTravel(costOfTravel){ +// this.costOfTravel = costOfTravel; +// } +// +// setNeighbors(field){ +// if(field.isShelf == false){ +// this.neighbors.push(field) +// } // } // } - -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 aStar(startField, goalField){ - let closedSet = []; - let openSet = []; - var path; - let animationFrame = 0 - - // addToOpenSet(openSet, startField) - openSet.push(startField); - colorGreen(startField, animationFrame); - - while(openSet.length > 0){ - - // 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 = [] - 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++){ - animationFrame = colorYellow(path[i], animationFrame); - } - return path; - } - - removeFromSet(openSet, current); - // addToClosedSet(closedSet, current); - closedSet.push(current); - animationFrame = colorRed(current, animationFrame); - - var neighbors = current.neighbors; - - for(var i = 0; i < neighbors.length; i++){ - var neighbor = neighbors[i]; - - if(!closedSet.includes(neighbor)){ - var tempG = current.g + neighbor.costOfTravel; - - if(openSet.includes(neighbor)){ - if(tempG < neighbor.g){ - neighbor.g = tempG; - } - } else { - neighbor.g = tempG; - // addToOpenSet(openSet, neighbor); - openSet.push(neighbor); - animationFrame = colorGreen(neighbor,animationFrame); - } - neighbor.h = getDistance(neighbor, goalField); - neighbor.f = neighbor.g + neighbor.h; - - neighbor.previous = current; - } - } - } -} - -function executePath(agent, path){ - -} - - -let board = createBoard(10,10); -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'); - -addNeighbors(board); - -function delayedFunction(func, index, time) { - setTimeout(() => func(), time * index); -} - -function start(){ - showBoard(board); - - // 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()) -} - - - +// +// class Shelf{ +// constructor(accessY, accessX, havePlace, box1, box2, box3){ +// this.accessX = accessX; +// this.accessY = accessY; +// this.havePlace = true; +// this.box1 = box1; +// this.box2 = box2; +// this.box3 = box3; +// } +// +// getParams(){ +// let params = {}; +// +// xField = this.xField; +// yField = this.yField; +// havePlace = this.havePlace; +// box1 = this.box1; +// box2 = this.box2; +// box3 = this.box3; +// params = {xField, yField, havePlace, box1, box2, box3} +// +// return params; +// } +// +// setbox1(box1){ +// this.box1 = box1; +// } +// +// setbox2(box1){ +// this.box2 = box2; +// } +// +// setbox3(box1){ +// this.box3 = box3; +// } +// +// setHavePlace(havePlace){ +// this.havePlace = havePlace; +// } +// } +// +// class Candy{ +// constructor(type, taste, mark, id){ +// this.type = type; +// this.taste = taste; +// this.mark = mark; +// this.id = id; +// } +// +// getId(){ +// return this.id; +// } +// +// } +// +// //funkcje +// +// function createBoard(rangeX, rangeY){ +// const board = []; +// +// for(let y = 0; y < rangeY; y++){ +// const row = []; +// for(let x = 0; x < rangeX; x++){ +// let field = new Field(x, y, false, false, 1); +// row.push(field) +// } +// board.push(row) +// } +// return board +// } +// +// function createShelfOnBoard(board){ +// //pierwszy poziom +// board[2][2].setIsShelf(true); +// board[3][2].setIsShelf(true); +// +// board[2][4].setIsShelf(true); +// board[3][4].setIsShelf(true); +// +// board[2][6].setIsShelf(true); +// board[3][6].setIsShelf(true); +// +// board[2][8].setIsShelf(true); +// board[3][8].setIsShelf(true); +// +// //drugi poziom +// board[5][2].setIsShelf(true); +// board[5][3].setIsShelf(true); +// +// board[5][7].setIsShelf(true); +// board[5][8].setIsShelf(true); +// +// //trzeci poziom +// board[7][2].setIsShelf(true); +// board[8][2].setIsShelf(true); +// +// board[7][4].setIsShelf(true); +// board[8][4].setIsShelf(true); +// +// board[7][6].setIsShelf(true); +// board[8][6].setIsShelf(true); +// +// board[7][8].setIsShelf(true); +// board[8][8].setIsShelf(true); +// return board +// } +// +// function createCostofField(board, rangeX, rangeY){ +// let cost; +// let number; +// for(let y = 0; y < rangeY; y++){ +// for(let x = 0; x < rangeX; x++){ +// number = 0; +// if(y<9 && board[y+1][x].getIsShelf()){ +// number = number + 1; +// } +// if(y>0 && board[y-1][x].getIsShelf()){ +// number = number + 1; +// } +// if(x<9 && board[y][x+1].getIsShelf()){ +// number = number + 1; +// } +// if(x>0 && board[y][x-1].getIsShelf()){ +// number = number + 1; +// } +// cost = number*2; +// if(cost==0){ +// cost = 1; +// } +// board[y][x].setCostOfTravel(cost); +// } +// } +// return board; +// } +// +// function createShelf(){ +// const listOfShelf = []; +// +// //pierwszy poziom +// let shelf = new Shelf(2, 1, true, 211, 212, 0); +// listOfShelf.push(shelf) +// shelf = new Shelf(3, 1, true, 311, 0, 0); +// listOfShelf.push(shelf) +// +// shelf = new Shelf(4, 4, false, 441, 442, 443); +// listOfShelf.push(shelf) +// shelf = new Shelf(1, 4, true, 0, 0, 0); +// listOfShelf.push(shelf) +// +// shelf = new Shelf(4, 6, true, 461, 462, 0); +// listOfShelf.push(shelf) +// shelf = new Shelf(1, 6, true, 161, 0, 0); +// listOfShelf.push(shelf) +// +// shelf = new Shelf(3, 9, true, 391, 0, 0); +// listOfShelf.push(shelf) +// shelf = new Shelf(2, 9, true, 291, 0, 0); +// listOfShelf.push(shelf) +// +// //drugi poziom +// shelf = new Shelf(5, 1, true, 511, 0, 0); +// listOfShelf.push(shelf) +// shelf = new Shelf(5, 4, true, 541, 0, 0); +// listOfShelf.push(shelf) +// +// shelf = new Shelf(5, 6, true, 561, 0, 0); +// listOfShelf.push(shelf) +// shelf = new Shelf(5, 9, true, 591, 0, 0); +// listOfShelf.push(shelf) +// +// //trzeci poziom +// shelf = new Shelf(7, 1, true, 711, 0, 0); +// listOfShelf.push(shelf) +// shelf = new Shelf(8, 1, true, 811, 0, 0); +// listOfShelf.push(shelf) +// +// shelf = new Shelf(6, 4, true, 641, 0, 0); +// listOfShelf.push(shelf) +// shelf = new Shelf(9, 4, true, 941, 0, 0); +// listOfShelf.push(shelf) +// +// shelf = new Shelf(6, 6, true, 661, 0, 0); +// listOfShelf.push(shelf) +// shelf = new Shelf(9, 6, true, 961, 0, 0); +// listOfShelf.push(shelf) +// +// shelf = new Shelf(7, 9, true, 791, 0, 0); +// listOfShelf.push(shelf) +// shelf = new Shelf(8, 9, true, 891, 0, 0); +// listOfShelf.push(shelf) +// +// +// return listOfShelf; +// } +// +// function showBoard(board){ +// for(let y = board.length - 1; y >= 0 ; y--){ +// document.getElementById("board").innerHTML += "
"; +// +// for(let x = 0; x < board[y].length; x++){ +// if(board[y][x].getIsShelf()) +// document.getElementById("row-" + y).innerHTML += "
" +// else +// document.getElementById("row-" + y).innerHTML += "
"+ board[y][x].costOfTravel + "
" +// } +// } +// } +// +// 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"; +// // } +// +// 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 +// } +// +// const colorYellow = colorFactory('yellow') +// const colorGreen = colorFactory('green') +// const colorRed = colorFactory('red') +// +// // 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 aStar(startField, goalField){ +// let closedSet = []; +// let openSet = []; +// var path; +// let animationFrame = 0 +// +// // addToOpenSet(openSet, startField) +// openSet.push(startField); +// colorGreen(startField, animationFrame); +// +// while(openSet.length > 0){ +// +// // 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 = [] +// 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++){ +// animationFrame = colorYellow(path[i], animationFrame); +// } +// return path; +// } +// +// removeFromSet(openSet, current); +// // addToClosedSet(closedSet, current); +// closedSet.push(current); +// animationFrame = colorRed(current, animationFrame); +// +// var neighbors = current.neighbors; +// +// for(var i = 0; i < neighbors.length; i++){ +// var neighbor = neighbors[i]; +// +// if(!closedSet.includes(neighbor)){ +// var tempG = current.g + neighbor.costOfTravel; +// +// if(openSet.includes(neighbor)){ +// if(tempG < neighbor.g){ +// neighbor.g = tempG; +// } +// } else { +// neighbor.g = tempG; +// // addToOpenSet(openSet, neighbor); +// openSet.push(neighbor); +// animationFrame = colorGreen(neighbor,animationFrame); +// } +// neighbor.h = getDistance(neighbor, goalField); +// neighbor.f = neighbor.g + neighbor.h; +// +// neighbor.previous = current; +// } +// } +// } +// } +// +// function executePath(agent, path){ +// +// } +// +// +// let board = createBoard(10,10); +// 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'); +// +// addNeighbors(board); +// +// function delayedFunction(func, index, time) { +// setTimeout(() => func(), time * index); +// } +// +// function start(){ +// showBoard(board); +// +// // 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()) +// } +// +// +// diff --git a/__pycache__/data.cpython-37.pyc b/__pycache__/data.cpython-37.pyc new file mode 100644 index 0000000..77ec7fd Binary files /dev/null and b/__pycache__/data.cpython-37.pyc differ diff --git a/__pycache__/decision_tree.cpython-37.pyc b/__pycache__/decision_tree.cpython-37.pyc new file mode 100644 index 0000000..5b41fdd Binary files /dev/null and b/__pycache__/decision_tree.cpython-37.pyc differ diff --git a/__pycache__/field.cpython-37.pyc b/__pycache__/field.cpython-37.pyc index bd80bce..2a25f24 100644 Binary files a/__pycache__/field.cpython-37.pyc and b/__pycache__/field.cpython-37.pyc differ diff --git a/__pycache__/shelf.cpython-37.pyc b/__pycache__/shelf.cpython-37.pyc new file mode 100644 index 0000000..12cbc7c Binary files /dev/null and b/__pycache__/shelf.cpython-37.pyc differ diff --git a/__pycache__/sweets.cpython-37.pyc b/__pycache__/sweets.cpython-37.pyc new file mode 100644 index 0000000..c1890e1 Binary files /dev/null and b/__pycache__/sweets.cpython-37.pyc differ diff --git a/candy.py b/candy.py new file mode 100644 index 0000000..186a58b --- /dev/null +++ b/candy.py @@ -0,0 +1,9 @@ + + +class Candy: + def __init__(self, producent, type, price): + + self.producent = producent + self.type = type + self.price = price + diff --git a/data.py b/data.py index ac9ddb2..4c3a444 100644 --- a/data.py +++ b/data.py @@ -25,7 +25,39 @@ def create_data_sweets(): sweet = Sweets('Maoam', 'truskawkowy', 'guma', 'maly', 0.25) allProducts.append(sweet) - return allProducts + return allProducts + + +learning_data = [ + # kolor, kształt, waga, rozmiar, nazwa + ['black', 'rectangle', 51, 'small', 'Mars'], + ['gold', 'pack', 100, 'big', 'Haribo'], + ['purple', 'rectangle', 100, 'big', 'Milka'], + ['brown', 'pack', 45, 'small', 'M&M'], + ['blue', 'rectangle', 50, 'medium', 'Bounty'], + ['blue', 'square', 40, 'small', 'Knoppers'], + ['blue', 'rectangle', 35, 'small', 'Milky-way'], + ['gold', 'rectangle', 40, 'medium', 'Twix'], + ['gold', 'rectangle', 50, 'medium', 'Prince-polo'], + ['brown', 'rectangle', 55, 'medium', 'Snickers'], + ['brown', 'rectangle', 45, 'medium', 'Lion'], + ['white', 'rectangle', 40, 'medium', 'Kinder-bueno'], + ['red', 'rectangle', 50, 'medium', 'Kit-kat'], + ['blue', 'rectangle', 115, 'big', 'Wedel'], + ['white', 'rectangle', 15, 'small', 'Krowka'], + ['red', 'pack', 70, 'medium', 'Skittles'], + ['orange', 'rectangle', 45, 'medium', 'Reeses'], + ['blue', 'rectangle', 55, 'medium', 'Oreo'], + ['gold', 'rectangle', 120, 'big', 'Ferrero-rocher'], + ['white', 'rectangle', 120, 'big', 'Rafaello'], + ['white', 'jar', 600, 'big', 'Nutella'], + ['white', 'rectangle', 25, 'small', 'Duplo'], + ['brown', 'jar', 500, 'big', 'GoOn'], + ['brown', 'jar', 470, 'big', 'Active Orzechowe'], + ['red', 'jar', 250, 'medium', 'Strawberry Jam'], + ['black', 'jar', 250, 'medium', 'Blackberry Jam'], + ['orange', 'jar', 250, 'medium', 'Peach Jam'], +] def create_data_dict(): @@ -52,4 +84,4 @@ def create_data_dict(): sweet = Sweets('Maoam', 'truskawkowy', 'guma', 'maly', 0.25) products_as_dict.append(vars(sweet)) - return products_as_dict + return products_as_dict diff --git a/data.py.orig b/data.py.orig new file mode 100644 index 0000000..d7b921a --- /dev/null +++ b/data.py.orig @@ -0,0 +1,91 @@ +from sweets import Sweets + + +def create_data_sweets(): + allProducts = [] + + sweet = Sweets('Mars', 'czekoladowy', 'baton', 'sredni', 2.49) + allProducts.append(sweet) + sweet = Sweets('Mars', 'czekoladowy', 'czekolada', 'duzy', 4.99) + allProducts.append(sweet) + sweet = Sweets('Mars', 'czekoladowy', 'czekolada', 'ogromny', 11.26) + allProducts.append(sweet) + sweet = Sweets('M&M', 'czekoladowy', 'czekolada', 'duzy', 3.99) + allProducts.append(sweet) + sweet = Sweets('M&M', 'czekoladowy', 'baton', 'sredni', 2.89) + allProducts.append(sweet) + sweet = Sweets('Nestle', 'bananowy', 'landrynka', 'maly', 0.39) + allProducts.append(sweet) + sweet = Sweets('Nestle', 'truskawkowy', 'landrynka', 'maly', 0.39) + allProducts.append(sweet) + sweet = Sweets('Nestle', 'cola', 'landrynka', 'maly', 0.49) + allProducts.append(sweet) + sweet = Sweets('Wedel', 'czekoladowy', 'baton', 'sredni', 1.99) + allProducts.append(sweet) + sweet = Sweets('Maoam', 'truskawkowy', 'guma', 'maly', 0.25) + allProducts.append(sweet) + +<<<<<<< HEAD + return allProducts + + +learning_data = [ + # kolor, kształt, waga, rozmiar, nazwa + ['black', 'rectangle', 51, 'small', 'Mars'], + ['gold', 'pack', 100, 'big', 'Haribo'], + ['purple', 'rectangle', 100, 'big', 'Milka'], + ['brown', 'pack', 45, 'small', 'M&M'], + ['blue', 'rectangle', 50, 'medium', 'Bounty'], + ['blue', 'square', 40, 'small', 'Knoppers'], + ['blue', 'rectangle', 35, 'small', 'Milky-way'], + ['gold', 'rectangle', 40, 'medium', 'Twix'], + ['gold', 'rectangle', 50, 'medium', 'Prince-polo'], + ['brown', 'rectangle', 55, 'medium', 'Snickers'], + ['brown', 'rectangle', 45, 'medium', 'Lion'], + ['white', 'rectangle', 40, 'medium', 'Kinder-bueno'], + ['red', 'rectangle', 50, 'medium', 'Kit-kat'], + ['blue', 'rectangle', 115, 'big', 'Wedel'], + ['white', 'rectangle', 15, 'small', 'Krowka'], + ['red', 'pack', 70, 'medium', 'Skittles'], + ['orange', 'rectangle', 45, 'medium', 'Reeses'], + ['blue', 'rectangle', 55, 'medium', 'Oreo'], + ['gold', 'rectangle', 120, 'big', 'Ferrero-rocher'], + ['white', 'rectangle', 120, 'big', 'Rafaello'], + ['white', 'jar', 600, 'big', 'Nutella'], + ['white', 'rectangle', 25, 'small', 'Duplo'], + ['brown', 'jar', 500, 'big', 'GoOn'], + ['brown', 'jar', 470, 'big', 'Active Orzechowe'], + ['red', 'jar', 250, 'medium', 'Strawberry Jam'], + ['black', 'jar', 250, 'medium', 'Blackberry Jam'], + ['orange', 'jar', 250, 'medium', 'Peach Jam'], +] +======= + return allProducts + + +def create_data_dict(): + products_as_dict = [] + + sweet = Sweets('Mars', 'czekoladowy', 'baton', 'sredni', 2.49) + products_as_dict.append(vars(sweet)) + sweet = Sweets('Mars', 'czekoladowy', 'czekolada', 'duzy', 4.99) + products_as_dict.append(vars(sweet)) + sweet = Sweets('Mars', 'czekoladowy', 'czekolada', 'ogromny', 11.26) + products_as_dict.append(vars(sweet)) + sweet = Sweets('M&M', 'czekoladowy', 'czekolada', 'duzy', 3.99) + products_as_dict.append(vars(sweet)) + sweet = Sweets('M&M', 'czekoladowy', 'baton', 'sredni', 2.89) + products_as_dict.append(vars(sweet)) + sweet = Sweets('Nestle', 'bananowy', 'landrynka', 'maly', 0.39) + products_as_dict.append(vars(sweet)) + sweet = Sweets('Nestle', 'truskawkowy', 'landrynka', 'maly', 0.39) + products_as_dict.append(vars(sweet)) + sweet = Sweets('Nestle', 'cola', 'landrynka', 'maly', 0.49) + products_as_dict.append(vars(sweet)) + sweet = Sweets('Wedel', 'czekoladowy', 'baton', 'sredni', 1.99) + products_as_dict.append(vars(sweet)) + sweet = Sweets('Maoam', 'truskawkowy', 'guma', 'maly', 0.25) + products_as_dict.append(vars(sweet)) + + return products_as_dict +>>>>>>> upstream/master diff --git a/decision_tree.py b/decision_tree.py new file mode 100644 index 0000000..1f6c05b --- /dev/null +++ b/decision_tree.py @@ -0,0 +1,184 @@ +import data + +training_data = data.learning_data + +header = ['color', 'shape', 'weight', 'size', 'name'] + + +# funkcja która zwraca listę unikalnych wartości z każdej kolumny +def uniqie_vals(rows, col): + return set([row[col] for row in rows]) + + +# zliczamy liczbę wystąpień danego typu w zestawie danych +def class_counts(rows): + counts = {} # label -> count + + for row in rows: + name = row[-1] + if name not in counts: + counts[name] = 0 + counts[name] += 1 + return counts + + +# funkcja do sprawdzania czy wartość jest wartością numeryczną +def is_numeric(val): + return isinstance(val, int) or isinstance(val, float) + + +# klasa do zadawania pytań +class Question: + def __init__(self, column, value): + self.column = column + self.value = value + + def match(self, example): + val = example[self.column] + + if is_numeric(val): + return val >= self.value + else: + return val == self.value + + def __repr__(self): + condition = '==' + if is_numeric(self.value): + condition = '>=' + return "Is %s %s %s?" % (header[self.column], condition, str(self.value)) + + +def partition(rows, question): + """ podział zbioru informacji + dla każdego rzędu w zbiorze, sprawdź czy zgadza się z pytaniem, jeśli tak + dodaj do 'true' inaczej dodaj do 'false' """ + true_rows, false_rows = [], [] + for row in rows: + if question.match(row): + true_rows.append(row) + else: + false_rows.append(row) + return true_rows, false_rows + + +def gini(rows): + """ Gini impurity is a measure of how often a randomly chosen element from + the set would be incorrectly labeled if it was randomly labeled according to + the distribution of labels in the subset. """ + + counts = class_counts(rows) + impurity = 1 + for lbl in counts: + prob_of_lbl = counts[lbl] / float(len(rows)) + impurity -= prob_of_lbl ** 2 + return impurity + + +def info_gain(left, right, current_uncertainty): + p = float(len(left)) / (len(left) + len(right)) + return current_uncertainty - p * gini(left) - (1 - p) * gini(right) + + +def find_best_split(rows): + """ znajdź najlepsze możliwe pytanie do zadania, sprawdzając wszystkie + właściwośći oraz licząc dla nich 'info_gain' """ + best_gain = 0 + best_question = None + current_uncertainty = gini(rows) + n_features = len(rows[0]) - 1 + + for col in range(n_features): + values = set([row[col] for row in rows]) + + for val in values: + question = Question(col, val) + + true_rows, false_rows = partition(rows, question) + + if len(true_rows) == 0 or len(false_rows) == 0: + continue + + gain = info_gain(true_rows, false_rows, current_uncertainty) + + if gain > best_gain: + best_gain, best_question = gain, question + + return best_gain, best_question + + +class Leaf: + def __init__(self, rows): + self.predicions = class_counts(rows) + + +class DecisionNode: + def __init__(self, question, true_branch, false_branch): + self.question = question + self.true_branch = true_branch + self.false_branch = false_branch + + +def build_tree(rows): + gain, question = find_best_split(rows) + + if gain == 0: + return Leaf(rows) + + true_rows, false_rows = partition(rows, question) + + true_branch = build_tree(true_rows) + + false_branch = build_tree(false_rows) + + return DecisionNode(question, true_branch, false_branch) + + +def print_tree(node, spacing=""): + if isinstance(node, Leaf): + print(spacing + "Predict", node.predicions) + + else: + print(spacing + str(node.question)) + + print(spacing + '--> True:') + print_tree(node.true_branch, spacing + " ") + + print(spacing + '--> False:') + print_tree(node.false_branch, spacing + " ") + + +def classify(row, node): + if isinstance(node, Leaf): + return node.predicions + + if node.question.match(row): + return classify(row, node.true_branch) + else: + return classify(row, node.false_branch) + + +def print_leaf(counts): + probs = [] + for lbl in counts.keys(): + probs.append(lbl) + return probs + + +# my_tree = build_tree(training_data) +# +# print_tree(my_tree) +# +# testing_data = [ +# ['gold', 'rectangle', 50, 'medium', 'Name'], +# ['brown', 'rectangle', 55, 'medium', 'Snickers'], +# ['white', 'rectangle', 120, 'big', 'Name'] +# ] +# +# test = ['white', 'rectangle', 120, 'big', 'Name'] +# +# # for row in testing_data: +# # print(print_leaf(classify(row, my_tree))) +# +# wynik = print_leaf(classify(test, my_tree))[0] +# print(wynik) + diff --git a/field.py b/field.py index c425d69..d90ffa6 100644 --- a/field.py +++ b/field.py @@ -23,6 +23,9 @@ class Field: self.f = 0 self.previous = None + # Przedmiot, który podnosi agent + self.item = [] + # Te rzeczy są potrzebne do wyświetlenia pola self.image = pygame.image.load('img/Field.png') self.rect = self.image.get_rect() diff --git a/main.py b/main.py index 63bd66d..f71fa65 100644 --- a/main.py +++ b/main.py @@ -2,11 +2,13 @@ import pygame import functions import sys import time +import decision_tree +import data from agent import Agent from settings import Settings from board import create_board, draw_board -from random import randint +from random import randint, choice # Inicjalizacja programu i utworzenie obiektu ekrany @@ -17,10 +19,11 @@ def run(): pygame.display.set_caption("Inteligentny wózek widłowy") agent = Agent(screen, 50, 50, "Down") board = create_board(screen) + my_tree = decision_tree.build_tree(data.learning_data) - for row in board: - for field in row: - print(field.cost_of_travel) + # for row in board: + # for field in row: + # print(field.cost_of_travel) path = [] next_step = None @@ -41,7 +44,10 @@ def run(): agent.move_forward(board) print(agent.x, agent.y) elif event.key == pygame.K_SPACE: - field = board[randint(0, 9)][randint(0, 9)] + board[9][0].item = choice(data.learning_data) + print("Wybrano: " + board[9][0].item[-1]) + board[9][0].item[-1] = 'Something' + field = board[9][0] if not field.is_shelf: path = functions.a_star(board[agent.y][agent.x], field, board) path.pop(len(path) - 1) @@ -56,17 +62,19 @@ def run(): next_step = path.pop() else: next_step = None - print(next_step, path) + # print(next_step, path) for row in board: for field in row: if not field.is_shelf: field.image = pygame.image.load('img/Field.png') - for row in board: - for field in row: - print(field.g, field.h, field.f, field.previous) + else: functions.change_turn(agent, next_step) - print(agent.x, agent.y) + + if board[agent.y][agent.x].item: + prediction = decision_tree.print_leaf(decision_tree.classify(board[agent.y][agent.x].item, my_tree)) + print("Agent uważa, że przedmiot to: " + prediction[0]) + board[agent.y][agent.x].item = [] draw_board(board) agent.blitme()