From 6866e825ce537b60b344a0f4ae0159c6e8a21ba3 Mon Sep 17 00:00:00 2001 From: Jeremi Lisek Date: Thu, 9 Jun 2022 21:54:18 +0200 Subject: [PATCH] fin implemented DecisionTree and GeneticOrder --- GameModel.py | 16 +++-- genetic_order/GeneticOrder.py | 2 +- main.py | 130 +++++++++++++++++----------------- tree/DecisionTree.py | 79 ++++++++++++++++++++- 4 files changed, 153 insertions(+), 74 deletions(-) diff --git a/GameModel.py b/GameModel.py index d99e82a..78630a7 100644 --- a/GameModel.py +++ b/GameModel.py @@ -183,15 +183,23 @@ class GameModel(Model): if self.phase == Phase.CLIENT_SORTING: orders: [Order] = self.orderList 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 - genericOrder: GeneticOrder = GeneticOrder(orders) - self.orderList = genericOrder.get_orders_sorted(orders) + genericOrder: GeneticOrder = GeneticOrder(orders_with_prio) + 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") self.phase = Phase.EXECUTION diff --git a/genetic_order/GeneticOrder.py b/genetic_order/GeneticOrder.py index 931b20c..7037a9a 100644 --- a/genetic_order/GeneticOrder.py +++ b/genetic_order/GeneticOrder.py @@ -15,7 +15,7 @@ class GeneticOrder: best_fit_super_special = 20 population_size = 200 - number_of_populations = 10000 + number_of_populations = 1000 punish_low = 500 punish_med = 300 diff --git a/main.py b/main.py index a180ea2..c6e2056 100644 --- a/main.py +++ b/main.py @@ -100,71 +100,69 @@ if __name__ == '__main__': [grid, readyText, provided_itesm, recognised_items, ordersText, fulfilled_orders], "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): - return order.id + # def order_rule(order: Order): + # 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) - - 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 - # server.launch() + server.port = 8888 + server.launch() diff --git a/tree/DecisionTree.py b/tree/DecisionTree.py index bdb6cb7..968aec3 100644 --- a/tree/DecisionTree.py +++ b/tree/DecisionTree.py @@ -1,11 +1,17 @@ import csv +import numpy as np import pandas import sklearn from sklearn import metrics, preprocessing from sklearn.model_selection import train_test_split 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 @@ -58,6 +64,63 @@ class DecisionTree: 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: @@ -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) + # print(len(X_train[0])) + # print(X_train[0]) + drugTree = DecisionTreeClassifier(criterion="entropy", max_depth=4) clf = drugTree.fit(X_train, y_train) predicted = drugTree.predict(X_test) + # print(type(X_test)) + 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 \ No newline at end of file + return drugTree + + + +# kurwa = DecisionTree() +# kurwa.get_data_good(InitialStateFactory.generate_order_list(50)) \ No newline at end of file