Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2021
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.31 KB | None | 0 0
  1. import pygmo as pg
  2. from itertools import combinations
  3.  
  4. def juego(partido,decision,puntos):
  5.     if decision == 0: #gana local
  6.         puntos[str(partido[0])] += resultados['victoria']
  7.     if decision == 1: #gana visitante
  8.         puntos[str(partido[1])] += resultados['victoria']
  9.     if decision == 2:
  10.         puntos[str(partido[0])] += resultados['empate']
  11.         puntos[str(partido[1])] += resultados['empate']
  12.  
  13. def comprobador(puntos,posicion):
  14.     ordenado = sorted(puntos,key=puntos.get)
  15.     clasificacion = list(reversed(ordenado))
  16.     return puntos[clasificacion[posicion-1]]
  17.  
  18. def resetear_puntos():
  19.     puntos = {'a':0,'b':0,'c':0,'d':0,'e':0,'f':0,'g':0,'h':0,'i':0,'j':0,'k':0,'l':0,'m':0,'n':0,'o':0,'p':0,'q':0,'r':0,'s':0,'t':0}
  20.     return puntos
  21.  
  22. puntos = {'a':0,'b':0,'c':0,'d':0,'e':0,'f':0,'g':0,'h':0,'i':0,'j':0,'k':0,'l':0,'m':0,'n':0,'o':0,'p':0,'q':0,'r':0,'s':0,'t':0}
  23.  
  24. resultados = {'victoria':3, 'empate':1}
  25.  
  26. partidos_ida = list(combinations(puntos,2))
  27. partidos_vuelta = []
  28. for partido in partidos_ida:
  29.     partidos_vuelta.append(list(reversed(partido)))
  30.  
  31. total_partidos = partidos_ida+partidos_vuelta
  32.  
  33. def objetivo(partidos_jugar,x):
  34.     puntos = resetear_puntos()
  35.     for i in range(len(partidos_jugar)):
  36.         juego(partidos_jugar[i],x[i],puntos)
  37.     puesto = comprobador(puntos,18)
  38.     return puesto
  39.  
  40. #x is a N-dimensional array (N being number of games) with 0,1,2 as possible dim values
  41.  
  42. #objetivo is a function that takes all games, points, and x, and returns the value of the 18th team in the table,
  43.  
  44. def run():
  45.  
  46.     if __name__ == "__main__":
  47.  
  48.         class PROBLEMA_MV():
  49.  
  50.             def fitness(self, x):
  51.                 puntos_18 = objetivo(partidos_ida,x)
  52.                 return [-puntos_18]
  53.  
  54.             def get_bounds(self):
  55.                 return([0]*190,[2]*190)
  56.  
  57.             def get_nix(self):
  58.                 return 190
  59.  
  60.         prob = pg.problem(PROBLEMA_MV())
  61.  
  62.         algo = pg.algorithm(pg.sga(gen=2000,mutation='uniform',crossover='exponential',cr=0.9,m=0.5))
  63.         #algo = algo = pg.algorithm(pg.gaco(1000, 190, 1.0, 1e9, 0.0, 1, 7, 100000, 100000, 0.0, False, 23))
  64.         algo.set_verbosity(1)
  65.  
  66.         pop = pg.population(prob,2000)
  67.         pop = algo.evolve(pop)
  68.  
  69.         print(pop.champion_f * 2)
  70.         print(pop.champion_x)
  71.  
  72. run()
  73.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement