Compare commits

...

4 Commits

Author SHA1 Message Date
9c3d698f05 Zaktualizuj 'injectCode.py' 2020-06-09 01:52:50 +00:00
525ec6cde1 DecisionTree 2020-06-09 01:52:24 +00:00
25bcbe6751 Zaktualizuj 'Main.cpp' 2020-06-09 01:51:22 +00:00
2fdd4767d0 Prześlij pliki do '' 2020-06-09 01:47:37 +00:00
6 changed files with 702 additions and 353 deletions

164
Main.cpp
View File

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

BIN
decisionTree.sav Normal file

Binary file not shown.

33
injectCode.py Normal file
View File

@ -0,0 +1,33 @@
import pickle
import sys
def prediction(warzywo, nawoz ,srodek, stan_wzrostu):
filename = 'decisionTree.sav'
tree = pickle.load(open(filename, 'rb'))
val = (tree.predict([[warzywo, nawoz, srodek, stan_wzrostu]]))
save(val)
def save(prediction):
pred = str(prediction)
plik = open('dec.txt', 'w')
plik.write(pred[1])
plik.close()
def decision(prediction):
if prediction == 0:
return "Nie_podejmuj_dzialania"
elif prediction == 1:
return "Zastosuj_nawoz"
elif prediction == 2:
return "Zastosuj_srodek"
elif prediction == 4:
return "Zbierz"
elif prediction == 5:
return "Roslina_juz_zgnila__zbierz_i_wyrzuc"
if __name__ == '__main__':
# Map command line arguments to function arguments.
prediction(*sys.argv[1:])