fin implemented DecisionTree and GeneticOrder

This commit is contained in:
Jeremi Lisek 2022-06-09 21:54:18 +02:00
parent abd60f9c15
commit 6866e825ce
4 changed files with 153 additions and 74 deletions

View File

@ -183,15 +183,23 @@ class GameModel(Model):
if self.phase == Phase.CLIENT_SORTING: if self.phase == Phase.CLIENT_SORTING:
orders: [Order] = self.orderList orders: [Order] = self.orderList
tree: DecisionTree = DecisionTree() tree: DecisionTree = DecisionTree()
readyTree = tree.get_decision_tree()
# CLIENT RECOGNITION
orders_with_prio = tree.get_data_good(orders)
# TODO CLIENT RECOGNITION # print("before:" )
# for i in range(len(orders_with_prio)):
# print("ORDER {}, PRIO: {}".format(orders_with_prio[i].id, orders_with_prio[i].priority))
# GENERICS SORTING # GENERICS SORTING
genericOrder: GeneticOrder = GeneticOrder(orders) genericOrder: GeneticOrder = GeneticOrder(orders_with_prio)
self.orderList = genericOrder.get_orders_sorted(orders) new_orders = genericOrder.get_orders_sorted(orders)
# print("after:" )
# for i in range(len(new_orders)):
# print("ORDER {}, PRIO: {}".format(new_orders[i].id, new_orders[i].priority))
self.orderList = new_orders
print("FINISHED CLIENT ORDER SORTING") print("FINISHED CLIENT ORDER SORTING")
self.phase = Phase.EXECUTION self.phase = Phase.EXECUTION

View File

@ -15,7 +15,7 @@ class GeneticOrder:
best_fit_super_special = 20 best_fit_super_special = 20
population_size = 200 population_size = 200
number_of_populations = 10000 number_of_populations = 1000
punish_low = 500 punish_low = 500
punish_med = 300 punish_med = 300

130
main.py
View File

@ -100,71 +100,69 @@ if __name__ == '__main__':
[grid, readyText, provided_itesm, recognised_items, ordersText, [grid, readyText, provided_itesm, recognised_items, ordersText,
fulfilled_orders], fulfilled_orders],
"Automatyczny Wózek Widłowy", "Automatyczny Wózek Widłowy",
dict(width=gridHeight, height=gridWidth, graph=diagram, items=50, orders=3, classificator=model)) dict(width=gridHeight, height=gridWidth, graph=diagram, items=50, orders=20, classificator=model))
def order_rule(order: Order): # def order_rule(order: Order):
return order.id # return order.id
#
#
# punish_low = 500
# punish_med = 300
# def sum_wrong(member: [Order]) -> int:
# last_high = 0
# last_med = 0
#
# sum_high = 0
# sum_med = 0
# sum_low = 0
#
# counter = 0
#
# for i in range(len(member)):
# o: Order = member[i]
# if o.priority == Priority.HIGH :
# sum_high += 1
# last_high = i
# elif o.priority == Priority.MEDIUM:
# sum_med += 1
# last_med = i
# else:
# sum_low += 1
#
# for i in range(last_high):
# o: Order = member[i]
# if o.priority == Priority.MEDIUM:
# counter += punish_med
# elif o.priority == Priority.LOW:
# counter += punish_low
#
# for i in range(last_med):
# o: Order = member[i]
# if o.priority == Priority.LOW:
# counter += punish_low
#
# print("sum: high:", sum_high, "med:", sum_med, "low:", sum_low)
# print("last_high:", last_high, "last_med:", last_med, "sum:", counter)
# return counter
#
# orders = InitialStateFactory.generate_order_list_XD(20)
# test: GeneticOrder = GeneticOrder(orders)
# punish_low = test.punish_low
# punish_med = test.punish_med
#
# print("SIEMA before: ")
# sum_wrong(orders)
# for i in orders:
# print("id:", i.id, "priority:", i.priority, "sum/time:", i.sum/i.time)
# # print("id:", i.id, "priority:", i.priority)
#
# newOrders = test.get_orders_sorted(orders)
#
# print("NAURA after:")
# sum_wrong(newOrders)
# for i in newOrders:
# print("id:", i.id, "priority:", i.priority, "sum/time:", i.sum/i.time)
# # print("id:", i.id, "priority:", i.priority)
server.port = 8888
punish_low = 500 server.launch()
punish_med = 300
def sum_wrong(member: [Order]) -> int:
last_high = 0
last_med = 0
sum_high = 0
sum_med = 0
sum_low = 0
counter = 0
for i in range(len(member)):
o: Order = member[i]
if o.priority == Priority.HIGH :
sum_high += 1
last_high = i
elif o.priority == Priority.MEDIUM:
sum_med += 1
last_med = i
else:
sum_low += 1
for i in range(last_high):
o: Order = member[i]
if o.priority == Priority.MEDIUM:
counter += punish_med
elif o.priority == Priority.LOW:
counter += punish_low
for i in range(last_med):
o: Order = member[i]
if o.priority == Priority.LOW:
counter += punish_low
print("sum: high:", sum_high, "med:", sum_med, "low:", sum_low)
print("last_high:", last_high, "last_med:", last_med, "sum:", counter)
return counter
orders = InitialStateFactory.generate_order_list_XD(20)
test: GeneticOrder = GeneticOrder(orders)
punish_low = test.punish_low
punish_med = test.punish_med
print("SIEMA before: ")
sum_wrong(orders)
for i in orders:
print("id:", i.id, "priority:", i.priority, "sum/time:", i.sum/i.time)
# print("id:", i.id, "priority:", i.priority)
newOrders = test.get_orders_sorted(orders)
print("NAURA after:")
sum_wrong(newOrders)
for i in newOrders:
print("id:", i.id, "priority:", i.priority, "sum/time:", i.sum/i.time)
# print("id:", i.id, "priority:", i.priority)
# server.port = 8888
# server.launch()

View File

@ -1,11 +1,17 @@
import csv import csv
import numpy as np
import pandas import pandas
import sklearn import sklearn
from sklearn import metrics, preprocessing from sklearn import metrics, preprocessing
from sklearn.model_selection import train_test_split from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier from sklearn.tree import DecisionTreeClassifier
from InitialStateFactory import InitialStateFactory
from data.ClientParams import ClientParams
from data.Order import Order
from data.enum.CompanySize import CompanySize
from data.enum.Priority import Priority
from util.ClientParamsFactory import ClientParamsFactory from util.ClientParamsFactory import ClientParamsFactory
@ -58,6 +64,63 @@ class DecisionTree:
print("\nDecisionTrees's Accuracy: ", metrics.accuracy_score(y, prediction)) print("\nDecisionTrees's Accuracy: ", metrics.accuracy_score(y, prediction))
def get_data_good(self, orders: [Order]) -> [Order]:
n_array_input = []
for i in range(len(orders)):
o:Order = orders[i]
cp: ClientParams = o.client_params
pom = []
pom.append(cp.payment_delay)
pom.append(cp.payed)
pom.append(cp.net_worth)
pom.append(cp.infuence_rate)
pom.append(cp.is_skarbowka)
pom.append(cp.membership)
pom.append(cp.is_hat)
size: CompanySize = cp.company_size
if(size == CompanySize.NO):
pom.append(0)
if (size == CompanySize.SMALL):
pom.append(1)
if (size == CompanySize.NORMAL):
pom.append(2)
if (size == CompanySize.BIG):
pom.append(3)
if (size == CompanySize.HUGE):
pom.append(4)
if (size == CompanySize.GIGANTISHE):
pom.append(5)
n_array_input.append(pom)
n_array = np.array(n_array_input)
# print(n_array)
# print(n_array[0])
tree = self.get_decision_tree()
priority = tree.predict(n_array)
for i in range(len(orders)):
print(orders[i].priority)
orders[i].priority = priority[i]
if priority[i] == "LOW":
orders[i].priority = Priority.LOW
if priority[i] == "MEDIUM":
orders[i].priority = Priority.MEDIUM
if priority[i] == "HIGH":
orders[i].priority = Priority.HIGH
print(orders[i].priority)
return orders
def get_decision_tree(self) -> DecisionTreeClassifier: def get_decision_tree(self) -> DecisionTreeClassifier:
@ -72,15 +135,25 @@ class DecisionTree:
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.2, train_size=0.8) X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.2, train_size=0.8)
# print(len(X_train[0]))
# print(X_train[0])
drugTree = DecisionTreeClassifier(criterion="entropy", max_depth=4) drugTree = DecisionTreeClassifier(criterion="entropy", max_depth=4)
clf = drugTree.fit(X_train, y_train) clf = drugTree.fit(X_train, y_train)
predicted = drugTree.predict(X_test) predicted = drugTree.predict(X_test)
# print(type(X_test))
y_test = y_test.to_list() y_test = y_test.to_list()
self.print_logs(X_test, y_test, predicted) # self.print_logs(X_test, y_test, predicted)
print(sklearn.tree.export_text(clf, feature_names=X_headers)) # print(sklearn.tree.export_text(clf, feature_names=X_headers))
return drugTree return drugTree
# kurwa = DecisionTree()
# kurwa.get_data_good(InitialStateFactory.generate_order_list(50))