genetic -> master #4

Merged
s464943 merged 4 commits from genetic into master 2022-06-09 22:05:44 +02:00
3 changed files with 47 additions and 23 deletions
Showing only changes of commit abd60f9c15 - Show all commits

View File

@ -18,8 +18,10 @@ from data.Order import Order
from data.enum.ItemType import ItemType
from decision.Action import Action
from decision.ActionType import ActionType
from genetic_order.GeneticOrder import GeneticOrder
from imageClasification.Classificator import image_classification
from pathfinding.PathfinderOnStates import PathFinderOnStates, PathFinderState
from tree.DecisionTree import DecisionTree
from util.PathByEnum import PathByEnum
from util.PathDefinitions import GridLocation, GridWithWeights
@ -179,8 +181,18 @@ class GameModel(Model):
self.recognised_items.append(recognised)
if self.phase == Phase.CLIENT_SORTING:
# TODO GENERICS SORTING
sorted(self.orderList, key=lambda x: len(x.items))
orders: [Order] = self.orderList
tree: DecisionTree = DecisionTree()
readyTree = tree.get_decision_tree()
# TODO CLIENT RECOGNITION
# GENERICS SORTING
genericOrder: GeneticOrder = GeneticOrder(orders)
self.orderList = genericOrder.get_orders_sorted(orders)
print("FINISHED CLIENT ORDER SORTING")
self.phase = Phase.EXECUTION

View File

@ -15,10 +15,12 @@ class GeneticOrder:
best_fit_super_special = 20
population_size = 200
number_of_populations = 1000
number_of_populations = 10000
punish_low = 5
punish_med = 3
punish_low = 500
punish_med = 300
punish_sum = 50
def __init__(self, orders: [Order]) -> None:
self.orders = orders
@ -35,10 +37,10 @@ class GeneticOrder:
return GeneticMutationType.CROSS
def mutation(self, population: [int]) -> [int]:
x = random.randint(0, len(population)-1)
y = random.randint(0, len(population)-1)
x = random.randint(0, len(population) - 1)
y = random.randint(0, len(population) - 1)
while x == y:
y = random.randint(0, len(population)-1)
y = random.randint(0, len(population) - 1)
result = population
@ -46,13 +48,13 @@ class GeneticOrder:
result[x] = population[y]
result[y] = pom
if(result[x] == result[y]):
if (result[x] == result[y]):
print("PIZDA I CHUJ")
return result
def cross(self, population: [int]) -> [int]:
x = random.randint(1, len(population)-1)
x = random.randint(1, len(population) - 1)
result = []
@ -63,7 +65,7 @@ class GeneticOrder:
def reverse(self, population: [int]) -> [int]:
x = random.randint(0, len(population))
y = random.randint(0, len(population)-1)
y = random.randint(0, len(population) - 1)
while y - x > 2 or x >= y:
x = random.randint(0, len(population))
y = random.randint(0, len(population) - 1)
@ -97,18 +99,30 @@ class GeneticOrder:
#
# return [list(x) for x in result]
def correct_sum(self, last_prio: Priority, last_sum: float, o: Order) -> bool:
if o.priority == last_prio:
return last_sum > o.sum / o.time
return True
def sum_wrong(self, member: [int]) -> int:
last_high = 0
last_med = 0
last_prio = Priority.HIGH
last_sum = 0
counter = 0
for i in range(len(member)):
o: Order = self.orders[member[i]]
if o.priority == Priority.HIGH :
if o.priority == Priority.HIGH:
last_high = i
elif o.priority == Priority.MEDIUM:
last_med = i
if not self.correct_sum(last_prio, last_sum, o):
counter += int(last_sum - (o.sum / o.time))
last_prio = o.priority
last_sum = o.sum / o.time
for i in range(last_high):
o: Order = self.orders[member[i]]
if o.priority == Priority.MEDIUM:
@ -123,7 +137,6 @@ class GeneticOrder:
return counter
def evaluate(self, member: [int]) -> int:
# result = 0
# for i in range(len(self.orders) - 1):
@ -196,11 +209,10 @@ class GeneticOrder:
if self.evaluate(best_fit) < self.evaluate(population[0]):
population[0] = best_fit
best: [int] = population[0]
result: [Order] = []
for i in range(len(best)):
result.append(self.orders[best[i]])
return result
return result

16
main.py
View File

@ -106,8 +106,8 @@ if __name__ == '__main__':
return order.id
punish_low = 5
punish_med = 3
punish_low = 500
punish_med = 300
def sum_wrong(member: [Order]) -> int:
last_high = 0
last_med = 0
@ -152,17 +152,17 @@ if __name__ == '__main__':
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)
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)
for i in newOrders:
print("id:", i.id, "priority:", i.priority, "sum/time:", i.sum/i.time)
# print("id:", i.id, "priority:", i.priority)