def busquedaheurgenetica(init, solucion): start = time.clock() root = nodo(init) root.pos = searchrobot(root.map) tree = [root] issolved = False listedtree = [] profundidad = 0 while (not issolved) and (len(tree) > 0): node = tree.pop() listedtree.append(node) if checksol(solucion, node): end = time.clock() - start print('Tiempo en usando heuristica con algoritmo genetico' + str(end)) return node else: nodohijo = childgen(node) nodohijo = setvalue(nodohijo, root) k = 100 for i in range(k): nodosel = genetico(nodohijo) if nodosel != None: for i in range(len(nodosel)): if nodosel[i] != None: if (not nodosel[i].islisted(listedtree)) and (not nodosel[i].islisted(tree)): tree.append(nodosel[i]) node.addchild(nodosel[i]) node.prof = profundidad profundidad += 1 break
def busquedalargo(init, solucion): start = time.clock() root = nodo(init) root.pos = searchrobot(root.map) tree = [root] issolved = False listedtree = [] profundidad = 0 while (not issolved) and (len(tree) > 0): node = tree.pop() listedtree.append(node) if checksol(solucion, node): end = time.clock() - start print('Tiempo en usando busqueda a lo largo' + str(end)) return node else: nodohijo = childgen(node) best_value = 1000 if nodohijo != None: for i in range(len(nodohijo)): if nodohijo[i] != None: if nodohijo[i].value < best_value: best_value = nodohijo[i].prof besti = i if (not nodohijo[besti].islisted(listedtree)) and (not nodohijo[besti].islisted(tree)): tree.append(nodohijo[besti]) node.addchild(nodohijo[besti]) node.prof = profundidad profundidad += 1
def view_tree(self): """ Info about the tree """ print("Node info for tree are:") for _ in self.tree.keys(): tmp = nodo(self.tree[_]['key'], self.tree[_]['child_left'], self.tree[_]['child_right'], self.tree[_]['parent'], self.tree[_]['label']) print('\n', tmp.get_info(), '\n') return self.tree
def __init__(self, er): #er: Expresion regular nh = numHoja(er) self.pila = [] for x in reversed(list(er)): if x == "|": izq = self.pila.pop(len(self.pila) - 1) der = self.pila.pop(len(self.pila) - 1) if (isinstance(izq,nodo) and isinstance(der,nodo)): n = nodo(x, Type.Or, 0, izq, der) self.pila.append(n) elif x == ".": izq = self.pila.pop(len(self.pila) - 1) der = self.pila.pop(len(self.pila) - 1) if (isinstance(izq,nodo) and isinstance(der,nodo)): n = nodo(x, Type.And, 0, izq, der) self.pila.append(n) elif x == "*": unario = self.pila.pop(len(self.pila) - 1) if (isinstance(unario,nodo)): n = nodo(x, Type.Kleene, 0, unario, None) self.pila.append(n) elif x == "?": unario = self.pila.pop(len(self.pila) - 1) if (isinstance(unario, nodo)): n = nodo(x, Type.Interr, 0, unario, None) self.pila.append(n) else: n = nodo(x, Type.Hoja, nh.getNum(), None, None) self.pila.append(n) Hojas.addHoja(n) # Importante agregar la hoja self.root = self.pila.pop(len(self.pila)-1)
def add_node(self, new_node_key, new_node_label): """ Adding a node in the tree, if empty the tree inserts the node as the root""" z = nodo(new_node_key, None, None, None, new_node_label) if z.key is None or z.label is None: return Exception('Chiave o label errati!') y = None x = self.tree['root'] while x != None: y = x if z.key < x['key']: x = x['child_left'] else: x = x['child_right'] z.parent_node = y if y == None: self.tree['root'] = { 'label': z.label, 'key': z.key, 'child_right': z.child_right, 'child_left': z.child_left, 'parent': z.parent_node } elif z.key < y['key']: # self.tree [z.label] to print all separate nodes; See view_tree () method self.tree[z.label] = { 'label': z.label, 'key': z.key, 'child_right': z.child_right, 'child_left': z.child_left, 'parent': z.parent_node } y['child_left'] = { 'label': z.label, 'key': z.key, 'child_right': z.child_right, 'child_left': z.child_left, 'parent': y['label'] } else: # self.tree [z.label] to print all separate nodes; See view_tree () method self.tree[z.label] = { 'label': z.label, 'key': z.key, 'child_right': z.child_right, 'child_left': z.child_left, 'parent': z.parent_node } y['child_right'] = { 'label': z.label, 'key': z.key, 'child_right': z.child_right, 'child_left': z.child_left, 'parent': y['label'] } return self.tree
def add_node_root(self, key): """ Adding the root node in the tree object """ label = 'root' node = nodo(key, None, None, None, label) self.tree[label] = { 'label': label, 'key': node.key, 'child_right': node.child_right, 'child_left': node.child_left, 'parent': node.parent_node } return self.tree
def movup(map): newmap = [x[:] for x in map] pos = searchrobot(map) node = nodo(newmap) if pos[0] - 1 >= 0: newpos = [(pos[0] - 1), (pos[1])] if (newmap[newpos[0]][newpos[1]] == 0): newmap[newpos[0]][newpos[1]] = 2 newmap[pos[0]][pos[1]] = 0 if newmap != map: node.map = newmap node.pos = newpos return node else: return None else: return None
def movdown(map): newmap = [x[:] for x in map] pos = searchrobot(map) node = nodo(newmap) if ((pos[0] + 1 )< len(map)): newpos = [(pos[0] + 1), pos[1]] if (newmap[newpos[0]][newpos[1]] == 0): newmap[newpos[0]][newpos[1]] = 2 newmap[pos[0]][pos[1]] = 0 if newmap != map: node.map = newmap node.pos = newpos return node else: return None else: return None
def movright(map): newmap = [x[:] for x in map] pos = searchrobot(map) node = nodo(newmap) if (pos[1] + 1 < len(map[0])): newpos = [pos[0], pos[1] + 1] if (newmap[newpos[0]][newpos[1]] == 0): newmap[newpos[0]][newpos[1]] = 2 newmap[pos[0]][pos[1]] = 0 if newmap != map: node.map = newmap node.pos = newpos return node else: return None else: return None
def busquedaancho(init, solucion): start = time.clock() root = nodo(init) tree = [] tree.append(root) issolved = False listedtree = [] while (not issolved) and (len(tree) > 0): node = tree.pop(0) listedtree.append(node) if checksol(solucion, node): end = time.clock() - start print('Tiempo en busqueda a lo ancho ' + str(end)) return node else: nodohijo = childgen(node) for i in range(len(nodohijo)): if nodohijo[i] is not None: if (not nodohijo[i].islisted(listedtree)) and (not nodohijo[i].islisted(tree)): tree.append(nodohijo[i]) node.addchild(nodohijo[i])