Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygmo as pg
- from itertools import combinations
- def juego(partido,decision,puntos):
- if decision == 0: #gana local
- puntos[str(partido[0])] += resultados['victoria']
- if decision == 1: #gana visitante
- puntos[str(partido[1])] += resultados['victoria']
- if decision == 2:
- puntos[str(partido[0])] += resultados['empate']
- puntos[str(partido[1])] += resultados['empate']
- def comprobador(puntos,posicion):
- ordenado = sorted(puntos,key=puntos.get)
- clasificacion = list(reversed(ordenado))
- return puntos[clasificacion[posicion-1]]
- def resetear_puntos():
- 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}
- return puntos
- 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}
- resultados = {'victoria':3, 'empate':1}
- partidos_ida = list(combinations(puntos,2))
- partidos_vuelta = []
- for partido in partidos_ida:
- partidos_vuelta.append(list(reversed(partido)))
- total_partidos = partidos_ida+partidos_vuelta
- def objetivo(partidos_jugar,x):
- puntos = resetear_puntos()
- for i in range(len(partidos_jugar)):
- juego(partidos_jugar[i],x[i],puntos)
- puesto = comprobador(puntos,18)
- return puesto
- #x is a N-dimensional array (N being number of games) with 0,1,2 as possible dim values
- #objetivo is a function that takes all games, points, and x, and returns the value of the 18th team in the table,
- def run():
- if __name__ == "__main__":
- class PROBLEMA_MV():
- def fitness(self, x):
- puntos_18 = objetivo(partidos_ida,x)
- return [-puntos_18]
- def get_bounds(self):
- return([0]*190,[2]*190)
- def get_nix(self):
- return 190
- prob = pg.problem(PROBLEMA_MV())
- algo = pg.algorithm(pg.sga(gen=2000,mutation='uniform',crossover='exponential',cr=0.9,m=0.5))
- #algo = algo = pg.algorithm(pg.gaco(1000, 190, 1.0, 1e9, 0.0, 1, 7, 100000, 100000, 0.0, False, 23))
- algo.set_verbosity(1)
- pop = pg.population(prob,2000)
- pop = algo.evolve(pop)
- print(pop.champion_f * 2)
- print(pop.champion_x)
- run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement