Best Python code snippet using fMBT_python
Node.py
Source:Node.py
...17 #depending on the number of received parameters18 #if a node is received (just 1 single argument)19 if len(args) == 1: 20 self.setCost(args[0].getCost())21 self.setHeuristic(args[0].getHeuristic())22 self.setEvaluation(args[0].getEvaluation())23 self.setParent(args[0].getParent())24 self.setNextNode(args[0].getNextNode())25 self.setListSatellites(args[0].getListSatellites())26 self.setListObservations(args[0].getListObservations())27 28 #receiving 3 paremeters: parentNode, list of satellites and list of observations29 if len(args) == 3:30 self.setParent(args[0])31 self.setListSatellites(args[1])32 self.setListObservations(args[2])33 #COMPUTE HEURISTIC34 #------------------------------------------------------------------------------------------------------------------------------ 35 #method that computes the heuristic36 #it receives the final node to where to the heuristics will be calculated and 37 #the heuristic function that will be employed. There are two options: Manhattan and Hamming distance38 def computeHeuristic(self, finalNode, heuristicType):39 self.setHeuristic(0)40 if(heuristicType == "manhattan"): #Manhattan distance41 for satellite in self.__listSatellites:42 for observation in self.__listObservations:43 self.setHeuristic(abs(satellite.getPosition() - observation.getPosition()) + abs(satellite.getBand() - observation.getBand()))44 elif(heuristicType == "hamming"): #Hamming distance45 for n in range(len(finalNode.getListSatellites())):46 if (self.getListSatellites()[n] != finalNode.getListSatellites()[n]):47 self.setHeuristic(self.getHeuristic() +1 )48 49 50 51 #EQUALS52 #------------------------------------------------------------------------------------------------------------------------------ 53 #method that checks if the information from another node is equal to the one we have54 def equals (self,otherNode):55 #print("In method equals, observations: {0} {1}".format(len(self.__listObservations), range(len(self.__listObservations))))56 #checking that the lists of observations are equal, but looping through through them57 for x in range(len(self.__listObservations)):58 if (self.__listObservations[x].getIdNumber() != otherNode.getListObservations()[x].getIdNumber()59 or self.__listObservations[x].getBand() != otherNode.getListObservations()[x].getBand()60 or self.__listObservations[x].getPosition() != otherNode.getListObservations()[x].getPosition()61 or self.__listObservations[x].getMeasured() != otherNode.getListObservations()[x].getMeasured()):62 return False63 #checking that the lists of satellites are equal, but looping through through them64 #print("In method equals, satellites: {0} {1}".format(len(self.__listObservations), range(len(self.__listObservations))))65 for x in range(len(self.__listSatellites)):66 if (self.__listSatellites[x].getIdNumber() != otherNode.getListSatellites()[x].getIdNumber()67 or self.__listSatellites[x].getBand() != otherNode.getListSatellites()[x].getBand()68 or self.__listSatellites[x].getPosition() != otherNode.getListSatellites()[x].getPosition()69 or self.__listSatellites[x].getEnergy() != otherNode.getListSatellites()[x].getEnergy() ):70 return False71 return True #otherwise, they are equal, so "true" is returned 72 73 #COMPUTE EVALUATION74 #------------------------------------------------------------------------------------------------------------------------------ 75 76 #method that executes the evaluation function of the problem for the node77 def computeEvaluation(self):78 self.__evaluation = self.__cost + self.__heuristic79 80 #SETTERS81 #------------------------------------------------------------------------------------------------------------------------------ 82 83 def setCost(self, cost):84 self.__cost = cost85 def setHeuristic(self, heuristic):86 self.__heuristic = heuristic87 def setEvaluation(self, evaluation):88 self.__evaluation = evaluation89 def setParent(self, parent):90 self.__parent = parent91 def setNextNode(self, nextNodeList):92 self.__nextNodeList = nextNodeList93 def setListSatellites (self, satellites):94 self.__listSatellites = satellites95 def setListObservations (self, observations):96 self.__listObservations = observations97 98 #GETTERS99 #------------------------------------------------------------------------------------------------------------------------------ ...
Graph.py
Source:Graph.py
...5 self.name = node_name6 self.heuristic = 07 self.posX = posX8 self.posY - posY9 def setHeuristic(self, goal):10 self.heuristic = math.sqrt(math.pow((self.posX - goal.posX),2) + math.pow((self.posY - goal.posY),2))11class Edge:12 def __init__(self, node1, node2, weight):13 self.node1 = node1.name14 self.node2 = node2.name15 self.weight = weight16class Graph:17 def __init__(self):18 self.graf = {}19 self.Node = {}20 def addEdge(self, froms, goals, weight):21 if (froms not in self.graf):22 self.graf[froms] = {}23 if (goals not in self.graf):24 self.graf[goals] = {}25 self.graf[froms][goals] = weight26 self.graf[goals][froms] = weight27 def getHeuristic(self, node):28 return self.Node[node][2]29 30 def getPos(self, node):31 return self.Node[node][0], self.Node[node][1]32 33 def addNode(self, nama, posX, posY):34 if (nama in self.Node.keys()):35 return36 self.Node[nama] = [posX, posY, 0]37 def setHeuristic(self, goals):38 for item in self.Node.keys():39 self.Node[item][2] = math.sqrt(math.pow((self.Node[item][0] - self.Node[goals][0]),2) + math.pow((self.Node[item][1] - self.Node[goals][1]),2))40'''41g1 = Graph()42g1.addNode('A', 40, 23)43g1.addNode('B', 23, 44)44g1.addNode('C', 74, 55)45g1.addNode('D', 24, 45)46g1.addEdge('A', 'B', 5)47g1.addEdge('B', 'C', 3)48g1.setHeuristic('C')49a1 = Astar()...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!