From 6f0b782f0a9e40f5e16a61cd16c125ca65237242 Mon Sep 17 00:00:00 2001 From: Karol Piotrowski Date: Sun, 7 Jun 2020 16:08:58 +0000 Subject: [PATCH] =?UTF-8?q?Prze=C5=9Blij=20pliki=20do=20'backups'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backups/Main.cpp | 1163 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 1163 insertions(+) create mode 100644 backups/Main.cpp diff --git a/backups/Main.cpp b/backups/Main.cpp new file mode 100644 index 0000000..525a1f8 --- /dev/null +++ b/backups/Main.cpp @@ -0,0 +1,1163 @@ +//Main drzewa decyzyjne 1 + algorytm genetyczny + +#include +#include +#include +#include +#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][6]; +int pozycjaTraktoraX = 1, pozycjaTraktoraY = 1; +char currentWay = 'S'; + + +//algorytm genetyczny +int scoreBuraki = 0; +int scoreZiemniaki = 0; +int rozmiarPopulacji = 500; +string * zebraneBuraki = new string[rozmiarPopulacji]; +string * zebraneZiemniaki = new string[rozmiarPopulacji]; +string * burakiDoSadzenia = new string[20]; +string * ziemniakiDoSadzenia = new string[20]; +int gmoLeftBuraki; +int gmoLeftZiemniaki; +string kod_genetyczny[27][27]; + +string generateValue() { + char trash[100]; + string x = itoa(rand() % 1000,trash,10); + if (x.size() == 2) { + x = "0" + x; + } + else if (x.size() == 1) { + x = "00" + x; + } + + return x; + +} + + +string generateVegetable() { + + string taste = generateValue(); + string colour = generateValue(); + string size = generateValue(); + + return taste + colour + size; + +} + + +void generatePopulation(string * population,int length) { + + int i; + for(i=0;i&i, const pair&j) +{ + return i.first > j.first; +} + + +void ranking(string * population,string * parents, int populationSize, int parentsNumber) { + + int i; + pair fitnessTable[populationSize]; + for(i=0;i fitnessTable[populationSize]; + for(i=0;i=0;j--) { + if(not exists(parentsNumber,taken,j)) { + sum += fitnessTable[j].first; + fitnessTable[j].first = sum; + } + + } + roulette = rand() % fitnessTable[0].first; + j = 0; + while(exists(parentsNumber,taken,j)) { + j += 1; + } + while(roulette > fitnessTable[j].first and j= nextGenSize) { + break; + } + else { + nextGen[counter] = parents[i]; + counter +=1; + } + } + while(counter < nextGenSize) { + for(i=0;i= nextGenSize) { + break; + } + else { + for(j=i;j= nextGenSize) { + break; + } + else { + string couple[2]; + couple[0] = parents[i]; + couple[1] = parents[j]; + nextGen[counter] = cross(couple); + counter += 1; + } + } + } + } + } +} + + +void genetic_algorithm(string * population, int populationSize, int parentsNumber,string * outcome, int outcomeSize) { + + int iteration,i; + for(iteration=0;iteration<5;iteration++) { + string * parents = new string[parentsNumber]; + selection(population,parents,populationSize,parentsNumber); + + string * nextGen = new string[populationSize]; + crossover(parents,nextGen,parentsNumber,populationSize); + + for(i=0;i 0) { + string temp = burakiDoSadzenia[gmoLeftBuraki - 1]; + gmoLeftBuraki -= 1; + return temp; + } + else { + return generateVegetable(); + } + } + else { + if (gmoLeftZiemniaki > 0) { + string temp = ziemniakiDoSadzenia[gmoLeftZiemniaki - 1]; + gmoLeftZiemniaki -= 1; + return temp; + } + else { + return generateVegetable(); + } + } +} + + +void przypiszKodGenetyczny(int i, int j, char plant) { + if (plant == 'B') { + kod_genetyczny[i][j] = przypiszKod("buraki"); + } + else if (plant == 'Z') { + kod_genetyczny[i][j] = przypiszKod("ziemniaki"); + } +} + + +void obslugaAlgorytmuGenetycznego() { + cout << "Zebrane buraki: " << scoreBuraki << endl; + cout << "Zebrane ziemniaki: " << scoreZiemniaki << endl; + if(scoreBuraki>=rozmiarPopulacji) { + scoreBuraki = 0; + + for(int i = 0;i<20;i++) { + burakiDoSadzenia[i] = "000000000"; + } + genetic_algorithm(zebraneBuraki, rozmiarPopulacji, rozmiarPopulacji - 5, burakiDoSadzenia, 20); + gmoLeftBuraki = 20; + for(int i = 0; i=rozmiarPopulacji) { + scoreZiemniaki = 0; + + for(int i = 0;i<20;i++) { + ziemniakiDoSadzenia[i] = "000000000"; + } + genetic_algorithm(zebraneZiemniaki, rozmiarPopulacji, rozmiarPopulacji - 5, ziemniakiDoSadzenia, 20); + gmoLeftZiemniaki = 20; + for(int i = 0; i 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 (p.first > pozycjaTraktoraX) + Move('d'); + if (p.first < pozycjaTraktoraX) + Move('a'); + if (p.second > pozycjaTraktoraY) + Move('s'); + if (p.second < pozycjaTraktoraY) + Move('w'); + + //printf("-> (%d,%d) ", p.first, p.second); //---- informacja wierzchołku + Sleep(1000); + } + + return; +} +void aStarSearch(int grid[][COL],Pair src, Pair dest) +{ + 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++) + { + 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); + 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); + 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); + 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); + 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); + +} + +void test1() +{ + pole[1][3][0] = 'B'; + pole[1][3][1] = '9'; + pole[3][1][0] = 'Z'; + pole[3][1][1] = '9'; + + kod_genetyczny[1][3] = przypiszKod("buraki"); + kod_genetyczny[3][1] = przypiszKod("ziemniaki"); +} +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'; + + kod_genetyczny[i][j] = przypiszKod("buraki"); + } + } + test1(); + updatePola(); +} + +void testSI1() +{ + for (int i = 1; i < 26; i++) + { + for (int j = 1; j < 26; j++) + { + if (j % 3 == 0) + { + pole[i][j][2] = 'z'; //zyzne + pole[i][j][3] = 'n'; //nawodnione + pole[i][j][4] = 'c'; //w cieniu + pole[i][j][5] = 'k'; //kwasne + } + else + { + if (j % 3 == 1) + { + pole[i][j][2] = 'j'; //jalowe + pole[i][j][3] = 'n'; //nawodnione + pole[i][j][4] = 's'; //w sloncu + pole[i][j][5] = 'n'; //neutralne + } + else + { + pole[i][j][2] = 'z'; //zyzne + pole[i][j][3] = 's'; //suche + pole[i][j][4] = 's'; //sloneczne + pole[i][j][5] = 'z'; //zasadowe + } + } + } + } +} + +void sendState() +{ + ofstream write("dane.txt"); + for (int i = 1; i < 26; i++) + { + for (int j = 1; j < 26; j++) + { + string a; + a += pole[i][j][2]; + a += ' '; + a += pole[i][j][3]; + a += ' '; + a += pole[i][j][4]; + a += ' '; + a += pole[i][j][5]; + write << a << endl; + } + } + write.close(); +} +void reciveState() +{ + ifstream read("decyzje.txt"); + if (read.is_open()) + { + char plant; + int i = 1; + int j = 1; + while (read >> plant) + { + if (j == 25) + { + gogo(1, i+1); + } + else + { + gogo(j+1 , i ); + } + pole[i][j][0] = plant; + + przypiszKodGenetyczny(i,j,plant); + + if (plant == '.') + { + pole[i][j][1] = '1'; + } + else + { + pole[i][j][1] = '9'; + } + if (j == 25) + { + j = 1; + i += 1; + } + else + { + j += 1; + } + + + } + } +} + +void start1() +{ + int goalX = 3, goalY = 4; + test1(); + testSI1(); + pole[1][1][0] = 'T'; + pole[1][1][1] = '1'; + pole[goalY][goalX][0] = 'G'; + pole[goalY][goalX][1] = '9'; + gogo(goalX, goalY); + gogo(goalX - 1, goalY); + pole[goalY][goalX][0] = 'Z'; + pole[goalY][goalX][1] = '9'; + + kod_genetyczny[goalY][goalX] = przypiszKod("ziemniaki"); + + updatePola(); + + + //sendState(); //trzeba ręcznie zmieniać między wysyłaniem stanu a pobieraniem stanu pola + reciveState(); +} +void start2() +{ + int goalX = 6, goalY = 6; + test2(); + pole[1][1][0] = 'T'; + pole[1][1][1] = '1'; + pole[goalY][goalX][0] = 'G'; + pole[goalY][goalX][1] = '9'; + gogo(goalX, goalY); +} +void start3() +{ + int goalX = 6, goalY = 9; + test2(); + pole[1][1][0] = 'T'; + pole[1][1][1] = '1'; + pole[goalY][goalX][0] = 'G'; + pole[goalY][goalX][1] = '9'; + gogo(goalX, goalY); +} + +int main() +{ + srand(time(0)); + + 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'; + } + } + + for (int i = 0; i < 25; i++) + { + pole[i + 1][i + 1][0] = 'B'; + pole[i + 1][i + 1][1] = '9'; + } + + for (int i = 0; i <25; i++) { + for (int j = 0; j <10; j++) { + pole[j + 1][i + 1][0] = 'B'; + pole[j + 1][i + 1][1] = '9'; + } + } + for (int i = 0; i <25; i++) { + for (int j = 10; j <20; j++) { + pole[j + 1][i + 1][0] = 'Z'; + pole[j + 1][i + 1][1] = '9'; + } + } + + generujKody(); + + updatePola(); + + start1(); // testy start 1-3 + + //---------start---------// + bool traktorDziala = true; + + char akcja; + + do + { + akcja = _getch(); + if (akcja == 'w' || akcja == 's' || akcja == 'a' || akcja == 'd') + { + Move(akcja); + } + if (akcja == '0') + { + traktorDziala = false; + } + + + } while (traktorDziala); + //---------end---------// + + return 0; +} +