175 lines
5.3 KiB
Python
175 lines
5.3 KiB
Python
import itertools
|
|
import random
|
|
|
|
from data.Order import Order
|
|
from data.enum.GeneticMutationType import GeneticMutationType
|
|
from data.enum.Priority import Priority
|
|
|
|
|
|
class GeneticOrder:
|
|
mutation_chance = 50
|
|
reverse_chance = 20
|
|
cross_chance = 10
|
|
best_fit_special = 40
|
|
best_fit_special_2 = 20
|
|
population_size = 500
|
|
|
|
def __init__(self, orders: [Order]) -> None:
|
|
self.number_of_populations = 10000
|
|
self.orders = orders
|
|
|
|
def get_mutation_type(self) -> GeneticMutationType:
|
|
x = random.randint(0, self.mutation_chance + self.cross_chance + self.reverse_chance)
|
|
|
|
if (x < self.mutation_chance):
|
|
return GeneticMutationType.MUTATION
|
|
|
|
if (x > self.mutation_chance + self.cross_chance):
|
|
return GeneticMutationType.REVERSE
|
|
|
|
return GeneticMutationType.CROSS
|
|
|
|
def mutation(self, population: [int]) -> [int]:
|
|
x = random.randint(0, len(population)-1)
|
|
y = random.randint(0, len(population)-1)
|
|
while x == y:
|
|
y = random.randint(0, len(population)-1)
|
|
|
|
result = population
|
|
|
|
pom = population[x]
|
|
result[x] = population[y]
|
|
result[y] = pom
|
|
|
|
if(result[x] == result[y]):
|
|
print("PIZDA I CHUJ")
|
|
|
|
return result
|
|
|
|
def cross(self, population: [int]) -> [int]:
|
|
x = random.randint(1, len(population)-1)
|
|
|
|
result = []
|
|
|
|
for i in range(len(population)):
|
|
result.append(population[(i + x) % len(population)])
|
|
|
|
return result
|
|
|
|
def reverse(self, population: [int]) -> [int]:
|
|
x = random.randint(0, len(population))
|
|
y = random.randint(0, len(population)-1)
|
|
while x >= y:
|
|
x = random.randint(0, len(population))
|
|
y = random.randint(0, len(population)-1)
|
|
|
|
result = []
|
|
# print("X: ", x, " y: ", y)
|
|
|
|
for i in range(len(population)):
|
|
if x <= i <= y:
|
|
new_i = i - x
|
|
# print("len:", len(population), " new_i: ", new_i)
|
|
result.append(population[y - new_i])
|
|
else:
|
|
result.append(population[i])
|
|
|
|
return result
|
|
|
|
def generate_first_population(self, k: int) -> [[int]]:
|
|
result = []
|
|
|
|
s = range(len(self.orders))
|
|
p = itertools.permutations(s)
|
|
while len(result) < k:
|
|
n = p.__next__()
|
|
if n not in result:
|
|
result.append(n)
|
|
|
|
return [list(x) for x in result]
|
|
|
|
# result = itertools.permutations(range(len(self.orders)))
|
|
#
|
|
# return [list(x) for x in result]
|
|
|
|
def evaluate(self, member: [int]) -> int:
|
|
result = 0
|
|
for i in range(len(self.orders) - 1):
|
|
x: Order = self.orders[member[i]]
|
|
y: Order = self.orders[member[i + 1]]
|
|
|
|
if ((x.priority == Priority.MEDIUM or x.priority == Priority.LOW) and y.priority == Priority.HIGH) or (x.priority == Priority.LOW and y.priority == Priority.MEDIUM):
|
|
result += 5000
|
|
elif x.sum / x.time < y.sum / y.time:
|
|
result += y.sum * 5 + y.time
|
|
|
|
return result
|
|
|
|
def mutate_population(self, order_population: [[int]]) -> [[int]]:
|
|
result = []
|
|
|
|
for i in range(len(order_population)):
|
|
member: [int] = order_population[i]
|
|
operation: GeneticMutationType = self.get_mutation_type()
|
|
|
|
if operation == GeneticMutationType.MUTATION:
|
|
member = self.mutation(member)
|
|
elif operation == GeneticMutationType.REVERSE:
|
|
member = self.reverse(member)
|
|
else:
|
|
member = self.cross(member)
|
|
|
|
result.append(member)
|
|
|
|
return result
|
|
|
|
def get_next_population(self, population: [[int]]) -> [[int]]:
|
|
result = []
|
|
|
|
result = population
|
|
|
|
# for i in range(len(population) - self.best_fit_special):
|
|
# result.append(population[i])
|
|
#
|
|
# k = len(population) - self.best_fit_special
|
|
# while k < len(population) - self.best_fit_special_2:
|
|
# n = random.randint(0, self.best_fit_special)
|
|
# result.append(population[n])
|
|
#
|
|
# left_size = len(population) - k
|
|
# while left_size < len(population):
|
|
# n = random.randint(0, self.best_fit_special_2)
|
|
# result.append(population[n])
|
|
|
|
return result
|
|
|
|
def get_orders_sorted(self, orders: [Order]) -> [Order]:
|
|
self.orders = orders
|
|
|
|
population: [[int]] = self.generate_first_population(self.population_size)
|
|
print(population)
|
|
|
|
population.sort(key=self.evaluate)
|
|
best_fit: [int] = population[0]
|
|
|
|
for i in range(self.number_of_populations):
|
|
# print("population: ", i)
|
|
population = self.mutate_population(population)
|
|
population.sort(key=self.evaluate)
|
|
|
|
if self.evaluate(best_fit) > self.evaluate(population[0]):
|
|
best_fit = population[0]
|
|
|
|
# population = self.get_next_population(population).sort(key=self.evaluate)
|
|
|
|
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 |