def prediction_model(self): if self.PREDICTION_MODEL: with open(self.PREDICTION_MODEL, "r") as inf: self.assignments = np.fromiter(inf.readlines(), dtype=np.int32) else: if self.graph_modification_functions: self.G = self._edge_expansion(self.G) self.assignments = self.prediction_model_algorithm.generate_prediction_model( self.G, self.num_iterations, self.num_partitions, self.assignments, self.fixed) if self.verbose > 0: print("PREDICTION MODEL") print("----------------\n") run_metrics = [self._print_score()] self._print_assignments() if self.verbose > 0: nodes_fixed = len([o for o in self.fixed if o == 1]) print("\nFixed: {}".format(nodes_fixed)) utils.print_partitions(self.G, self.assignments, self.num_partitions) if self.use_virtual_nodes: self.init_virtual_nodes() return run_metrics
def batch_arrival(self): if self.verbose > 0: print("Assigning in batches of {}".format(self.restream_batches)) print("--------------------------------\n") if self.PARTITIONER_ALGORITHM == 'SCOTCH': # update SCOTCH Strategy to 'balance' partition weights, rather than default 'quality' strategy self.partition_algorithm.partitionStrategy = 'balance' batch_arrived = [] run_metrics = [] for i, a in enumerate(self.arrival_order): # check if node is already arrived if self.fixed[a] == 1: continue # GRAPH MODIFICATION FUNCTIONS if self.graph_modification_functions: # remove nodes that don't need a shelter if self.simulated_arrival_list[a] == 0: self.G.remove_node(a) continue # set 100% node weight for those that need a shelter if self.alter_arrived_node_weight_to_100: self.G.node[a]['weight'] = 100 # skip nodes that don't need a shelter if self.simulated_arrival_list[a] == 0: continue batch_arrived.append(a) # batch processing if self.restream_batches == len(batch_arrived): run_metrics += self.process_batch(batch_arrived) if not self.sliding_window: batch_arrived = [] # process remaining nodes in incomplete batch run_metrics += self.process_batch(batch_arrived, assign_all=True) # remove nodes not fixed for i in range(0, len(self.assignments)): if self.fixed[i] == -1: self.assignments[i] = -1 if self.verbose > 0: self._print_assignments() nodes_fixed = len([o for o in self.fixed if o == 1]) print("\nFixed: {}".format(nodes_fixed)) utils.print_partitions(self.G, self.assignments, self.num_partitions) return run_metrics
def assign_cut_off(self): # stop assignments when x% of arriving people is assinged cut_off_value = int(self.prediction_model_cut_off * self.number_simulated_arrivals) # @deprecated cut_off_value when x% of whole population arrives #cut_off_value = int(self.prediction_model_cut_off * self.G.number_of_nodes()) if self.verbose > 0: if self.prediction_model_cut_off == 0: print("Discarding prediction model\n") else: print( "Assign first {} arrivals using prediction model, then discard\n" .format(cut_off_value)) # fix arrivals for a in self.arrival_order: # check if node needs a shelter if self.simulated_arrival_list[a] == 0: continue # set 100% node weight for those that need a shelter if self.graph_modification_functions and self.alter_arrived_node_weight_to_100: self.G.node[a]['weight'] = 100 nodes_fixed = len([o for o in self.fixed if o == 1]) if nodes_fixed >= cut_off_value: break self.fixed[a] = 1 self.nodes_arrived.append(a) # remove nodes not fixed, ie. discard prediction model for i in range(0, len(self.assignments)): if self.fixed[i] == -1: self.assignments[i] = -1 GSub = self.G.subgraph(self.nodes_arrived) run_metrics = [self._print_score(GSub)] self._print_assignments() if self.verbose > 0: nodes_fixed = len([o for o in self.fixed if o == 1]) print("\nFixed: {}".format(nodes_fixed)) utils.print_partitions(self.G, self.assignments, self.num_partitions) return run_metrics
def prediction_model(self): if self.PREDICTION_MODEL_ALGORITHM == 'SCOTCH': # update SCOTCH Strategy to 'quality' partition weights for prediction model self.partition_algorithm.partitionStrategy = 'quality' if self.apply_prediction_model_weights: self.apply_graph_prediction_weights() if self.PREDICTION_MODEL: with open(self.PREDICTION_MODEL, "r") as inf: self.assignments = np.fromiter(inf.readlines(), dtype=np.int32) self.assignments_prediction_model = np.array(self.assignments, copy=True) else: if self.graph_modification_functions: self.G = self._edge_expansion(self.G) self.assignments = self.prediction_model_algorithm.generate_prediction_model( self.G, self.num_iterations, self.num_partitions, self.assignments, self.fixed) self.assignments_prediction_model = np.array(self.assignments, copy=True) if self.verbose > 0: print("PREDICTION MODEL") print("----------------\n") run_metrics = [self._print_score()] self._print_assignments() if self.verbose > 0: nodes_fixed = len([o for o in self.fixed if o == 1]) print("\nFixed: {}".format(nodes_fixed)) utils.print_partitions(self.G, self.assignments, self.num_partitions) if self.use_virtual_nodes: self.init_virtual_nodes() if self.apply_prediction_model_weights: self.remove_graph_prediction_weights() if self.PREDICTION_MODEL_ALGORITHM == 'FENNEL': # store a copy of the original graph in the fennel partitioner self.partition_algorithm.original_graph = self.G return run_metrics