diff --git a/MainNewTomasz.cpp b/MainNewTomasz.cpp new file mode 100644 index 0000000..15ae452 --- /dev/null +++ b/MainNewTomasz.cpp @@ -0,0 +1,1687 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +const float maxFloat=FLT_MAX; +const int ROW = 27; +const int COL = 27; +typedef pair Pair; +typedef pair> pPair; +struct cell +{ + int parent_i, parent_j; + double f, g, h; +}; + +char pole[27][27][2]; +double poleInt[27][27][2]; +int pozycjaTraktoraX = 1, pozycjaTraktoraY = 1; +char currentWay = 'S'; +char underTraktor = '.'; +double timeToDest = 0.0; +double **weightMatrix; +double **baseMatrix; +double *outputLayer; +double neuroOutputPole[25][25]; +double *inputNeurons; +double **grad; +double **avrGrad; +double numberOfTests; +double oldcost; +bool closedList[ROW][COL]; + +void color(string foregroundColor, string backgroundColor) +{ + HANDLE hOut; + hOut = GetStdHandle(STD_OUTPUT_HANDLE); + int foregroundCode = 15; + if (foregroundColor == "black") + foregroundCode = 0; + if (foregroundColor == "dark_blue") + foregroundCode = 1; + if (foregroundColor == "green") + foregroundCode = 2; + if (foregroundColor == "cyan") + foregroundCode = 3; + if (foregroundColor == "dark_red") + foregroundCode = 4; + if (foregroundColor == "purple") + foregroundCode = 5; + if (foregroundColor == "dark_yellow") + foregroundCode = 6; + if (foregroundColor == "light_gray") + foregroundCode = 7; + if (foregroundColor == "gray") + foregroundCode = 8; + if (foregroundColor == "blue") + foregroundCode = 9; + if (foregroundColor == "lime") + foregroundCode = 10; + if (foregroundColor == "light_blue") + foregroundCode = 11; + if (foregroundColor == "red") + foregroundCode = 12; + if (foregroundColor == "magenta") + foregroundCode = 13; + if (foregroundColor == "yellow") + foregroundCode = 14; + if (foregroundColor == "white") + foregroundCode = 15; + + int backgroundCode = 0; + if (backgroundColor == "black") + backgroundCode = 0; + if (backgroundColor == "dark_blue") + backgroundCode = 1; + if (backgroundColor == "green") + backgroundCode = 2; + if (backgroundColor == "cyan") + backgroundCode = 3; + if (backgroundColor == "dark_red") + backgroundCode = 4; + if (backgroundColor == "purple") + backgroundCode = 5; + if (backgroundColor == "dark_yellow") + backgroundCode = 6; + if (backgroundColor == "light_gray") + backgroundCode = 7; + if (backgroundColor == "gray") + backgroundCode = 8; + if (backgroundColor == "blue") + backgroundCode = 9; + if (backgroundColor == "lime") + backgroundCode = 10; + if (backgroundColor == "light_blue") + backgroundCode = 11; + if (backgroundColor == "red") + backgroundCode = 12; + if (backgroundColor == "magenta") + backgroundCode = 13; + if (backgroundColor == "yellow") + backgroundCode = 14; + if (backgroundColor == "white") + backgroundCode = 15; + SetConsoleTextAttribute(hOut, foregroundCode + backgroundCode * 16); +} + +void SetWindow(int Width, int Height) +{ + _COORD coord; + coord.X = Width; + coord.Y = Height; + + _SMALL_RECT Rect; + Rect.Top = 0; + Rect.Left = 0; + Rect.Bottom = Height - 1; + Rect.Right = Width - 1; + + HANDLE Handle = GetStdHandle(STD_OUTPUT_HANDLE); // Get Handle + SetConsoleScreenBufferSize(Handle, coord); // Set Buffer Size + SetConsoleWindowInfo(Handle, TRUE, &Rect); // Set Window Size +} + +void updatePola() +{ + system("cls"); + for (int i = 0; i < 27; i++) + { + for (int j = 0; j < 27; j++) + { + char item = pole[i][j][0]; + switch (item) + { + case 'B': + { + color("purple", "dark_yellow"); + }break; + case 'Z': + { + color("lime", "dark_yellow"); + }break; + case 'T': + { + color("red", "dark_yellow"); + }break; + case 'G': + { + color("lime", "dark_yellow"); + }break; + case '.': + { + color("yellow", "dark_yellow"); + }break; + case '#': + { + color("light_gray", "gray"); + }break; + } + cout << pole[i][j][0]; + + } + cout << endl; + color("white", "black"); + } +} + +void correctMovement(char wantedWay) +{ + while (currentWay != wantedWay) + { + switch (currentWay) + { + case 'N': + { + if (wantedWay == 'S') + currentWay = wantedWay; + else + currentWay = 'W'; + }break; + case 'S': + { + if (wantedWay == 'N') + currentWay = wantedWay; + else + currentWay = 'W'; + }break; + case 'W': + { + if (wantedWay == 'E') + currentWay = wantedWay; + else + currentWay = 'N'; + }break; + case 'E': + { + if (wantedWay == 'W') + currentWay = wantedWay; + else + currentWay = 'N'; + }break; + } + } +} +void Move(char kierunek) +{ + switch (kierunek) + { + + //góra-(w) + case 'w': + { + if (pole[pozycjaTraktoraY - 1][pozycjaTraktoraX][0] != '#') + { + correctMovement('N'); + pole[pozycjaTraktoraY][pozycjaTraktoraX][0] = underTraktor; + pozycjaTraktoraY--; + underTraktor = pole[pozycjaTraktoraY][pozycjaTraktoraX][0]; + pole[pozycjaTraktoraY][pozycjaTraktoraX][0] = 'T'; + } + updatePola(); + }break; + //dół-(s) + case 's': + { + if (pole[pozycjaTraktoraY + 1][pozycjaTraktoraX][0] != '#') + { + correctMovement('S'); + pole[pozycjaTraktoraY][pozycjaTraktoraX][0] = underTraktor; + pozycjaTraktoraY++; + underTraktor = pole[pozycjaTraktoraY][pozycjaTraktoraX][0]; + pole[pozycjaTraktoraY][pozycjaTraktoraX][0] = 'T'; + } + updatePola(); + }break; + //lewo-(a) + case 'a': + { + if (pole[pozycjaTraktoraY][pozycjaTraktoraX - 1][0] != '#') + { + correctMovement('W'); + pole[pozycjaTraktoraY][pozycjaTraktoraX][0] = underTraktor; + pozycjaTraktoraX--; + underTraktor = pole[pozycjaTraktoraY][pozycjaTraktoraX][0]; + pole[pozycjaTraktoraY][pozycjaTraktoraX][0] = 'T'; + } + updatePola(); + }break; + //prawo-(d) + case 'd': + { + if (pole[pozycjaTraktoraY][pozycjaTraktoraX + 1][0] != '#') + { + correctMovement('E'); + pole[pozycjaTraktoraY][pozycjaTraktoraX][0] = underTraktor; + pozycjaTraktoraX++; + underTraktor = pole[pozycjaTraktoraY][pozycjaTraktoraX][0]; + pole[pozycjaTraktoraY][pozycjaTraktoraX][0] = 'T'; + } + updatePola(); + }break; + } +} + +bool isValid(int x, int y) +{ + if (pole[x][y][0] != '#') + { + return true; + } + return false; +} +bool isDestination(int x, int y,Pair dest) +{ + if (dest.first == x && dest.second == y) + { + return true; + } + return false; +} +double calculateHValue(int x, int y, Pair dest) +{ + return abs(x - dest.first) + abs(y - dest.second); +} +void tracePath(cell cellDetails[][COL], Pair dest, string action) +{ + //printf("\nThe Path is "); //----start info + int row = dest.first; + int col = dest.second; + pairsrc = make_pair(pozycjaTraktoraX, pozycjaTraktoraY); + timeToDest = 0; + + stack Path; + + while (!(cellDetails[row][col].parent_i == row + && cellDetails[row][col].parent_j == col)) + { + Path.push(make_pair(row, col)); + int temp_row = cellDetails[row][col].parent_i; + int temp_col = cellDetails[row][col].parent_j; + row = temp_row; + col = temp_col; + } + + Path.push(make_pair(row, col)); + + while (!Path.empty()) + { + pair p = Path.top(); + Path.pop(); + if (action == "move" || action == "moveWithTime" || action == "moveWithPick") + { + if (p.first > pozycjaTraktoraX) + Move('d'); + if (p.first < pozycjaTraktoraX) + Move('a'); + if (p.second > pozycjaTraktoraY) + Move('s'); + if (p.second < pozycjaTraktoraY) + Move('w'); + Sleep(100); + } + if ((action == "pick" || action == "moveWithPick") && p.first == dest.first && p.second==dest.second) + { + underTraktor = '.'; + pole[pozycjaTraktoraY][pozycjaTraktoraX][1] = '1'; + poleInt[pozycjaTraktoraY][pozycjaTraktoraX][0] = 0; + poleInt[pozycjaTraktoraY][pozycjaTraktoraX][1] = 0; + } + if (action == "time" || action == "moveWithTime") + { + if ((p.first != src.first || p.second != src.second) + && (p.first != dest.first || p.second != dest.second)) + { + timeToDest += ((int)pole[p.second][p.first][1] - 48)*1.0; + } + } + + //printf("-> (%d,%d) ", p.first, p.second); //---- informacja wierzchołku + + } + if (action == "time" || action == "moveWithTime") + { + timeToDest /= 2; + } + + return; +} +void aStarSearch(int grid[][COL], Pair src, Pair dest, string action) +{ + //bool closedList[ROW][COL]; + //memset(closedList, false, sizeof(closedList)); + + + cell cellDetails[ROW][COL]; + + int i, j; + for (i = 0; i < ROW; i++) + { + for (j = 0; j < COL; j++) + { + closedList[i][j] = false; + cellDetails[i][j].f = maxFloat; + cellDetails[i][j].g = maxFloat; + cellDetails[i][j].h = maxFloat; + cellDetails[i][j].parent_i = -1; + cellDetails[i][j].parent_j = -1; + } + } + i = src.first, j = src.second; + cellDetails[i][j].f = 0.0; + cellDetails[i][j].g = 0.0; + cellDetails[i][j].h = 0.0; + cellDetails[i][j].parent_i = i; + cellDetails[i][j].parent_j = j; + + set openList; + openList.insert(make_pair(0.0, make_pair(i, j))); + bool foundDest = false; + + while (!openList.empty()) + { + pPair p = *openList.begin(); + openList.erase(openList.begin()); + i = p.second.first; + j = p.second.second; + closedList[i][j] = true; + + double gNew, hNew, fNew; + double waga = 1.0; + waga = ((double)pole[j][i][1] - 48)*1.0;//----waga + + //----------- 1st Successor (North) ------------ + if (isValid(i - 1, j) == true) + { + if (isDestination(i - 1, j, dest) == true) + { + cellDetails[i - 1][j].parent_i = i; + cellDetails[i - 1][j].parent_j = j; + //printf("The destination cell is found\n"); + tracePath(cellDetails, dest, action); + foundDest = true; + return; + } + else if (closedList[i - 1][j] == false) + { + gNew = cellDetails[i][j].g + waga; + hNew = calculateHValue(i - 1, j, dest); + fNew = gNew + hNew; + + if (cellDetails[i - 1][j].f == maxFloat || + cellDetails[i - 1][j].f > fNew) + { + openList.insert(make_pair(fNew, + make_pair(i - 1, j))); + + + cellDetails[i - 1][j].f = fNew; + cellDetails[i - 1][j].g = gNew; + cellDetails[i - 1][j].h = hNew; + cellDetails[i - 1][j].parent_i = i; + cellDetails[i - 1][j].parent_j = j; + } + } + } + + //----------- 2nd Successor (South) ------------ + if (isValid(i + 1, j) == true) + { + if (isDestination(i + 1, j, dest) == true) + { + cellDetails[i + 1][j].parent_i = i; + cellDetails[i + 1][j].parent_j = j; + //printf("The destination cell is found\n"); + tracePath(cellDetails, dest, action); + foundDest = true; + return; + } + else if (closedList[i + 1][j] == false) + { + gNew = cellDetails[i][j].g + waga; + hNew = calculateHValue(i + 1, j, dest); + fNew = gNew + hNew; + if (cellDetails[i + 1][j].f == maxFloat || + cellDetails[i + 1][j].f > fNew) + { + openList.insert(make_pair(fNew, make_pair(i + 1, j))); + cellDetails[i + 1][j].f = fNew; + cellDetails[i + 1][j].g = gNew; + cellDetails[i + 1][j].h = hNew; + cellDetails[i + 1][j].parent_i = i; + cellDetails[i + 1][j].parent_j = j; + } + } + } + + //----------- 3rd Successor (East) ------------ + if (isValid(i, j + 1) == true) + { + if (isDestination(i, j + 1, dest) == true) + { + cellDetails[i][j + 1].parent_i = i; + cellDetails[i][j + 1].parent_j = j; + //printf("The destination cell is found\n"); + tracePath(cellDetails, dest, action); + foundDest = true; + return; + } + else if (closedList[i][j + 1] == false) + { + gNew = cellDetails[i][j].g + waga; + hNew = calculateHValue(i, j + 1, dest); + fNew = gNew + hNew; + if (cellDetails[i][j + 1].f == maxFloat || + cellDetails[i][j + 1].f > fNew) + { + openList.insert(make_pair(fNew, + make_pair(i, j + 1))); + cellDetails[i][j + 1].f = fNew; + cellDetails[i][j + 1].g = gNew; + cellDetails[i][j + 1].h = hNew; + cellDetails[i][j + 1].parent_i = i; + cellDetails[i][j + 1].parent_j = j; + } + } + } + + //----------- 4th Successor (West) ------------ + if (isValid(i, j - 1) == true) + { + if (isDestination(i, j - 1, dest) == true) + { + cellDetails[i][j - 1].parent_i = i; + cellDetails[i][j - 1].parent_j = j; + //printf("The destination cell is found\n"); + tracePath(cellDetails, dest, action); + foundDest = true; + return; + } + else if (closedList[i][j - 1] == false) + { + gNew = cellDetails[i][j].g + waga; + hNew = calculateHValue(i, j - 1, dest); + fNew = gNew + hNew; + if (cellDetails[i][j - 1].f == maxFloat || + cellDetails[i][j - 1].f > fNew) + { + openList.insert(make_pair(fNew, + make_pair(i, j - 1))); + cellDetails[i][j - 1].f = fNew; + cellDetails[i][j - 1].g = gNew; + cellDetails[i][j - 1].h = hNew; + cellDetails[i][j - 1].parent_i = i; + cellDetails[i][j - 1].parent_j = j; + } + } + } + } + + /*if (foundDest == false) + printf("Failed to find the Destination Cell\n");*/ + + return; +} + +void gogo(int endX,int endY) +{ + updatePola(); + Sleep(1000); + int grid[27][27]; + for (int i = 0; i < 27; i++) + { + for (int j = 0; j < 27; j++) + { + grid[i][j] = 0; + } + } + Pair src = make_pair(pozycjaTraktoraX, pozycjaTraktoraY); + Pair dest = make_pair(endX, endY); + //aStarSearch(grid, src, dest, "time"); + //aStarSearch(grid, src, dest, "move"); + aStarSearch(grid, src, dest, "moveWithPick"); + //aStarSearch(grid, src, dest, "moveWithTime"); + //cout << timeToDest; +} +double countTimeToDest(int endX, int endY) +{ + //updatePola(); + int grid[27][27]; + for (int i = 0; i < 27; i++) + { + for (int j = 0; j < 27; j++) + { + grid[i][j] = 0; + } + } + Pair src = make_pair(pozycjaTraktoraX, pozycjaTraktoraY); + Pair dest = make_pair(endX, endY); + aStarSearch(grid, src, dest, "time"); + return timeToDest; +} + + +double Sigmoid(double number) +{ + int tempInt = 0; + if (number < 0) + { + tempInt = 1; + } + return tempInt + (number / (1.0 + abs(number))); +} +double pSigmoid(double number) +{ + int tempInt = 1; + if (number < 0) + { + tempInt = -1; + } + return tempInt * (number / ((1.0 + abs(number))*(1.0 + abs(number)))); +} +double lookOfVege(int x, int y) +{ + int state = poleInt[y][x][1]; + int proOrFer = poleInt[y][x][0]; + if (state == 0)// - brak + { + return 0.0; + } + if (state >= 1 && state < 15)// - kiełek + { + return 1.0; + } + if (state >= 15 && state < 30)// - młoda roślina + { + return 2.0; + } + if (state >= 30 && state < 60)// - dojrzała + { + return 3.0; + } + if (state >= 60 && state < 85)// - przejrzała + { + if (proOrFer == 2 && state < 70)// - z środkiem dojrzała + { + return 3.0; + } + return 4.0; + } + if (state >= 85 && state <= 100)// - zniszczona + { + if (proOrFer == 2 && state < 90)// - z środkiem przejrzała + { + return 4.0; + } + return 5.0; + } +} +double setValusesRange(double a, double b, double num) +{ + int temp = 1; + if (a > b) + { + temp = -1; + } + double avr = ((a + b) / 2)*temp; + return Sigmoid(num - avr); +} +void gradient(double desiredOutput[25][25]) +{ + const int numberOfCellsInPole = (25 * 25); + const int inputNeuronsCount = numberOfCellsInPole * 4; + double z; + for (int i = 0; i < numberOfCellsInPole; i++) + { + for (int j = 0; j < inputNeuronsCount; j++) + { + if (weightMatrix[i][j] != 0) + { + int x, y; + y = i / 25; + x = i % 25; + grad[i][j] = 2 * pSigmoid(weightMatrix[i][j] * inputNeurons[j]) * inputNeurons[j] * (neuroOutputPole[y][x] - desiredOutput[y][x]); + } + else + { + grad[i][j] = 0; + } + } + } + //cout << "grad set" << endl; +} + +void matrixFromFile() +{ + ifstream file; + file.open("matrix.txt"); + string line; + int im = 0; + while (getline(file, line)) { + istringstream iss(line); + double a, b, c, d; + iss >> a >> b >> c >> d; + weightMatrix[im][(im * 4)] = a; + weightMatrix[im][(im * 4) + 1] = b; + weightMatrix[im][(im * 4) + 2] = c; + weightMatrix[im][(im * 4) + 3] = d; + im++; + } + file.close(); +} + +void buildFirstMatrix() +{ + const int numberOfCellsInPole = (25 * 25); + const int inputNeuronsCount = numberOfCellsInPole * 4; + weightMatrix = (double **)malloc(numberOfCellsInPole * sizeof(double *)); + inputNeurons = (double *)malloc(inputNeuronsCount * sizeof(double)); + outputLayer = (double *)malloc(numberOfCellsInPole * sizeof(double)); + //memset(closedList, false, sizeof(closedList)); + for (int i = 0; i < numberOfCellsInPole; i++) + { + weightMatrix[i] = (double *)malloc(inputNeuronsCount * sizeof(double)); + } +} +void buildMatrix() +{ + const int numberOfCellsInPole = (25 * 25); + const int inputNeuronsCount = numberOfCellsInPole * 4; + for (int i = 0; i < numberOfCellsInPole; i++) + { + for (int j = 0; j < inputNeuronsCount; j++) + { + weightMatrix[i][j] = 0.0; + } + } + matrixFromFile(); + /*for (int i = 0; i < numberOfCellsInPole; i++) + { + for (int j = 0; j < inputNeuronsCount; j++) + { + if (j >= (i * 4) && j < ((i + 1) * 4)) + { + weightMatrix[i][j] = 1.0; + } + else + { + weightMatrix[i][j] = 0.0; + } + } + }*/ +} +void buildAvrGrad() +{ + const int numberOfCellsInPole = (25 * 25); + const int inputNeuronsCount = numberOfCellsInPole * 4; + avrGrad = (double **)malloc(numberOfCellsInPole * sizeof(double *)); + baseMatrix = (double **)malloc(numberOfCellsInPole * sizeof(double *)); + grad = (double **)malloc(numberOfCellsInPole * sizeof(double *)); + for (int i = 0; i < numberOfCellsInPole; i++) + { + avrGrad[i] = (double *)malloc(inputNeuronsCount * sizeof(double)); + baseMatrix[i] = (double *)malloc(inputNeuronsCount * sizeof(double)); + grad[i] = (double *)malloc(inputNeuronsCount * sizeof(double)); + } + for (int i = 0; i < numberOfCellsInPole; i++) + { + for (int j = 0; j < inputNeuronsCount; j++) + { + avrGrad[i][j] = 0; + baseMatrix[i][j] = weightMatrix[i][j]; + } + } +} + +double neuronsInputBuild(double desiredOutput[25][25]) +{ + const int numberOfCellsInPole = (25 * 25);// -1; + const int inputNeuronsCount = numberOfCellsInPole * 4; + + double typeOfVege[numberOfCellsInPole]; + double timeToGetToVege[numberOfCellsInPole]; + double protectOrFertilize[numberOfCellsInPole]; + double stateOfVege[numberOfCellsInPole]; + + for (int i = 1; i <= 25; i++) + { + for (int j = 1; j <= 25; j++) + { + int tempCell = (((i - 1) * 25) + (j - 1)); + if (pole[i][j][0] == 'T') + { + /*if (j >= pozycjaTraktoraX && i >= pozycjaTraktoraY) + { + int tempCell = (((i - 1) * 25) + (j - 1))-1; + }*/ + typeOfVege[tempCell] = 0;//type after weight 1-9 + timeToGetToVege[tempCell] = 0;//time x.0 + protectOrFertilize[tempCell] = 0;//0.0 1.0 2.0 3.0 + stateOfVege[tempCell] = 0;//0.0-5.0 + } + else + { + typeOfVege[tempCell] = setValusesRange(1, 9, ((double)pole[i][j][1]-48));//type after weight 1-9 + timeToGetToVege[tempCell] = setValusesRange( 0, 25 * 25 * 9, countTimeToDest(j, i));//time x.0 + protectOrFertilize[tempCell] = setValusesRange(3, 0, poleInt[i][j][0]);//0.0 1.0 2.0 3.0 + stateOfVege[tempCell] = setValusesRange(0, 5, lookOfVege(j, i));//0.0-5.0 + } + } + } + //cout << "set neurons"; + for (int i = 0; i < numberOfCellsInPole; i++) + { + inputNeurons[i * 4] = Sigmoid(typeOfVege[i]); + inputNeurons[(i * 4) + 1] = Sigmoid(timeToGetToVege[i]); + inputNeurons[(i * 4) + 2] = Sigmoid(protectOrFertilize[i]); + inputNeurons[(i * 4) + 3] = Sigmoid(stateOfVege[i]); + } + + /*double **weightMatrix = (double **)malloc(numberOfCellsInPole * sizeof(double *)); + for (int i = 0; i < numberOfCellsInPole; i++) + { + weightMatrix[i] = (double *)malloc(inputNeuronsCount * sizeof(double)); + } + for (int i = 0; i < numberOfCellsInPole; i++) + { + for (int j = 0; j < inputNeuronsCount; j++) + { + if (j >= (i * 4) && j < ((i + 1) * 4)) + { + weightMatrix[i][j] = 1; + } + else + { + weightMatrix[i][j] = 0; + } + } + }*/ + //0 1 2 inp + //1 + //2 + //num + + //inp -> a inp(0-3) + //inp -> a1 inp(4-7) + //inp -> a2 inp(8-11) + + //updatePola(); + //cout << "matrix setup"; + + //firstHiddenLayer(); + //updatePola(); + for (int i = 0; i < numberOfCellsInPole; i++) + { + double sum = 0; + for (int j = 0; j < inputNeuronsCount; j++) + { + sum += weightMatrix[i][j] * inputNeurons[j]; + } + outputLayer[i] = Sigmoid(sum); + } + for (int i = 0; i < 25; i++) + { + for (int j = 0; j < 25; j++) + { + int tempCell = ((i * 25) + j); + neuroOutputPole[i][j] = outputLayer[tempCell]; + } + } + double cost = 0.0; + for (int i = 0; i < 25; i++) + { + for (int j = 0; j < 25; j++) + { + double tempNum = neuroOutputPole[i][j] - desiredOutput[i][j]; + cost += (tempNum*tempNum); + } + } + //updatePola(); + return cost; +} +void backProp(double desiredOuput[25][25]) +{ + const int numberOfCellsInPole = (25 * 25); + const int inputNeuronsCount = numberOfCellsInPole * 4; + double cost; + cost = neuronsInputBuild(desiredOuput); + oldcost = cost + 2; + int i = 0; + while ((abs(cost - oldcost) > 0.05 || cost > 1) && i < 100) + { + cout << i << "-" << cost << " "; + gradient(desiredOuput); + + for (int i = 0; i < numberOfCellsInPole; i++) + { + for (int j = 0; j < inputNeuronsCount; j++) + { + weightMatrix[i][j] -= grad[i][j]; + } + } + oldcost = cost; + cost = neuronsInputBuild(desiredOuput); + i++; + } + cout << "--END--" << endl; + for (int i = 0; i < numberOfCellsInPole; i++) + { + for (int j = 0; j < inputNeuronsCount; j++) + { + if (weightMatrix[i][j] != 0) + { + avrGrad[i][j] += ((baseMatrix[i][j] - weightMatrix[i][j])/numberOfTests); + } + } + } +} +void network(int desiredX,int desiredY) +{ + double desiredPole[25][25]; + for (int i = 0; i < 25; i++) + { + for (int j = 0; j < 25; j++) + { + desiredPole[i][j] = 0; + } + } + desiredPole[desiredY - 1][desiredX - 1] = 1; + //double cost = neuronsInputBuild(desiredPole); + backProp(desiredPole); +} +void network2(int noDesiredX, int noDesiredY) +{ + double desiredPole[25][25]; + for (int i = 0; i < 25; i++) + { + for (int j = 0; j < 25; j++) + { + desiredPole[i][j] = 1; + } + } + desiredPole[noDesiredY - 1][noDesiredX - 1] = 0; + backProp(desiredPole); +} +void bestMatrixBuild() +{ + const int numberOfCellsInPole = (25 * 25); + const int inputNeuronsCount = numberOfCellsInPole * 4; + for (int i = 0; i < numberOfCellsInPole; i++) + { + for (int j = 0; j < inputNeuronsCount; j++) + { + weightMatrix[i][j] -= avrGrad[i][j]; + } + } +} + +void matrixToFile() +{ + const int numberOfCellsInPole = (25 * 25); + const int inputNeuronsCount = numberOfCellsInPole * 4; + ofstream file; + file.open("matrix.txt"); + file.clear(); + if (file.is_open()) + { + for (int i = 0; i < numberOfCellsInPole; i++) + { + file << weightMatrix[i][i * 4] << " "; + file << weightMatrix[i][i * 4 + 1] << " "; + file << weightMatrix[i][i * 4 + 2] << " "; + file << weightMatrix[i][i * 4 + 3] << "\n"; + } + file.close(); + } +} + +void test1() +{ + pole[1][3][0] = 'B'; + pole[1][3][1] = '9'; + pole[3][1][0] = 'B'; + pole[3][1][1] = '9'; + poleInt[1][3][1] = 1; + poleInt[3][1][1] = 1; +} +void test2() +{ + for (int i = 1; i < 26; i++) + { + for (int j = 1; j < i; j++) + { + pole[i][j][0] = 'B'; + pole[i][j][1] = '9'; + poleInt[i][j][1] = 1; + } + } + test1(); + updatePola(); +} +void baseSetup() +{ + buildAvrGrad(); + buildMatrix(); + for (int i = 0; i < 25; i++) + { + pole[i + 1][i + 1][0] = 'B'; + pole[i + 1][i + 1][1] = '9'; + poleInt[i + 1][i + 1][1] = 1; + } + pole[1][1][0] = 'T'; + pole[1][1][1] = '1'; + updatePola(); +} + +void start1() +{ + int goalX = 3, goalY = 4; + test1(); + baseSetup(); + pole[goalY][goalX][0] = 'G'; + pole[goalY][goalX][1] = '9'; + pole[goalY][goalX][1] = 1; + gogo(goalX, goalY); +} +void start2() +{ + int goalX = 6, goalY = 6; + test2(); + baseSetup(); + pole[goalY][goalX][0] = 'G'; + pole[goalY][goalX][1] = '9'; + pole[goalY][goalX][1] = 1; + gogo(goalX, goalY); +} +void start3() +{ + int goalX = 6, goalY = 9; + test2(); + baseSetup(); + pole[goalY][goalX][0] = 'G'; + pole[goalY][goalX][1] = '9'; + pole[goalY][goalX][1] = 1; + gogo(goalX, goalY); +} + +void neuroTestS() +{ + int sx=6, sy=6; + pole[sy][sx][0] = 'B'; + pole[sy][sx][1] = '9'; + poleInt[sy][sx][0] = 0; + poleInt[sy][sx][1] = 70; + updatePola(); + network(sx, 5); + pole[sy][sx][0] = '.'; + pole[sy][sx][1] = '1'; + poleInt[sy][sx][0] = 0; + poleInt[sy][sx][1] = 0; + updatePola(); +} +void neuroTest1(int bX,int bY) +{ + pole[bY][bX][0] = 'B'; + pole[bY][bX][1] = '9'; + poleInt[bY][bX][0] = 0; + poleInt[bY][bX][1] = 70; + updatePola(); + network(bX, bY); + pole[bY][bX][0] = '.'; + pole[bY][bX][1] = '1'; + poleInt[bY][bX][0] = 0; + poleInt[bY][bX][1] = 0; + updatePola(); +} +void neuroTest2(int bX, int bY) +{ + pole[bY][bX][0] = 'B'; + pole[bY][bX][1] = '9'; + poleInt[bY][bX][0] = 0; + poleInt[bY][bX][1] = 8; + updatePola(); + network2(bX, bY); + pole[bY][bX][0] = '.'; + pole[bY][bX][1] = '1'; + poleInt[bY][bX][0] = 0; + poleInt[bY][bX][1] = 0; + updatePola(); +} +void neuroTest3(int bX, int bY, int nX, int nY) +{ + pole[bY][bX][0] = 'B'; + pole[bY][bX][1] = '9'; + poleInt[bY][bX][0] = 0; + poleInt[bY][bX][1] = 70; + pole[nY][nX][0] = 'B'; + pole[nY][nX][1] = '9'; + poleInt[nY][nX][0] = 0; + poleInt[nY][nX][1] = 8; + updatePola(); + network(bX, bY); + pole[bY][bX][0] = '.'; + pole[bY][bX][1] = '1'; + poleInt[bY][bX][0] = 0; + poleInt[bY][bX][1] = 0; + pole[nY][nX][0] = '.'; + pole[nY][nX][1] = '1'; + poleInt[nY][nX][0] = 0; + poleInt[nY][nX][1] = 0; + updatePola(); +} +void neuroTest1Z(int bX, int bY) +{ + pole[bY][bX][0] = 'Z'; + pole[bY][bX][1] = '5'; + poleInt[bY][bX][0] = 0; + poleInt[bY][bX][1] = 70; + updatePola(); + network(bX, bY); + pole[bY][bX][0] = '.'; + pole[bY][bX][1] = '1'; + poleInt[bY][bX][0] = 0; + poleInt[bY][bX][1] = 0; + updatePola(); +} +void neuroTest2Z(int bX, int bY) +{ + pole[bY][bX][0] = 'Z'; + pole[bY][bX][1] = '5'; + poleInt[bY][bX][0] = 0; + poleInt[bY][bX][1] = 8; + updatePola(); + network2(bX, bY); + pole[bY][bX][0] = '.'; + pole[bY][bX][1] = '1'; + poleInt[bY][bX][0] = 0; + poleInt[bY][bX][1] = 0; + updatePola(); +} +void neuroTest3Z(int bX, int bY, int nX, int nY) +{ + pole[bY][bX][0] = 'Z'; + pole[bY][bX][1] = '5'; + poleInt[bY][bX][0] = 0; + poleInt[bY][bX][1] = 70; + pole[nY][nX][0] = 'Z'; + pole[nY][nX][1] = '5'; + poleInt[nY][nX][0] = 0; + poleInt[nY][nX][1] = 8; + updatePola(); + network(bX, bY); + pole[bY][bX][0] = '.'; + pole[bY][bX][1] = '1'; + poleInt[bY][bX][0] = 0; + poleInt[bY][bX][1] = 0; + pole[nY][nX][0] = '.'; + pole[nY][nX][1] = '1'; + poleInt[nY][nX][0] = 0; + poleInt[nY][nX][1] = 0; + updatePola(); +} +void neuroTestZB(int bX, int bY, int zX, int zY) +{ + pole[bY][bX][0] = 'B'; + pole[bY][bX][1] = '9'; + poleInt[bY][bX][0] = 0; + poleInt[bY][bX][1] = 70; + pole[zY][zX][0] = 'Z'; + pole[zY][zX][1] = '5'; + poleInt[zY][zX][0] = 0; + poleInt[zY][zX][1] = 70; + updatePola(); + network(bX, bY); + pole[bY][bX][0] = '.'; + pole[bY][bX][1] = '1'; + poleInt[bY][bX][0] = 0; + poleInt[bY][bX][1] = 0; + pole[zY][zX][0] = '.'; + pole[zY][zX][1] = '1'; + poleInt[zY][zX][0] = 0; + poleInt[zY][zX][1] = 0; + updatePola(); +} +void neuroTestZZB(int bX, int bY, int zX, int zY,int z2X,int z2Y) +{ + pole[bY][bX][0] = 'B'; + pole[bY][bX][1] = '9'; + poleInt[bY][bX][0] = 0; + poleInt[bY][bX][1] = 10; + pole[zY][zX][0] = 'Z'; + pole[zY][zX][1] = '5'; + poleInt[zY][zX][0] = 0; + poleInt[zY][zX][1] = 70; + pole[z2Y][z2X][0] = 'Z'; + pole[z2Y][z2X][1] = '5'; + poleInt[z2Y][z2X][0] = 0; + poleInt[z2Y][z2X][1] = 10; + updatePola(); + network(zX, zY); + pole[bY][bX][0] = '.'; + pole[bY][bX][1] = '1'; + poleInt[bY][bX][0] = 0; + poleInt[bY][bX][1] = 0; + pole[zY][zX][0] = '.'; + pole[zY][zX][1] = '1'; + poleInt[zY][zX][0] = 0; + poleInt[zY][zX][1] = 0; + pole[z2Y][z2X][0] = '.'; + pole[z2Y][z2X][1] = '1'; + poleInt[z2Y][z2X][0] = 0; + poleInt[z2Y][z2X][1] = 0; + updatePola(); +} +void neuroTestZBB(int bX, int bY, int b2X, int b2Y, int zX, int zY) +{ + pole[bY][bX][0] = 'B'; + pole[bY][bX][1] = '9'; + poleInt[bY][bX][0] = 0; + poleInt[bY][bX][1] = 70; + pole[b2Y][b2X][0] = 'B'; + pole[b2Y][b2X][1] = '9'; + poleInt[b2Y][b2X][0] = 0; + poleInt[b2Y][b2X][1] = 10; + pole[zY][zX][0] = 'Z'; + pole[zY][zX][1] = '5'; + poleInt[zY][zX][0] = 0; + poleInt[zY][zX][1] = 70; + updatePola(); + network(bX, bY); + pole[bY][bX][0] = '.'; + pole[bY][bX][1] = '1'; + poleInt[bY][bX][0] = 0; + poleInt[bY][bX][1] = 0; + pole[b2Y][b2X][0] = '.'; + pole[b2Y][b2X][1] = '1'; + poleInt[b2Y][b2X][0] = 0; + poleInt[b2Y][b2X][1] = 0; + pole[zY][zX][0] = '.'; + pole[zY][zX][1] = '1'; + poleInt[zY][zX][0] = 0; + poleInt[zY][zX][1] = 0; + updatePola(); +} +void neuroTestX(int bX, int bY) +{ + srand(time(NULL)); + int L, E; + for (int i = -1; i < 2; i++) + { + for (int j = -1; j < 2; j++) + { + L = rand() % 2 + 1; + E = rand() % 14 + 1; + if (L == 1) + { + pole[bY + i][bX + j][0] = 'B'; + pole[bY + i][bX + j][1] = '9'; + } + else + { + pole[bY + i][bX + j][0] = 'Z'; + pole[bY + i][bX + j][1] = '5'; + } + poleInt[bY + i][bX + j][0] = 0; + poleInt[bY + i][bX + j][1] = E; + } + } + pole[bY][bX][0] = 'B'; + pole[bY][bX][1] = '9'; + poleInt[bY][bX][0] = 0; + poleInt[bY][bX][1] = 70; + updatePola(); + network(bX, bY); + for (int i = -1; i < 2; i++) + { + for (int j = -1; j < 2; j++) + { + pole[bY + i][bX + j][0] = '.'; + pole[bY + i][bX + j][1] = '1'; + poleInt[bY + i][bX + j][0] = 0; + poleInt[bY + i][bX + j][1] = 0; + } + } + updatePola(); +} +void neuroTestSX() +{ + numberOfTests = 26; + for (int i = 1; i < 25; i++) + { + pole[i + 1][i + 1][0] = 'B'; + pole[i + 1][i + 1][1] = '9'; + poleInt[i + 1][i + 1][0] = 0; + poleInt[i + 1][i + 1][1] = 1; + } + pole[1][3][0] = 'Z'; + pole[1][3][1] = '9'; + poleInt[1][3][0] = 0; + poleInt[1][3][1] = 60; + pole[1][4][0] = 'B'; + pole[1][4][1] = '9'; + poleInt[1][4][0] = 0; + poleInt[1][4][1] = 70; + updatePola(); + network(4, 1); + gogo(4, 1); + updatePola(); + buildMatrix(); + network(3, 1); + gogo(3, 1); + updatePola(); + buildMatrix(); + for (int i = 2; i < 26; i++) + { + network(i, i); + gogo(i, i); + updatePola(); + buildMatrix(); + } + updatePola(); +} +void neuroTestSX2() +{ + numberOfTests = 27; + for (int i = 2; i < 26; i++) + { + pole[i][26 - i][0] = 'B'; + pole[i][26 - i][1] = '9'; + poleInt[i][26 - i][0] = 0; + poleInt[i][26 - i][1] = 1; + } + pole[10][3][0] = 'Z'; + pole[10][3][1] = '9'; + poleInt[10][3][0] = 0; + poleInt[10][3][1] = 60; + pole[10][4][0] = 'B'; + pole[10][4][1] = '9'; + poleInt[10][4][0] = 0; + poleInt[10][4][1] = 70; + pole[10][15][0] = 'Z'; + pole[10][15][1] = '9'; + poleInt[10][15][0] = 0; + poleInt[10][15][1] = 30; + updatePola(); + network(4, 10); + gogo(4, 10); + updatePola(); + buildMatrix(); + network(3, 10); + gogo(3, 10); + updatePola(); + buildMatrix(); + network(15, 10); + gogo(15, 10); + updatePola(); + buildMatrix(); + for (int i = 10; i < 25; i++) + { + network(i, 26 - i); + gogo(i, 26 - i); + updatePola(); + buildMatrix(); + } + for (int i = 9; i >= 1; i--) + { + network(i, 26 - i); + gogo(i, 26 - i); + updatePola(); + buildMatrix(); + } + updatePola(); +} + +void neuroStartS() +{ + pozycjaTraktoraX = 1, pozycjaTraktoraY = 1; + pole[pozycjaTraktoraY][pozycjaTraktoraX][0] = 'T'; + pole[pozycjaTraktoraY][pozycjaTraktoraX][1] = '1'; + buildMatrix(); + buildAvrGrad(); + numberOfTests = 1; + neuroTestS(); + buildMatrix(); + bestMatrixBuild(); + matrixToFile(); +} +void neuroStart1() +{ + pozycjaTraktoraX = 1, pozycjaTraktoraY = 1; + pole[pozycjaTraktoraY][pozycjaTraktoraX][0] = 'T'; + pole[pozycjaTraktoraY][pozycjaTraktoraX][1] = '1'; + buildMatrix(); + buildAvrGrad(); + numberOfTests = 25*24; + for (int j = 1; j < 25; j++) + { + for (int i = 0; i < 25; i++) + { + neuroTest1(j + 1, i + 1); + buildMatrix(); + } + } + bestMatrixBuild(); + matrixToFile(); +} +void neuroStart2() +{ + pozycjaTraktoraX = 1, pozycjaTraktoraY = 1; + pole[pozycjaTraktoraY][pozycjaTraktoraX][0] = 'T'; + pole[pozycjaTraktoraY][pozycjaTraktoraX][1] = '1'; + buildMatrix(); + buildAvrGrad(); + numberOfTests = 25*3; + for (int j = 22; j < 25; j++) + { + for (int i = 0; i < 25; i++) + { + neuroTest2(j + 1, i + 1); + buildMatrix(); + } + } + bestMatrixBuild(); + matrixToFile(); +} +void neuroStart3() +{ + pozycjaTraktoraX = 1, pozycjaTraktoraY = 1; + pole[pozycjaTraktoraY][pozycjaTraktoraX][0] = 'T'; + pole[pozycjaTraktoraY][pozycjaTraktoraX][1] = '1'; + buildMatrix(); + buildAvrGrad(); + numberOfTests = 25 * 3; + for (int j = 21; j < 24; j++) + { + for (int i = 0; i < 25; i++) + { + neuroTest3(j + 1, i + 1, j + 2, i + 1); + buildMatrix(); + } + } + bestMatrixBuild(); + matrixToFile(); +} +void neuroStart1Z() +{ + pozycjaTraktoraX = 1, pozycjaTraktoraY = 1; + pole[pozycjaTraktoraY][pozycjaTraktoraX][0] = 'T'; + pole[pozycjaTraktoraY][pozycjaTraktoraX][1] = '1'; + buildMatrix(); + buildAvrGrad(); + numberOfTests = 25 * 3; + for (int j = 1; j < 3; j++) + { + for (int i = 0; i < 25; i++) + { + neuroTest1Z(j + 1, i + 1); + buildMatrix(); + } + } + bestMatrixBuild(); + matrixToFile(); +} +void neuroStart2Z() +{ + pozycjaTraktoraX = 1, pozycjaTraktoraY = 1; + pole[pozycjaTraktoraY][pozycjaTraktoraX][0] = 'T'; + pole[pozycjaTraktoraY][pozycjaTraktoraX][1] = '1'; + buildMatrix(); + buildAvrGrad(); + numberOfTests = 25 * 3; + for (int j = 22; j < 25; j++) + { + for (int i = 0; i < 25; i++) + { + neuroTest2Z(j + 1, i + 1); + buildMatrix(); + } + } + bestMatrixBuild(); + matrixToFile(); +} +void neuroStart3Z() +{ + pozycjaTraktoraX = 1, pozycjaTraktoraY = 1; + pole[pozycjaTraktoraY][pozycjaTraktoraX][0] = 'T'; + pole[pozycjaTraktoraY][pozycjaTraktoraX][1] = '1'; + buildMatrix(); + buildAvrGrad(); + numberOfTests = 25 * 3; + for (int j = 21; j < 24; j++) + { + for (int i = 0; i < 25; i++) + { + neuroTest3Z(j + 1, i + 1, j + 2, i + 1); + buildMatrix(); + } + } + bestMatrixBuild(); + matrixToFile(); +} +void neuroStartZB() +{ + pozycjaTraktoraX = 1, pozycjaTraktoraY = 1; + pole[pozycjaTraktoraY][pozycjaTraktoraX][0] = 'T'; + pole[pozycjaTraktoraY][pozycjaTraktoraX][1] = '1'; + buildMatrix(); + buildAvrGrad(); + numberOfTests = 25 * 3; + for (int j = 22; j < 25; j++) + { + for (int i = 0; i < 25; i++) + { + neuroTestZB(j + 1, i + 1, j + 2, i + 1); + buildMatrix(); + } + } + bestMatrixBuild(); + matrixToFile(); +} +void neuroStartZZB() +{ + pozycjaTraktoraX = 1, pozycjaTraktoraY = 1; + pole[pozycjaTraktoraY][pozycjaTraktoraX][0] = 'T'; + pole[pozycjaTraktoraY][pozycjaTraktoraX][1] = '1'; + buildMatrix(); + buildAvrGrad(); + numberOfTests = 25 * 3; + for (int j = 20; j < 23; j++) + { + for (int i = 0; i < 25; i++) + { + neuroTestZZB(j + 1, i + 1, j + 2, i + 1, j + 3, i + 1); + buildMatrix(); + } + } + bestMatrixBuild(); + matrixToFile(); +} +void neuroStartZBB() +{ + pozycjaTraktoraX = 1, pozycjaTraktoraY = 1; + pole[pozycjaTraktoraY][pozycjaTraktoraX][0] = 'T'; + pole[pozycjaTraktoraY][pozycjaTraktoraX][1] = '1'; + buildMatrix(); + buildAvrGrad(); + numberOfTests = 25 * 3; + for (int j = 20; j < 23; j++) + { + for (int i = 0; i < 25; i++) + { + neuroTestZBB(j + 1, i + 1, j + 2, i + 1, j + 3, i + 1); + buildMatrix(); + } + } + bestMatrixBuild(); + matrixToFile(); +} +void neuroStartX() +{ + pozycjaTraktoraX = 1, pozycjaTraktoraY = 1; + pole[pozycjaTraktoraY][pozycjaTraktoraX][0] = 'T'; + pole[pozycjaTraktoraY][pozycjaTraktoraX][1] = '1'; + buildMatrix(); + buildAvrGrad(); + numberOfTests = 25; + for (int j = 5; j < 10; j++) + { + for (int i = 5; i < 10; i++) + { + neuroTestX(j + 1, i + 1); + buildMatrix(); + } + } + bestMatrixBuild(); + matrixToFile(); +} +void neuroStartSX() +{ + pozycjaTraktoraX = 1, pozycjaTraktoraY = 1; + pole[pozycjaTraktoraY][pozycjaTraktoraX][0] = 'T'; + pole[pozycjaTraktoraY][pozycjaTraktoraX][1] = '1'; + buildMatrix(); + buildAvrGrad(); + neuroTestSX(); + bestMatrixBuild(); + matrixToFile(); +} +void neuroStartSX2() +{ + pozycjaTraktoraX = 25, pozycjaTraktoraY = 10; + pole[pozycjaTraktoraY][pozycjaTraktoraX][0] = 'T'; + pole[pozycjaTraktoraY][pozycjaTraktoraX][1] = '1'; + buildMatrix(); + buildAvrGrad(); + neuroTestSX2(); + bestMatrixBuild(); + matrixToFile(); +} + +void chousePath() +{ + double tempOut[25][25]; + for (int i = 0; i < 25; i++) + { + for (int j = 0; j < 25; j++) + { + tempOut[i][j] = 0; + } + } + neuronsInputBuild(tempOut); + const int numberOfCellsInPole = (25 * 25); + const int inputNeuronsCount = numberOfCellsInPole * 4; + double bestX=0, bestY=0, bestChance=0; + for (int i = 0; i < 25; i++) + { + for (int j = 0; j < 25; j++) + { + //cout << neuroOutputPole[i][j] << " "; + + double tempChance; + if (pole[i + 1][j + 1][0] == 'T') + { + tempChance = 0; + } + else + { + tempChance = neuroOutputPole[i][j]; + } + //cout << tempChance << " "; + //cout << bestX + 1 << "_" << bestY + 1; + double diff=(int)((tempChance - bestChance)*pow(10,10)); + if (tempChance > bestChance && diff!=0) + { + bestX = j; + bestY = i; + bestChance = tempChance; + } + } + } + cout << bestChance << " " << bestX + 1 << " " << bestY + 1 << endl; + Sleep(1000); + gogo(bestX+1, bestY+1); + //Sleep(100000); +} + +void testOfNeuroMove() +{ + pole[1][3][0] = 'Z'; + pole[1][3][1] = '9'; + poleInt[1][3][0] = 0; + poleInt[1][3][1] = 60; + pole[1][4][0] = 'B'; + pole[1][4][1] = '9'; + poleInt[1][4][0] = 0; + poleInt[1][4][1] = 70; + updatePola(); +} + + +int main() +{ + SetWindow(50, 30); + //create pola// + for (int i = 0; i < 27; i++) + { + pole[i][0][0] = '#'; + pole[0][i][0] = '#'; + pole[26][i][0] = '#'; + pole[i][26][0] = '#'; + pole[i][0][1] = '9'; + pole[0][i][1] = '9'; + pole[26][i][1] = '9'; + pole[i][26][1] = '9'; + } + for (int i = 1; i < 26; i++) + { + for (int j = 1; j < 26; j++) + { + pole[i][j][0] = '.'; + pole[i][j][1] = '1'; + poleInt[i][j][0] = 0; + poleInt[i][j][1] = 0; + } + } + buildFirstMatrix(); + baseSetup(); + + updatePola(); + + //start3(); // testy start 1-3 + //neuroStart1(); + + + testOfNeuroMove(); + cout << "end" << endl; + //---------start---------// + bool traktorDziala = true; + + char akcja; + + do + { + chousePath(); + /*akcja = _getch(); + /if (akcja == 'w' || akcja == 's' || akcja == 'a' || akcja == 'd') + { + Move(akcja); + } + if (akcja == '0') + { + traktorDziala = false; + }*/ + } while (traktorDziala); + //---------end---------// + + return 0; +} \ No newline at end of file