def play(self): ''' Lance l'algo de resolution de mots croisés Si une instance est déja en train de tourner il l'arrete et lance une nouvelle instance :return: None ''' self.resetButton.configure(state="normal") if self.algo.hasRun: self.algo.stop() self.raz() self.algo = Algo(queue=self.queue, grille=self.algo.grille, traceframe=self.traceFrame, algoName=self.algo.algoName, heuristique=self.algo.heur) self.master.master.algo = self.algo self.master.algo = self.algo self.algo.start() self.after(1000, self.process_queue) self.master.send_To_Trace( "nombre de mot sur la grille :" + str( len(self.algo.grille.mots_horizontaux + self.algo.grille.mots_verticaux)) + "\n", "curr") self.master.send_To_Trace( "nombre de mot dans les Domaines :" + str(self.algo.grille.get_Domaines_Sizes()) + "\n", "curr")
def __init__(self, master, algo=None): Frame.__init__(self, master) # Options pour l'ouverture/sauvegarde des fichiers self.openfileoptions = dict() self.openfileoptions['filetypes'] = [('Fichier Mots croisés', '.mc'), ('Tout les fichiers', '.*')] self.openfileoptions['initialfile'] = 'ma_grille.mc' #self.openfileoptions['initialdir'] = GRILLE_PATH self.openfileoptions['parent'] = master # Queue pour les Threads self.thread_queue = Queue() self.createMenu() self.algo = Algo(algoName="AC3") self.algo.setQueue(self.thread_queue) self.mainFrame = MainFrame(self, self.algo, self.thread_queue) self.mainFrame.pack(fill=BOTH, expand=True) self.filename = None
def play(self): ''' Lance l'algo de resolution de mots croisés Si une instance est déja en train de tourner il l'arrete et lance une nouvelle instance :return: None ''' self.resetButton.configure(state="normal") if self.algo.hasRun: self.algo.stop() self.raz() self.algo = Algo(queue=self.queue, grille=self.algo.grille, traceframe=self.traceFrame, algoName=self.algo.algoName, heuristique=self.algo.heur) self.master.master.algo = self.algo self.master.algo = self.algo self.algo.start() self.after(1000, self.process_queue) self.master.send_To_Trace("nombre de mot sur la grille :" + str(len(self.algo.grille.mots_horizontaux + self.algo.grille.mots_verticaux))+"\n", "curr") self.master.send_To_Trace("nombre de mot dans les Domaines :" + str(self.algo.grille.get_Domaines_Sizes())+"\n", "curr")
class ButtonFrame(Frame): def __init__(self, master, queue, algo=None, traceFrame=None, motFrame=None): Frame.__init__(self, master) # variable pour l'affchage de la partie mot self.showMot = BooleanVar() self.showMot.set(False) # variable pour l'affchage de la partie trace self.showTrace = BooleanVar() self.showTrace.set(False) # bouton lancer l'algo self.playButton = Button(self, text="Lancer l'algo", command=self.play) self.playButton.grid(row=0, column=0) self.playButton.configure(state="disabled") # tant qu'une grille n'est pas ouverte on ne peut pas lancer l'algo # bouton continuer l'algo # s'il y a plusieurs reponse pour une grille ce bouton lance la recher de la reponse suivante self.continueButton = Button(self, text="Continuer l'algo", command=self.continueAlgo) self.continueButton.grid(row=0, column=1) self.continueButton.configure(state="disabled") # Bouton pour afficher les mots sur la droite self.advanceButton = Checkbutton(self, text="Voir les mots", variable=self.showMot, command=self.toggle_Mot) self.advanceButton.configure(state="disabled") self.advanceButton.grid(row=0, column=5, sticky=E) # remise a zéro de la grille self.resetButton = Button(self, text="Remise à zero", command=self.raz) self.resetButton.grid(row=0, column=2) self.resetButton.configure(state="disabled") # self.traceButton = Checkbutton(self, text="Trace algo", variable=self.showTrace, command=self.toggle_Trace) self.traceButton.grid(row=0, column=3, sticky=E) self.cleartraceButton = Button(self, text="Clear Trace", command=self.clearTrace) self.cleartraceButton.configure(state="disabled") self.cleartraceButton .grid(row=0, column=4, sticky=E) self.queue = queue self.algo = algo self.traceFrame = traceFrame self.motFrame = motFrame def play(self): ''' Lance l'algo de resolution de mots croisés Si une instance est déja en train de tourner il l'arrete et lance une nouvelle instance :return: None ''' self.resetButton.configure(state="normal") if self.algo.hasRun: self.algo.stop() self.raz() self.algo = Algo(queue=self.queue, grille=self.algo.grille, traceframe=self.traceFrame, algoName=self.algo.algoName, heuristique=self.algo.heur) self.master.master.algo = self.algo self.master.algo = self.algo self.algo.start() self.after(1000, self.process_queue) self.master.send_To_Trace("nombre de mot sur la grille :" + str(len(self.algo.grille.mots_horizontaux + self.algo.grille.mots_verticaux))+"\n", "curr") self.master.send_To_Trace("nombre de mot dans les Domaines :" + str(self.algo.grille.get_Domaines_Sizes())+"\n", "curr") #self.resetButton.configure(state="disabled") def raz(self): ''' Remet la grille a zéro :return: None ''' self.master.Ggrille(self.algo.grille.filePath) #self.resetButton.configure(state="disabled") def process_queue(self): ''' lance la procedure de la queue d'evenement de l'algo :return: None ''' try: result = self.queue.get(0) self.show(result) except Queue.Empty: self.after(1000, self.process_queue) def toggle_Mot(self): ''' Affiche sur la droite des mots de la grille :return: None ''' if self.showMot.get() and self.motFrame: self.master.toggle_Mot(True) else: self.master.toggle_Mot(False) def toggle_Trace(self): ''' Affiche en ba une Trace de l'algo :return: ''' if self.showTrace.get() and self.traceFrame: self.master.toggle_Trace(True) self.cleartraceButton.configure(state="normal") else: self.master.toggle_Trace(False) self.cleartraceButton.configure(state="disabled") def clearTrace(self): ''' Efface la console de Trace :return: ''' if self.traceFrame: self.master.clearTrace() def show(self, result): ''' :param result: Résultat de l'algo :return: ''' if self.algo.algoName is 'AC3': mess = 'Non Arc-Consistant' if result: mess = 'Arc-Consistant' self.master.updateGrille() top = Toplevel() top.title("Resultat de l'Arc-Consistance") msg = Message(top, text=mess, width=150) msg.pack() self.algo.stop() elif isinstance(result, tuple): if result[0] is 'AC3': self.master.updateGrille() elif result: self.continueButton.configure(state="normal") self.algo.grille.setResultat(result) self.master.updateGrille() for m in self.algo.grille.mots_horizontaux + self.algo.grille.mots_verticaux: self.master.send_To_Trace(str(m)+'\n', "curr") else: self.continueButton.configure(state="disabled") mess = 'Pas/plus de résultat' top = Toplevel() top.title("Resultat") msg = Message(top, text=mess) msg.pack() self.algo.stop() self.after(1000, self.process_queue) def continueAlgo(self): ''' Continue la resolution de la grille pour un nouveau resultat :return: ''' self.algo.resume() self.continueButton.configure(state="disabled") self.after(1000, self.process_queue) def enableMot(self, bool): ''' Autorise la selection des bouton mot et lancement de l'algo :param bool: :return: ''' if bool: self.playButton.configure(state="normal") self.advanceButton.configure(state="normal") else: self.advanceButton.configure(state="disabled") self.playButton.configure(state="disabled")
class ButtonFrame(Frame): def __init__(self, master, queue, algo=None, traceFrame=None, motFrame=None): Frame.__init__(self, master) # variable pour l'affchage de la partie mot self.showMot = BooleanVar() self.showMot.set(False) # variable pour l'affchage de la partie trace self.showTrace = BooleanVar() self.showTrace.set(False) # bouton lancer l'algo self.playButton = Button(self, text="Lancer l'algo", command=self.play) self.playButton.grid(row=0, column=0) self.playButton.configure( state="disabled" ) # tant qu'une grille n'est pas ouverte on ne peut pas lancer l'algo # bouton continuer l'algo # s'il y a plusieurs reponse pour une grille ce bouton lance la recher de la reponse suivante self.continueButton = Button(self, text="Continuer l'algo", command=self.continueAlgo) self.continueButton.grid(row=0, column=1) self.continueButton.configure(state="disabled") # Bouton pour afficher les mots sur la droite self.advanceButton = Checkbutton(self, text="Voir les mots", variable=self.showMot, command=self.toggle_Mot) self.advanceButton.configure(state="disabled") self.advanceButton.grid(row=0, column=5, sticky=E) # remise a zéro de la grille self.resetButton = Button(self, text="Remise à zero", command=self.raz) self.resetButton.grid(row=0, column=2) self.resetButton.configure(state="disabled") # self.traceButton = Checkbutton(self, text="Trace algo", variable=self.showTrace, command=self.toggle_Trace) self.traceButton.grid(row=0, column=3, sticky=E) self.cleartraceButton = Button(self, text="Clear Trace", command=self.clearTrace) self.cleartraceButton.configure(state="disabled") self.cleartraceButton.grid(row=0, column=4, sticky=E) self.queue = queue self.algo = algo self.traceFrame = traceFrame self.motFrame = motFrame def play(self): ''' Lance l'algo de resolution de mots croisés Si une instance est déja en train de tourner il l'arrete et lance une nouvelle instance :return: None ''' self.resetButton.configure(state="normal") if self.algo.hasRun: self.algo.stop() self.raz() self.algo = Algo(queue=self.queue, grille=self.algo.grille, traceframe=self.traceFrame, algoName=self.algo.algoName, heuristique=self.algo.heur) self.master.master.algo = self.algo self.master.algo = self.algo self.algo.start() self.after(1000, self.process_queue) self.master.send_To_Trace( "nombre de mot sur la grille :" + str( len(self.algo.grille.mots_horizontaux + self.algo.grille.mots_verticaux)) + "\n", "curr") self.master.send_To_Trace( "nombre de mot dans les Domaines :" + str(self.algo.grille.get_Domaines_Sizes()) + "\n", "curr") #self.resetButton.configure(state="disabled") def raz(self): ''' Remet la grille a zéro :return: None ''' self.master.Ggrille(self.algo.grille.filePath) #self.resetButton.configure(state="disabled") def process_queue(self): ''' lance la procedure de la queue d'evenement de l'algo :return: None ''' try: result = self.queue.get(0) self.show(result) except Queue.Empty: self.after(1000, self.process_queue) def toggle_Mot(self): ''' Affiche sur la droite des mots de la grille :return: None ''' if self.showMot.get() and self.motFrame: self.master.toggle_Mot(True) else: self.master.toggle_Mot(False) def toggle_Trace(self): ''' Affiche en ba une Trace de l'algo :return: ''' if self.showTrace.get() and self.traceFrame: self.master.toggle_Trace(True) self.cleartraceButton.configure(state="normal") else: self.master.toggle_Trace(False) self.cleartraceButton.configure(state="disabled") def clearTrace(self): ''' Efface la console de Trace :return: ''' if self.traceFrame: self.master.clearTrace() def show(self, result): ''' :param result: Résultat de l'algo :return: ''' if self.algo.algoName is 'AC3': mess = 'Non Arc-Consistant' if result: mess = 'Arc-Consistant' self.master.updateGrille() top = Toplevel() top.title("Resultat de l'Arc-Consistance") msg = Message(top, text=mess, width=150) msg.pack() self.algo.stop() elif isinstance(result, tuple): if result[0] is 'AC3': self.master.updateGrille() elif result: self.continueButton.configure(state="normal") self.algo.grille.setResultat(result) self.master.updateGrille() for m in self.algo.grille.mots_horizontaux + self.algo.grille.mots_verticaux: self.master.send_To_Trace(str(m) + '\n', "curr") else: self.continueButton.configure(state="disabled") mess = 'Pas/plus de résultat' top = Toplevel() top.title("Resultat") msg = Message(top, text=mess) msg.pack() self.algo.stop() self.after(1000, self.process_queue) def continueAlgo(self): ''' Continue la resolution de la grille pour un nouveau resultat :return: ''' self.algo.resume() self.continueButton.configure(state="disabled") self.after(1000, self.process_queue) def enableMot(self, bool): ''' Autorise la selection des bouton mot et lancement de l'algo :param bool: :return: ''' if bool: self.playButton.configure(state="normal") self.advanceButton.configure(state="normal") else: self.advanceButton.configure(state="disabled") self.playButton.configure(state="disabled")
class Gui(Frame): def __init__(self, master, algo=None): Frame.__init__(self, master) # Options pour l'ouverture/sauvegarde des fichiers self.openfileoptions = dict() self.openfileoptions['filetypes'] = [('Fichier Mots croisés', '.mc'), ('Tout les fichiers', '.*')] self.openfileoptions['initialfile'] = 'ma_grille.mc' #self.openfileoptions['initialdir'] = GRILLE_PATH self.openfileoptions['parent'] = master # Queue pour les Threads self.thread_queue = Queue() self.createMenu() self.algo = Algo(algoName="AC3") self.algo.setQueue(self.thread_queue) self.mainFrame = MainFrame(self, self.algo, self.thread_queue) self.mainFrame.pack(fill=BOTH, expand=True) self.filename = None def update_Algo(self, arg0): self.algo.setAlgoName(arg0) self.mainFrame.setAlgo() def update_Dico(self): start = time.time() self.dico = Dico(self.dliste[self.radio_dico.get()-1]) end = time.time() - start self.mainFrame.send_To_Trace("Création du dictionnaire "+self.dliste[self.radio_dico.get()-1].split("/")[-1]+" :"+str(end) + "\n", "temps") if self.algo.grille: self.algo.grille.updateDico(self.dico) def file_chooser(self): self.filename = None self.openfileoptions['initialdir'] = GRILLEVIDE_PATH self.filename = askopenfilename(**self.openfileoptions) if self.filename: self.Ggrille(self.filename) def Ggrille(self, file): self.algo.setGrille(Grille(filePath=file)) self.algo.grille.updateDico(self.dico) self.setGrille() def genere_Grille(self): Up = Toplevel() Up.title("Générer Grille") li = IntVar() li.set(10) Label(Up, text ="Nombre de ligne :").grid(row=1, column=0, sticky="w") w = Scale(Up, from_=5, to=20, orient=HORIZONTAL, var=li) w.grid(row=1, column=1, sticky="w") col = IntVar() col.set(10) Label(Up, text ="Nombre de colonne :").grid(row=2, column=0, sticky="w") w = Scale(Up, from_=5, to=20, orient=HORIZONTAL, var=col) w.grid(row=2, column=1, sticky="w") nbN = IntVar() nbN.set(30) Label(Up, text ="% de case noir :").grid(row=3, column=0, sticky="w") w = Scale(Up, from_=10, to=50, orient=HORIZONTAL, var=nbN) w.grid(row=3, column=1, sticky="w") button = Button(Up, text ="Créer la Grille", command = Up.destroy).grid(row =5,columnspan=2, sticky=N+S+E+W) Up.wait_window() self.algo.setGrille(Grille(taille=(li.get(),col.get()),alea=True, percent=nbN.get())) self.algo.grille.updateDico(self.dico) self.setGrille() def file_saver(self): self.openfileoptions['initialdir'] = GRILLEPLEIN_PATH if self.algo.grille: self.openfileoptions['initialfile'] = str(self.algo.grille.nomGrille)+"_"+str(self.algo.algoName) filename = asksaveasfilename(**self.openfileoptions) if filename and self.algo.grille: self.algo.grille.sauvegarder_grille(filename) def setGrille(self): self.mainFrame.open_grille() def createMenu(self): menubar = Menu(self.master) filemenu = Menu(menubar, tearoff=0) algomenu = Menu(menubar, tearoff=0) dicomenu = Menu(menubar, tearoff=0) newGrilleMenu = Menu(menubar, tearoff=0) newGrilleMenu.add_command(label="Ouvrir Grille", command=self.file_chooser) newGrilleMenu.add_command(label="Générer Grille", command=self.genere_Grille) filemenu.add_cascade(label="Nouvelle grille", menu=newGrilleMenu) filemenu.add_cascade(label="Sauver Grille", command=self.file_saver) filemenu.add_separator() filemenu.add_command(label="Quitter", command=self.master.quit) self.radio_algo = StringVar() algomenu.add_radiobutton(label="Arc-Consistance 3", variable=self.radio_algo, value=1, command=lambda arg0="AC3": self.update_Algo(arg0)) algomenu.add_radiobutton(label="Forward Checking", variable=self.radio_algo, value=2, command=lambda arg0="FC": self.update_Algo(arg0)) algomenu.add_radiobutton(label="Forward Checking avec AC3", variable=self.radio_algo, value=3, command=lambda arg0="FC_AC3": self.update_Algo(arg0)) algomenu.add_radiobutton(label="Conflict BackJumping avec AC3", variable=self.radio_algo, value=4, command=lambda arg0="CBJ_AC3": self.update_Algo(arg0)) algomenu.add_radiobutton(label="Conflict BackJumping", variable=self.radio_algo, value=5, command=lambda arg0="CBJ": self.update_Algo(arg0)) algomenu.add_radiobutton(label="Branch & Bound Valué", variable=self.radio_algo, value=6, command=lambda arg0="VAL": self.update_Algo(arg0)) self.radio_algo.set(1) self.radio_dico = IntVar() self.listDico = [] self.dliste = [] for dirname, dirnames, filenames in os.walk(DICO_PATH): for filename in filenames: self.dliste += ["".join([DICO_PATH, filename])] self.listDico += [filename[:-4]] i = 1 for l in self.listDico: dicomenu.add_radiobutton(label=l, variable=self.radio_dico, value=i, command=self.update_Dico) i += 1 self.radio_dico.set(5) menubar.add_cascade(label="Fichier", menu=filemenu) menubar.add_cascade(label="Algorithme", menu=algomenu) menubar.add_cascade(label="Dictionnaire", menu=dicomenu) self.dico = Dico(self.dliste[self.radio_dico.get()-1]) self.master.config(menu=menubar)
class Gui(Frame): def __init__(self, master, algo=None): Frame.__init__(self, master) # Options pour l'ouverture/sauvegarde des fichiers self.openfileoptions = dict() self.openfileoptions['filetypes'] = [('Fichier Mots croisés', '.mc'), ('Tout les fichiers', '.*')] self.openfileoptions['initialfile'] = 'ma_grille.mc' #self.openfileoptions['initialdir'] = GRILLE_PATH self.openfileoptions['parent'] = master # Queue pour les Threads self.thread_queue = Queue() self.createMenu() self.algo = Algo(algoName="AC3") self.algo.setQueue(self.thread_queue) self.mainFrame = MainFrame(self, self.algo, self.thread_queue) self.mainFrame.pack(fill=BOTH, expand=True) self.filename = None def update_Algo(self, arg0): self.algo.setAlgoName(arg0) self.mainFrame.setAlgo() def update_Dico(self): start = time.time() self.dico = Dico(self.dliste[self.radio_dico.get() - 1]) end = time.time() - start self.mainFrame.send_To_Trace( "Création du dictionnaire " + self.dliste[self.radio_dico.get() - 1].split("/")[-1] + " :" + str(end) + "\n", "temps") if self.algo.grille: self.algo.grille.updateDico(self.dico) def file_chooser(self): self.filename = None self.openfileoptions['initialdir'] = GRILLEVIDE_PATH self.filename = askopenfilename(**self.openfileoptions) if self.filename: self.Ggrille(self.filename) def Ggrille(self, file): self.algo.setGrille(Grille(filePath=file)) self.algo.grille.updateDico(self.dico) self.setGrille() def genere_Grille(self): Up = Toplevel() Up.title("Générer Grille") li = IntVar() li.set(10) Label(Up, text="Nombre de ligne :").grid(row=1, column=0, sticky="w") w = Scale(Up, from_=5, to=20, orient=HORIZONTAL, var=li) w.grid(row=1, column=1, sticky="w") col = IntVar() col.set(10) Label(Up, text="Nombre de colonne :").grid(row=2, column=0, sticky="w") w = Scale(Up, from_=5, to=20, orient=HORIZONTAL, var=col) w.grid(row=2, column=1, sticky="w") nbN = IntVar() nbN.set(30) Label(Up, text="% de case noir :").grid(row=3, column=0, sticky="w") w = Scale(Up, from_=10, to=50, orient=HORIZONTAL, var=nbN) w.grid(row=3, column=1, sticky="w") button = Button(Up, text="Créer la Grille", command=Up.destroy).grid(row=5, columnspan=2, sticky=N + S + E + W) Up.wait_window() self.algo.setGrille( Grille(taille=(li.get(), col.get()), alea=True, percent=nbN.get())) self.algo.grille.updateDico(self.dico) self.setGrille() def file_saver(self): self.openfileoptions['initialdir'] = GRILLEPLEIN_PATH if self.algo.grille: self.openfileoptions['initialfile'] = str( self.algo.grille.nomGrille) + "_" + str(self.algo.algoName) filename = asksaveasfilename(**self.openfileoptions) if filename and self.algo.grille: self.algo.grille.sauvegarder_grille(filename) def setGrille(self): self.mainFrame.open_grille() def createMenu(self): menubar = Menu(self.master) filemenu = Menu(menubar, tearoff=0) algomenu = Menu(menubar, tearoff=0) dicomenu = Menu(menubar, tearoff=0) newGrilleMenu = Menu(menubar, tearoff=0) newGrilleMenu.add_command(label="Ouvrir Grille", command=self.file_chooser) newGrilleMenu.add_command(label="Générer Grille", command=self.genere_Grille) filemenu.add_cascade(label="Nouvelle grille", menu=newGrilleMenu) filemenu.add_cascade(label="Sauver Grille", command=self.file_saver) filemenu.add_separator() filemenu.add_command(label="Quitter", command=self.master.quit) self.radio_algo = StringVar() algomenu.add_radiobutton( label="Arc-Consistance 3", variable=self.radio_algo, value=1, command=lambda arg0="AC3": self.update_Algo(arg0)) algomenu.add_radiobutton( label="Forward Checking", variable=self.radio_algo, value=2, command=lambda arg0="FC": self.update_Algo(arg0)) algomenu.add_radiobutton( label="Forward Checking avec AC3", variable=self.radio_algo, value=3, command=lambda arg0="FC_AC3": self.update_Algo(arg0)) algomenu.add_radiobutton( label="Conflict BackJumping avec AC3", variable=self.radio_algo, value=4, command=lambda arg0="CBJ_AC3": self.update_Algo(arg0)) algomenu.add_radiobutton( label="Conflict BackJumping", variable=self.radio_algo, value=5, command=lambda arg0="CBJ": self.update_Algo(arg0)) algomenu.add_radiobutton( label="Branch & Bound Valué", variable=self.radio_algo, value=6, command=lambda arg0="VAL": self.update_Algo(arg0)) self.radio_algo.set(1) self.radio_dico = IntVar() self.listDico = [] self.dliste = [] for dirname, dirnames, filenames in os.walk(DICO_PATH): for filename in filenames: self.dliste += ["".join([DICO_PATH, filename])] self.listDico += [filename[:-4]] i = 1 for l in self.listDico: dicomenu.add_radiobutton(label=l, variable=self.radio_dico, value=i, command=self.update_Dico) i += 1 self.radio_dico.set(5) menubar.add_cascade(label="Fichier", menu=filemenu) menubar.add_cascade(label="Algorithme", menu=algomenu) menubar.add_cascade(label="Dictionnaire", menu=dicomenu) self.dico = Dico(self.dliste[self.radio_dico.get() - 1]) self.master.config(menu=menubar)
'VAL' ] for gr in GRILLE: for al in ALGO: for dic in DICO: times = [] print 'debut algo ' + str(al) + " " + str(dic[1]) + ' Grille :' + str(gr[1]) for i in range(5): dico = Dico(dic[0]) grille = Grille(gr[0]) grille.updateDico(dico) algo = Algo(grille=grille, algoName=al, stat=True) start = time.time() algo.start() algo.join(300) elapse = time.time() - start print 'fin ' + str(i+1) + ': ' + str(elapse) times += [elapse] fichier = open(PATHSAVE+"temps_" + gr[1] + "_" + al + ".stat", "a") fichier.write(dic[1]) moy = 0 for t in times: fichier.write(" "+str(t)) moy += t print 'moyenne :' + str(moy/5) fichier.write("\n") fichier.close()