From 537173bc70c795f4daee677aea9c69d11be728c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wojciech=20=C5=81ukasik?= Date: Tue, 19 May 2020 13:14:19 +0200 Subject: [PATCH] dodanie decision tree --- .idea/workspace.xml | 31 +- Archiwum/frontend/js/main.js | 1132 +++++++++++----------- __pycache__/data.cpython-37.pyc | Bin 0 -> 1657 bytes __pycache__/decision_tree.cpython-37.pyc | Bin 0 -> 4767 bytes __pycache__/field.cpython-37.pyc | Bin 1367 -> 1383 bytes __pycache__/sweets.cpython-37.pyc | Bin 0 -> 471 bytes candy.py | 9 + data.py | 34 +- decision_tree.py | 184 ++++ field.py | 3 + main.py | 26 +- 11 files changed, 835 insertions(+), 584 deletions(-) create mode 100644 __pycache__/data.cpython-37.pyc create mode 100644 __pycache__/decision_tree.cpython-37.pyc create mode 100644 __pycache__/sweets.cpython-37.pyc create mode 100644 candy.py create mode 100644 decision_tree.py diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 95a134d..e7d3537 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,9 +2,11 @@ - + - + + + + - + \ No newline at end of file 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 0000000000000000000000000000000000000000..90788d557c53ca70d1a55bc79d69a7a29fff6fd0 GIT binary patch literal 1657 zcmb7^&u<$=6vub%-L)OtiPQ8)($FR}5Mo-FGzD5p+qh{|5NUo;BY{R1Fy0w9Td#M_ z>@KdY^hA5+UjT994?yaWF^%b-*3*fuYb?j|_cW4Y1~Zw(3asduti;AxnN?U7 zJPy0!32+VeNoWp%r(i$Kjfz??r;|`0N<`v{$N;PAwj@Vbhr=d}K&SZL(zX4WvIE&cVz$(Au<&@8{ z74Z4Y{yK8L0j{xg>`mA&WPB0)7I*>nOUU^)*vR-Y_;kis9MhR4d3Lp>)mIZ$_qrZi ztpVp@V5_Zqa#$ADb*!L*Ryf*SEkrw18&nIvH1Fzr+SmG?{tcRwdD6B#$AeZK@RX5F z#Z;c@QRh?-c*fZTzWO9P^bF6`b3KPs&yfQ$N3-v9?0{z)&78qG$N%lw(@?hAt=QP4 zG9YDp#Jj?yObmv^?9fp7#0n&5zU%z*{)3bEpX8gQv@fE>V9{vk{Jzxw>fvuwO;Qk! z6g_|F+<&6Pi_KAfb8$0^oM<|8slCu7))o&!kCS4D`qWp4l|1S*IrO_o+N0in(w|mI zIh0Y*rGu`L88&X`sN^TiF(vQtMUA>TF*(< z{@9}ZKKEHYN6OUmcBEjD9R|b<)Tn%?Ni`o&Ppw~Sx59|Iv<<~=4n_Og&Bn^|%5t00 zke=-ixO((*>HWvWup^uUywY8XH?j5!$y~eNq{)1s+COOMXU{6 zwo8*6YEx`=-4H_&5KBmuf)nc&=K=P{w&WtY1JP*w2A7gc(U8L4<&qR{(a$LNJREcz z_4o*lCWWmigo!3IDy_L5^*!PKir+jj-Vxir`y+5vyJm;(7kqwOj<~%m20VG1=5K|P z4mvnwIR75)jmFnhd;Z>+b~v?nm20bRpn&PWOgdg^JL9Uysq|gn`9uv%I#U056>vmc Zyxha&dHgD_p#>N5s^p6&D*FHb{0Y|RyFCB^ literal 0 HcmV?d00001 diff --git a/__pycache__/decision_tree.cpython-37.pyc b/__pycache__/decision_tree.cpython-37.pyc new file mode 100644 index 0000000000000000000000000000000000000000..5b41fddbf90b73b53fd4046cff5d588de6ac5353 GIT binary patch literal 4767 zcmbVPO^@5g8J-!E5~bD3>$PLAKbkPH9Y=}drfJ%wuH$TzAVH0Ek=6kMs7?tkhuWo< zBDF)R>m?|ljaZ%jz4lDee2L zZ_1WzqqXFc?4WJD&*ZY~-es@&aO4&wf`AL+h z(n`0zanNt<1>x3qR+?#;2c;Q%qrexm5f8N{ih+`zL6x!3*nmqe#eKVA@3I-6(aFY! z&#_9+{1u3crKeGCv4VZlneu`^VCUE$#IAXVea%z@OFdDF>#vlCO7?oZG(eV~{`RGJ zZ`{3;O=RfZ{ZJ*tz|YdVKl$M0S6_McmAf+V!!%4{H&a1yeZ2o|`(_lTnV*ctw@6tj z5zt`yGm|*n4FmVrUX)(H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7F&Vekofd-r?h!(3SS;iI@1~F-wsW~Zn1(mlr;^Q;(GE3s)*??M#RVK$U$xp6i ziqsGSa*ITPgeGH=1c(hX5<*A;S-04daxzPDQ&l$qV#;IG0;#ygnv|bdl!C61H!&q8 zJ})&hJtHZg6AVLX5AdCjHK;{=Q0SOi$E(W=agNZ|qgBdKQ$y6i= MlgH3F*`8Gb06xu6X#fBK delta 320 zcmaFPb)AdXiIW=jABp8(@XEDi7?q&+t5CkeN5&;sLj78!gHb^&wkOZ=Bu_fhXmgJ@? zZsuanW7GnvxW$^3pIDTFu8=n|B_%#DH8VXUDZi*#W%44H&oe9MT-jU@=XmA_15@hQ`VMtP%i4Lqz%j diff --git a/__pycache__/sweets.cpython-37.pyc b/__pycache__/sweets.cpython-37.pyc new file mode 100644 index 0000000000000000000000000000000000000000..c1890e13459b48e17170ea71c4e4c1cde2d78a98 GIT binary patch literal 471 zcmYk2%}N6?5XY132Wzni;uDCMJy=SQB2quVixi;;VOb2zCczxMx=W}%1ZrM0$!Y=eIsio- zf 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 033612c..99ade93 100644 --- a/field.py +++ b/field.py @@ -21,6 +21,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..6a0a9cc 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) @@ -61,12 +67,14 @@ def run(): 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()