2020-06-09 19:12:24 +02:00
|
|
|
|
//Main oba drzewa decyzyjne + algorytm genetyczny
|
2020-06-07 01:04:18 +02:00
|
|
|
|
|
|
|
|
|
#include<iostream>
|
|
|
|
|
#include<stdlib.h>
|
|
|
|
|
#include<windows.h>
|
|
|
|
|
#include<conio.h>
|
|
|
|
|
#include<string>
|
|
|
|
|
#include<list>
|
|
|
|
|
#include<set>
|
|
|
|
|
#include<math.h>
|
|
|
|
|
#include<stack>
|
|
|
|
|
#include<fstream>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <cstdlib>
|
|
|
|
|
#include <ctime>
|
|
|
|
|
#include <algorithm>
|
2020-06-09 19:12:24 +02:00
|
|
|
|
#include <utility>
|
|
|
|
|
#include<sstream>
|
|
|
|
|
//#include<bits/stdc++.h>
|
|
|
|
|
|
2020-06-07 01:04:18 +02:00
|
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
2020-06-09 19:12:24 +02:00
|
|
|
|
const float maxFloat = FLT_MAX;
|
2020-06-07 01:04:18 +02:00
|
|
|
|
const int ROW = 27;
|
|
|
|
|
const int COL = 27;
|
|
|
|
|
typedef pair<int, int> Pair;
|
|
|
|
|
typedef pair<double, pair<int, int> > 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';
|
2020-06-09 19:12:24 +02:00
|
|
|
|
//========================================================
|
|
|
|
|
|
|
|
|
|
double poleInt[27][27][2];
|
|
|
|
|
char underTraktor = '.';
|
|
|
|
|
char underTraktorWeight = '1';
|
|
|
|
|
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];
|
|
|
|
|
|
|
|
|
|
//========================================================
|
|
|
|
|
|
|
|
|
|
//drzewo decyzyjne
|
|
|
|
|
|
|
|
|
|
int stan[27][27][2];
|
|
|
|
|
string polecenie;
|
|
|
|
|
int decyzja = NULL;
|
|
|
|
|
|
|
|
|
|
// Do dodania wywołanie odpowiednich funkcji - zbieranie - nawożenie -
|
|
|
|
|
void akcja() {
|
|
|
|
|
if (decyzja == 0) {
|
|
|
|
|
//nic nie rób
|
|
|
|
|
}
|
|
|
|
|
else if (decyzja == 1) {
|
|
|
|
|
//Zastosuj nawoz
|
|
|
|
|
}
|
|
|
|
|
else if (decyzja == 2) {
|
|
|
|
|
//Zastosuj srodek
|
|
|
|
|
}
|
|
|
|
|
else if (decyzja == 4) {
|
|
|
|
|
//Zbierz rośline i licznik++
|
|
|
|
|
}
|
|
|
|
|
else if (decyzja == 5) {
|
|
|
|
|
//Zbierz rośline ale nie dodawaj do licznika
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
void decisionTree(string polecenie) {
|
|
|
|
|
|
|
|
|
|
std::string str = polecenie;
|
|
|
|
|
const char* c = str.c_str();
|
|
|
|
|
system(c);
|
|
|
|
|
|
|
|
|
|
int line = 0;
|
|
|
|
|
ifstream inFile;
|
|
|
|
|
inFile.open("dec.txt");
|
|
|
|
|
if (!inFile) {
|
|
|
|
|
cout << "Unable to open file";
|
|
|
|
|
exit(1); // terminate with error
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (inFile >> line) {
|
|
|
|
|
decyzja = line;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inFile.close();
|
|
|
|
|
akcja();
|
|
|
|
|
}
|
|
|
|
|
void stanPola(int x, int y) {
|
|
|
|
|
//[x][x][0] = 0 - brak chemii
|
|
|
|
|
//[x][x][0] = 1 - tylko nawóz
|
|
|
|
|
//[x][x][0] = 2 - tylko środek
|
|
|
|
|
//[x][x][0] = 3 - środek i nawóz
|
|
|
|
|
//[x][x][1] - wartość wzrostu rośliny
|
|
|
|
|
|
|
|
|
|
polecenie = "python injectCode.py 1 ";
|
|
|
|
|
|
|
|
|
|
if (stan[x][y][0] == 0)
|
|
|
|
|
polecenie.append("0 0 ");
|
|
|
|
|
if (stan[x][y][0] == 1)
|
|
|
|
|
polecenie.append("1 0 ");
|
|
|
|
|
if (stan[x][y][0] == 2)
|
|
|
|
|
polecenie.append("0 1 ");
|
|
|
|
|
if (stan[x][y][0] == 3)
|
|
|
|
|
polecenie.append("1 1 ");
|
|
|
|
|
int w = (stan[x][y][1]);
|
|
|
|
|
std::string s = std::to_string(w);
|
|
|
|
|
polecenie.append(s);
|
|
|
|
|
|
|
|
|
|
decisionTree(polecenie);
|
|
|
|
|
}
|
2020-06-07 01:04:18 +02:00
|
|
|
|
|
|
|
|
|
|
2020-06-09 19:12:24 +02:00
|
|
|
|
//========================================================
|
|
|
|
|
|
2020-06-07 01:04:18 +02:00
|
|
|
|
//algorytm genetyczny
|
2020-06-09 19:12:24 +02:00
|
|
|
|
int scoreBuraki = 0;
|
|
|
|
|
int scoreZiemniaki = 0;
|
2020-06-07 01:04:18 +02:00
|
|
|
|
int rozmiarPopulacji = 500;
|
2020-06-09 19:12:24 +02:00
|
|
|
|
string* zebraneBuraki = new string[rozmiarPopulacji];
|
|
|
|
|
string* zebraneZiemniaki = new string[rozmiarPopulacji];
|
|
|
|
|
string* burakiDoSadzenia = new string[20];
|
|
|
|
|
string* ziemniakiDoSadzenia = new string[20];
|
2020-06-07 01:04:18 +02:00
|
|
|
|
int gmoLeftBuraki;
|
|
|
|
|
int gmoLeftZiemniaki;
|
|
|
|
|
string kod_genetyczny[27][27];
|
2020-06-09 19:12:24 +02:00
|
|
|
|
char poleRecive[27][27];
|
2020-06-07 01:04:18 +02:00
|
|
|
|
|
|
|
|
|
string generateValue() {
|
|
|
|
|
char trash[100];
|
2020-06-08 23:19:05 +02:00
|
|
|
|
string x;
|
2020-06-09 19:12:24 +02:00
|
|
|
|
//srand(time(NULL));
|
|
|
|
|
int random = (rand() % 1000);//, 1);
|
2020-06-08 23:19:05 +02:00
|
|
|
|
//string x = itoa(rand() % 1000,trash,10);
|
|
|
|
|
x = to_string(random);
|
2020-06-07 01:04:18 +02:00
|
|
|
|
if (x.size() == 2) {
|
|
|
|
|
x = "0" + x;
|
|
|
|
|
}
|
|
|
|
|
else if (x.size() == 1) {
|
|
|
|
|
x = "00" + x;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-09 19:12:24 +02:00
|
|
|
|
return x;
|
2020-06-07 01:04:18 +02:00
|
|
|
|
|
2020-06-09 19:12:24 +02:00
|
|
|
|
}
|
2020-06-07 01:04:18 +02:00
|
|
|
|
string generateVegetable() {
|
2020-06-09 19:12:24 +02:00
|
|
|
|
|
|
|
|
|
string taste = generateValue();
|
2020-06-07 01:04:18 +02:00
|
|
|
|
string colour = generateValue();
|
|
|
|
|
string size = generateValue();
|
2020-06-09 19:12:24 +02:00
|
|
|
|
|
2020-06-07 01:04:18 +02:00
|
|
|
|
return taste + colour + size;
|
|
|
|
|
|
2020-06-09 19:12:24 +02:00
|
|
|
|
}
|
|
|
|
|
void generatePopulation(string* population, int length) {
|
2020-06-07 01:04:18 +02:00
|
|
|
|
|
|
|
|
|
int i;
|
2020-06-09 19:12:24 +02:00
|
|
|
|
for (i = 0;i < length;i++) {
|
2020-06-07 01:04:18 +02:00
|
|
|
|
population[i] = generateVegetable();
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-06-09 19:12:24 +02:00
|
|
|
|
int power(int x, int y) {
|
2020-06-07 01:04:18 +02:00
|
|
|
|
|
2020-06-09 19:12:24 +02:00
|
|
|
|
if (y == 0) return 1;
|
|
|
|
|
if (y == 1) return x;
|
2020-06-07 01:04:18 +02:00
|
|
|
|
|
2020-06-09 19:12:24 +02:00
|
|
|
|
int temp = power(x, y / 2);
|
|
|
|
|
if (y % 2 == 0) return temp * temp;
|
|
|
|
|
else return x * temp * temp;
|
2020-06-07 01:04:18 +02:00
|
|
|
|
}
|
2020-06-09 19:12:24 +02:00
|
|
|
|
int stringToInt(string str, int size) {
|
2020-06-07 01:04:18 +02:00
|
|
|
|
|
|
|
|
|
int x = 0;
|
|
|
|
|
int i;
|
2020-06-09 19:12:24 +02:00
|
|
|
|
reverse(str.begin(), str.end());
|
|
|
|
|
|
|
|
|
|
for (i = 0;i < size;i++) {
|
|
|
|
|
x += (str[i] - '0') * power(10, i);
|
2020-06-07 01:04:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-09 19:12:24 +02:00
|
|
|
|
reverse(str.begin(), str.end());
|
2020-06-07 01:04:18 +02:00
|
|
|
|
|
2020-06-09 19:12:24 +02:00
|
|
|
|
return x;
|
2020-06-07 01:04:18 +02:00
|
|
|
|
}
|
2020-06-09 19:12:24 +02:00
|
|
|
|
int fitness(string vegetable) {
|
2020-06-07 01:04:18 +02:00
|
|
|
|
|
2020-06-09 19:12:24 +02:00
|
|
|
|
int taste = stringToInt(vegetable.substr(0, 3), 3);
|
|
|
|
|
int colour = stringToInt(vegetable.substr(3, 3), 3);
|
|
|
|
|
int size = stringToInt(vegetable.substr(6, 3), 3);
|
2020-06-07 01:04:18 +02:00
|
|
|
|
|
2020-06-09 19:12:24 +02:00
|
|
|
|
return (taste + colour + size) / 3;
|
|
|
|
|
}
|
|
|
|
|
bool comparePair(const pair<int, string>& i, const pair<int, string>& j)
|
2020-06-07 01:04:18 +02:00
|
|
|
|
{
|
2020-06-09 19:12:24 +02:00
|
|
|
|
return i.first > j.first;
|
2020-06-07 01:04:18 +02:00
|
|
|
|
}
|
2020-06-09 19:12:24 +02:00
|
|
|
|
void ranking(string* population, string* parents, int populationSize, int parentsNumber) {
|
2020-06-07 01:04:18 +02:00
|
|
|
|
|
|
|
|
|
int i;
|
2020-06-09 19:12:24 +02:00
|
|
|
|
pair <int, string>* fitnessTable = new pair <int, string>[populationSize];
|
|
|
|
|
for (i = 0;i < populationSize;i++) {
|
|
|
|
|
fitnessTable[i] = make_pair(fitness(population[i]), population[i]);
|
2020-06-07 01:04:18 +02:00
|
|
|
|
}
|
2020-06-09 19:12:24 +02:00
|
|
|
|
|
|
|
|
|
sort(fitnessTable, fitnessTable + populationSize, comparePair);
|
|
|
|
|
|
|
|
|
|
for (i = 0;i < parentsNumber;i++) {
|
2020-06-07 01:04:18 +02:00
|
|
|
|
parents[i] = fitnessTable[i].second;
|
2020-06-09 19:12:24 +02:00
|
|
|
|
}
|
2020-06-08 23:19:05 +02:00
|
|
|
|
delete[] fitnessTable;
|
2020-06-07 01:04:18 +02:00
|
|
|
|
}
|
2020-06-09 19:12:24 +02:00
|
|
|
|
bool exists(int len, int* array, int element) {
|
2020-06-07 01:04:18 +02:00
|
|
|
|
|
|
|
|
|
int i;
|
2020-06-09 19:12:24 +02:00
|
|
|
|
for (i = 0;i < len;i++) {
|
2020-06-07 01:04:18 +02:00
|
|
|
|
if (array[i] == element) return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2020-06-09 19:12:24 +02:00
|
|
|
|
void selection(string* population, string* parents, int populationSize, int parentsNumber) {
|
2020-06-07 01:04:18 +02:00
|
|
|
|
|
2020-06-09 19:12:24 +02:00
|
|
|
|
int i, j, k;
|
|
|
|
|
pair <int, string>* fitnessTable = new pair <int, string>[populationSize];
|
|
|
|
|
for (i = 0;i < populationSize;i++) {
|
|
|
|
|
fitnessTable[i] = make_pair(fitness(population[i]), population[i]);
|
2020-06-07 01:04:18 +02:00
|
|
|
|
}
|
2020-06-09 19:12:24 +02:00
|
|
|
|
|
|
|
|
|
sort(fitnessTable, fitnessTable + populationSize, comparePair);
|
|
|
|
|
|
2020-06-07 01:04:18 +02:00
|
|
|
|
int roulette;
|
2020-06-09 19:12:24 +02:00
|
|
|
|
int* taken = new int[parentsNumber];
|
2020-06-07 01:04:18 +02:00
|
|
|
|
int sum = 0;
|
2020-06-09 19:12:24 +02:00
|
|
|
|
for (i = 0;i < parentsNumber;i++) {
|
|
|
|
|
for (j = populationSize - 1;j >= 0;j--) {
|
|
|
|
|
if (not exists(parentsNumber, taken, j)) {
|
|
|
|
|
sum += fitnessTable[j].first;
|
2020-06-07 01:04:18 +02:00
|
|
|
|
fitnessTable[j].first = sum;
|
|
|
|
|
}
|
2020-06-09 19:12:24 +02:00
|
|
|
|
|
2020-06-07 01:04:18 +02:00
|
|
|
|
}
|
|
|
|
|
roulette = rand() % fitnessTable[0].first;
|
|
|
|
|
j = 0;
|
2020-06-09 19:12:24 +02:00
|
|
|
|
while (exists(parentsNumber, taken, j)) {
|
2020-06-07 01:04:18 +02:00
|
|
|
|
j += 1;
|
|
|
|
|
}
|
2020-06-09 19:12:24 +02:00
|
|
|
|
while (roulette > fitnessTable[j].first && j < populationSize) {
|
|
|
|
|
if (not exists(parentsNumber, taken, j)) {
|
2020-06-07 01:04:18 +02:00
|
|
|
|
roulette -= fitnessTable[j].first;
|
|
|
|
|
}
|
2020-06-09 19:12:24 +02:00
|
|
|
|
j += 1;
|
2020-06-07 01:04:18 +02:00
|
|
|
|
}
|
|
|
|
|
parents[i] = fitnessTable[j].second;
|
|
|
|
|
taken[i] = j;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-06-09 19:12:24 +02:00
|
|
|
|
|
|
|
|
|
}
|
2020-06-07 01:04:18 +02:00
|
|
|
|
string mutate(string child) {
|
2020-06-09 19:12:24 +02:00
|
|
|
|
|
2020-06-07 01:04:18 +02:00
|
|
|
|
int d3 = rand() % 3;
|
|
|
|
|
string mutation = generateValue();
|
2020-06-09 19:12:24 +02:00
|
|
|
|
switch (d3) {
|
|
|
|
|
case 0:
|
|
|
|
|
child = mutation + child.substr(3, 6);
|
|
|
|
|
break;
|
|
|
|
|
case 1:
|
|
|
|
|
child = child.substr(0, 3) + mutation + child.substr(6, 3);
|
|
|
|
|
break;
|
|
|
|
|
case 2:
|
|
|
|
|
child = child.substr(0, 6) + mutation;
|
|
|
|
|
break;
|
2020-06-07 01:04:18 +02:00
|
|
|
|
}
|
2020-06-09 19:12:24 +02:00
|
|
|
|
|
2020-06-07 01:04:18 +02:00
|
|
|
|
return child;
|
|
|
|
|
}
|
|
|
|
|
string cross(string parent[2]) {
|
2020-06-09 19:12:24 +02:00
|
|
|
|
|
2020-06-07 01:04:18 +02:00
|
|
|
|
int i;
|
|
|
|
|
string child = "";
|
2020-06-09 19:12:24 +02:00
|
|
|
|
for (i = 0;i < 3;i++) {
|
|
|
|
|
child += parent[rand() % 2].substr(i * 3, 3);
|
2020-06-07 01:04:18 +02:00
|
|
|
|
}
|
|
|
|
|
if (child == parent[0] or child == parent[1]) {
|
|
|
|
|
string other;
|
|
|
|
|
if (child == parent[0]) other = parent[1];
|
|
|
|
|
else other = parent[0];
|
|
|
|
|
int d3 = rand() % 3;
|
2020-06-09 19:12:24 +02:00
|
|
|
|
switch (d3) {
|
|
|
|
|
case 0:
|
|
|
|
|
child = other.substr((rand() % 3) * 3, 3) + child.substr(3, 6);
|
|
|
|
|
break;
|
|
|
|
|
case 1:
|
|
|
|
|
child = child.substr(0, 3) + other.substr((rand() % 3) * 3, 3) + child.substr(6, 3);
|
|
|
|
|
break;
|
|
|
|
|
case 2:
|
|
|
|
|
child = child.substr(0, 6) + other.substr((rand() % 3) * 3, 3);
|
|
|
|
|
break;
|
2020-06-07 01:04:18 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
int d1000 = rand() % 1000;
|
|
|
|
|
if (rand() % 100 == 0) {
|
|
|
|
|
child = mutate(child);
|
|
|
|
|
}
|
|
|
|
|
return child;
|
|
|
|
|
}
|
2020-06-09 19:12:24 +02:00
|
|
|
|
void crossover(string* parents, string* nextGen, int parentsNumber, int nextGenSize) {
|
2020-06-07 01:04:18 +02:00
|
|
|
|
|
|
|
|
|
int counter = 0;
|
2020-06-09 19:12:24 +02:00
|
|
|
|
int i, j;
|
|
|
|
|
for (i = 0;i < parentsNumber;i++) {
|
2020-06-07 01:04:18 +02:00
|
|
|
|
if (counter >= nextGenSize) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
nextGen[counter] = parents[i];
|
2020-06-09 19:12:24 +02:00
|
|
|
|
counter += 1;
|
2020-06-07 01:04:18 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-06-09 19:12:24 +02:00
|
|
|
|
while (counter < nextGenSize) {
|
|
|
|
|
for (i = 0;i < parentsNumber;i++) {
|
2020-06-07 01:04:18 +02:00
|
|
|
|
if (counter >= nextGenSize) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
else {
|
2020-06-09 19:12:24 +02:00
|
|
|
|
for (j = i;j < parentsNumber;j++) {
|
2020-06-07 01:04:18 +02:00
|
|
|
|
if (counter >= nextGenSize) {
|
2020-06-09 19:12:24 +02:00
|
|
|
|
break;
|
2020-06-07 01:04:18 +02:00
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
string couple[2];
|
|
|
|
|
couple[0] = parents[i];
|
|
|
|
|
couple[1] = parents[j];
|
|
|
|
|
nextGen[counter] = cross(couple);
|
|
|
|
|
counter += 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-06-09 19:12:24 +02:00
|
|
|
|
void genetic_algorithm(string* population, int populationSize, int parentsNumber, string* outcome, int outcomeSize) {
|
2020-06-07 01:04:18 +02:00
|
|
|
|
|
2020-06-09 19:12:24 +02:00
|
|
|
|
int iteration, i;
|
|
|
|
|
for (iteration = 0;iteration < 5;iteration++) {
|
|
|
|
|
string* parents = new string[parentsNumber];
|
|
|
|
|
selection(population, parents, populationSize, parentsNumber);
|
2020-06-07 01:04:18 +02:00
|
|
|
|
|
2020-06-09 19:12:24 +02:00
|
|
|
|
string* nextGen = new string[populationSize];
|
|
|
|
|
crossover(parents, nextGen, parentsNumber, populationSize);
|
|
|
|
|
|
|
|
|
|
for (i = 0;i < populationSize;i++) {
|
2020-06-07 01:04:18 +02:00
|
|
|
|
population[i] = nextGen[i];
|
|
|
|
|
}
|
2020-06-07 15:24:23 +02:00
|
|
|
|
|
2020-06-07 01:04:18 +02:00
|
|
|
|
}
|
2020-06-09 19:12:24 +02:00
|
|
|
|
ranking(population, outcome, populationSize, outcomeSize);
|
2020-06-07 01:04:18 +02:00
|
|
|
|
}
|
|
|
|
|
string przypiszKod(string warzywa) {
|
2020-06-09 19:12:24 +02:00
|
|
|
|
if (warzywa == "buraki") {
|
|
|
|
|
if (gmoLeftBuraki > 0) {
|
|
|
|
|
string temp = burakiDoSadzenia[gmoLeftBuraki - 1];
|
|
|
|
|
gmoLeftBuraki -= 1;
|
|
|
|
|
return temp;
|
|
|
|
|
}
|
2020-06-07 01:04:18 +02:00
|
|
|
|
else {
|
2020-06-09 19:12:24 +02:00
|
|
|
|
return generateVegetable();
|
2020-06-07 01:04:18 +02:00
|
|
|
|
}
|
2020-06-09 19:12:24 +02:00
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
if (gmoLeftZiemniaki > 0) {
|
|
|
|
|
string temp = ziemniakiDoSadzenia[gmoLeftZiemniaki - 1];
|
|
|
|
|
gmoLeftZiemniaki -= 1;
|
|
|
|
|
return temp;
|
2020-06-07 01:04:18 +02:00
|
|
|
|
}
|
2020-06-09 19:12:24 +02:00
|
|
|
|
else {
|
|
|
|
|
return generateVegetable();
|
2020-06-07 01:04:18 +02:00
|
|
|
|
}
|
2020-06-09 19:12:24 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
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");
|
|
|
|
|
}
|
2020-06-07 01:04:18 +02:00
|
|
|
|
}
|
|
|
|
|
void obslugaAlgorytmuGenetycznego() {
|
2020-06-09 19:12:24 +02:00
|
|
|
|
cout << "Zebrane buraki: " << scoreBuraki << endl;
|
|
|
|
|
cout << "Zebrane ziemniaki: " << scoreZiemniaki << endl;
|
|
|
|
|
if (scoreBuraki >= rozmiarPopulacji) {
|
|
|
|
|
scoreBuraki = 0;
|
2020-06-07 15:24:23 +02:00
|
|
|
|
|
2020-06-09 19:12:24 +02:00
|
|
|
|
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;i++) {
|
|
|
|
|
zebraneBuraki[i] = "000000000";
|
|
|
|
|
}
|
|
|
|
|
for (int i = 0;i < 20;i++) {
|
|
|
|
|
cout << burakiDoSadzenia[i] << endl;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (scoreZiemniaki >= rozmiarPopulacji) {
|
|
|
|
|
scoreZiemniaki = 0;
|
2020-06-07 15:24:23 +02:00
|
|
|
|
|
2020-06-09 19:12:24 +02:00
|
|
|
|
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 < rozmiarPopulacji;i++) {
|
|
|
|
|
zebraneZiemniaki[i] = "000000000";
|
|
|
|
|
}
|
|
|
|
|
for (int i = 0;i < 20;i++) {
|
|
|
|
|
cout << ziemniakiDoSadzenia[i] << endl;
|
|
|
|
|
}
|
2020-06-07 01:04:18 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
void generujKody() {
|
2020-06-09 19:12:24 +02:00
|
|
|
|
for (int i = 0;i < 27;i++) {
|
|
|
|
|
for (int j = 0;j < 27;j++) {
|
2020-06-07 01:04:18 +02:00
|
|
|
|
if (pole[i][j][0] == 'B') {
|
2020-06-09 19:12:24 +02:00
|
|
|
|
kod_genetyczny[i][j] = przypiszKod("buraki");
|
|
|
|
|
}
|
2020-06-07 01:04:18 +02:00
|
|
|
|
else if (pole[i][j][0] == 'Z') {
|
2020-06-09 19:12:24 +02:00
|
|
|
|
kod_genetyczny[i][j] = przypiszKod("ziemniaki");
|
|
|
|
|
}
|
2020-06-07 01:04:18 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-06-09 19:12:24 +02:00
|
|
|
|
//========================================================
|
2020-06-07 01:04:18 +02:00
|
|
|
|
|
|
|
|
|
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("cyan", "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");
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-09 19:12:24 +02:00
|
|
|
|
}
|
2020-06-07 01:04:18 +02:00
|
|
|
|
void correctMovement(char wantedWay)
|
|
|
|
|
{
|
|
|
|
|
while (currentWay != wantedWay)
|
|
|
|
|
{
|
|
|
|
|
switch (currentWay)
|
|
|
|
|
{
|
2020-06-09 19:12:24 +02:00
|
|
|
|
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 increseState()
|
|
|
|
|
{
|
|
|
|
|
for (int i = 1; i < 26; i++)
|
|
|
|
|
{
|
|
|
|
|
for (int j = 1; j < 26; j++)
|
|
|
|
|
{
|
|
|
|
|
if (pole[j][i][0] != 'T' && pole[j][i][0] != '.')
|
2020-06-07 01:04:18 +02:00
|
|
|
|
{
|
2020-06-09 19:12:24 +02:00
|
|
|
|
if (poleInt[j][i][1] != 100 && poleInt[j][i][1] != 0)
|
|
|
|
|
{
|
|
|
|
|
poleInt[j][i][1] += 0.5;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-06-07 01:04:18 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
void Move(char kierunek)
|
2020-06-09 19:12:24 +02:00
|
|
|
|
{
|
2020-06-07 01:04:18 +02:00
|
|
|
|
switch (kierunek)
|
|
|
|
|
{
|
|
|
|
|
//gA3ra-(w)
|
2020-06-09 19:12:24 +02:00
|
|
|
|
case 'w':
|
|
|
|
|
{
|
|
|
|
|
if (pole[pozycjaTraktoraY - 1][pozycjaTraktoraX][0] != '#')
|
2020-06-07 01:04:18 +02:00
|
|
|
|
{
|
2020-06-09 19:12:24 +02:00
|
|
|
|
correctMovement('N');
|
|
|
|
|
pole[pozycjaTraktoraY][pozycjaTraktoraX][0] = underTraktor;
|
|
|
|
|
pole[pozycjaTraktoraY][pozycjaTraktoraX][1] = underTraktorWeight;
|
|
|
|
|
pozycjaTraktoraY--;
|
|
|
|
|
increseState();
|
|
|
|
|
underTraktor = pole[pozycjaTraktoraY][pozycjaTraktoraX][0];
|
|
|
|
|
underTraktorWeight = pole[pozycjaTraktoraY][pozycjaTraktoraX][1];
|
|
|
|
|
pole[pozycjaTraktoraY][pozycjaTraktoraX][0] = 'T';
|
|
|
|
|
}
|
|
|
|
|
updatePola();
|
|
|
|
|
}break;
|
|
|
|
|
//dA3A‚-(s)
|
|
|
|
|
case 's':
|
|
|
|
|
{
|
|
|
|
|
if (pole[pozycjaTraktoraY + 1][pozycjaTraktoraX][0] != '#')
|
2020-06-07 01:04:18 +02:00
|
|
|
|
{
|
2020-06-09 19:12:24 +02:00
|
|
|
|
correctMovement('S');
|
|
|
|
|
pole[pozycjaTraktoraY][pozycjaTraktoraX][0] = underTraktor;
|
|
|
|
|
pole[pozycjaTraktoraY][pozycjaTraktoraX][1] = underTraktorWeight;
|
|
|
|
|
pozycjaTraktoraY++;
|
|
|
|
|
increseState();
|
|
|
|
|
underTraktor = pole[pozycjaTraktoraY][pozycjaTraktoraX][0];
|
|
|
|
|
underTraktorWeight = pole[pozycjaTraktoraY][pozycjaTraktoraX][1];
|
|
|
|
|
pole[pozycjaTraktoraY][pozycjaTraktoraX][0] = 'T';
|
|
|
|
|
}
|
|
|
|
|
updatePola();
|
|
|
|
|
}break;
|
|
|
|
|
//lewo-(a)
|
|
|
|
|
case 'a':
|
|
|
|
|
{
|
|
|
|
|
if (pole[pozycjaTraktoraY][pozycjaTraktoraX - 1][0] != '#')
|
2020-06-07 01:04:18 +02:00
|
|
|
|
{
|
2020-06-09 19:12:24 +02:00
|
|
|
|
correctMovement('W');
|
|
|
|
|
pole[pozycjaTraktoraY][pozycjaTraktoraX][0] = underTraktor;
|
|
|
|
|
pole[pozycjaTraktoraY][pozycjaTraktoraX][1] = underTraktorWeight;
|
|
|
|
|
pozycjaTraktoraX--;
|
|
|
|
|
increseState();
|
|
|
|
|
underTraktor = pole[pozycjaTraktoraY][pozycjaTraktoraX][0];
|
|
|
|
|
underTraktorWeight = pole[pozycjaTraktoraY][pozycjaTraktoraX][1];
|
|
|
|
|
pole[pozycjaTraktoraY][pozycjaTraktoraX][0] = 'T';
|
|
|
|
|
}
|
|
|
|
|
updatePola();
|
|
|
|
|
}break;
|
|
|
|
|
//prawo-(d)
|
|
|
|
|
case 'd':
|
|
|
|
|
{
|
|
|
|
|
if (pole[pozycjaTraktoraY][pozycjaTraktoraX + 1][0] != '#')
|
2020-06-07 01:04:18 +02:00
|
|
|
|
{
|
2020-06-09 19:12:24 +02:00
|
|
|
|
correctMovement('E');
|
|
|
|
|
pole[pozycjaTraktoraY][pozycjaTraktoraX][0] = underTraktor;
|
|
|
|
|
pole[pozycjaTraktoraY][pozycjaTraktoraX][1] = underTraktorWeight;
|
|
|
|
|
pozycjaTraktoraX++;
|
|
|
|
|
increseState();
|
|
|
|
|
underTraktor = pole[pozycjaTraktoraY][pozycjaTraktoraX][0];
|
|
|
|
|
underTraktorWeight = pole[pozycjaTraktoraY][pozycjaTraktoraX][1];
|
|
|
|
|
pole[pozycjaTraktoraY][pozycjaTraktoraX][0] = 'T';
|
|
|
|
|
}
|
|
|
|
|
updatePola();
|
|
|
|
|
}break;
|
2020-06-07 01:04:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-06-09 19:12:24 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-07 01:04:18 +02:00
|
|
|
|
bool isValid(int x, int y)
|
|
|
|
|
{
|
|
|
|
|
if (pole[x][y][0] != '#')
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2020-06-09 19:12:24 +02:00
|
|
|
|
bool isDestination(int x, int y, Pair dest)
|
2020-06-07 01:04:18 +02:00
|
|
|
|
{
|
|
|
|
|
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);
|
|
|
|
|
}
|
2020-06-09 19:12:24 +02:00
|
|
|
|
void plantVeg()
|
|
|
|
|
{
|
|
|
|
|
int x = pozycjaTraktoraX, y = pozycjaTraktoraY;
|
|
|
|
|
underTraktor = poleRecive[x][y];
|
|
|
|
|
przypiszKodGenetyczny(x, y, poleRecive[x][y]);
|
|
|
|
|
if (poleRecive[x][y] == '.')
|
|
|
|
|
{
|
|
|
|
|
underTraktorWeight = '1';
|
|
|
|
|
poleInt[x][y][0] = 0;
|
|
|
|
|
poleInt[x][y][1] = 0;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (poleRecive[x][y] == 'Z')
|
|
|
|
|
{
|
|
|
|
|
underTraktorWeight = '1';
|
|
|
|
|
poleInt[x][y][0] = 0;
|
|
|
|
|
poleInt[x][y][1] = 1;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
underTraktorWeight = '1';
|
|
|
|
|
poleInt[x][y][0] = 0;
|
|
|
|
|
poleInt[x][y][1] = 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
void tracePath(cell cellDetails[][COL], Pair dest, string action)
|
2020-06-07 01:04:18 +02:00
|
|
|
|
{
|
|
|
|
|
//printf("\nThe Path is "); //----start info
|
|
|
|
|
int row = dest.first;
|
2020-06-09 19:12:24 +02:00
|
|
|
|
int col = dest.second;
|
|
|
|
|
pair<int, int>src = make_pair(pozycjaTraktoraX, pozycjaTraktoraY);
|
|
|
|
|
timeToDest = 0;
|
2020-06-07 01:04:18 +02:00
|
|
|
|
|
|
|
|
|
stack<Pair> 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<int, int> p = Path.top();
|
|
|
|
|
Path.pop();
|
2020-06-09 19:12:24 +02:00
|
|
|
|
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(1000);
|
|
|
|
|
}
|
|
|
|
|
if (action == "time" || action == "moveWithTime")
|
|
|
|
|
{
|
|
|
|
|
if ((p.first != src.first || p.second != src.second)
|
|
|
|
|
&& (p.first != dest.first || p.second != dest.second))
|
|
|
|
|
{
|
|
|
|
|
timeToDest += ((double)pole[p.second][p.first][1] - 48)*1.0;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-06-07 01:04:18 +02:00
|
|
|
|
//printf("-> (%d,%d) ", p.first, p.second); //---- informacja wierzchołku
|
2020-06-09 19:12:24 +02:00
|
|
|
|
}
|
|
|
|
|
if (action == "moveWithPick")
|
|
|
|
|
{
|
|
|
|
|
if (underTraktor == 'B') {
|
|
|
|
|
zebraneBuraki[scoreBuraki] = kod_genetyczny[pozycjaTraktoraY][pozycjaTraktoraX];
|
|
|
|
|
scoreBuraki += 1;
|
|
|
|
|
kod_genetyczny[pozycjaTraktoraY][pozycjaTraktoraX] = "000000000";
|
|
|
|
|
}
|
|
|
|
|
else if (underTraktor == 'Z') {
|
|
|
|
|
zebraneZiemniaki[scoreZiemniaki] = kod_genetyczny[pozycjaTraktoraY][pozycjaTraktoraX];
|
|
|
|
|
scoreZiemniaki += 1;
|
|
|
|
|
kod_genetyczny[pozycjaTraktoraY][pozycjaTraktoraX] = "000000000";
|
|
|
|
|
}
|
|
|
|
|
obslugaAlgorytmuGenetycznego();
|
|
|
|
|
plantVeg();
|
2020-06-07 01:04:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-06-09 19:12:24 +02:00
|
|
|
|
void aStarSearch(int grid[][COL], Pair src, Pair dest, string action)
|
2020-06-07 01:04:18 +02:00
|
|
|
|
{
|
2020-06-09 19:12:24 +02:00
|
|
|
|
//bool closedList[ROW][COL];
|
2020-06-07 01:04:18 +02:00
|
|
|
|
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;
|
2020-06-09 19:12:24 +02:00
|
|
|
|
|
2020-06-07 01:04:18 +02:00
|
|
|
|
set<pPair> 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;
|
2020-06-09 19:12:24 +02:00
|
|
|
|
waga = ((double)pole[j][i][1] - 48) * 1.0;//----waga
|
2020-06-07 01:04:18 +02:00
|
|
|
|
|
|
|
|
|
//----------- 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");
|
2020-06-09 19:12:24 +02:00
|
|
|
|
tracePath(cellDetails, dest, action);
|
2020-06-07 01:04:18 +02:00
|
|
|
|
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");
|
2020-06-09 19:12:24 +02:00
|
|
|
|
tracePath(cellDetails, dest, action);
|
2020-06-07 01:04:18 +02:00
|
|
|
|
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");
|
2020-06-09 19:12:24 +02:00
|
|
|
|
tracePath(cellDetails, dest, action);
|
2020-06-07 01:04:18 +02:00
|
|
|
|
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");
|
2020-06-09 19:12:24 +02:00
|
|
|
|
tracePath(cellDetails, dest, action);
|
2020-06-07 01:04:18 +02:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-09 19:12:24 +02:00
|
|
|
|
void gogo(int endX, int endY)
|
2020-06-07 01:04:18 +02:00
|
|
|
|
{
|
|
|
|
|
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);
|
2020-06-09 19:12:24 +02:00
|
|
|
|
//aStarSearch(grid, src, dest);
|
|
|
|
|
aStarSearch(grid, src, dest, "moveWithPick");
|
2020-06-07 01:04:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void test1()
|
|
|
|
|
{
|
|
|
|
|
pole[1][3][0] = 'B';
|
|
|
|
|
pole[1][3][1] = '9';
|
2020-06-09 19:12:24 +02:00
|
|
|
|
poleInt[1][3][0] = 0;
|
|
|
|
|
poleInt[1][3][1] = 1;
|
2020-06-07 01:04:18 +02:00
|
|
|
|
pole[3][1][0] = 'Z';
|
|
|
|
|
pole[3][1][1] = '9';
|
2020-06-09 19:12:24 +02:00
|
|
|
|
poleInt[3][1][0] = 0;
|
|
|
|
|
poleInt[3][1][1] = 1;
|
|
|
|
|
|
2020-06-07 15:24:23 +02:00
|
|
|
|
kod_genetyczny[1][3] = przypiszKod("buraki");
|
|
|
|
|
kod_genetyczny[3][1] = przypiszKod("ziemniaki");
|
2020-06-07 01:04:18 +02:00
|
|
|
|
}
|
|
|
|
|
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';
|
2020-06-09 19:12:24 +02:00
|
|
|
|
poleInt[i][j][0] = 0;
|
|
|
|
|
poleInt[i][j][1] = 1;
|
2020-06-07 15:24:23 +02:00
|
|
|
|
kod_genetyczny[i][j] = przypiszKod("buraki");
|
2020-06-07 01:04:18 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-09 19:12:24 +02:00
|
|
|
|
/*void plantVeg()
|
|
|
|
|
{
|
|
|
|
|
int x = pozycjaTraktoraX, y = pozycjaTraktoraY;
|
|
|
|
|
underTraktor = poleRecive[x][y];
|
|
|
|
|
przypiszKodGenetyczny(x, y, poleRecive[x][y]);
|
|
|
|
|
if (poleRecive[x][y] == '.')
|
|
|
|
|
{
|
|
|
|
|
underTraktorWeight = '1';
|
|
|
|
|
poleInt[x][y][0] = 0;
|
|
|
|
|
poleInt[x][y][1] = 0;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (poleRecive[x][y] == 'Z')
|
|
|
|
|
{
|
|
|
|
|
underTraktorWeight = '5';
|
|
|
|
|
poleInt[x][y][0] = 0;
|
|
|
|
|
poleInt[x][y][1] = 1;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
underTraktorWeight = '9';
|
|
|
|
|
poleInt[x][y][0] = 0;
|
|
|
|
|
poleInt[x][y][1] = 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}*/
|
2020-06-07 01:04:18 +02:00
|
|
|
|
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)
|
|
|
|
|
{
|
2020-06-09 19:12:24 +02:00
|
|
|
|
poleRecive[j][i] = plant;
|
2020-06-07 01:04:18 +02:00
|
|
|
|
if (j == 25)
|
|
|
|
|
{
|
|
|
|
|
j = 1;
|
|
|
|
|
i += 1;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
j += 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-09 19:12:24 +02:00
|
|
|
|
//----------neuro------------//
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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]);
|
|
|
|
|
}
|
|
|
|
|
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 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);
|
|
|
|
|
/*
|
|
|
|
|
cout.precision(15);
|
|
|
|
|
for (int i = 0; i < 25; i++)
|
|
|
|
|
{
|
|
|
|
|
for (int j = 0; j < 25; j++)
|
|
|
|
|
{
|
|
|
|
|
cout << neuroOutputPole[i][j] << " ";
|
|
|
|
|
}
|
|
|
|
|
}*/
|
|
|
|
|
//Sleep(1000);
|
|
|
|
|
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 << j + 1 << "_" << i + 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);
|
|
|
|
|
}
|
|
|
|
|
//----------end------------//
|
|
|
|
|
|
|
|
|
|
|
2020-06-07 01:04:18 +02:00
|
|
|
|
void start1()
|
|
|
|
|
{
|
2020-06-09 19:12:24 +02:00
|
|
|
|
//int goalX = 3, goalY = 4;
|
|
|
|
|
test1();//yes
|
|
|
|
|
testSI1();//trzeba
|
2020-06-07 01:04:18 +02:00
|
|
|
|
pole[1][1][0] = 'T';
|
2020-06-09 19:12:24 +02:00
|
|
|
|
pole[1][1][1] = '9';
|
|
|
|
|
poleInt[1][1][0] = 0;
|
|
|
|
|
poleInt[1][1][1] = 0;
|
|
|
|
|
//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");
|
|
|
|
|
|
2020-06-07 01:04:18 +02:00
|
|
|
|
updatePola();
|
2020-06-09 19:12:24 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//sendState(); //trzeba recznie zmienial miedzy wysylaniem stanu a pobieraniem stanu pola
|
2020-06-07 01:04:18 +02:00
|
|
|
|
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));
|
2020-06-09 19:12:24 +02:00
|
|
|
|
|
2020-06-07 01:04:18 +02:00
|
|
|
|
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';
|
2020-06-09 19:12:24 +02:00
|
|
|
|
poleInt[i][j][0] = 0;
|
|
|
|
|
poleInt[i][j][1] = 1;
|
2020-06-07 01:04:18 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < 25; i++)
|
|
|
|
|
{
|
|
|
|
|
pole[i + 1][i + 1][0] = 'B';
|
|
|
|
|
pole[i + 1][i + 1][1] = '9';
|
2020-06-09 19:12:24 +02:00
|
|
|
|
poleInt[i + 1][i + 1][0] = 0;
|
|
|
|
|
poleInt[i + 1][i + 1][1] = 1;
|
2020-06-07 01:04:18 +02:00
|
|
|
|
}
|
2020-06-09 19:12:24 +02:00
|
|
|
|
|
|
|
|
|
for (int i = 0; i < 25; i++) {
|
|
|
|
|
for (int j = 0; j < 10; j++) {
|
2020-06-07 01:04:18 +02:00
|
|
|
|
pole[j + 1][i + 1][0] = 'B';
|
|
|
|
|
pole[j + 1][i + 1][1] = '9';
|
2020-06-09 19:12:24 +02:00
|
|
|
|
poleInt[j + 1][i + 1][0] = 0;
|
|
|
|
|
poleInt[j + 1][i + 1][1] = 1;
|
|
|
|
|
}
|
2020-06-07 01:04:18 +02:00
|
|
|
|
}
|
2020-06-09 19:12:24 +02:00
|
|
|
|
for (int i = 0; i < 25; i++) {
|
|
|
|
|
for (int j = 10; j < 20; j++) {
|
2020-06-07 01:04:18 +02:00
|
|
|
|
pole[j + 1][i + 1][0] = 'Z';
|
2020-06-09 19:12:24 +02:00
|
|
|
|
pole[j + 1][i + 1][1] = '5';
|
|
|
|
|
poleInt[j + 1][i + 1][0] = 0;
|
|
|
|
|
poleInt[j + 1][i + 1][1] = 1;
|
|
|
|
|
}
|
2020-06-07 01:04:18 +02:00
|
|
|
|
}
|
2020-06-09 19:12:24 +02:00
|
|
|
|
|
2020-06-07 15:24:23 +02:00
|
|
|
|
generujKody();
|
2020-06-07 01:04:18 +02:00
|
|
|
|
|
2020-06-09 19:12:24 +02:00
|
|
|
|
buildFirstMatrix();
|
|
|
|
|
buildMatrix();
|
|
|
|
|
|
2020-06-07 01:04:18 +02:00
|
|
|
|
updatePola();
|
|
|
|
|
start1(); // testy start 1-3
|
|
|
|
|
|
|
|
|
|
//---------start---------//
|
|
|
|
|
bool traktorDziala = true;
|
|
|
|
|
|
|
|
|
|
char akcja;
|
|
|
|
|
|
|
|
|
|
do
|
|
|
|
|
{
|
2020-06-09 19:12:24 +02:00
|
|
|
|
chousePath();
|
|
|
|
|
/*
|
2020-06-07 01:04:18 +02:00
|
|
|
|
akcja = _getch();
|
|
|
|
|
if (akcja == 'w' || akcja == 's' || akcja == 'a' || akcja == 'd')
|
|
|
|
|
{
|
|
|
|
|
Move(akcja);
|
|
|
|
|
}
|
|
|
|
|
if (akcja == '0')
|
|
|
|
|
{
|
|
|
|
|
traktorDziala = false;
|
|
|
|
|
}
|
2020-06-09 19:12:24 +02:00
|
|
|
|
*/
|
2020-06-07 01:04:18 +02:00
|
|
|
|
|
|
|
|
|
} while (traktorDziala);
|
|
|
|
|
//---------end---------//
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|