forked from s452751/AI_PRO
decision tree update
This commit is contained in:
parent
3b44694641
commit
e10fbe4e48
4
Field.py
4
Field.py
@ -25,9 +25,9 @@ class Field:
|
|||||||
elif self.state == 'toFertilize':
|
elif self.state == 'toFertilize':
|
||||||
return 3
|
return 3
|
||||||
elif self.state == 'toWater':
|
elif self.state == 'toWater':
|
||||||
return 200
|
return 4
|
||||||
elif self.state == 'toCut':
|
elif self.state == 'toCut':
|
||||||
return 99
|
return 100
|
||||||
else:
|
else:
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
287
Testfile.py
287
Testfile.py
@ -1,221 +1,122 @@
|
|||||||
from itertools import product
|
from itertools import product
|
||||||
import numpy as np
|
|
||||||
import pandas as pd
|
|
||||||
import csv
|
import csv
|
||||||
import id3test
|
|
||||||
import pprint
|
# stan pola: toCut, toPlow, toWater, toSeed, toFertilize
|
||||||
import sys
|
# pora dnia: dzień, noc
|
||||||
import pandas
|
# pogoda: sunny, cloudy, rainy, hail
|
||||||
from sklearn import tree
|
# temperatura: freezing, cold, mild, hot
|
||||||
import pydotplus
|
# wiatr: windless, strong wind, gale
|
||||||
from sklearn.tree import DecisionTreeClassifier
|
# humidy: low, high
|
||||||
import matplotlib.pyplot as plt
|
|
||||||
import matplotlib.image as pltimg
|
|
||||||
|
|
||||||
field_states = ['toPlow', 'toWater', 'toSeed', 'toFertilize', 'toCut']
|
field_states = ['toPlow', 'toWater', 'toSeed', 'toFertilize', 'toCut']
|
||||||
header = ['Equipped', 'Not Equipped']
|
day_time = ['Day', 'Night']
|
||||||
hitch = ['Tillage Unit', 'Crop Trailer']
|
weather = ['Clear Sky', 'Cloudy', 'Rainy', 'Hail']
|
||||||
tillage_unit = ['Seeds', 'Water', 'Fertilizer', 'Nothing']
|
temperature = ['Freezing', 'Cold', 'Mild', 'Hot']
|
||||||
engine_working = ['Yes', 'No']
|
wind = ['Windless', 'Strong Wind', 'Gale']
|
||||||
in_base = ['Yes', 'No']
|
humidy = ['Low', 'High']
|
||||||
fuel = ['Enough', 'Not_Enough']
|
|
||||||
|
|
||||||
# output = list(product(field_states, header, hitch, tillage_unit, engine_working, in_base, fuel))
|
output = list(product(field_states, day_time, weather, temperature, wind, humidy))
|
||||||
output = list(product(field_states, header, hitch, tillage_unit, in_base))
|
|
||||||
|
|
||||||
dict = []
|
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)):
|
for x in range(len(output)):
|
||||||
aField = output[x][0]
|
if x % 3 == 0:
|
||||||
aHeader = output[x][1]
|
while True:
|
||||||
aHitch = output[x][2]
|
mField = output[x][0]
|
||||||
aTillage_unit = output[x][3]
|
mDay_time = output[x][1]
|
||||||
aIn_base = output[x][4]
|
mWeather = output[x][2]
|
||||||
|
mTemperature = output[x][3]
|
||||||
|
mWind = output[x][4]
|
||||||
|
mHumidy = output[x][5]
|
||||||
|
mDecision = 'null'
|
||||||
|
|
||||||
while True:
|
# pora dnia: dzień 2, noc -2
|
||||||
curr_decision = "Make Action"
|
# 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 aField == 'toCut':
|
if mDay_time == 'Day':
|
||||||
if not aHeader == 'Equipped':
|
valDay_time = 2
|
||||||
if aIn_base == 'Yes':
|
else:
|
||||||
curr_decision = 'Change Header'
|
valDay_time = -3
|
||||||
break
|
|
||||||
else:
|
|
||||||
curr_decision = 'Go To Base'
|
|
||||||
break
|
|
||||||
|
|
||||||
if not aHitch == 'Crop Trailer':
|
if mWeather == 'Clear Sky' or 'Cloudy':
|
||||||
if aIn_base == 'Yes':
|
valWeather = 3
|
||||||
curr_decision = 'Change Hitch'
|
elif mWeather == 'Rainy':
|
||||||
break
|
valWeather = -2
|
||||||
else:
|
else:
|
||||||
curr_decision = 'Go To Base'
|
valWeather = -5
|
||||||
break
|
|
||||||
|
|
||||||
if aField == 'toPlow':
|
if mTemperature == 'Freezing':
|
||||||
if aHeader == 'Equipped':
|
valTemperature = -3
|
||||||
if aIn_base == "Yes":
|
elif mTemperature == 'Cold':
|
||||||
curr_decision = 'Change Header'
|
valTemperature = -1
|
||||||
break
|
elif mTemperature == 'Mild':
|
||||||
else:
|
valTemperature = 4
|
||||||
curr_decision = 'Go To Base'
|
else:
|
||||||
break
|
valTemperature = 2
|
||||||
|
|
||||||
if not aHitch == "Tillage Unit":
|
if mWind == 'Windless':
|
||||||
if aIn_base == "Yes":
|
valWind = +2
|
||||||
curr_decision = "Change Hitch"
|
elif mWind == 'Strong Wind':
|
||||||
break
|
valWind = -1
|
||||||
else:
|
else:
|
||||||
curr_decision = "Go To Base"
|
valWind = -3
|
||||||
break
|
|
||||||
if not aTillage_unit == "Nothing":
|
|
||||||
if aIn_base == "Yes":
|
|
||||||
curr_decision = "Change Load"
|
|
||||||
break
|
|
||||||
else:
|
|
||||||
curr_decision = "Go To Base"
|
|
||||||
break
|
|
||||||
|
|
||||||
if aField == 'toSeed':
|
if humidy == 'Low':
|
||||||
if aHeader == 'Equipped':
|
valHumidy = 2
|
||||||
if aIn_base == "Yes":
|
else:
|
||||||
curr_decision = 'Change Header'
|
valHumidy = -2
|
||||||
break
|
|
||||||
else:
|
|
||||||
curr_decision = 'Go To Base'
|
|
||||||
break
|
|
||||||
|
|
||||||
if not aHitch == "Tillage Unit":
|
result = valDay_time + valWeather + valTemperature + valWind + valHumidy
|
||||||
if aIn_base == "Yes":
|
if result >= 0:
|
||||||
curr_decision = "Change Hitch"
|
mDecision = "Make Action"
|
||||||
break
|
else:
|
||||||
else:
|
mDecision = "Wait"
|
||||||
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
|
|
||||||
|
|
||||||
if aField == 'toWater':
|
#Special conditions
|
||||||
if aHeader == 'Equipped':
|
if mDay_time == 'Night' and (mField == 'toWater' or mField == 'toCut'):
|
||||||
if aIn_base == "Yes":
|
mDecision = "Make Action"
|
||||||
curr_decision = 'Change Header'
|
break
|
||||||
break
|
else:
|
||||||
else:
|
mDecision = "Wait"
|
||||||
curr_decision = 'Go To Base'
|
|
||||||
break
|
|
||||||
|
|
||||||
if not aHitch == "Tillage Unit":
|
if mWeather == 'Rainy' and (mField == 'toPlow' or mField == 'toSeed'):
|
||||||
if aIn_base == "Yes":
|
mDecision = "Make Action"
|
||||||
curr_decision = "Change Hitch"
|
break
|
||||||
break
|
else:
|
||||||
else:
|
mDecision = 'Wait'
|
||||||
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 mWeather == 'Hail':
|
||||||
if aHeader == 'Equipped':
|
mDecision = 'Wait'
|
||||||
if aIn_base == "Yes":
|
break
|
||||||
curr_decision = 'Change Header'
|
|
||||||
break
|
|
||||||
else:
|
|
||||||
curr_decision = 'Go To Base'
|
|
||||||
break
|
|
||||||
|
|
||||||
if not aHitch == "Tillage Unit":
|
if mTemperature == 'Freezing':
|
||||||
if aIn_base == "Yes":
|
mDecision = 'Wait'
|
||||||
curr_decision = "Change Hitch"
|
break
|
||||||
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)
|
if mWind == 'Gale':
|
||||||
|
mDecision = 'Wait'
|
||||||
|
break
|
||||||
|
|
||||||
fields = ['Field', 'Header', 'Hitch', 'Tillage_Unit', 'In_Base', 'Decision']
|
if mHumidy == 'High' and mField == 'toCut':
|
||||||
|
mDecision = "Wait"
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
mDecision = "Make Action"
|
||||||
|
|
||||||
filename = "treedata\\data2.csv"
|
break
|
||||||
|
|
||||||
|
dict.append({'Field': mField, 'Day Time': mDay_time, 'Weather': mWeather,
|
||||||
|
'Temperature': mTemperature, 'Wind': mWind, 'Humidy': mHumidy, 'Decision': mDecision})
|
||||||
|
|
||||||
|
fields = ['Field', 'Day Time', 'Weather', 'Temperature', 'Wind', 'Humidy', 'Decision']
|
||||||
|
|
||||||
|
filename = "treedata\\data.csv"
|
||||||
|
|
||||||
with open(filename, 'w') as csvfile:
|
with open(filename, 'w') as csvfile:
|
||||||
writer = csv.DictWriter(csvfile, fieldnames=fields)
|
writer = csv.DictWriter(csvfile, fieldnames=fields)
|
||||||
writer.writeheader()
|
writer.writeheader()
|
||||||
writer.writerows(dict)
|
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]]))
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import random
|
import random
|
||||||
|
|
||||||
def checkConditions():
|
def checkConditions():
|
||||||
weather = random.choice(['Sunny', 'Cloudy', 'Rainy', 'Hail'])
|
weather = random.choice(['Clear Sky', 'Cloudy', 'Rainy', 'Hail'])
|
||||||
day_time = random.choice(['Day', 'Night'])
|
day_time = random.choice(['Day', 'Night'])
|
||||||
temperature = random.choice(['Freezing', 'Cold', 'Mild', 'Hot'])
|
temperature = random.choice(['Freezing', 'Cold', 'Mild', 'Hot'])
|
||||||
wind = random.choice(['Windless', 'Strong Wind', 'Gale'])
|
wind = random.choice(['Windless', 'Strong Wind', 'Gale'])
|
||||||
|
@ -23,13 +23,12 @@ df['Temperature'] = df['Temperature'].map(d)
|
|||||||
d = {'Windless' : 0, 'Strong Wind' : 1, 'Gale': 2}
|
d = {'Windless' : 0, 'Strong Wind' : 1, 'Gale': 2}
|
||||||
df['Wind'] = df['Wind'].map(d)
|
df['Wind'] = df['Wind'].map(d)
|
||||||
|
|
||||||
d={'Low': 0, 'High': 1}
|
d = {'Low': 0, 'High': 1}
|
||||||
df['Humidy'] = df['Humidy'].map(d)
|
df['Humidy'] = df['Humidy'].map(d)
|
||||||
|
|
||||||
d = {'Wait' : 0, 'Make Action' : 1}
|
d = {'Wait' : 0, 'Make Action' : 1}
|
||||||
df['Decision'] = df['Decision'].map(d)
|
df['Decision'] = df['Decision'].map(d)
|
||||||
|
|
||||||
|
|
||||||
#Separate the feature columns from targert columns
|
#Separate the feature columns from targert columns
|
||||||
|
|
||||||
features = ['Field', 'Day Time', 'Weather', 'Temperature', 'Wind', 'Humidy']
|
features = ['Field', 'Day Time', 'Weather', 'Temperature', 'Wind', 'Humidy']
|
||||||
@ -37,7 +36,6 @@ features = ['Field', 'Day Time', 'Weather', 'Temperature', 'Wind', 'Humidy']
|
|||||||
X = df[features]
|
X = df[features]
|
||||||
y = df['Decision']
|
y = df['Decision']
|
||||||
|
|
||||||
|
|
||||||
dtree = DecisionTreeClassifier()
|
dtree = DecisionTreeClassifier()
|
||||||
dtree = dtree.fit(X, y)
|
dtree = dtree.fit(X, y)
|
||||||
|
|
||||||
|
BIN
mydecisiontree.png
Normal file
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
Loading…
Reference in New Issue
Block a user