Advertisement
IcaroPeretti

garrafas

Apr 29th, 2022
543
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from random import Random
  2. from time import time
  3. from math import cos
  4. from math import pi
  5. from inspyred import ec
  6. from inspyred.ec import terminators
  7. import numpy as np
  8. import os
  9.  
  10.  
  11. # Generate Population
  12. def generate_(random, args):
  13.     size = args.get('num_inputs', 2)
  14.     return [random.randint(0, 800) for i in range(size)]
  15.  
  16.  
  17. # Evaluate Fitness
  18. def evaluate_(candidates, args):
  19.     fitness = []
  20.     for cs in candidates:
  21.         fit = perform_fitness(cs[0], cs[1])
  22.         fitness.append(fit)
  23.     return fitness
  24.  
  25.  
  26. # Perform Fitness, calculate the fitness of each candidate
  27. def perform_fitness(L, S):
  28.     L = np.round(L)
  29.     S = np.round(S)
  30.  
  31.     fit = float((5*L + 4.5*S) / 7375)
  32.     h1 = np.maximum(0, float(((6*L+5*S)/100)-60)) / 15
  33.     h2 = np.maximum(0, float(((10*L+20*S)-15000))) / 3750
  34.     h3 = np.maximum(0, float(L-800)) / 200
  35.     h4 = np.maximum(0, float(S-750)) / 187.5
  36.  
  37.     fit = fit - (h1 + h2 + h3 + h4)
  38.     return fit
  39.  
  40. # Avaliação final do melhor indivíduo(objetivo)
  41.  
  42.  
  43. def solution_evaluation(L, S):
  44.     #L = cs[0]
  45.     #S = cs[1]
  46.  
  47.     L = np.round(L)
  48.     S = np.round(S)
  49.  
  50.     print
  51.     print("..RESUDO DA PRODUÇÃO DE GARRAFAS..")
  52.     print("Lucro total:", float(5*L+4.5*S))
  53.     print("Tempo de utilização semanal", float(10*L+20*S))
  54.     print("Garrafas de leite:", L)
  55.     print("Garrafas de suco:", S)
  56.  
  57.  
  58. def main():
  59.     rand = Random()
  60.     rand.seed(int(time()))
  61.  
  62.     ea = ec.GA(rand)  # Instancia o algoritmo genético
  63.     ea.selector = ec.selectors.tournament_selection  # Meteodo de seleção por torneio
  64.     ea.variator = [ec.variators.uniform_crossover,
  65.                    ec.variators.gaussian_mutation]  # Metodo cruzamento uniforme
  66.     ea.replacer = ec.replacers.steady_state_replacement  # Metodo de substituição
  67.  
  68.     # Função que determina o critério  de parada
  69.     # Critério de parada por geração
  70.     ea.terminator = terminators.generation_termination
  71.  
  72.     # Função para gerar estatistica da evolução
  73.     ea.observer = [ec.observers.stats_observer,
  74.                    ec.observers.file_observer]  # Estatistica
  75.  
  76.     final_pop = ea.evolve(generator=generate_,
  77.                           evaluator=evaluate_,
  78.                           pop_size=1000,
  79.                           maximize=True,
  80.                           bounder=ec.Bounder(0, 800),
  81.                           max_generations=10000,
  82.                           num_inputs=2,
  83.                           crossover_rate=0.25,
  84.                           mutation_rate=0.25,
  85.                           num_elites=1,
  86.                           num_selected=2,
  87.                           tournament_size=2,
  88.                           statistcs_fize=open("statistics.csv", "w"),
  89.                           individuals_file=open("individuals.csv", "w")
  90.                           )
  91.  
  92.     final_pop.sort(reverse=True)
  93.     #print(final_pop[0])
  94.  
  95.     perform_fitness(final_pop[0].candidate[0], final_pop[1].candidate[1])
  96.     solution_evaluation(final_pop[0].candidate[0], final_pop[1].candidate[1])
  97.  
  98.  
  99. main()
  100.  
Advertisement
RAW Paste Data Copied
Advertisement