Advertisement
IcaroPeretti

TesteIAAG

May 19th, 2022
699
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 12.29 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. # Gerar populações
  8.  
  9.  
  10. def generate_(random, args):
  11.     size = args.get("num_inputs", 12)
  12.     # Máximo 16.000 central
  13.     return [random.randint(0, 16000) for i in range(size)]
  14.  
  15. # função para avaliar soluções
  16.  
  17.  
  18. def evaluate_(candidates, args):
  19.     fitness = []
  20.     for cs in candidates:
  21.         fit = perform_fitness(cs[0], cs[1], cs[2], cs[3], cs[4],
  22.                               cs[5], cs[6], cs[7], cs[8], cs[9], cs[10], cs[11])
  23.         fitness.append(fit)
  24.     return fitness
  25.  
  26.  
  27. def perform_fitness(carga1_D, carga1_C, carga1_T, carga2_D, carga2_C, carga2_T, carga3_D, carga3_C, carga3_T, carga4_D, carga4_C, carga4_T):
  28.     # Arredondando
  29.     carga1_D = np.round(carga1_D)  # Dianteiro
  30.     carga1_C = np.round(carga1_C)  # Central
  31.     carga1_T = np.round(carga1_T)  # Traseiro
  32.  
  33.     carga2_D = np.round(carga2_D)  # Dianteiro
  34.     carga2_C = np.round(carga2_C)  # Central
  35.     carga2_T = np.round(carga2_T)  # Traseiro
  36.  
  37.     carga3_D = np.round(carga3_D)  # Dianteiro
  38.     carga3_C = np.round(carga3_C)  # Central
  39.     carga3_T = np.round(carga3_T)  # Traseiro
  40.  
  41.     carga4_D = np.round(carga4_D)  # Dianteiro
  42.     carga4_C = np.round(carga4_C)  # Central
  43.     carga4_T = np.round(carga4_T)  # Traseiro
  44.  
  45.     # C  | t  | m3/t | Lucro
  46.     # C1 | 18 | 480  | 310  
  47.     # C2 | 15 | 650  | 380  
  48.     # C3 | 23 | 580  | 350  
  49.     # C4 | 12 | 390  | 285  
  50.  
  51.     # Peso x Lucro
  52.     # 18*310 + 15*380 + 23*350 + 12 * 285 = 22750
  53.     # 22750: Estimativa de valor superior
  54.     lucro_max = 22750
  55.     num_h = 13
  56.  
  57.     fit = float(((0.310 * carga1_D + 0.310 * carga1_C + 0.310 * carga1_T) +
  58.                 (0.380 * carga2_D + 0.380 * carga2_C + 0.380 * carga2_T) +
  59.                 (0.350 * carga3_D + 0.350 * carga3_C + 0.350 * carga3_T) +
  60.                 (0.285 * carga4_D + 0.285 * carga4_C + 0.285 * carga4_T)) / lucro_max)
  61.  
  62.     # Compartimento | Capacidade (t) | Capacidade vol
  63.     #   Dianteiro   |     10         |    6800
  64.     #   Central     |     16         |    8700
  65.     #   Traseiro    |      8         |    5300
  66.  
  67.     # Restrição de carga por setor
  68.     h1 = np.maximum(0, float((carga1_D + carga2_D + carga3_D +
  69.                     carga4_D) - 10000)) / float(10000 / num_h)  # Comp dianteiro: peso máx 10.000
  70.  
  71.     h2 = h2 = np.maximum(0, float(
  72.         (carga1_C + carga2_C + carga3_C + carga4_C) - 16000)) / float(16000 / 13)  # Comp central: peso máx 16.000
  73.  
  74.     h3 = np.maximum(0, float((carga1_T + carga2_T + carga3_T + carga4_T) - 8000)
  75.                     ) / float(8000 / 13)  # Comp traseiro: peso máx 8.000
  76.  
  77.     # Restrição volume da carga
  78.     h4 = np.maximum(0, float((0.480 * carga1_D + 0.650 * carga2_D +
  79.                     0.580 * carga3_D + 0.390 * carga4_D) - 6800)) / float(6800 / 13)  # Comp dianteiro: vol máx 6800
  80.     h5 = np.maximum(0, float((0.480 * carga1_C + 0.650 * carga2_C +
  81.                     0.580 * carga3_C + 0.390 * carga4_C) - 8700)) / float(8700 / 13)  # Comp central: vol máx 8700
  82.     h6 = np.maximum(0, float((0.48 * carga1_T + 0.65 * carga2_T +
  83.                     0.58 * carga3_T + 0.39 * carga4_T) - 5300)) / float(5300 / 13)  # Comp traseiro: vol máx 5300
  84.  
  85.     peso_max = 34000
  86.  
  87.     # Proporção de cargas
  88.     prop_dianteira = float(10000 / peso_max)  # Máximo 10t
  89.     prop_central = float(16000 / peso_max)  # Máximo 16t
  90.     prop_traseira = float(8000 / peso_max)  # Máximo 8t
  91.  
  92.     carga_dianteira = float(carga1_D + carga2_D + carga3_D + carga4_D)
  93.     carga_central = float(carga1_C + carga2_C + carga3_C + carga4_C)
  94.     carga_traseira = float(carga1_T + carga2_T + carga3_T + carga4_T)
  95.     soma_total_cargas = float(carga_dianteira + carga_central + carga_traseira)
  96.  
  97.     # Restrição de carga por proporção
  98.     h7 = np.maximum(0, float(((carga_dianteira / soma_total_cargas) - prop_dianteira))
  99.                     ) / float(prop_dianteira / 13)  # Comp dianteiro
  100.     h8 = np.maximum(0, float(((carga_central / soma_total_cargas) -
  101.                               prop_central))) / float(prop_central / 13)  # Comp central
  102.  
  103.     h9 = np.maximum(0, float(((carga_traseira / soma_total_cargas) -
  104.                     prop_traseira))) / float(prop_traseira / 13)  # Comp traseiro
  105.  
  106.     # Restrição por peso máximo da carga
  107.     h10 = np.maximum(0, float((carga1_D + carga1_C + carga1_T) - 18000)
  108.                      ) / float(18000 / 13)  # Peso máximo: 18.000
  109.     h11 = np.maximum(0, float((carga2_D + carga2_C + carga2_T) - 15000)
  110.                      ) / float(15000 / 13)  # Peso máximo: 15.000
  111.     h12 = np.maximum(0, float((carga3_D + carga3_C + carga3_T) - 23000)
  112.                      ) / float(23000 / 13)  # Peso máximo: 23.000
  113.     h13 = np.maximum(0, float((carga4_D + carga4_C + carga4_T) - 12000)
  114.                      ) / float(12000 / 13)  # Peso máximo: 12.000
  115.  
  116.     fit = fit - (h1 + h2 + h3 + h4 + h5 + h6 + h7 +
  117.                  h8 + h9 + h10 + h11 + h12 + h13)
  118.  
  119.     return fit
  120.  
  121.  
  122. def solution_evaluation(carga1_D, carga1_C, carga1_T, carga2_D, carga2_C, carga2_T, carga3_D, carga3_C, carga3_T, carga4_D, carga4_C, carga4_T):
  123.     # Arredondando
  124.     carga1_D = np.round(carga1_D)  # Dianteiro
  125.     carga1_C = np.round(carga1_C)  # Central
  126.     carga1_T = np.round(carga1_T)  # Traseiro
  127.  
  128.     carga2_D = np.round(carga2_D)  # Dianteiro
  129.     carga2_C = np.round(carga2_C)  # Central
  130.     carga2_T = np.round(carga2_T)  # Traseiro
  131.  
  132.     carga3_D = np.round(carga3_D)  # Dianteiro
  133.     carga3_C = np.round(carga3_C)  # Central
  134.     carga3_T = np.round(carga3_T)  # Traseiro
  135.  
  136.     carga4_D = np.round(carga4_D)  # Dianteiro
  137.     carga4_C = np.round(carga4_C)  # Central
  138.     carga4_T = np.round(carga4_T)  # Traseiro
  139.  
  140.     compartimento_dianteiro = carga1_D + carga2_D + carga3_D + carga4_D
  141.     compartimento_central = carga1_C + carga2_C + carga3_C + carga4_C
  142.     compartimento_traseiro = carga1_T + carga2_T + carga3_T + carga4_T
  143.     total = compartimento_dianteiro + compartimento_central + compartimento_traseiro
  144.  
  145.     print("----- RESUMO -----")
  146.     print("\n --- Peso por compartimento --- \n")
  147.     print("Carga 1 Dianteiro:", carga1_D)
  148.     print("Carga 1 Central:", carga1_C)
  149.     print("Carga 1 Traseiro:", carga1_T)
  150.     total_carga1 = float(carga1_D + carga1_C + carga1_T)
  151.     print("Total carga 1", (total_carga1))
  152.  
  153.     print("\nCarga 2 Dianteiro:", carga2_D)
  154.     print("Carga 2 Central:", carga2_C)
  155.     print("Carga 2 Traseiro:", carga2_T)
  156.     total_carga2 = float(carga2_D + carga2_C + carga2_T)
  157.     print("Tota carga 2:", (total_carga2))
  158.  
  159.     print("\nCarga 3 Dianteiro:", carga3_D)
  160.     print("Carga 3 Central:", carga3_C)
  161.     print("Carga 3 Traseiro:", carga3_T)
  162.     total_carga3 = float(carga3_D + carga3_C + carga3_T)
  163.     print("Total carga 3:", (total_carga3))
  164.  
  165.     print("\nCarga 4 Dianteiro:", carga4_D)
  166.     print("Carga 4 Central:", carga4_C)
  167.     print("Carga 4 Traseiro:", carga4_T)
  168.     total_carga4 = float(carga4_D + carga4_C + carga4_T)
  169.     print("Total carga 4:", (total_carga4))
  170.  
  171.     cargas_total = float(total_carga1 + total_carga2 +
  172.                          total_carga3 + total_carga4)
  173.  
  174.     print("\nPeso total do carregamento: ", cargas_total)
  175.     print("\nCarga dianteira total:", carga1_D + carga2_D + carga3_D + carga4_D)
  176.     print("Carga central total:", carga1_C + carga2_C + carga3_C + carga4_C)
  177.     print("Carga traseira total:", carga1_T + carga2_T + carga3_T + carga4_T)
  178.  
  179.     # Peso da carga * volume
  180.     vol_dianteiro = float((carga1_D * 0.480)+(carga2_D * 0.650) +
  181.                           (carga3_D * 0.580) + (carga4_D * 0.390))
  182.     vol_central = float(
  183.         (carga1_C * 0.480) + (carga2_C * 0.650) + (carga3_C * 0.580) + (carga4_C * 0.390))
  184.     vol_traseiro = float(
  185.         (carga1_T * 0.480)+(carga2_T * 0.650) + (carga3_T * 0.580) + (carga4_T * 0.390))
  186.  
  187.     print("\n ---- Volume por compartimento ---- ")
  188.     print("Total de volume dianteiro : ", vol_dianteiro)
  189.     print("Total de volume central : ", vol_central)
  190.     print("Total de volume traseiro : ", vol_traseiro)
  191.  
  192.     print("\n ---- Proporção ---- ")
  193.     print("Proporção de Carga Dianteira: ", round(
  194.         ((carga1_D + carga2_D + carga3_D + carga4_D) / cargas_total), 4))
  195.     print("Proporção de Carga Central: ", round(
  196.         ((carga1_C + carga2_C + carga3_C + carga4_C) / cargas_total), 4))
  197.     print("Proporção de Carga Traseira: ", round(
  198.         ((carga1_T + carga2_T + carga3_T + carga4_T) / cargas_total), 4))
  199.  
  200.     lucro_c1 = float(0.310 * total_carga1)
  201.     lucro_c2 = float(0.380 * total_carga2)
  202.     lucro_c3 = float(0.350 * total_carga3)
  203.     lucro_c4 = float(0.285 * total_carga4)
  204.  
  205.     print("\n ---- Lucro por carga ---- \n")
  206.     print("Lucro carga 1:", lucro_c1)
  207.     print("Lucro carga 2:", lucro_c2)
  208.     print("Lucro carga 3:", lucro_c3)
  209.     print("Lucro carga 4:", lucro_c4)
  210.     print("Lucro Total :", round(lucro_c1 + lucro_c2 + lucro_c3 + lucro_c4), 2)
  211.  
  212.     if (((compartimento_dianteiro/total) >= 0.3) or ((compartimento_dianteiro/total) <= 0.29)):
  213.         print(
  214.             f"proporção dianteira excedida: {np.round((compartimento_dianteiro/total),4)}")
  215.     if (((compartimento_central/total) >= 0.48) or ((compartimento_central/total) <= 0.47)):
  216.         print(
  217.             f"proporção central excedida: {np.round((compartimento_central/total),4)}")
  218.     if (((compartimento_traseiro/total) >= 0.24) or ((compartimento_traseiro/total) <= 0.23)):
  219.         print(
  220.             f"proporção traseiro excedida: {np.round((compartimento_traseiro/total),4)}")
  221.  
  222.  
  223. def main():
  224.     rand = Random()
  225.     rand.seed(int(time()))  # inicar semente aleatoria
  226.  
  227.     ea = ec.GA(rand)
  228.     ea.selector = ec.selectors.tournament_selection  # metodo de seleção: torneio
  229.     ea.variator = [ec.variators.uniform_crossover,  # op genético crossover uniforme
  230.                    ec.variators.gaussian_mutation]  # op genético mutação gaussian_mutation
  231.  
  232.     # determina os sobreviventes da prox geração
  233.     ea.replacer = ec.replacers.steady_state_replacement
  234.  
  235.     # critério de parada: atingir o máx de geraçoes
  236.     ea.terminator = terminators.generation_termination
  237.  
  238.     # geração de estatísticas da evolução
  239.     ea.observer = [ec.observers.stats_observer, ec.observers.file_observer]
  240.  
  241.     final_pop = ea.evolve(generator=generate_,  # Função que gera a população
  242.                           evaluator=evaluate_,  # Função que avalia as soluções
  243.                           pop_size=15000,  # Tamanho da população
  244.                           maximize=True,  # False:minimização
  245.                           bounder=ec.Bounder(0, 16000),
  246.                           max_generations=5000,  # Qtd de gerações
  247.                           num_inputs=12,  # Qtd de genes (cromossomos)
  248.                           crossover_rate=1.0,  # Taxa de cruzamento
  249.                           num_crossover_points=1,   # Número de cortes do cruzamento
  250.                           mutation_rate=0.30,  # Taxa de mutação
  251.                           num_elites=1,  # numero de individuos elites a serem
  252.                           # selecionados para a póxima população
  253.                           num_selected=12,  # Numero de individuos
  254.                           tournament_size=12,  # Torneio aletorio
  255.                           statistcs_fize=open("plane.csv", "w"),
  256.                           individuals_file=open("plane-individuals.csv", "w")
  257.                           )
  258.  
  259.     # Melhor população por primeiro. 0 = melhor individuo
  260.     final_pop.sort(reverse=True)  # população final
  261.     print("\n --- Melhor Individuo --- \n")
  262.     print(final_pop[0])
  263.  
  264.     # Fitness final
  265.     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],
  266.                     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])
  267.     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],
  268.                         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])
  269.  
  270.  
  271. main()
  272.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement