decision tree update

This commit is contained in:
Łukasz 2021-05-19 15:04:10 +02:00
parent 3b44694641
commit e10fbe4e48
6 changed files with 237 additions and 978 deletions

View File

@ -25,9 +25,9 @@ class Field:
elif self.state == 'toFertilize':
return 3
elif self.state == 'toWater':
return 200
return 4
elif self.state == 'toCut':
return 99
return 100
else:
return 0

View File

@ -1,221 +1,122 @@
from itertools import product
import numpy as np
import pandas as pd
import csv
import id3test
import pprint
import sys
import pandas
from sklearn import tree
import pydotplus
from sklearn.tree import DecisionTreeClassifier
import matplotlib.pyplot as plt
import matplotlib.image as pltimg
# stan pola: toCut, toPlow, toWater, toSeed, toFertilize
# pora dnia: dzień, noc
# pogoda: sunny, cloudy, rainy, hail
# temperatura: freezing, cold, mild, hot
# wiatr: windless, strong wind, gale
# humidy: low, high
field_states = ['toPlow', 'toWater', 'toSeed', 'toFertilize', 'toCut']
header = ['Equipped', 'Not Equipped']
hitch = ['Tillage Unit', 'Crop Trailer']
tillage_unit = ['Seeds', 'Water', 'Fertilizer', 'Nothing']
engine_working = ['Yes', 'No']
in_base = ['Yes', 'No']
fuel = ['Enough', 'Not_Enough']
day_time = ['Day', 'Night']
weather = ['Clear Sky', 'Cloudy', 'Rainy', 'Hail']
temperature = ['Freezing', 'Cold', 'Mild', 'Hot']
wind = ['Windless', 'Strong Wind', 'Gale']
humidy = ['Low', 'High']
# output = list(product(field_states, header, hitch, tillage_unit, engine_working, in_base, fuel))
output = list(product(field_states, header, hitch, tillage_unit, in_base))
output = list(product(field_states, day_time, weather, temperature, wind, humidy))
dict = []
# for x in range(len(output)):
# # dict.append({'field': output[x][0], 'header' : output[x][1], 'hitch' : output[x][2],
# # 'tillage_unit' : output[x][3], 'engine_working' : output[x][4],
# # 'in_base' : output[x][5], 'fuel': output[x][6]})
# print(x+1, output[x])
decisions = []
curr_decision = "Make Action"
for x in range(len(output)):
aField = output[x][0]
aHeader = output[x][1]
aHitch = output[x][2]
aTillage_unit = output[x][3]
aIn_base = output[x][4]
if x % 3 == 0:
while True:
curr_decision = "Make Action"
mField = output[x][0]
mDay_time = output[x][1]
mWeather = output[x][2]
mTemperature = output[x][3]
mWind = output[x][4]
mHumidy = output[x][5]
mDecision = 'null'
if aField == 'toCut':
if not aHeader == 'Equipped':
if aIn_base == 'Yes':
curr_decision = 'Change Header'
# pora dnia: dzień 2, noc -2
# pogoda: sunny+3, cloudy+3, rainy-2, hail-5
# temperatura: freezing -3, cold-1, mild+4, hot+2
# wiatr: windless +2, strong wind-1, gale-3
# humidy: low+2, high-3
if mDay_time == 'Day':
valDay_time = 2
else:
valDay_time = -3
if mWeather == 'Clear Sky' or 'Cloudy':
valWeather = 3
elif mWeather == 'Rainy':
valWeather = -2
else:
valWeather = -5
if mTemperature == 'Freezing':
valTemperature = -3
elif mTemperature == 'Cold':
valTemperature = -1
elif mTemperature == 'Mild':
valTemperature = 4
else:
valTemperature = 2
if mWind == 'Windless':
valWind = +2
elif mWind == 'Strong Wind':
valWind = -1
else:
valWind = -3
if humidy == 'Low':
valHumidy = 2
else:
valHumidy = -2
result = valDay_time + valWeather + valTemperature + valWind + valHumidy
if result >= 0:
mDecision = "Make Action"
else:
mDecision = "Wait"
#Special conditions
if mDay_time == 'Night' and (mField == 'toWater' or mField == 'toCut'):
mDecision = "Make Action"
break
else:
curr_decision = 'Go To Base'
mDecision = "Wait"
if mWeather == 'Rainy' and (mField == 'toPlow' or mField == 'toSeed'):
mDecision = "Make Action"
break
else:
mDecision = 'Wait'
if mWeather == 'Hail':
mDecision = 'Wait'
break
if not aHitch == 'Crop Trailer':
if aIn_base == 'Yes':
curr_decision = 'Change Hitch'
break
else:
curr_decision = 'Go To Base'
if mTemperature == 'Freezing':
mDecision = 'Wait'
break
if aField == 'toPlow':
if aHeader == 'Equipped':
if aIn_base == "Yes":
curr_decision = 'Change Header'
break
else:
curr_decision = 'Go To Base'
if mWind == 'Gale':
mDecision = 'Wait'
break
if not aHitch == "Tillage Unit":
if aIn_base == "Yes":
curr_decision = "Change Hitch"
if mHumidy == 'High' and mField == 'toCut':
mDecision = "Wait"
break
else:
curr_decision = "Go To Base"
break
if not aTillage_unit == "Nothing":
if aIn_base == "Yes":
curr_decision = "Change Load"
break
else:
curr_decision = "Go To Base"
mDecision = "Make Action"
break
if aField == 'toSeed':
if aHeader == 'Equipped':
if aIn_base == "Yes":
curr_decision = 'Change Header'
break
else:
curr_decision = 'Go To Base'
break
dict.append({'Field': mField, 'Day Time': mDay_time, 'Weather': mWeather,
'Temperature': mTemperature, 'Wind': mWind, 'Humidy': mHumidy, 'Decision': mDecision})
if not aHitch == "Tillage Unit":
if aIn_base == "Yes":
curr_decision = "Change Hitch"
break
else:
curr_decision = "Go To Base"
break
if not aTillage_unit == "Seeds":
if aIn_base == "Yes":
curr_decision = "Change Load"
break
else:
curr_decision = "Go To Base"
break
fields = ['Field', 'Day Time', 'Weather', 'Temperature', 'Wind', 'Humidy', 'Decision']
if aField == 'toWater':
if aHeader == 'Equipped':
if aIn_base == "Yes":
curr_decision = 'Change Header'
break
else:
curr_decision = 'Go To Base'
break
if not aHitch == "Tillage Unit":
if aIn_base == "Yes":
curr_decision = "Change Hitch"
break
else:
curr_decision = "Go To Base"
break
if not aTillage_unit == "Water":
if aIn_base == "Yes":
curr_decision = "Change Load"
break
else:
curr_decision = "Go To Base"
break
if aField == 'toFertilize':
if aHeader == 'Equipped':
if aIn_base == "Yes":
curr_decision = 'Change Header'
break
else:
curr_decision = 'Go To Base'
break
if not aHitch == "Tillage Unit":
if aIn_base == "Yes":
curr_decision = "Change Hitch"
break
else:
curr_decision = "Go To Base"
break
if not aTillage_unit == "Fertilizer":
if aIn_base == "Yes":
curr_decision = "Change Load"
break
else:
curr_decision = "Go To Base"
break
if aIn_base == 'Yes':
curr_decision = 'Go To Field'
break
dict.append({'Field': aField, 'Header': aHeader, 'Hitch': aHitch, 'Tillage_Unit': aTillage_unit,
'In_Base': aIn_base, 'Decision': curr_decision})
print(dict)
fields = ['Field', 'Header', 'Hitch', 'Tillage_Unit', 'In_Base', 'Decision']
filename = "treedata\\data2.csv"
filename = "treedata\\data.csv"
with open(filename, 'w') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=fields)
writer.writeheader()
writer.writerows(dict)
df = pandas.read_csv("treedata\\data2.csv")
print(df)
# Map text values to number values
d = {'toPlow': 0, 'toWater': 1, 'toSeed': 2, 'toFertilize': 3, 'toCut': 4}
df['Field'] = df['Field'].map(d)
d = {'Not Equipped': 0, 'Equipped': 1}
df['Header'] = df['Header'].map(d)
d = {'Tillage Unit': 0, 'Crop Trailer': 1}
df['Hitch'] = df['Hitch'].map(d)
d = {'Nothing': 0, 'Seeds': 1, 'Water': 2, 'Fertilizer': 3}
df['Tillage_Unit'] = df['Tillage_Unit'].map(d)
d = {'No': 0, 'Yes': 1}
df['In_Base'] = df['In_Base'].map(d)
d = {'Make Action': 0, 'Change Header': 1, 'Go To Base': 2, 'Change Hitch': 3, 'Change Load': 4, 'Go To Field': 5}
df['Decision'] = df['Decision'].map(d)
# Separate the feature columns from targert columns
features = ['Field', 'Header', 'Hitch', 'Tillage_Unit', 'In_Base']
X = df[features]
y = df['Decision']
# FIELD 'toPlow' : 0, 'toWater' : 1, 'toSeed' : 2, 'toFertilize' : 3, 'toCut' : 4
# HEADER 'Not Equipped' : 0, 'Equipped' : 1
# HITCH 'Tillage Unit' : 0, 'Crop Trailer' : 1
# TILLAGE 'Nothing' : 0, 'Seeds' : 1, 'Water': 2, 'Fertilizer': 3
# IN BASE 'No' : 0, 'Yes' : 1
dtree = DecisionTreeClassifier()
dtree = dtree.fit(X, y)
data = tree.export_graphviz(dtree, out_file=None, feature_names=features)
graph = pydotplus.graph_from_dot_data(data)
graph.write_png('mydecisiontree.png')
img = pltimg.imread('mydecisiontree.png')
imgplot = plt.imshow(img)
plt.show()
print(dtree.predict([[0, 1, 1, 3, 0]]))

