Pouknouki

Et là c'est le drame

May 19th, 2016
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.47 KB | None | 0 0
  1. import random as rd
  2. import copy as cp
  3.  
  4. #ez comme dirait B2o
  5.  
  6. Durees = {"Maths1" : 2, "Maths2" : 3, "Physique" : 3, "Anglais" : 4}
  7.  
  8. Profs = {"Maths1" : [], "Maths2" : [], "Physique" : [], "Anglais" : []}
  9.  
  10. Eleves = [1, 2, 3, 4, 5]
  11. Epreuves = ["Maths1", "Maths2", "Physique", "Anglais"]
  12.  
  13. longueur = len(Eleves) * len(Epreuves)
  14.  
  15. rd.seed()
  16.  
  17. def trier(Prof):
  18.     renvoye = []
  19.     heures = [o[0] for o in Prof]
  20.     heures.sort()
  21.     for heure in heures:
  22.         for oral in Prof:
  23.             if oral[0] == heure:
  24.                 renvoye.append(oral)
  25.                 break
  26.     return renvoye
  27.  
  28. def EleveDisponible(Jurys, Eleve, Time, Duree):
  29.     OrauxEleve = []
  30.     for Jury in Jurys.keys():
  31.         EDT = Jurys[Jury]
  32.         for Oral in EDT:
  33.             if Oral[1] == Eleve:
  34.                 OrauxEleve.append((Oral[0], Oral[0] + Durees[Jury]))
  35.     for Oral in OrauxEleve:
  36.         DebutOral, FinOral = Oral[0], Oral[1]
  37.         if Time == DebutOral:
  38.             return False
  39.         if Time < DebutOral:
  40.             if Time + Duree + 2 > DebutOral:
  41.                 return False
  42.         else:
  43.             if FinOral + 2 > Time:
  44.                 return False
  45.     return True
  46.    
  47. def fit(Chromosome, afficher):
  48.     Jurys = cp.deepcopy(Profs)
  49.     for Oral in Chromosome:
  50.         Prof = Oral.split("/")[0]
  51.         Eleve = Oral.split("/")[1]
  52.         Pose = False
  53.         if len(Jurys[Prof]) == 0:
  54.             time = 0
  55.             while EleveDisponible(Jurys, Eleve, time, Durees[Prof]) == False:
  56.                 time += 1
  57.             Jurys[Prof].append((time, Eleve))
  58.             Pose = True
  59.         else:
  60.             for i in range(len(Jurys[Prof]) - 1):
  61.                 if abs(Jurys[Prof][i+1][0] - Jurys[Prof][i][0]) >= 2*Durees[Prof]:
  62.                     time = (Jurys[Prof][i][0] + Durees[Prof])
  63.                     while time + Durees[Prof] + 2 <= Jurys[Prof][i+1][0] and EleveDisponible(Jurys, Eleve, time, Durees[Prof]) == False:
  64.                         time += 1
  65.                     if time + Durees[Prof] + 2 <= Jurys[Prof][i+1][0]:
  66.                         Jurys[Prof].append((time, Eleve))
  67.                         Pose = True
  68.                     else:
  69.                         Pose = False
  70.         if Pose == False:
  71.             temp = (max([x[0] for x in Jurys[Prof]]))
  72.             time = temp + Durees[Prof]
  73.             while EleveDisponible(Jurys, Eleve, time, Durees[Prof]) == False:
  74.                 time += 1
  75.             Jurys[Prof].append((time,Eleve))
  76.         trier(Jurys[Prof])
  77.     #On a généré l'emploi du temps. C'est très bien. Maintenant on va trouver à quelle heure il finit (et en plus c'est facile que de compter le nombre de trous)
  78.     if afficher:
  79.         print(Jurys)
  80.     maxi = -1
  81.     for Prof in Jurys.keys():
  82.         for Oral in Jurys[Prof]:
  83.             maxi = max(maxi, Oral[0])
  84.     return maxi
  85.    
  86. Chromosomes = []
  87. fitness = []
  88.  
  89. for j in range(0, 10000):
  90.     Chromosomes.append([])
  91.     for i in range(longueur):
  92.         indice = str(Epreuves[rd.randint(0, len(Epreuves) - 1)]) + "/" + str(Eleves[rd.randint(0, len(Eleves) - 1)])
  93.         while indice in Chromosomes[j]:
  94.             indice = str(Epreuves[rd.randint(0, len(Epreuves) - 1)]) + "/" + str(Eleves[rd.randint(0, len(Eleves) - 1)])
  95.         Chromosomes[j].append(indice)
  96.    
  97. #Génération de l'EDT lié à un chromosome
  98. for Chromosome in Chromosomes:
  99.     fitness.append(fit(Chromosome, False))
  100. indice = fitness.index(min(fitness))
  101. print(fit(Chromosomes[indice], True))
Advertisement
Add Comment
Please, Sign In to add comment