def solve(self, problem, all_solutions=False): self.reset() self.problem = problem # Generate the initial (root) node node_factory = NodeFactory(False, True) self.max_frontier_node_count = 0 node = node_factory.make_node(problem.initial_state) # For efficiency, check if node is goal state BEFORE putting on Q if problem.is_goal(node.state): self.solution = node self.total_node_count = 1 if all_solutions == False: #added return node else: self.problem.pretty_print(self.solution.state) counter = counter + 1 # Start the frontier Q by adding the root frontier = deque() frontier.append(node) loops = 0 # Search tree til nothing left to explore (i.e. frontier is empty) # OR a solution is found while len(frontier) > 0: node = frontier.popleft() #POTENTIAL IMPROVEMENT: Use generator for child in node_factory.expand(node, problem): if problem.is_goal(child.state): if self.verbose: print("Max Frontier Count: ", self.max_frontier_node_count) self.solution = child # added for all_solutions being true, print after one is found. If all_solutions # is false, just return child if all_solutions == True: self.problem.pretty_print(self.solution.state) else: self.total_node_count = node_factory.node_count return child frontier.append(child) if len(frontier) > self.max_frontier_node_count: self.max_frontier_node_count = len(frontier) # added for all_solutions being true, will return the last child, however, in execute # a case for this is changed so duplicate solutions won't be printed. if all_solutions == True: self.total_node_count = node_factory.node_count return child self.solution = None self.total_node_count = node_factory.node_count return None
def solve(self, problem, path=True, all_solutions=False): self.reset() self.problem = problem # Generate the initial (root) node node_factory = NodeFactory(verbose=self.verbose, record_parent=path) self.max_frontier_node_count = 0 node = node_factory.make_node(problem.initial_state) # For efficiency, check if node is goal state BEFORE putting on Q if problem.is_goal(node.state): self.solution.append(node) self.total_node_count = 1 if not all_solutions: return self.solution # Start the frontier Q by adding the root frontier = deque() frontier.append(node) self.visited.append(node.state) # Select a node from the frontier (using the til nothing left to explore (i.e. frontier is empty) # OR a solution is found while len(frontier) > 0: #print(frontier) # vvvvvvvvvvvvvvvvvvvvvvvvv add code block for if and elif: #added #If BFS, pop front because it is a queue. #If DFS, pop back because it is a stack. if self.strategy == "BFS": node = frontier.popleft() elif self.strategy == "DFS": node = frontier.pop() for child in self.valid_children(node, problem, node_factory): if child.depth > self.max_depth: self.max_depth = child.depth if problem.is_goal(child.state): if self.verbose: print("Max Frontier Count: ", self.max_frontier_node_count) self.solution.append(child) self.total_node_count = node_factory.node_count if not all_solutions: return child frontier.append(child) if len(frontier) > self.max_frontier_node_count: self.max_frontier_node_count = len(frontier) self.total_node_count = node_factory.node_count if self.solution == []: self.solution = None return self.solution
def solve(self, problem): self.problem = problem # Not a great name for it, but Node is a useful structure for annealing node_factory = NodeFactory(verbose=self.verbose) node = node_factory.make_node(problem.get_initial_state()) node.value = problem.apply_objective_function(node.state) # at each iteration, self.solution will contain the best seen so far self.solution = [node] if self.verbose: print("Initial state: ", problem.pretty_print(node)) print("Evaluation: ", node.value) if node.value == 0: self.total_node_count = 1 return self.solution while self.temperature > self.end_temp: self.steps = self.steps + 1 # get a neighbor and decide to change to that state next_node = node_factory.make_node( problem.get_random_neighbor(node.state)) next_node.value = problem.apply_objective_function(next_node.state) self.value_data.append(next_node.value) if next_node.value == 0: self.solution = [node] self.total_node_count = node_factory.node_count return self.solution if next_node.value <= node.value: node = next_node self.moves_to_better += 1 if node.value < self.solution[0].value: self.solution = [node] else: if random.uniform( 0, 1) <= self.calculate_probability(node.value - next_node.value): node = next_node self.moves_to_worse += 1 self.adjust_temperature() self.total_node_count = node_factory.node_count self.elapsed_time = self.calculate_elapsed_time() if self.verbose: print("Elapsed Time: %sms" % (str(self.elapsed_time))) return self.solution
def run(self): sync = Sync([]) nf = NodeFactory() store = LocalStoreManager() # Get data from metadata db start = self.start.value.replace('~', '!') # Both characters are allowed query = "select identifier, location, node from nodes where identifier like '%s%%' order by identifier" % start res = store.query(query) # Check through nodes under starting point for record in res: vosid = record['identifier'] location = record['location'] # Get file metadata meta = sync.getMetadata(location[7:]) # Remove file:// scheme # File existence if len(meta) == 0: print("The file %s is missing" % location) if self.fix.value: # Resolve by deleting record store.delete_node(vosid) continue node = nf.get_node(etree.fromstring(record['node'])) # File size if cfg.LENGTH not in node.properties: print("The file size is missing for: %s" % vosid) elif node.properties[cfg.LENGTH] != meta['size']: print("The sizes for %s and %s do not match" % (vosid, location)) if self.fix.value: node.properties[cfg.LENGTH] = meta['size'] store.update_node(vosid, node, vosid) # Date if relativedelta( parser.parse(node.properties[cfg.DATE]) - utc.localize(parser.parse(meta['date'])) ).seconds > 1: # Current tolerance is 1s. print( "The dates for %s and %s do not match: %s %s" % (vosid, location, node.properties[cfg.DATE], meta['date'])) if self.fix.value: node.properties[cfg.DATE] = meta['size'] store.update_node(vosid, node, vosid)
def solve(self, problem, path=True, all_solutions=False): self.reset() self.problem = problem # Generate the initial (root) node node_factory = NodeFactory(verbose=self.verbose, record_parent=path ) self.max_frontier_node_count = 0 if self.strategy == "DL_DFS": print("Depth limit: ", self.max_depth) result = self.depth_limited_search(problem, node_factory, self.max_depth) if result == 'cutoff': print("No solution found") return result if self.verbose: print("Searching nodes") node = node_factory.make_node( problem.initial_state ) # if self.strategy == "BFS" and self.tree == False: # return BFS_Graph.breadth_first_graph_search(self, problem, node_factory, all_solutions) # For efficiency, check if node is goal state BEFORE putting on Q if problem.is_goal( node.state ): self.solution.append(node) self.total_node_count = 1 if not all_solutions: return self.solution # Start the frontier Q by adding the root frontier=deque() frontier.append(node) self.visited.append(node.state) # Select a node from the frontier (using the til nothing left to explore (i.e. frontier is empty) # OR a solution is found while len(frontier) > 0: #print(frontier) # vvvvvvvvvvvvvvvvvvvvvvvvv add code block for if and elif: if self.strategy=="BFS": node = frontier.popleft() elif self.strategy=="DFS": node = frontier.pop() for child in self.valid_children(node, problem, node_factory): if child.depth > self.max_depth: self.max_depth = child.depth if problem.is_goal( child.state ): if self.verbose: print("") print("") print("Max Frontier Count: ", self.max_frontier_node_count) print("Visited: ", len(self.visited)) self.solution.append(child) self.total_node_count = node_factory.node_count if not all_solutions: return child frontier.append(child) if len(frontier) > self.max_frontier_node_count: self.max_frontier_node_count = len(frontier) self.total_node_count = node_factory.node_count if self.solution==[]: self.solution = None return self.solution
def solve(self, problem, path=True, all_solutions=False): self.reset() self.problem = problem # Generate the initial (root) node node_factory = NodeFactory(verbose=self.verbose, record_parent=path) self.max_frontier_node_count = 0 node = node_factory.make_node(problem.initial_state) # For efficiency, check if node is goal state BEFORE putting on Q if problem.is_goal(node.state): self.solution.append(node) self.total_node_count = 1 if not all_solutions: return self.solution # Start the frontier Q by adding the root frontier = deque() frontier.append(node) # if self.dupstrat == "simple_list": self.visited.append(node.state) if self.dupstrat == "advanced_list": self.visited.append(node) # Select a node from the frontier (using the til nothing left to explore (i.e. frontier is empty) # OR a solution is found while len(frontier) > 0: while len(frontier) > 0: self.steps = self.steps + 1 #print(self.max_depth) #print(frontier) # vvvvvvvvvvvvvvvvvvvvvvvvv add code block for if and elif: #--------------------------------------------------------------------------------------- if self.strategy == "BFS": node = frontier.popleft() elif self.strategy == "DFS": node = frontier.pop() elif self.strategy == "IDDFS": node = frontier.pop() for child in self.valid_children(node, problem, node_factory): if self.strategy == "IDDFS": if child.depth > self.max_depth: self.max_depth = child.depth if problem.is_goal(child.state): if self.verbose: print("Max Frontier Count: ", self.max_frontier_node_count) self.solution.append(child) self.total_node_count = node_factory.node_count if not all_solutions: return child if child.depth <= self.id_depth: frontier.append(child) if len(frontier) > self.max_frontier_node_count: self.max_frontier_node_count = len(frontier) else: if child.depth > self.max_depth: self.max_depth = child.depth if problem.is_goal(child.state): if self.verbose: print("Max Frontier Count: ", self.max_frontier_node_count) self.solution.append(child) self.total_node_count = node_factory.node_count if not all_solutions: return child frontier.append(child) if len(frontier) > self.max_frontier_node_count: self.max_frontier_node_count = len(frontier) if self.strategy == "IDDFS" and self.id_depth < self.max_id_depth: self.id_depth = self.id_depth + 1 #print("Inc depth limit: ",self.id_depth) node = node_factory.make_node(problem.initial_state) frontier.append(node) self.visited = [] else: self.total_node_count = node_factory.node_count if self.solution == []: self.solution = None return self.solution
#Import python ledger object, data type to be updated to allow easier modifictaion ledger = pickle.load(open(ledger_dir, "rb")) #Import secret key seed = pickle.load(open(seed_dir, "rb")) signing_key = nacl.signing.SigningKey(seed.encode("ascii")) verify_key = signing_key.verify_key pubkey = verify_key.encode(encoder=nacl.encoding.HexEncoder) print(myIP) print(pubkey) #Enter address for node block rewards my_address = pubkey factory = NodeFactory(reactor, ledger, my_address, signing_key, PEER_PORT, "myIP", ns) reactor.callLater(5, factory.startPOW) stdio.StandardIO(factory.buildCommandProtocol()) if args.peer: reactor.connectTCP(BOOTSTRAP_ADDRESS, PEER_PORT, factory) # def maintainPeerList(factory): # """ Looping call function for maintaing a list of peers """ # if factory.peerListSize() < ns.PEER_LIST_SIZE: # factory.requestPeers() # print("maintain") # lc = LoopingCall(maintainPeerList, factory) # # reactor.callLater(5, lc)