def on_button_click_sim(self,event): self.labelWorking = wx.StaticText(self,-1,label="L'algorithme est entraîn de calculer..", pos=(218, 660)) self.labelWorking.SetForegroundColour((78, 50, 255)) self.labelWorking.Update() fitnessFunct = [self.network.nbr_valves] if self.cb_chooseCriteria_safe.GetValue()==True: fitnessFunct.append(self.network.average_segment_size) if self.cb_chooseCriteria_cost.GetValue()==True: fitnessFunct.append(self.network.standard_deviation_segment_size) if self.cb_chooseCriteria_distance.GetValue()==True: fitnessFunct.append(self.network.average_valves_per_segment) if self.cb_chooseCriteria_isolation.GetValue()==True: fitnessFunct.append(self.network.average_unintended_isolation) coolS=float(self.inputCoolSpeed.GetValue())/1000 modif=float(self.inputModifictionRate.GetValue())/100 self.network=GlobalNetwork("randomNetwork", '../../input/randomNetwork.dot') pattern = [(0,1) for i in range(self.network.network.nbrPipes*2)] #solution = SimulatedAnnealing(pattern, self.network.average_segment_size, temperature=self.inputTemperature.GetValue(), coolSpeed=self.inputCoolSpeed.GetValue(), modificationRate=self.inputModifictionRate.GetValue()) solution = SimulatedAnnealing(pattern, fitnessFunct, showInfo = self.updateValves, temperature=self.inputTemperature.GetValue(), coolSpeed=coolS, modificationRate=modif) self.network.update(solution.solution) self.updateVar() self.iterationCount=0 self.panel.Refresh() self.showSolutionScores() self.labelWorking.SetLabel("")
def OnClick_button_Next(self,event): if self.verifyInput() == True: self.failVerify.SetLabel("L'algorithme est entraîn de calculer, veuillez attendre...") self.failVerify.SetForegroundColour((78,50,255)) self.failVerify.Update() self.network = GlobalNetwork("randomNetwork", '../../input/randomNetwork.dot') fitnessFunct = [self.network.nbr_valves] if self.cb_chooseCriteria_safe.GetValue()==True: fitnessFunct.append(self.network.average_segment_size) if self.cb_chooseCriteria_cost.GetValue()==True: fitnessFunct.append(self.network.standard_deviation_segment_size) if self.cb_chooseCriteria_distance.GetValue()==True: fitnessFunct.append(self.network.average_valves_per_segment) if self.cb_chooseCriteria_isolation.GetValue()==True: fitnessFunct.append(self.network.average_unintended_isolation) if self.cb_chooseGen.GetValue()==True: miter = self.inputNumberGenetic.GetValue() solGen = GuiShowSolution(self.x, self.y, "L'algorithme génétique", fitnessFunct, miter) solGen.Show() if self.cb_chooseSim.GetValue()==True: mtemperature = self.inputNumberSimulatedAnnealing.GetValue() solSim = GuiShowSolution(self.x, self.y, "L'algorithme de recuit simulé", fitnessFunct, mtemperature) solSim.Show() self.Close(True) else: self.failVerify.SetLabel('Données erronées ou incomplètes dans un ou plusieurs champs.') self.failVerify.SetForegroundColour((255,0,0))
def __init__(self, x, y, algotitle, fitnessFunct, complexity): wx.Frame.__init__(self, wx.GetApp().TopWindow, size=(500, 550), style=wx.DEFAULT_FRAME_STYLE ^ (wx.RESIZE_BORDER | wx.MAXIMIZE_BOX)) self.x=x self.y=y self.Center() self.algo="gen" if algotitle == "L'algorithme de recuit simulé": self.algo="sim" self.SetTitle("Le graphe de solution : " + algotitle) self.network=GlobalNetwork("randomNetwork", '../../input/randomNetwork.dot') self.iterationCount=0 self.updateVar() self.Show(True) self.panel = wx.Panel(self, size=(500, 500)) self.panel.Bind(wx.EVT_PAINT, self.draw_graph) self.initialize() self.runAlgorithm(fitnessFunct, complexity) self.Fit()
def on_button_click_gen(self,event): self.labelWorking = wx.StaticText(self,-1,label="L'algorithme est entraîn de calculer..", pos=(218, 715)) self.labelWorking.SetForegroundColour((78, 50, 255)) self.labelWorking.Update() fact = float(self.inputSelectionRate.GetValue())/100 elit = float(self.inputElitisme.GetValue())/1000 cross = float(self.inputCrossOverProb.GetValue())/100 muta = float(self.inputMutationProba.GetValue())/100 fitnessFunct = [self.network.nbr_valves] if self.cb_chooseCriteria_safe.GetValue()==True: fitnessFunct.append(self.network.average_segment_size) if self.cb_chooseCriteria_cost.GetValue()==True: fitnessFunct.append(self.network.standard_deviation_segment_size) if self.cb_chooseCriteria_distance.GetValue()==True: fitnessFunct.append(self.network.average_valves_per_segment) if self.cb_chooseCriteria_isolation.GetValue()==True: fitnessFunct.append(self.network.average_unintended_isolation) self.network=GlobalNetwork("randomNetwork", '../../input/randomNetwork.dot') pattern = [(0,1) for i in range(self.network.network.nbrPipes*2)] solution = GeneticAlgorithm(pattern, fitnessFunct, showInfo = self.updateValves, nbrOfGenerations = self.inputMaxIter.GetValue(), populationSize=self.inputPopulationSize.GetValue(), tournamentSize=self.inputTournmanetSize.GetValue(), selectionRate=fact, elitisme=elit, crossoverProb=cross, mutationProba=muta) self.network.update(solution.solution) self.updateVar() self.iterationCount=0 self.panel.Refresh() self.showSolutionScores() self.labelWorking.SetLabel("")
def __init__(self): self.network = GlobalNetwork("randomNetwork", '../../input/randomNetwork.dot') self.updateVar() wx.Frame.__init__(self, wx.GetApp().TopWindow, size=(500, 550), style=wx.DEFAULT_FRAME_STYLE ^ (wx.RESIZE_BORDER | wx.MAXIMIZE_BOX)) self.initialize() self.Center() self.SetTitle('Sélection du graphe') self.panel = wx.Panel(self, size=(500, 500)) self.createMadeGraph5() self.type="Hwandon" self.panel.Bind(wx.EVT_PAINT, self.draw_graph) self.Fit()
class GuiShowSolution(wx.Frame): def __init__(self, x, y, algotitle, fitnessFunct, complexity): wx.Frame.__init__(self, wx.GetApp().TopWindow, size=(500, 550), style=wx.DEFAULT_FRAME_STYLE ^ (wx.RESIZE_BORDER | wx.MAXIMIZE_BOX)) self.x=x self.y=y self.Center() self.algo="gen" if algotitle == "L'algorithme de recuit simulé": self.algo="sim" self.SetTitle("Le graphe de solution : " + algotitle) self.network=GlobalNetwork("randomNetwork", '../../input/randomNetwork.dot') self.iterationCount=0 self.updateVar() self.Show(True) self.panel = wx.Panel(self, size=(500, 500)) self.panel.Bind(wx.EVT_PAINT, self.draw_graph) self.initialize() self.runAlgorithm(fitnessFunct, complexity) self.Fit() def runAlgorithm(self, fitnessFunct, complexity): pattern = [(0,1) for i in range(self.network.network.nbrPipes*2)] if self.algo=="gen": solution = GeneticAlgorithm(pattern, fitnessFunct, showInfo = self.updateValves, nbrOfGenerations = complexity) else: solution = SimulatedAnnealing(pattern, fitnessFunct, showInfo = self.updateValves, temperature=complexity) self.network.update(solution.solution) self.updateVar() self.iterationCount=0 self.panel.Refresh() self.showSolutionScores() def initialize(self): self.label = wx.StaticText(self,-1,label='', pos=(0, 0)) button_next = wx.Button(self, -1, label='Suivant', pos = (398, 525), size=(100, 25)) self.Bind(wx.EVT_BUTTON, self.on_button_click_next, button_next) button_prec = wx.Button(self, -1, label='Précédent', pos = (398, 500), size=(100, 25)) self.Bind(wx.EVT_BUTTON, self.on_button_click_prec, button_prec) self.showScores(); self.Show(True) def showSolutionScores(self): self.label2 = wx.StaticText(self,-1,label="Nombre moyen de segments : " + str(round(self.network.average_segment_size(), 3)), pos=(0, 500)) self.label3 = wx.StaticText(self,-1,label="Taille de la déviation de segments : " + str(round(self.network.standard_deviation_segment_size(), 3)), pos=(0, 515)) self.label4 = wx.StaticText(self,-1,label="Nombre moyen de vannes par segment : " + str(round(self.network.average_valves_per_segment(), 3)), pos=(0, 530)) self.label5 = wx.StaticText(self,-1,label="Nombre de valves : " + str(self.network.nbr_valves()) + " ; Coût (pour le prix d'une vanne de 500€) : " + str(self.network.nbr_valves()*500) + "€", pos=(0, 545)) #self.label8 = wx.StaticText(self,-1,label="Nombre moyen d'isolements involontaires" + str(round(self.network.average_unintended_isolation)), pos=(0, 545)) self.label7 = wx.StaticText(self,-1,label="___________________________________________________________________________________", pos=(0, 557)) self.label6 = wx.StaticText(self,-1,label="Paramètres de l'algorithme:", pos=(0, 575)) self.label6.SetForegroundColour((255,0,0)) def showCriteria(self): self.labelRed = wx.StaticText(self,-1,label="Critère d'exécution:", pos=(250, 575)) self.labelRed.SetForegroundColour((255,0,0)) self.cb_chooseCriteria_cost = wx.CheckBox(self, -1, 'Moyenne de la taille de chaque segment', pos=(250, 590)) self.cb_chooseCriteria_distance = wx.CheckBox(self, -1, "Différence de taille entre chaque segment", pos=(250, 605)) self.cb_chooseCriteria_safe = wx.CheckBox(self, -1, 'Nbre moyen de vannes pour isoler un segment', pos=(250, 620)) self.cb_chooseCriteria_isolation = wx.CheckBox(self, -1, "Nombre moyen d'isolements involontaires", pos=(250, 635)) def showScores(self): self.showSolutionScores() self.showCriteria() if self.algo=="sim": self.initSim() else: self.initGen() def initSim(self): self.label = wx.StaticText(self,-1,label="Vitesse de refroidissement (1-1000):", pos=(0, 590)) self.inputCoolSpeed = wx.SpinCtrl(self, value='5', pos=(178, 587), size=(60, -1), min=0, max=1000) self.label = wx.StaticText(self,-1,label="Echelle de modification (1-100):", pos=(0, 610)) self.inputModifictionRate = wx.SpinCtrl(self, value='10', pos=(178, 607), size=(60, -1), min=0, max=100) self.label = wx.StaticText(self,-1,label="Compléxite (température):", pos=(0, 630)) self.inputTemperature = wx.SpinCtrl(self, value='100000', pos=(158, 627), size=(80, -1), min=1, max=9999999) button_sim = wx.Button(self, -1, label='Mettre à jour', pos = (398, 655), size=(100, 25)) self.Bind(wx.EVT_BUTTON, self.on_button_click_sim, button_sim) def initGen(self): self.label = wx.StaticText(self,-1,label="Taille de la population:", pos=(0, 590)) self.inputPopulationSize = wx.SpinCtrl(self, value='40', pos=(150, 587), size=(60, -1), min=0, max=250) self.label = wx.StaticText(self,-1,label="Taille du tournoi:", pos=(0, 610)) self.inputTournmanetSize = wx.SpinCtrl(self, value='2', pos=(150, 607), size=(60, -1), min=0, max=100) self.label = wx.StaticText(self,-1,label="Facteur de sélection (%):", pos=(0, 630)) self.inputSelectionRate = wx.SpinCtrl(self, value='50', pos=(150, 627), size=(60, -1), min=0, max=100) self.label = wx.StaticText(self,-1,label="Probabilité de crossover (%): ", pos=(0, 650)) self.inputCrossOverProb = wx.SpinCtrl(self, value='50', pos=(150, 647), size=(60, -1), min=0, max=100) self.label = wx.StaticText(self,-1,label="Probabilité de mutation (%):", pos=(0, 670)) self.inputMutationProba = wx.SpinCtrl(self, value='1', pos=(150, 667), size=(60, -1), min=0, max=100) self.label = wx.StaticText(self,-1,label="Facteur d'élitisme (1-1000):", pos=(0, 690)) self.inputElitisme = wx.SpinCtrl(self, value='125', pos=(150, 687), size=(60, -1), min=0, max=1000) self.label = wx.StaticText(self,-1,label="Compléxite (itérations max):", pos=(0, 710)) self.inputMaxIter = wx.SpinCtrl(self, value='30', pos=(150, 707), size=(60, -1), min=1, max=99999999) button_gen = wx.Button(self, -1, label='Mettre à jour', pos = (398, 710), size=(100, 25)) self.Bind(wx.EVT_BUTTON, self.on_button_click_gen, button_gen) def on_button_click_sim(self,event): self.labelWorking = wx.StaticText(self,-1,label="L'algorithme est entraîn de calculer..", pos=(218, 660)) self.labelWorking.SetForegroundColour((78, 50, 255)) self.labelWorking.Update() fitnessFunct = [self.network.nbr_valves] if self.cb_chooseCriteria_safe.GetValue()==True: fitnessFunct.append(self.network.average_segment_size) if self.cb_chooseCriteria_cost.GetValue()==True: fitnessFunct.append(self.network.standard_deviation_segment_size) if self.cb_chooseCriteria_distance.GetValue()==True: fitnessFunct.append(self.network.average_valves_per_segment) if self.cb_chooseCriteria_isolation.GetValue()==True: fitnessFunct.append(self.network.average_unintended_isolation) coolS=float(self.inputCoolSpeed.GetValue())/1000 modif=float(self.inputModifictionRate.GetValue())/100 self.network=GlobalNetwork("randomNetwork", '../../input/randomNetwork.dot') pattern = [(0,1) for i in range(self.network.network.nbrPipes*2)] #solution = SimulatedAnnealing(pattern, self.network.average_segment_size, temperature=self.inputTemperature.GetValue(), coolSpeed=self.inputCoolSpeed.GetValue(), modificationRate=self.inputModifictionRate.GetValue()) solution = SimulatedAnnealing(pattern, fitnessFunct, showInfo = self.updateValves, temperature=self.inputTemperature.GetValue(), coolSpeed=coolS, modificationRate=modif) self.network.update(solution.solution) self.updateVar() self.iterationCount=0 self.panel.Refresh() self.showSolutionScores() self.labelWorking.SetLabel("") self.labelWorking.Update() def on_button_click_gen(self,event): self.labelWorking = wx.StaticText(self,-1,label="L'algorithme est entraîn de calculer..", pos=(218, 715)) self.labelWorking.SetForegroundColour((78, 50, 255)) self.labelWorking.Update() fact = float(self.inputSelectionRate.GetValue())/100 elit = float(self.inputElitisme.GetValue())/1000 cross = float(self.inputCrossOverProb.GetValue())/100 muta = float(self.inputMutationProba.GetValue())/100 fitnessFunct = [self.network.nbr_valves] if self.cb_chooseCriteria_safe.GetValue()==True: fitnessFunct.append(self.network.average_segment_size) if self.cb_chooseCriteria_cost.GetValue()==True: fitnessFunct.append(self.network.standard_deviation_segment_size) if self.cb_chooseCriteria_distance.GetValue()==True: fitnessFunct.append(self.network.average_valves_per_segment) if self.cb_chooseCriteria_isolation.GetValue()==True: fitnessFunct.append(self.network.average_unintended_isolation) self.network=GlobalNetwork("randomNetwork", '../../input/randomNetwork.dot') pattern = [(0,1) for i in range(self.network.network.nbrPipes*2)] solution = GeneticAlgorithm(pattern, fitnessFunct, showInfo = self.updateValves, nbrOfGenerations = self.inputMaxIter.GetValue(), populationSize=self.inputPopulationSize.GetValue(), tournamentSize=self.inputTournmanetSize.GetValue(), selectionRate=fact, elitisme=elit, crossoverProb=cross, mutationProba=muta) self.network.update(solution.solution) self.updateVar() self.iterationCount=0 self.panel.Refresh() self.showSolutionScores() self.labelWorking.SetLabel("") self.labelWorking.Update() def on_button_click_next(self,event): algo = GuiTerminateSimulation() algo.Show() self.Close(True) def on_button_click_prec(self,event): algo = GuiAlgorithmChoice(self.x, self.y) algo.Show() self.Close(True) def updateVar(self): self.nodes = self.network.network.nodeMap.values() self.pipes = self.network.network.pipeMap.values() def updateValves(self, valvesCode, data): self.data=data self.network.update(valvesCode) self.updateVar() self.iterationCount=self.iterationCount+1 self.showUpdateScores(data) self.panel.Refresh() self.panel.Update() def showUpdateScores(self, data): if self.algo=="gen": self.label5.SetLabel("Nombre de valves : " + str(data[0]) + " ; Coût (pour le prix d'une vanne de 500€) : " + str(data[0]*500) + "€") self.label5.Update() def draw_graph(self, event): #LINES = PIPES, CIRCLE = NODES, RECTANGLE = VALVES dc = wx.PaintDC(self.panel) r=5 dc.SetPen(wx.Pen('red', 1)) dc.SetBrush(wx.Brush('yellow')) x_positions= self.x y_positions= self.y scoreTotal=0 for score in self.data: scoreTotal=scoreTotal+score if self.iterationCount!=0: dc.DrawText("Itération " + str(self.iterationCount) + " ; Score : " + str(round(scoreTotal, 3)), 5, 5) else: dc.DrawText("Qualité globale de la solution : " + str(round(scoreTotal, 3)) , 5, 5) for i, node in enumerate(self.nodes): if i==0: dc.DrawRectangle(x_positions[node]-7, y_positions[node]-7, r+10, r+10) else: dc.DrawCircle(x_positions[node], y_positions[node], r) for pipe in self.pipes: nodeA = pipe.id[0] nodeB = pipe.id[1] dc.SetPen(wx.Pen('green', 1)) dc.SetBrush(wx.Brush('blue')) dc.DrawLine(x_positions[nodeA], y_positions[nodeA], x_positions[nodeB], y_positions[nodeB]) y2 = float(y_positions[nodeB]) y1 = float(y_positions[nodeA]) x2 = float(x_positions[nodeB]) x1 = float(x_positions[nodeA]) b=0 if x1==x2: dist=0 signe=1 if y1>y2: dist=y1-y2 signe=-1 else: dist=y2-y1 if pipe.valveAtStart: dc.SetPen(wx.Pen('blue', 1)) dc.SetBrush(wx.Brush('blue')) rect = wx.Rect(x1, y1+(dist*0.2*signe), 6, 6) dc.DrawRoundedRectangleRect(rect, 1) if pipe.valveAtEnd : dc.SetPen(wx.Pen('blue', 1)) dc.SetBrush(wx.Brush('blue')) #rect = wx.Rect(x2 + (-signe*15) , a*(x2-signe*15)+b, 6, 6) rect = wx.Rect(x2, y2+(dist*0.2*(-signe)), 6, 6) dc.DrawRoundedRectangleRect(rect, 1) if x1!=x2: b = (y2-(y1*x2/x1))/(1-(x2/x1)) a = (y1-b)/x1 pixel = 15 if abs(a)>4: pixel=2 signe = -1 if x1<x2: signe = 1 if pipe.valveAtStart: dc.SetPen(wx.Pen('blue', 1)) dc.SetBrush(wx.Brush('blue')) #rect = wx.Rect(x1+(signe*15), a*(x1+signe*15)+b, 6, 6) rect = wx.Rect(x1+(signe*pixel), a*(x1+signe*pixel)+b, 6, 6) dc.DrawRoundedRectangleRect(rect, 1) if pipe.valveAtEnd : dc.SetPen(wx.Pen('blue', 1)) dc.SetBrush(wx.Brush('blue')) #rect = wx.Rect(x2 + (-signe*15) , a*(x2-signe*15)+b, 6, 6) rect = wx.Rect(x2 + (-signe*pixel) , a*(x2-signe*pixel)+b, 6, 6) dc.DrawRoundedRectangleRect(rect, 1) for node in self.nodes: text = node.id
def updateGraph(self): self.network = GlobalNetwork("randomNetwork", '../../input/randomNetwork.dot') self.updateVar() self.panel.Refresh()