def get_dict_features_from_df_parallel(self, df, nworkers=8): print("extracting features...") df_split = np.array_split(df, nworkers) pool = Pool(nworkers) res_dicts = pool.map(self.get_dict_features_from_df, df_split) pool.close( ) # informs the processor that no new tasks will be added to the pool pool.join( ) # stops and waits for all of the results to be finished and collected before proceeding with the rest of big_dic = defaultdict(lambda: defaultdict(int)) # merge feature dictionaries created for data frame splits into one big dictionary for dic in res_dicts: for k, v in dic.items(): big_dic[k] = v return pd.concat([ pd.get_dummies(df[df.columns.difference(["event", "venue"])], prefix="@", columns=["month", "weekday"]), pd.DataFrame.from_dict(big_dic, orient='index') ], axis=1, join_axes=[df.index]).fillna(0.)
def parallelize_dataframe(self, df, func): df_split = np.array_split(df, 1) pool = Pool(1) rr = pool.map(func, df_split) df = pd.concat(rr) pool.close() pool.join() return df
def compute(self, list_of_start_vectors, parallel): sol_func = self.sol_func def function_to_dill(start_vector): return sol_func(start_vector) if parallel: pool = Pool(processes=len(list_of_start_vectors)) list_of_results = pool.map(function_to_dill, list_of_start_vectors) else: list_of_results = [ function_to_dill(sv) for sv in list_of_start_vectors ] return np.array(list_of_results)
def l_minima(self): """ Find the local minima using the chosen local minimisation method with the minimisers as starting points. """ # Sort to start with lowest minimizer Min_ind = self.minimizers(self.K_opt) Min_fun = self.F[Min_ind] fun_min_ind = numpy.argsort(Min_fun) Min_ind = Min_ind[fun_min_ind] Min_fun = Min_fun[fun_min_ind] # Init storages self.x_vals = [] self.Func_min = numpy.zeros_like(Min_ind, dtype=float) if self.maxfev is not None: # Update number of sampling points self.maxfev -= self.n # Pool processes if multiprocessing if self.multiproc: p = Pool() lres_list = p.map(self.process_pool, Min_ind) for i, ind in zip(range(len(Min_ind)), Min_ind): if not self.multiproc: if self.callback is not None: print('Callback for ' 'minimizer starting at {}:'.format(self.C[ind, :], )) if self.disp: print('Starting local ' 'minimization at {}...'.format(self.C[ind, :])) # Find minimum x vals lres = scipy.optimize.minimize(self.func, self.C[ind, :], **self.minimizer_kwargs) elif self.multiproc: lres = lres_list[i] self.x_vals.append(lres.x) self.Func_min[i] = lres.fun # Local function evals for all minimisers self.res.nlfev += lres.nfev if self.maxfev is not None: self.maxfev -= lres.nfev self.minimizer_kwargs['options']['maxfev'] = self.maxfev if self.maxfev <= 0: self.res.message = 'Maximum number of function' \ ' evaluations exceeded' self.res.success = False self.break_routine = True if self.disp: print('Maximum number of function evaluations exceeded' 'breaking' 'minimizations at {}...'.format(self.C[ind, :])) if not self.multiproc: for j in range(i + 1, len(Min_ind)): self.x_vals.append(self.C[Min_ind[j], :]) self.Func_min[j] = self.F[Min_ind[j]] if not self.multiproc: break self.x_vals = numpy.array(self.x_vals) # Sort and save ind_sorted = numpy.argsort(self.Func_min) # Sorted indexes in Func_min # Save ordered list of minima self.res.xl = self.x_vals[ind_sorted] # Ordered x vals self.res.funl = self.Func_min[ind_sorted] # Ordered fun values # Find global of all minimisers self.res.x = self.x_vals[ind_sorted[0]] # Save global minima x_global_min = self.x_vals[ind_sorted[0]][0] self.res.fun = self.Func_min[ind_sorted[0]] # Save global fun value return x_global_min
def connect(self, turbine_coordinates): from multiprocessing_on_dill import Pool #print turbine_coordinates from site_conditions.terrain.terrain_models import depth from farm_energy.wake_model_mean_new.wake_1angle import energy_one_angle from farm_energy.wake_model_mean_new.wake_1angle_turbulence import max_turbulence_one_angle # from costs.investment_costs.BOS_cost.cable_cost.Hybrid import draw_cables from farm_description import cable_list, central_platform #print "=== PREPARING WIND CONDITIONS ===" self.windrose = self.inflow_model() self.wind_directions = self.windrose.direction self.direction_probabilities = self.windrose.dir_probability if self.inflow_model == MeanWind: self.wind_speeds = self.windrose.speed self.freestream_turbulence = [0.11] self.wind_speeds_probabilities = [ [100.0] for _ in range(len(self.wind_directions)) ] # self.wind_speeds = [8.5 for _ in self.wind_speeds] elif self.inflow_model == WeibullWind: self.wind_speeds = [ range(25) for _ in range(len(self.wind_directions)) ] self.freestream_turbulence = [ 0.11 for _ in range(len(self.wind_speeds[0])) ] self.wind_speeds_probabilities = self.windrose.speed_probabilities( self.wind_speeds[0]) #print "=== CALCULATING WATER DEPTH ===" self.water_depths = depth(turbine_coordinates, self.depth_model) #print "=== OPTIMISING INFIELD CABLE TOPOLOGY (COST)===" # draw_cables(turbine_coordinates, central_platform, cable_list) self.cable_topology_costs, self.cable_topology = self.cable_topology_model( turbine_coordinates) #print str(self.cable_topology_costs) + " EUR" self.energies_per_angle = [] self.turbulences_per_angle = [] self.cable_efficiencies_per_angle = [] self.array_efficiencies = [] # #print [sum(self.wind_speeds_probabilities[i]) for i in range(len(self.wind_speeds_probabilities))] #print "=== CALCULATING ENERGY, TURBULENCE PER WIND DIRECTION ===" def angle_loop(i): self.aero_energy_one_angle, self.powers_one_angle = energy_one_angle( turbine_coordinates, self.wind_speeds[i], self.wind_speeds_probabilities[i], self.wind_directions[i], self.freestream_turbulence, self.wake_mean_model, self.power_model, self.thrust_coefficient_model, self.wake_merging_model) # #print self.aero_energy_one_angle # #print self.powers_one_angle, max(self.powers_one_angle) # #print turbine_coordinates, self.wind_speeds[i], self.windrose.direction[i], self.freestream_turbulence[0], Jensen, self.thrust_coefficient_model, self.wake_turbulence_model self.turbulences = max_turbulence_one_angle( turbine_coordinates, self.wind_speeds[i], self.windrose.direction[i], self.freestream_turbulence, Jensen, self.thrust_coefficient_model, self.wake_turbulence_model) self.cable_topology_efficiency = self.cable_efficiency_model( self.cable_topology, turbine_coordinates, self.powers_one_angle) self.energy_one_angle_weighted = self.aero_energy_one_angle * self.direction_probabilities[ i] / 100.0 self.array_efficiency = (self.aero_energy_one_angle / (float(len(turbine_coordinates)) * max(self.powers_one_angle) * 8760.0)) self.array_efficiencies_weighted = self.array_efficiency * self.direction_probabilities[ i] / 100.0 self.array_efficiencies.append(self.array_efficiencies_weighted) self.energies_per_angle.append(self.energy_one_angle_weighted) self.turbulences_per_angle.append(self.turbulences) self.cable_efficiencies_per_angle.append( self.cable_topology_efficiency) p = Pool(8) p.map(angle_loop, range(12)) # #print self.array_efficiencies #print " --- Array efficiency---" self.array_efficiency = sum(self.array_efficiencies) #print str(self.array_efficiency * 100.0) + " %\n" #print " --- Farm annual energy without losses---" self.farm_annual_energy = sum(self.energies_per_angle) #print str(self.farm_annual_energy / 1000000.0) + " MWh\n" #print " --- Infield cable system efficiency ---" self.cable_efficiency = sum(self.cable_efficiencies_per_angle) / len( self.cable_efficiencies_per_angle) #print str(self.cable_efficiency * 100.0) + " %\n" #print " --- Maximum wind turbulence intensity ---" self.turbulence = max(self.turbulences_per_angle) #print str(self.turbulence * 100.0) + " %\n" #print " --- Support structure costs ---" self.support_costs = self.support_design_model(self.water_depths, self.turbulence) #print str(self.support_costs) + " EUR\n" self.aeroloads = 0.0 self.hydroloads = 0.0 #print " --- O&M costs ---" self.om_costs, self.availability = self.OandM_model( self.farm_annual_energy, self.aeroloads, self.hydroloads, turbine_coordinates) #print str(self.om_costs * 20.0) + " EUR\n" #print " --- AEP ---" self.aep = self.aep_model(self.farm_annual_energy, self.availability, self.cable_topology_efficiency) * 20.0 #print str(self.aep / 1000000.0) + " MWh\n" #print " --- Total costs ---" self.total_costs = self.costs_model(self.cable_topology_costs, self.support_costs, self.om_costs * 20.0) #print str(self.total_costs) + " EUR\n" #print " --- COE ---" self.finance = self.finance_model(self.total_costs * 100.0, self.aep / 1000.0) print(str(self.finance) + " cents/kWh\n")
def l_minima(self): """ Find the local minima using the chosen local minimisation method with the minimisers as starting points. """ # Sort to start with lowest minimizer Min_ind = self.minimizers(self.K_opt) Min_fun = self.F[Min_ind] fun_min_ind = numpy.argsort(Min_fun) Min_ind = Min_ind[fun_min_ind] Min_fun = Min_fun[fun_min_ind] # Init storages self.x_vals = [] self.Func_min = numpy.zeros_like(Min_ind, dtype=float) if self.maxfev is not None: # Update number of sampling points self.maxfev -= self.n # Pool processes if multiprocessing if self.multiproc: p = Pool() lres_list = p.map(self.process_pool, Min_ind) for i, ind in zip(range(len(Min_ind)), Min_ind): if not self.multiproc: if self.callback is not None: print('Callback for ' 'minimizer starting at {}:'.format(self.C[ind, :], )) if self.disp: print('Starting local ' 'minimization at {}...'.format(self.C[ind, :])) # Find minimum x vals lres = scipy.optimize.minimize(self.func, self.C[ind, :], **self.minimizer_kwargs) elif self.multiproc: lres = lres_list[i] self.x_vals.append(lres.x) self.Func_min[i] = lres.fun # Local function evals for all minimisers self.res.nlfev += lres.nfev if self.maxfev is not None: self.maxfev -= lres.nfev self.minimizer_kwargs['options']['maxfev'] = self.maxfev if self.maxfev <= 0: self.res.message = 'Maximum number of function' \ ' evaluations exceeded' self.res.success = False self.break_routine = True if self.disp: print('Maximum number of function evaluations exceeded' 'breaking' 'minimizations at {}...'.format(self.C[ind, :])) if not self.multiproc: for j in range(i + 1, len(Min_ind)): self.x_vals.append(self.C[Min_ind[j], :]) self.Func_min[j] = self.F[Min_ind[j]] if not self.multiproc: break self.x_vals = numpy.array(self.x_vals) # Sort and save ind_sorted = numpy.argsort(self.Func_min) # Sorted indexes in Func_min # Save ordered list of minima self.res.xl = self.x_vals[ind_sorted] # Ordered x vals self.res.funl = self.Func_min[ind_sorted] # Ordered fun values # Find global of all minimisers self.res.x = self.x_vals[ind_sorted[0]] # Save global minima x_global_min = self.x_vals[ind_sorted[0]][0] self.res.fun = self.Func_min[ind_sorted[0]] # Save global fun value return x_global_min