View File

@ -1,7 +1,7 @@
import random
def checkConditions():
weather = random.choice(['Sunny', 'Cloudy', 'Rainy', 'Hail'])
weather = random.choice(['Clear Sky', 'Cloudy', 'Rainy', 'Hail'])
day_time = random.choice(['Day', 'Night'])
temperature = random.choice(['Freezing', 'Cold', 'Mild', 'Hot'])
wind = random.choice(['Windless', 'Strong Wind', 'Gale'])

View File

@ -23,13 +23,12 @@ df['Temperature'] = df['Temperature'].map(d)
d = {'Windless' : 0, 'Strong Wind' : 1, 'Gale': 2}
df['Wind'] = df['Wind'].map(d)
d={'Low': 0, 'High': 1}
d = {'Low': 0, 'High': 1}
df['Humidy'] = df['Humidy'].map(d)
d = {'Wait' : 0, 'Make Action' : 1}
df['Decision'] = df['Decision'].map(d)
#Separate the feature columns from targert columns
features = ['Field', 'Day Time', 'Weather', 'Temperature', 'Wind', 'Humidy']
@ -37,7 +36,6 @@ features = ['Field', 'Day Time', 'Weather', 'Temperature', 'Wind', 'Humidy']
X = df[features]
y = df['Decision']
dtree = DecisionTreeClassifier()
dtree = dtree.fit(X, y)

BIN
mydecisiontree.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 122 KiB

File diff suppressed because it is too large Load Diff