Advertisement
IcaroPeretti

plane

May 6th, 2022
957
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.83 KB | None | 0 0
  1. from random import Random
  2. from time import time
  3. from inspyred import ec
  4. from inspyred.ec import terminators
  5. import numpy as np
  6.  
  7. #gerador de população
  8. def generate_(random, args):
  9.     size = args.get('num_inputs', 12)
  10.     return [random.randint(0, 16000) for i in range(size)]
  11.  
  12. #função para avaliar soluções
  13. def evaluate_(candidates, args):
  14.     fitness = []
  15.     for cs in candidates:
  16.         fit = perform_fitness(cs[0], cs[1], cs[2], cs[3], cs[4], cs[5], cs[6], cs[7], cs[8], cs[9], cs[10], cs[11])
  17.         fitness.append(fit)
  18.     return fitness
  19.  
  20. #função para calcular o fitness
  21. def perform_fitness(cs1, cs2, cs3, cs4, cs5, cs6, cs7, cs8, cs9, cs10, cs11, cs12):
  22.     cd1 = np.round(cs1)
  23.     cc1 = np.round(cs2)
  24.     ct1 = np.round(cs3)
  25.     cd2 = np.round(cs4)
  26.     cc2 = np.round(cs5)
  27.     ct2 = np.round(cs6)
  28.     cd3 = np.round(cs7)
  29.     cc3 = np.round(cs8)
  30.     ct3 = np.round(cs9)
  31.     cd4 = np.round(cs10)
  32.     cc4 = np.round(cs11)
  33.     ct4 = np.round(cs12)
  34.     fit = float(((cd1 * 0.31) + (cc1 * 0.31) + (ct1 * 0.31) + (cd2 * 0.38) + (cc2 * 0.38) + (ct2 * 0.38) + (cd3 * 0.35) + (cc3 * 0.35) + (ct3 * 0.35) + (cd4 * 0.285) + (cc4 * 0.285) + (ct4 * 0.285)) / 42565.92)
  35.  
  36.     #restrições
  37.     cargaTotal = cd1 + cd2 + cd3 + cd4 + cc1 + cc2 + cc3 + cc4 + ct1 + ct2 + ct3 + ct4
  38.     num = 14
  39.  
  40.     #máximo de 10000kg na dianteira
  41.     h1 = np.maximum(0, float((cd1 + cd2 + cd3 + cd4) - 10000)) / (10000 / num)
  42.  
  43.     #máximo de 16000kg na central
  44.     h2 = np.maximum(0, float((cc1 + cc2 + cc3 + cc4) - 16000)) / (16000 / num)
  45.  
  46.     #máximo de 8000kg na traseira
  47.     h3 = np.maximum(0, float((ct1 + ct2 + ct3 + ct4) - 8000)) / ( 8000 / num)
  48.  
  49.     #máximo de 18000kg da carga 1
  50.     h4 = np.maximum(0, float((cd1 + cc1 + ct1) - 18000))/ (18000 / num)
  51.  
  52.     #máximo de 15000kg da carga 2
  53.     h5 = np.maximum(0, float((cd2 + cc2 + ct2) - 15000)) / (15000 / num)
  54.  
  55.     #máximo de 23000kg da carga 3
  56.     h6 = np.maximum(0, float((cd3 + cc3 + ct3) - 23000)) / (23000 / num)
  57.  
  58.     #máximo de 12000kg da carga 4
  59.     h7 = np.maximum(0, float((cd4 + cc4 + ct4) - 12000))/ (12000 / num)
  60.  
  61.     #máximo de 6800m³ na dianteira
  62.     h8 = np.maximum(0, float(((cd1 * 0.48) + (cd2 * 0.65) + (cd3 * 0.58) + (cd4 * 0.39)) - 6800)) / (6800 / num )
  63.  
  64.     #máximo de 8700m³ na central
  65.     h9 = np.maximum(0, float(((cc1 * 0.48) + (cc2 * 0.65) + (cc3 * 0.58) + (cc4 * 0.39)) - 8700)) / (8700 / num)
  66.  
  67.     #máximo de 5300m³ na traseira
  68.     h10 = np.maximum(0, float(((ct1 * 0.48) + (ct2 * 0.65) + (ct3 * 0.58) + (ct4 * 0.39)) - 5300)) / (5300 / num)
  69.  
  70.     #proporção da carga dianteira
  71.     h11 = np.maximum(0, float((((cd1 + cd2 + cd3 + cd4) / cargaTotal) - 0.29411764706))) / (0.29411764706 / num)
  72.  
  73.     #proporção da carga central
  74.     h12 = np.maximum(0, float((((cc1 + cc2 + cc3 + cc4) / cargaTotal) - 0.47058823529))) / (0.47058823529 / num)
  75.    
  76.     #proporção da carga traseira
  77.     h13 = np.maximum(0, float((((ct1 + ct2 + ct3 + ct4) / cargaTotal) - 0.23529411765))) / (0.23529411765 / num)
  78.  
  79.     #peso total no avião
  80.     h14 = np.maximum(0, float(cargaTotal - 34000)) / (34000 / num)
  81.  
  82.     fit = fit - (h1 + h2 + h3 + h4 + h5 + h6 + h7 + h8 + h9 + h10 + h11 + h12 + h13 + h14)
  83.     return fit
  84.  
  85.  
  86. def solution_evaluation(CD1, CC1, CT1, CD2, CC2, CT2, CD3, CC3, CT3, CD4, CC4, CT4):
  87.     CD1 = np.round(CD1)
  88.     CD2 = np.round(CD2)
  89.     CD3 = np.round(CD3)
  90.     CD4 = np.round(CD4)
  91.     CT1 = np.round(CT1)
  92.     CT2 = np.round(CT2)
  93.     CT3 = np.round(CT3)
  94.     CT4 = np.round(CT4)
  95.     CC1 = np.round(CC1)
  96.     CC2 = np.round(CC2)
  97.     CC3 = np.round(CC3)
  98.     CC4 = np.round(CC4)
  99.  
  100.     volumeDianteiro = float((CD1*0.48)+(CD2*0.65)+(CD3*0.58)+(CD4*0.39))
  101.     volumeCentral = float((CC1*0.48)+(CC2*0.65)+(CC3*0.58)+(CC4*0.39))
  102.     volumeTraseiro = float((CT1*0.48)+(CT2*0.65)+(CT3*0.58)+(CT4*0.39))
  103.     pesoDianteiro = float(CD1 + CD2 + CD3 + CD4)
  104.     pesoCentral = float(CC1 + CC2 + CC3 + CC4)
  105.     pesoTraseiro = float(CT1 + CT2 + CT3 + CT4)
  106.     pesoTotal = pesoDianteiro + pesoCentral + pesoTraseiro
  107.  
  108.     lucro1 = float((CD1*0.31)+(CC1*0.31)+(CT1*0.31))
  109.     lucro2 = float((CD2*0.38)+(CC2*0.38)+(CT2*0.38))
  110.     lucro3 = float((CD3*0.35)+(CC3*0.35)+(CT3*0.35))
  111.     lucro4 = float((CD4*0.285)+(CC4*0.285)+(CT4*0.285))
  112.  
  113.     print("")
  114.     print("RESUMO DA EXECUÇÃO")
  115.     print("")
  116.     print("Lucro total: ", (lucro1 + lucro2 + lucro3 + lucro4))
  117.     print("Compartimento dianteiro: Peso = ", pesoDianteiro, "kg - ", (pesoDianteiro/pesoTotal)*100, '%  |  Volume = ', volumeDianteiro)
  118.     print("Compartimento central: Peso = ", pesoCentral, "kg - ", (pesoCentral/pesoTotal)*100, '%  |  Volume = ', volumeCentral)
  119.     print("Compartimento traseiro: Peso = ", pesoTraseiro, "kg - ", (pesoTraseiro/pesoTotal)*100, '%  |  Volume = ', volumeTraseiro)
  120.     print("Total de peso: ", pesoTotal, "kg | Total volumétrico: ", (volumeDianteiro + volumeCentral + volumeTraseiro))
  121.     print("")
  122.     print("Carga 1 no compartimento dianteiro: ", CD1)
  123.     print("Carga 1 no compartimento central: ", CC1)
  124.     print("Carga 1 no compartimento traseiro: ", CT1)
  125.     print("Total da carga 1: ", CD1 + CC1 + CT1)
  126.     print("Lucro carga 1: ", lucro1)
  127.     print("")
  128.     print("Carga 2 no compartimento dianteiro: ", CD2)
  129.     print("Carga 2 no compartimento central: ", CC2)
  130.     print("Carga 2 no compartimento traseiro: ", CT2)
  131.     print("Total da carga 2: ", CD2 + CC2 + CT2)
  132.     print("Lucro carga 2: ", lucro2)
  133.     print("")
  134.     print("Carga 3 no compartimento dianteiro: ", CD3)
  135.     print("Carga 3 no compartimento central: ", CC3)
  136.     print("Carga 3 no compartimento traseiro: ", CT3)
  137.     print("Total da carga 3: ", CD3 + CC3 + CT3)
  138.     print("Lucro carga 3: ", lucro3)
  139.     print("")
  140.     print("Carga 4 no compartimento dianteiro: ", CD4)
  141.     print("Carga 4 no compartimento central: ", CC4)
  142.     print("Carga 4 no compartimento traseiro: ", CT4)
  143.     print("Total da carga 4: ", CD4 + CC4 + CT4)
  144.     print("Lucro carga 4: ", lucro4)
  145.  
  146. def main():
  147.     rand = Random()
  148.     rand.seed(int(time()))
  149.  
  150.     ea = ec.GA(rand)
  151.     ea.selector = ec.selectors.tournament_selection
  152.     ea.variator = [ec.variators.uniform_crossover,
  153.                    ec.variators.gaussian_mutation]
  154.  
  155.     ea.replacer = ec.replacers.steady_state_replacement
  156.  
  157.     ea.terminator = terminators.generation_termination
  158.  
  159.     ea.observer = [ec.observers.stats_observer, ec.observers.file_observer]
  160.  
  161.     final_pop = ea.evolve(generator=generate_,
  162.                           evaluator=evaluate_,
  163.                           pop_size=10000,
  164.                           maximize=True,
  165.                           bounder=ec.Bounder(0, 16000),
  166.                           max_generations=2000,
  167.                           num_inputs= 12,
  168.                           crossover_rate = 1.0,
  169.                           num_crossover_points = 1,
  170.                           mutation_rate = 0.6,
  171.                           num_elites = 1,
  172.                           num_selected = 12,
  173.                           tournament_size = 12,
  174.                           statistics_file=open("plane_stats.csv", "w"),
  175.                           individuals_file=open("plane_individuais.csv", "w"))
  176.  
  177.     final_pop.sort(reverse=True)
  178.     print(final_pop[0])
  179.  
  180.     perform_fitness(final_pop[0].candidate[0], final_pop[0].candidate[1], final_pop[0].candidate[2], final_pop[0].candidate[3], final_pop[0].candidate[4], final_pop[0].candidate[5], final_pop[0].candidate[6], final_pop[0].candidate[7], final_pop[0].candidate[8], final_pop[0].candidate[9], final_pop[0].candidate[10], final_pop[0].candidate[11])
  181.     solution_evaluation(final_pop[0].candidate[0], final_pop[0].candidate[1], final_pop[0].candidate[2], final_pop[0].candidate[3], final_pop[0].candidate[4], final_pop[0].candidate[5], final_pop[0].candidate[6], final_pop[0].candidate[7], final_pop[0].candidate[8], final_pop[0].candidate[9], final_pop[0].candidate[10], final_pop[0].candidate[11])
  182.  
  183. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement