genetic -> master #4
16
GameModel.py
16
GameModel.py
@ -18,8 +18,10 @@ from data.Order import Order
|
|||||||
from data.enum.ItemType import ItemType
|
from data.enum.ItemType import ItemType
|
||||||
from decision.Action import Action
|
from decision.Action import Action
|
||||||
from decision.ActionType import ActionType
|
from decision.ActionType import ActionType
|
||||||
|
from genetic_order.GeneticOrder import GeneticOrder
|
||||||
from imageClasification.Classificator import image_classification
|
from imageClasification.Classificator import image_classification
|
||||||
from pathfinding.PathfinderOnStates import PathFinderOnStates, PathFinderState
|
from pathfinding.PathfinderOnStates import PathFinderOnStates, PathFinderState
|
||||||
|
from tree.DecisionTree import DecisionTree
|
||||||
from util.PathByEnum import PathByEnum
|
from util.PathByEnum import PathByEnum
|
||||||
from util.PathDefinitions import GridLocation, GridWithWeights
|
from util.PathDefinitions import GridLocation, GridWithWeights
|
||||||
|
|
||||||
@ -179,8 +181,18 @@ class GameModel(Model):
|
|||||||
self.recognised_items.append(recognised)
|
self.recognised_items.append(recognised)
|
||||||
|
|
||||||
if self.phase == Phase.CLIENT_SORTING:
|
if self.phase == Phase.CLIENT_SORTING:
|
||||||
# TODO GENERICS SORTING
|
orders: [Order] = self.orderList
|
||||||
sorted(self.orderList, key=lambda x: len(x.items))
|
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")
|
print("FINISHED CLIENT ORDER SORTING")
|
||||||
self.phase = Phase.EXECUTION
|
self.phase = Phase.EXECUTION
|
||||||
|
|
||||||
|
@ -15,10 +15,12 @@ class GeneticOrder:
|
|||||||
best_fit_super_special = 20
|
best_fit_super_special = 20
|
||||||
|
|
||||||
population_size = 200
|
population_size = 200
|
||||||
number_of_populations = 1000
|
number_of_populations = 10000
|
||||||
|
|
||||||
punish_low = 5
|
punish_low = 500
|
||||||
punish_med = 3
|
punish_med = 300
|
||||||
|
|
||||||
|
punish_sum = 50
|
||||||
|
|
||||||
def __init__(self, orders: [Order]) -> None:
|
def __init__(self, orders: [Order]) -> None:
|
||||||
self.orders = orders
|
self.orders = orders
|
||||||
@ -35,10 +37,10 @@ class GeneticOrder:
|
|||||||
return GeneticMutationType.CROSS
|
return GeneticMutationType.CROSS
|
||||||
|
|
||||||
def mutation(self, population: [int]) -> [int]:
|
def mutation(self, population: [int]) -> [int]:
|
||||||
x = random.randint(0, len(population)-1)
|
x = random.randint(0, len(population) - 1)
|
||||||
y = random.randint(0, len(population)-1)
|
y = random.randint(0, len(population) - 1)
|
||||||
while x == y:
|
while x == y:
|
||||||
y = random.randint(0, len(population)-1)
|
y = random.randint(0, len(population) - 1)
|
||||||
|
|
||||||
result = population
|
result = population
|
||||||
|
|
||||||
@ -46,13 +48,13 @@ class GeneticOrder:
|
|||||||
result[x] = population[y]
|
result[x] = population[y]
|
||||||
result[y] = pom
|
result[y] = pom
|
||||||
|
|
||||||
if(result[x] == result[y]):
|
if (result[x] == result[y]):
|
||||||
print("PIZDA I CHUJ")
|
print("PIZDA I CHUJ")
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def cross(self, population: [int]) -> [int]:
|
def cross(self, population: [int]) -> [int]:
|
||||||
x = random.randint(1, len(population)-1)
|
x = random.randint(1, len(population) - 1)
|
||||||
|
|
||||||
result = []
|
result = []
|
||||||
|
|
||||||
@ -63,7 +65,7 @@ class GeneticOrder:
|
|||||||
|
|
||||||
def reverse(self, population: [int]) -> [int]:
|
def reverse(self, population: [int]) -> [int]:
|
||||||
x = random.randint(0, len(population))
|
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:
|
while y - x > 2 or x >= y:
|
||||||
x = random.randint(0, len(population))
|
x = random.randint(0, len(population))
|
||||||
y = random.randint(0, len(population) - 1)
|
y = random.randint(0, len(population) - 1)
|
||||||
@ -97,18 +99,30 @@ class GeneticOrder:
|
|||||||
#
|
#
|
||||||
# return [list(x) for x in result]
|
# 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:
|
def sum_wrong(self, member: [int]) -> int:
|
||||||
last_high = 0
|
last_high = 0
|
||||||
last_med = 0
|
last_med = 0
|
||||||
|
last_prio = Priority.HIGH
|
||||||
|
last_sum = 0
|
||||||
counter = 0
|
counter = 0
|
||||||
|
|
||||||
for i in range(len(member)):
|
for i in range(len(member)):
|
||||||
o: Order = self.orders[member[i]]
|
o: Order = self.orders[member[i]]
|
||||||
if o.priority == Priority.HIGH :
|
if o.priority == Priority.HIGH:
|
||||||
last_high = i
|
last_high = i
|
||||||
elif o.priority == Priority.MEDIUM:
|
elif o.priority == Priority.MEDIUM:
|
||||||
last_med = i
|
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):
|
for i in range(last_high):
|
||||||
o: Order = self.orders[member[i]]
|
o: Order = self.orders[member[i]]
|
||||||
if o.priority == Priority.MEDIUM:
|
if o.priority == Priority.MEDIUM:
|
||||||
@ -123,7 +137,6 @@ class GeneticOrder:
|
|||||||
|
|
||||||
return counter
|
return counter
|
||||||
|
|
||||||
|
|
||||||
def evaluate(self, member: [int]) -> int:
|
def evaluate(self, member: [int]) -> int:
|
||||||
# result = 0
|
# result = 0
|
||||||
# for i in range(len(self.orders) - 1):
|
# for i in range(len(self.orders) - 1):
|
||||||
@ -196,11 +209,10 @@ class GeneticOrder:
|
|||||||
if self.evaluate(best_fit) < self.evaluate(population[0]):
|
if self.evaluate(best_fit) < self.evaluate(population[0]):
|
||||||
population[0] = best_fit
|
population[0] = best_fit
|
||||||
|
|
||||||
|
|
||||||
best: [int] = population[0]
|
best: [int] = population[0]
|
||||||
result: [Order] = []
|
result: [Order] = []
|
||||||
|
|
||||||
for i in range(len(best)):
|
for i in range(len(best)):
|
||||||
result.append(self.orders[best[i]])
|
result.append(self.orders[best[i]])
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
16
main.py
16
main.py
@ -106,8 +106,8 @@ if __name__ == '__main__':
|
|||||||
return order.id
|
return order.id
|
||||||
|
|
||||||
|
|
||||||
punish_low = 5
|
punish_low = 500
|
||||||
punish_med = 3
|
punish_med = 300
|
||||||
def sum_wrong(member: [Order]) -> int:
|
def sum_wrong(member: [Order]) -> int:
|
||||||
last_high = 0
|
last_high = 0
|
||||||
last_med = 0
|
last_med = 0
|
||||||
@ -152,17 +152,17 @@ if __name__ == '__main__':
|
|||||||
|
|
||||||
print("SIEMA before: ")
|
print("SIEMA before: ")
|
||||||
sum_wrong(orders)
|
sum_wrong(orders)
|
||||||
# for i in 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, "sum/time:", i.sum/i.time)
|
||||||
# print("id:", i.id, "priority:", i.priority)
|
# print("id:", i.id, "priority:", i.priority)
|
||||||
|
|
||||||
newOrders = test.get_orders_sorted(orders)
|
newOrders = test.get_orders_sorted(orders)
|
||||||
|
|
||||||
print("NAURA after:")
|
print("NAURA after:")
|
||||||
sum_wrong(newOrders)
|
sum_wrong(newOrders)
|
||||||
# for i in 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, "sum/time:", i.sum/i.time)
|
||||||
# print("id:", i.id, "priority:", i.priority)
|
# print("id:", i.id, "priority:", i.priority)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user