Prześlij pliki do ''

This commit is contained in:
Szymon Parafiński 2020-06-09 01:47:37 +00:00
parent 5978098747
commit 2fdd4767d0
4 changed files with 1484 additions and 1168 deletions

168
Main.cpp
View File

@ -1,4 +1,4 @@
//Main drzewa decyzyjne 1 + algorytm genetyczny //Main drzewa decyzyjne 1 + algorytm genetyczny
#include<iostream> #include<iostream>
#include<stdlib.h> #include<stdlib.h>
@ -13,9 +13,10 @@
#include <stdio.h> #include <stdio.h>
#include <cstdlib> #include <cstdlib>
#include <ctime> #include <ctime>
//#include<bits/stdc++.h>
#include <algorithm> #include <algorithm>
#include <utility> #include <utility>
//#include<bits/stdc++.h>
using namespace std; using namespace std;
@ -34,6 +35,81 @@ char pole[27][27][6];
int pozycjaTraktoraX = 1, pozycjaTraktoraY = 1; int pozycjaTraktoraX = 1, pozycjaTraktoraY = 1;
char currentWay = 'S'; char currentWay = 'S';
//========================================================
//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("pliki/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 pliki/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);
}
//========================================================
//algorytm genetyczny //algorytm genetyczny
int scoreBuraki = 0; int scoreBuraki = 0;
@ -65,7 +141,6 @@ string generateValue() {
} }
string generateVegetable() { string generateVegetable() {
string taste = generateValue(); string taste = generateValue();
@ -76,7 +151,6 @@ string generateVegetable() {
} }
void generatePopulation(string* population, int length) { void generatePopulation(string* population, int length) {
int i; int i;
@ -85,7 +159,6 @@ void generatePopulation(string * population,int length) {
} }
} }
int power(int x, int y) { int power(int x, int y) {
if (y == 0) return 1; if (y == 0) return 1;
@ -96,7 +169,6 @@ int power(int x, int y) {
else return x * temp * temp; else return x * temp * temp;
} }
int stringToInt(string str, int size) { int stringToInt(string str, int size) {
int x = 0; int x = 0;
@ -112,7 +184,6 @@ int stringToInt(string str,int size) {
return x; return x;
} }
int fitness(string vegetable) { int fitness(string vegetable) {
int taste = stringToInt(vegetable.substr(0, 3), 3); int taste = stringToInt(vegetable.substr(0, 3), 3);
@ -122,13 +193,11 @@ int fitness(string vegetable) {
return (taste + colour + size) / 3; return (taste + colour + size) / 3;
} }
bool comparePair(const pair<int, string>& i, const pair<int, string>& j) bool comparePair(const pair<int, string>& i, const pair<int, string>& j)
{ {
return i.first > j.first; return i.first > j.first;
} }
void ranking(string* population, string* parents, int populationSize, int parentsNumber) { void ranking(string* population, string* parents, int populationSize, int parentsNumber) {
int i; int i;
@ -145,7 +214,6 @@ void ranking(string * population,string * parents, int populationSize, int paren
delete[] fitnessTable; delete[] fitnessTable;
} }
bool exists(int len, int* array, int element) { bool exists(int len, int* array, int element) {
int i; int i;
@ -155,7 +223,6 @@ bool exists(int len, int * array, int element) {
return false; return false;
} }
void selection(string* population, string* parents, int populationSize, int parentsNumber) { void selection(string* population, string* parents, int populationSize, int parentsNumber) {
int i, j, k; int i, j, k;
@ -182,7 +249,7 @@ void selection(string * population,string * parents, int populationSize, int par
while (exists(parentsNumber, taken, j)) { while (exists(parentsNumber, taken, j)) {
j += 1; j += 1;
} }
while(roulette > fitnessTable[j].first and j<populationSize) { while (roulette > fitnessTable[j].first && j < populationSize) {
if (not exists(parentsNumber, taken, j)) { if (not exists(parentsNumber, taken, j)) {
roulette -= fitnessTable[j].first; roulette -= fitnessTable[j].first;
} }
@ -196,7 +263,6 @@ void selection(string * population,string * parents, int populationSize, int par
} }
string mutate(string child) { string mutate(string child) {
int d3 = rand() % 3; int d3 = rand() % 3;
@ -216,7 +282,6 @@ string mutate(string child) {
return child; return child;
} }
string cross(string parent[2]) { string cross(string parent[2]) {
int i; int i;
@ -248,7 +313,6 @@ string cross(string parent[2]) {
return child; return child;
} }
void crossover(string* parents, string* nextGen, int parentsNumber, int nextGenSize) { void crossover(string* parents, string* nextGen, int parentsNumber, int nextGenSize) {
int counter = 0; int counter = 0;
@ -285,7 +349,6 @@ void crossover(string * parents,string * nextGen,int parentsNumber,int nextGenSi
} }
} }
void genetic_algorithm(string* population, int populationSize, int parentsNumber, string* outcome, int outcomeSize) { void genetic_algorithm(string* population, int populationSize, int parentsNumber, string* outcome, int outcomeSize) {
int iteration, i; int iteration, i;
@ -304,7 +367,6 @@ void genetic_algorithm(string * population, int populationSize, int parentsNumbe
ranking(population, outcome, populationSize, outcomeSize); ranking(population, outcome, populationSize, outcomeSize);
} }
string przypiszKod(string warzywa) { string przypiszKod(string warzywa) {
if (warzywa == "buraki") { if (warzywa == "buraki") {
if (gmoLeftBuraki > 0) { if (gmoLeftBuraki > 0) {
@ -328,7 +390,6 @@ string przypiszKod(string warzywa) {
} }
} }
void przypiszKodGenetyczny(int i, int j, char plant) { void przypiszKodGenetyczny(int i, int j, char plant) {
if (plant == 'B') { if (plant == 'B') {
kod_genetyczny[i][j] = przypiszKod("buraki"); kod_genetyczny[i][j] = przypiszKod("buraki");
@ -338,7 +399,6 @@ void przypiszKodGenetyczny(int i, int j, char plant) {
} }
} }
void obslugaAlgorytmuGenetycznego() { void obslugaAlgorytmuGenetycznego() {
cout << "Zebrane buraki: " << scoreBuraki << endl; cout << "Zebrane buraki: " << scoreBuraki << endl;
cout << "Zebrane ziemniaki: " << scoreZiemniaki << endl; cout << "Zebrane ziemniaki: " << scoreZiemniaki << endl;
@ -374,7 +434,6 @@ void obslugaAlgorytmuGenetycznego() {
} }
} }
void generujKody() { void generujKody() {
for (int i = 0;i < 27;i++) { for (int i = 0;i < 27;i++) {
for (int j = 0;j < 27;j++) { for (int j = 0;j < 27;j++) {
@ -388,7 +447,7 @@ void generujKody() {
} }
} }
//--------------------------------------------------------------------- //========================================================
void color(string foregroundColor, string backgroundColor) void color(string foregroundColor, string backgroundColor)
@ -985,7 +1044,7 @@ void testSI1()
void sendState() void sendState()
{ {
ofstream write("dane.txt"); ofstream write("pliki/dane.txt");
for (int i = 1; i < 26; i++) for (int i = 1; i < 26; i++)
{ {
for (int j = 1; j < 26; j++) for (int j = 1; j < 26; j++)
@ -1005,7 +1064,7 @@ void sendState()
} }
void reciveState() void reciveState()
{ {
ifstream read("decyzje.txt"); ifstream read("pliki/decyzje.txt");
if (read.is_open()) if (read.is_open())
{ {
char plant; char plant;
@ -1091,6 +1150,68 @@ void start3()
gogo(goalX, goalY); gogo(goalX, goalY);
} }
void testTree() {
int x, y;
x = 3;
y = 3;
//Nie podejmuj
stan[x][y][0] = 0;
stan[x][y][1] = 10;
stanPola(x, y);
cout << decyzja;
}
void testTree1() {
int x, y;
x = 3;
y = 3;
//Nawoz
stan[x][y][0] = 0;
stan[x][y][1] = 15;
stanPola(x, y);
cout << decyzja;
}
void testTree2() {
int x, y;
x = 3;
y = 3;
//Nie podejmuj
stan[x][y][0] = 1;
stan[x][y][1] = 20;
stanPola(x, y);
cout << decyzja;
}
void testTree3() {
int x, y;
x = 3;
y = 3;
//Zbierz
stan[x][y][0] = 1;
stan[x][y][1] = 41;
stanPola(x, y);
cout << decyzja;
}
void testTree4() {
int x, y;
x = 3;
y = 3;
//Nie podejmuj
stan[x][y][0] = 1;
stan[x][y][1] = 90;
stanPola(x, y);
cout << decyzja;
}
void testTree5() {
int x, y;
x = 3;
y = 3;
//Srodek
stan[x][y][0] = 3;
stan[x][y][1] = 90;
stanPola(x, y);
cout << decyzja;
}
int main() int main()
{ {
srand(time(0)); srand(time(0));
@ -1142,6 +1263,7 @@ int main()
start1(); // testy start 1-3 start1(); // testy start 1-3
//---------start---------// //---------start---------//
bool traktorDziala = true; bool traktorDziala = true;

View File

@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29503.13
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Traktor_DrzewaDecyzyjne", "Traktor_DrzewaDecyzyjne.vcxproj", "{C2FF0872-AC9D-4BD9-A46D-DC65A728BFF5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{C2FF0872-AC9D-4BD9-A46D-DC65A728BFF5}.Debug|x64.ActiveCfg = Debug|x64
{C2FF0872-AC9D-4BD9-A46D-DC65A728BFF5}.Debug|x64.Build.0 = Debug|x64
{C2FF0872-AC9D-4BD9-A46D-DC65A728BFF5}.Debug|x86.ActiveCfg = Debug|Win32
{C2FF0872-AC9D-4BD9-A46D-DC65A728BFF5}.Debug|x86.Build.0 = Debug|Win32
{C2FF0872-AC9D-4BD9-A46D-DC65A728BFF5}.Release|x64.ActiveCfg = Release|x64
{C2FF0872-AC9D-4BD9-A46D-DC65A728BFF5}.Release|x64.Build.0 = Release|x64
{C2FF0872-AC9D-4BD9-A46D-DC65A728BFF5}.Release|x86.ActiveCfg = Release|Win32
{C2FF0872-AC9D-4BD9-A46D-DC65A728BFF5}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {891B470F-58C4-41E4-A102-A1D8CBB6BD64}
EndGlobalSection
EndGlobal

View File

@ -0,0 +1,159 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>16.0</VCProjectVersion>
<ProjectGuid>{C2FF0872-AC9D-4BD9-A46D-DC65A728BFF5}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>TraktorDrzewaDecyzyjne</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<PrecompiledHeader>
</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<PrecompiledHeader>
</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<PrecompiledHeader>
</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<PrecompiledHeader>
</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="Main.cpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup />
</Project>