Advertisement
IcaroPeretti

IA-11500

May 19th, 2022
706
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 12.46 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 / num_h)  # 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 / num_h)  # 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 / num_h)  # Comp dianteiro: vol máx 6800
  80.  
  81.     h5 = np.maximum(0, float((0.480 * carga1_C + 0.650 * carga2_C +
  82.                     0.580 * carga3_C + 0.390 * carga4_C) - 8700)) / float(8700 / num_h)  # Comp central: vol máx 8700
  83.  
  84.     h6 = np.maximum(0, float((0.48 * carga1_T + 0.65 * carga2_T +
  85.                     0.58 * carga3_T + 0.39 * carga4_T) - 5300)) / float(5300 / num_h)  # Comp traseiro: vol máx 5300
  86.  
  87.     peso_max = 34000
  88.  
  89.     # Proporção de cargas
  90.     prop_dianteira = float(10000 / peso_max)  # Máximo 10t
  91.     prop_central = float(16000 / peso_max)  # Máximo 16t
  92.     prop_traseira = float(8000 / peso_max)  # Máximo 8t
  93.  
  94.     carga_dianteira = float(carga1_D + carga2_D + carga3_D + carga4_D)
  95.     carga_central = float(carga1_C + carga2_C + carga3_C + carga4_C)
  96.     carga_traseira = float(carga1_T + carga2_T + carga3_T + carga4_T)
  97.     soma_total_cargas = float(carga_dianteira + carga_central + carga_traseira)
  98.  
  99.     # Restrição de carga por proporção
  100.     h7 = np.maximum(0, float(((carga_dianteira / soma_total_cargas) - prop_dianteira))
  101.                     ) / float(prop_dianteira / num_h)  # Comp dianteiro
  102.     h8 = np.maximum(0, float(((carga_central / soma_total_cargas) -
  103.                               prop_central))) / float(prop_central / num_h)  # Comp central
  104.  
  105.     h9 = np.maximum(0, float(((carga_traseira / soma_total_cargas) -
  106.                     prop_traseira))) / float(prop_traseira / num_h)  # Comp traseiro
  107.  
  108.     # Restrição por peso máximo da carga
  109.     h10 = np.maximum(0, float((carga1_D + carga1_C + carga1_T) - 18000)
  110.                      ) / float(18000 / 13)  # Peso máximo: 18.000
  111.     h11 = np.maximum(0, float((carga2_D + carga2_C + carga2_T) - 15000)
  112.                      ) / float(15000 / 13)  # Peso máximo: 15.000
  113.     h12 = np.maximum(0, float((carga3_D + carga3_C + carga3_T) - 23000)
  114.                      ) / float(23000 / 13)  # Peso máximo: 23.000
  115.     h13 = np.maximum(0, float((carga4_D + carga4_C + carga4_T) - 12000)
  116.                      ) / float(12000 / 13)  # Peso máximo: 12.000
  117.  
  118.     fit = fit - (h1 + h2 + h3 + h4 + h5 + h6 + h7 +
  119.                  h8 + h9 + h10 + h11 + h12 + h13)
  120.  
  121.     return fit
  122.  
  123.  
  124. 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):
  125.     # Arredondando
  126.     carga1_D = np.round(carga1_D)  # Dianteiro
  127.     carga1_C = np.round(carga1_C)  # Central
  128.     carga1_T = np.round(carga1_T)  # Traseiro
  129.  
  130.     carga2_D = np.round(carga2_D)  # Dianteiro
  131.     carga2_C = np.round(carga2_C)  # Central
  132.     carga2_T = np.round(carga2_T)  # Traseiro
  133.  
  134.     carga3_D = np.round(carga3_D)  # Dianteiro
  135.     carga3_C = np.round(carga3_C)  # Central
  136.     carga3_T = np.round(carga3_T)  # Traseiro
  137.  
  138.     carga4_D = np.round(carga4_D)  # Dianteiro
  139.     carga4_C = np.round(carga4_C)  # Central
  140.     carga4_T = np.round(carga4_T)  # Traseiro
  141.  
  142.     compartimento_dianteiro = carga1_D + carga2_D + carga3_D + carga4_D
  143.     compartimento_central = carga1_C + carga2_C + carga3_C + carga4_C
  144.     compartimento_traseiro = carga1_T + carga2_T + carga3_T + carga4_T
  145.     total = compartimento_dianteiro + compartimento_central + compartimento_traseiro
  146.  
  147.     print("----- RESUMO -----")
  148.     print("\n --- Peso por compartimento --- \n")
  149.     print("Carga 1 Dianteiro:", carga1_D)
  150.     print("Carga 1 Central:", carga1_C)
  151.     print("Carga 1 Traseiro:", carga1_T)
  152.     total_carga1 = float(carga1_D + carga1_C + carga1_T)
  153.     print("Total carga 1", (total_carga1))
  154.  
  155.     print("\nCarga 2 Dianteiro:", carga2_D)
  156.     print("Carga 2 Central:", carga2_C)
  157.     print("Carga 2 Traseiro:", carga2_T)
  158.     total_carga2 = float(carga2_D + carga2_C + carga2_T)
  159.     print("Tota carga 2:", (total_carga2))
  160.  
  161.     print("\nCarga 3 Dianteiro:", carga3_D)
  162.     print("Carga 3 Central:", carga3_C)
  163.     print("Carga 3 Traseiro:", carga3_T)
  164.     total_carga3 = float(carga3_D + carga3_C + carga3_T)
  165.     print("Total carga 3:", (total_carga3))
  166.  
  167.     print("\nCarga 4 Dianteiro:", carga4_D)
  168.     print("Carga 4 Central:", carga4_C)
  169.     print("Carga 4 Traseiro:", carga4_T)
  170.     total_carga4 = float(carga4_D + carga4_C + carga4_T)
  171.     print("Total carga 4:", (total_carga4))
  172.  
  173.     cargas_total = float(total_carga1 + total_carga2 +
  174.                          total_carga3 + total_carga4)
  175.  
  176.     print("\nPeso total do carregamento: ", cargas_total)
  177.     print("\nCarga dianteira total:", carga1_D + carga2_D + carga3_D + carga4_D)
  178.     print("Carga central total:", carga1_C + carga2_C + carga3_C + carga4_C)
  179.     print("Carga traseira total:", carga1_T + carga2_T + carga3_T + carga4_T)
  180.  
  181.     # Peso da carga * volume
  182.     volume_dianteiro = float((carga1_D * 0.480)+(carga2_D * 0.650) +
  183.                              (carga3_D * 0.580) + (carga4_D * 0.390))
  184.     volume_central = float(
  185.         (carga1_C * 0.480) + (carga2_C * 0.650) + (carga3_C * 0.580) + (carga4_C * 0.390))
  186.     volume_traseiro = float(
  187.         (carga1_T * 0.480)+(carga2_T * 0.650) + (carga3_T * 0.580) + (carga4_T * 0.390))
  188.  
  189.     print("\n --- Volume por compartimento --- ")
  190.     print("Total de volume dianteiro : ", volume_dianteiro)
  191.     print("Total de volume central : ", volume_central)
  192.     print("Total de volume traseiro : ", volume_traseiro)
  193.  
  194.     print("\n --- Proporção --- ")
  195.     print("Proporção de Carga Dianteira: ", round(
  196.         ((carga1_D + carga2_D + carga3_D + carga4_D) / cargas_total),3))
  197.     print("Proporção de Carga Central: ", round(
  198.         ((carga1_C + carga2_C + carga3_C + carga4_C) / cargas_total)))
  199.     print("Proporção de Carga Traseira: ", round(
  200.         ((carga1_T + carga2_T + carga3_T + carga4_T) / cargas_total)))
  201.  
  202.     lucro_c1 = float(0.310 * carga1_D + 0.310 * carga1_C + 0.310 * carga1_T)
  203.     lucro_c2 = float(0.380 * carga2_D + 0.380 * carga2_C + 0.380 * carga2_T)
  204.     lucro_c3 = float(0.350 * carga3_D + 0.350 * carga3_C + 0.350 * carga3_T)
  205.     lucro_c4 = float(0.285 * carga4_D + 0.285 * carga4_C + 0.285 * carga4_T)
  206.  
  207.     print("\n --- Lucro por carga --- \n")
  208.     print("Lucro carga 1:", lucro_c1)
  209.     print("Lucro carga 2:", lucro_c2)
  210.     print("Lucro carga 3:", lucro_c3)
  211.     print("Lucro carga 4:", lucro_c4)
  212.     print("Lucro Total :", lucro_c1 + lucro_c2 + lucro_c3 + lucro_c4)
  213.  
  214.     if (((compartimento_dianteiro/total) >= 0.3) or ((compartimento_dianteiro/total) <= 0.29)):
  215.         print(
  216.             f"proporção dianteira excedida: {np.round((compartimento_dianteiro/total),4)}")
  217.     if (((compartimento_central/total) >= 0.48) or ((compartimento_central/total) <= 0.47)):
  218.         print(
  219.             f"proporção central excedida: {np.round((compartimento_central/total),4)}")
  220.     if (((compartimento_traseiro/total) >= 0.24) or ((compartimento_traseiro/total) <= 0.23)):
  221.         print(
  222.             f"proporção traseiro excedida: {np.round((compartimento_traseiro/total),4)}")
  223.  
  224.  
  225. def main():
  226.     rand = Random()
  227.     rand.seed(int(time()))  # inicar semente aleatoria
  228.  
  229.     ea = ec.GA(rand)
  230.     ea.selector = ec.selectors.tournament_selection  # metodo de seleção: torneio
  231.     ea.variator = [ec.variators.uniform_crossover,  # op genético crossover uniforme
  232.                    ec.variators.gaussian_mutation]  # op genético mutação gaussian_mutation
  233.  
  234.     # determina os sobreviventes da prox geração
  235.     ea.replacer = ec.replacers.steady_state_replacement
  236.  
  237.     # critério de parada: atingir o máx de geraçoes
  238.     ea.terminator = terminators.generation_termination
  239.  
  240.     # geração de estatísticas da evolução
  241.     ea.observer = [ec.observers.stats_observer, ec.observers.file_observer]
  242.  
  243.     final_pop = ea.evolve(generator=generate_,  # Função que gera a população
  244.                           evaluator=evaluate_,  # Função que avalia as soluções
  245.                           pop_size=10000,  # Tamanho da população
  246.                           maximize=True,  # False:minimização
  247.                           bounder=ec.Bounder(0, 16000),
  248.                           max_generations=2000,  # Qtd de gerações
  249.                           num_inputs=12,  # Qtd de genes (cromossomos)
  250.                           crossover_rate=1.0,  # Taxa de cruzamento
  251.                           num_crossover_points=1,   # Número de cortes do cruzamento
  252.                           mutation_rate=0.60,  # Taxa de mutação
  253.                           num_elites=1,  # numero de individuos elites a serem
  254.                           # selecionados para a póxima população
  255.                           num_selected=12,  # Numero de individuos
  256.                           tournament_size=12,  # Torneio aletorio
  257.                           #statistcs_fize=open("plane.csv", "w"),
  258.                           #individuals_file=open("plane-individuals.csv", "w")
  259.                           )
  260.  
  261.    
  262.  
  263.     # Melhor população por primeiro 0 = melhor individuo
  264.     final_pop.sort(reverse=True)  # população final
  265.     print("\n --- Melhor Individuo --- \n")
  266.     print(final_pop[0])
  267.  
  268.     # Fitness final
  269.     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],
  270.                     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])
  271.     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],
  272.                         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])
  273.  
  274.  
  275. main()
  276.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement