def propagate(self, source, dists, c_matrix): propagation = [None] * (max(source.keys()) + 1) execution_order = hoplite_utils.get_execution_order(source) for elem in execution_order: if source[elem]['op'] == 'input': propagation[elem] = dists[self.input_rvars[elem]] if source[elem]['op'] == 'const': propagation[elem] = { sympy.S(1.0): float(source[elem]['value']) } if source[elem]['op'] == 'output': propagation[elem] = propagation[source[elem]['preds'][0]] if source[elem]['op'] == 'add': propagation[elem] = pce_ops.add( propagation[source[elem]['preds'][0]], propagation[source[elem]['preds'][1]]) if source[elem]['op'] == 'sub': propagation[elem] = pce_ops.sub( propagation[source[elem]['preds'][0]], propagation[source[elem]['preds'][1]]) if source[elem]['op'] == 'mul': propagation[elem] = pce_ops.mul( propagation[source[elem]['preds'][0]], propagation[source[elem]['preds'][1]], c_matrix) if source[elem]['op'] == 'noise': propagation[elem] = pce_ops.add( propagation[source[elem]['preds'][0]], self.noise_dists[source[elem]['symbol']]) return propagation
def _run_model(self, path, domain): start_time = time.time() propagation = self.propagate( path['path']['nodes'], domain, self.clean_c_matrix) # TODO: Generate C matrixes "on the fly" clean_outputs = {x: propagation[x] for x in path['path']['outputs']} # Generate system graphs with noises # We don't want to add noises to the outputs. nodes_to_add_noise = [ x for x in path['path']['nodes'].keys() if not x in path['path']['outputs'] ] # Generate the subsets of noises that will be introduced together. if not self.partitioner == None: noise_groups = self.partitioner.get_partitions( path['path'], nodes_to_add_noise, self.partition_size) else: noise_groups = [nodes_to_add_noise] noise_propagations = [] for group in noise_groups: group_graph = copy.deepcopy(path['path']) self.add_noises_to(group, group_graph) used_inputs = [ self.input_to_rvars[i] for i in path['path']['inputs'] ] # Generate C matrix for each group of random variables. noised_c_matrix = c_matrix.c_matrix( used_inputs + [ self.noise_equivs['n_' + str(v)][0] for v in group if 'n_' + str(v) in self.noise_equivs ], self.order) # Propagate the PCE coefficients through the system. iter_coeffs = dict(domain.items() + self.noise_dists.items()) noise_propagations.append( self.propagate(group_graph['nodes'], iter_coeffs, noised_c_matrix)) # Update the expectances with the results from this iteration. for x in noised_c_matrix.expectances: self.noised_expectances[x] = noised_c_matrix.expectances[x] noised_outputs = {} for output in path['path']['outputs']: output_result = {} for result in noise_propagations: noised_output = result[output] output_result = pce_ops.add( output_result, pce_ops.sub(noised_output, propagation[output])) noised_outputs[output] = pce_ops.add(output_result, propagation[output]) exeution_time = time.time() - start_time print("Propagating generic domain: %s seconds" % exeution_time) return (clean_outputs, noised_outputs)
def propagate(self, source, c_matrix): nodes = source['nodes'] propagation = [None] * (max(source['nodes'].keys()) + 1) execution_order = hoplite_utils.get_execution_order(nodes) for elem in execution_order: if nodes[elem]['op'] == 'input': propagation[elem] = self.input_dists[self.input_rvars[elem]] if nodes[elem]['op'] == 'const': propagation[elem] = {sympy.S(1.0): float(nodes[elem]['value'])} if nodes[elem]['op'] == 'output': propagation[elem] = propagation[nodes[elem]['preds'][0]] if nodes[elem]['op'] == 'add': propagation[elem] = pce_ops.add( propagation[nodes[elem]['preds'][0]], propagation[nodes[elem]['preds'][1]]) if nodes[elem]['op'] == 'sub': propagation[elem] = pce_ops.sub( propagation[nodes[elem]['preds'][0]], propagation[nodes[elem]['preds'][1]]) if nodes[elem]['op'] == 'mul': propagation[elem] = pce_ops.mul( propagation[nodes[elem]['preds'][0]], propagation[nodes[elem]['preds'][1]], c_matrix) if nodes[elem]['op'] == 'noise': propagation[elem] = pce_ops.add( propagation[nodes[elem]['preds'][0]], self.noise_dists[nodes[elem]['symbol']]) if nodes[elem]['op'] == 'div': print nodes[elem] print propagation[nodes[elem]['preds'][0]] print propagation[nodes[elem]['preds'][1]] sys.exit() return propagation
def compute(self): # # Generate C matrix for clean system. # clean_c_matrix = c_matrix.c_matrix(self.input_rvars, self.order) # # Propagate signal values. # self.signal_propagation = self.propagate(self.clean_source, clean_c_matrix) # Generate system graphs with noises # We don't want to add noises to the outputs. nodes_to_add_noise = [ x for x in self.clean_source['nodes'].keys() if not x in self.clean_source['outputs'] and not self.clean_source['nodes'][x]['op'] == 'const' ] # Generate the subsets of noises that will be introduced together. if not self.partitioner == None: noise_groups = self.partitioner.get_partitions( self.clean_source, nodes_to_add_noise, self.partition_size) else: noise_groups = [nodes_to_add_noise] print noise_groups sys.exit() noise_propagations = [] for group in noise_groups: group_graph = copy.deepcopy(self.clean_source) self.add_noises_to(group, group_graph) # Generate C matrix for each group of random variables. noised_c_matrix = c_matrix.c_matrix( self.input_rvars + [self.noise_equivs['n_' + str(v)][0] for v in group], self.order) # Propagate the PCE coefficients through the system. noise_propagations.append( self.propagate(group_graph, noised_c_matrix)) print len(noise_propagations) for output in self.clean_source['outputs']: output_result = {} for result in noise_propagations: noised_output = result[output] output_result = pce_ops.add( output_result, pce_ops.sub(noised_output, self.signal_propagation[output])) print '-----:', noised_output self.noised_outputs[output] = pce_ops.add( output_result, self.signal_propagation[output]) self.noise_inputs = sorted(self.noise_inputs, key=lambda x: int(x.split('_')[1])) self.noise_rvars = [self.noise_equivs[x][0] for x in self.noise_inputs] self.noise_wlvars = [ self.noise_equivs[x][1] for x in self.noise_inputs ] self.num_noises = len(self.noise_rvars) self.computed = True for o in self.noised_outputs: print o, ':', self.noised_outputs[o]
def apply_model(self, path, domain, j_k=1.0): # Generate system graphs with noises # We don't want to add noises to the outputs. nodes_to_add_noise = [ x for x in path['path']['nodes'].keys() if not x in path['path']['outputs'] ] # Generate the subsets of noises that will be introduced together. if not self.partitioner == None: noise_groups = self.partitioner.get_partitions( path['path'], nodes_to_add_noise, self.partition_size) else: noise_groups = [nodes_to_add_noise] print 'Noise groups:', noise_groups # Partition the current path with ME-gPC and work with the # returned list. me_gpc_partitions = self.get_me_gpc_partitions(path, domain) print len(me_gpc_partitions ), "ME-gPC partitions found in this execution path." # Solve the system for each partition. outputs = [] for partition in me_gpc_partitions: print 'Studying ME-gPC partition', len(outputs) + 1, 'out of', len( me_gpc_partitions) clean_outputs = { x: partition['propagation'][x] for x in path['path']['outputs'] } noise_propagations = [] for group in noise_groups: group_graph = copy.deepcopy(path['path']) self.add_noises_to(group, group_graph) used_inputs = [ self.input_to_rvars[i] for i in path['path']['inputs'] ] # Generate C matrix for each group of random variables. noised_c_matrix = c_matrix.c_matrix( used_inputs + [self.noise_equivs['n_' + str(v)][0] for v in group], self.order) # Propagate the PCE coefficients through the system. iter_coeffs = dict(partition['distributions'].items() + self.noise_dists.items()) noise_propagations.append( self.propagate(group_graph['nodes'], iter_coeffs, noised_c_matrix)) # Update the expectances with the results from this iteration. for x in noised_c_matrix.expectances: self.noised_expectances[x] = noised_c_matrix.expectances[x] noised_outputs = {} for output in path['path']['outputs']: output_result = {} for result in noise_propagations: noised_output = result[output] output_result = pce_ops.add( output_result, pce_ops.sub(noised_output, partition['propagation'][output])) noised_outputs[output] = pce_ops.add( output_result, partition['propagation'][output]) outputs.append({ 'clean_outputs': clean_outputs, 'noised_outputs': noised_outputs, 'j_k': partition['j_k'] }) return outputs
def apply_model(self, path, domain, in_i, in_j, j_k=1.0): self.mutex.acquire() print 'The propagated values are, indeed, i: {}, j: {}.'.format( str(in_i), str(in_j)) local_path = copy.deepcopy(path) local_domain = copy.deepcopy(domain) self.mutex.release() # Generate system graphs with noises # We don't want to add noises to the outputs. nodes_to_add_noise = [ x for x in local_path['path']['nodes'].keys() if not x in local_path['path']['outputs'] ] # Generate the subsets of noises that will be introduced together. if not self.partitioner == None: noise_groups = self.partitioner.get_partitions( local_path['path'], nodes_to_add_noise, self.partition_size) else: noise_groups = [nodes_to_add_noise] # Partition the current path with ME-gPC and work with the # returned list. me_gpc_partitions = self.get_me_gpc_partitions(local_path, local_domain) self.mutex.acquire() print len(me_gpc_partitions ), "ME-gPC partitions found in execution path {}.".format( str(in_i)) self.mutex.release() # Solve the system for each partition. outputs = [] for partition in me_gpc_partitions: self.mutex.acquire() print 'Studying ME-gPC partition', str( me_gpc_partitions.index(partition) + 1), 'out of', len(me_gpc_partitions) self.mutex.release() clean_outputs = { x: partition['propagation'][x] for x in local_path['path']['outputs'] } noise_propagations = [] for group in noise_groups: self.mutex.acquire() print len( me_gpc_partitions ), "Execution path {}, subdomain {} - Studying group {} of {}.".format( str(in_i), str(in_j), str(noise_groups.index(group) + 1), str(len(noise_groups))) self.mutex.release() group_graph = copy.deepcopy(local_path['path']) self.add_noises_to(group, group_graph) used_inputs = [ self.input_to_rvars[i] for i in local_path['path']['inputs'] ] # Generate C matrix for each group of random variables. noised_c_matrix = c_matrix.c_matrix( used_inputs + [self.noise_equivs['n_' + str(v)][0] for v in group], self.order) # Propagate the PCE coefficients through the system. iter_coeffs = dict(partition['distributions'].items() + self.noise_dists.items()) noise_propagations.append( self.propagate(group_graph['nodes'], iter_coeffs, noised_c_matrix)) # Update the expectances with the results from this iteration. for x in noised_c_matrix.expectances: self.mutex.acquire() self.noised_expectances[x] = noised_c_matrix.expectances[x] self.mutex.release() noised_outputs = {} for output in local_path['path']['outputs']: output_result = {} for result in noise_propagations: noised_output = result[output] output_result = pce_ops.add( output_result, pce_ops.sub(noised_output, partition['propagation'][output])) noised_outputs[output] = pce_ops.add( output_result, partition['propagation'][output]) outputs.append({ 'clean_outputs': clean_outputs, 'noised_outputs': noised_outputs, 'j_k': partition['j_k'] }) k = 1 for solution in outputs: pickle_file = os.path.join( self.dump_files, 'solution_{}_{}_{}'.format(str(in_i), str(j), str(k))) with open(pickle_file, 'w') as sol: pickle.dump(solution, sol) self.mutex.acquire() self.solution_files.update({(i, j, k): pickle_file}) self.mutex.release() k += 1