def optimize_fish_game(): # For some reason this needs to be imported inside the function import borg as bg strategies = ['Previous_Prey'] parallel = 1 # 1= master-slave (parallel), 0=serial nRBF = 2 # no. of RBFs to use nIn = 1 # no. of inputs (depending on selected strategy) nVars = nIn * nRBF * 3 # no. of variables to be optimized. ( no. of inputs * no. of RBFs * [center, radius, weight]) nObjs = 5 # no. of objectives to optimize for nCnstr = 1 # no. of constraints # Set optimization settings nSeeds = 20 dVar_range = [0, 1] # define decision variable (C, R, W) range epsilons = [0.0001] * nObjs # set epsilon values for objectives NFEs = 3000 # number of function evaluations for Borg to perform runtime_freq = 500 # Interval at which to print runtime details for each random seed # If using master-slave, start MPI. Only do once. if parallel == 1: bg.Configuration.startMPI() # start parallelization with MPI for selected_strategy in strategies: for j in range(nSeeds): # Create array containing the strategy to run & the SOW parameters additional_inputs = np.append([selected_strategy], [SOW_inputs]) # Instantiate borg class borg = bg.Borg( nVars, # number of variables nObjs, # number of objectives nCnstr, # number of constraints fish_game, # function to optimize additional_inputs=additional_inputs) # Set bounds and epsilon values borg.setBounds(*[dVar_range] * nVars) borg.setEpsilons(*epsilons) # Define runtime file path for each seed: runtime_filename = os.getcwd() + '/Reoptimized/' + str( SOW) + '/runtime/' + selected_strategy + '_' + str( j + 1) + '.runtime' if parallel == 1: # Run parallel Borg result = borg.solveMPI(maxEvaluations=NFEs, runtime=runtime_filename) if parallel == 0: # Run serial Borg result = borg.solve({ "maxEvaluations": NFEs, "runtimeformat": 'borg', "frequency": runtime_freq, "runtimefile": runtime_filename }) if result: # This particular seed is now finished being run in parallel. The result will only be returned from # one node in case running Master-Slave Borg. result.display() # Create/write objective values and decision variable values to files in folder "sets", 1 file per seed. f1 = open( os.getcwd() + '/Reoptimized/' + str(SOW) + '/sets/' + selected_strategy + '_' + str(j + 1) + '.set', 'w') for solution in result: line = '' for i in range(len(solution.getVariables())): line = line + (str(solution.getVariables()[i])) + ' ' for i in range(len(solution.getObjectives())): line = line + (str(solution.getObjectives()[i])) + ' ' f1.write(line[0:-1] + '\n') f1.write("#") f1.close() # Create/write only objective values to files in folder "objs", 1 file per seed. Purpose is so that # the file can be processed in MOEAFramework, where performance metrics may be evaluated across seeds. f2 = open( os.getcwd() + '/Reoptimized/' + str(SOW) + '/objs/' + selected_strategy + '_' + str(j + 1) + '.obj', 'w') for solution in result: line = '' for i in range(len(solution.getObjectives())): line = line + (str(solution.getObjectives()[i])) + ' ' f2.write(line[0:-1] + '\n') f2.write("#") f2.close() # Create/write only objective values to files in folder "objs", 1 file per seed. Purpose is so that # the file can be processed in MOEAFramework, where performance metrics may be evaluated across seeds. f3 = open( os.getcwd() + '/Reoptimized/' + str(SOW) + '/cnstrs/' + selected_strategy + '_' + str(j + 1) + '.cnstr', 'w') for solution in result: line = '' for i in range(len(solution.getConstraints())): line = line + (str(solution.getConstraints()[i])) + ' ' f3.write(line[0:-1] + '\n') f3.write("#") f3.close() print("Seed %s complete") % j if parallel == 1: bg.Configuration.stopMPI() # stop parallel function evaluation process return None
def Optimization_DPS_Borg(file_name='PySedSim_Input_Specifications.csv'): ''' Purpose: Perform main Borg optimization loop through scenarios and seeds. Calls PySedSim for simulation. Args: file_name: Name of the top-level input file that indicates where input files are located, etc. Default is 'PySedSim_Input_Specifications.csv'. Must be the same file used for the simulation that is to be connected to the optimization. Returns: Produces pareto outputs for each seed, exported to separate files in a "sets" folder created in the model output location specified in the input file. ''' import borg as bg # Import borg wrapper from processing_reference_set import Reference_Set from processing_reference_set import Policy_Reevaluation from processing_reference_set import Scatter_Plot_Objectives # Get operator for changing directory based on operating system, then import various assumptions about the simulation # from the top level input file. os_fold = Op_Sys_Folder_Operator() [ num_scenarios, simulation_titles_list, imported_specs, main_input_files_dir, main_output_file_dir, Monte_Carlo_Parameters_File_List, external_mc_data ] = Determine_Num_Scenarios(file_name) # Loop through as many optimization scenarios as user has specified for j in range(num_scenarios): simulation_title = simulation_titles_list[j] Borg_dict = Import_Optimization_Preferences(simulation_title, imported_specs, main_input_files_dir) if Borg_dict['optimization approach'] == 'DPS': DPS_dict = direct_policy_search.Import_DPS_Preferences( simulation_title=simulation_title, imported_specs=imported_specs, main_input_files_dir=main_input_files_dir) Borg_dict['n_vars'] = DPS_dict[ 'n_vars'] # Store num decision variables in Borg_dict, copied from DPS_dict. # Populate DPS input variable ranges to specify for borg borg_variable_range = [] # Direct Policy Search Radial Basis Function Parameters: for r in range(DPS_dict['RBF Specs']['Number']): for m in range(DPS_dict['num_inputs']): borg_variable_range.append([-1, 1]) # for normalized center borg_variable_range.append([0, 1]) # for normalized radius for k in range(DPS_dict['num_reservoirs']): borg_variable_range.append( [0, 1] ) # for weights, which are normalized later on as they must sum to 1. # Populate Flushing-related input variable ranges to specify for borg try: for flush_var in DPS_dict['Flushing Optimization'][ 'Ordered input variable list']: borg_variable_range.append(DPS_dict['Flushing Optimization'] ['Input variable'][flush_var]) except KeyError: pass # Compute total number of variables, which is a combination of DPS policy decision vars and flushing decision # vars. Borg_dict['total_vars'] = DPS_dict['total_vars'] # Create directory in which to store optimization output output_location = main_output_file_dir + os_fold + simulation_title + os_fold + 'sets' if not os.path.exists(output_location): os.makedirs(output_location) # Set up interface with Borg for parallel function evaluation, if Borg Maser-Slave is being used, and if this # is the first optimization scenario being considered. Can't start MPI twice. if Borg_dict['borg_type'] == 1 and j == 0: bg.Configuration.startMPI() # start parallelization with MPI # Set up interface with Borg for optimization for j in range(Borg_dict['nSeeds']): borg = bg.Borg(Borg_dict['total_vars'], Borg_dict['n_objs'], Borg_dict['n_constrs'], PySedSim_Caller, add_pysedsim_inputs=[ Borg_dict, DPS_dict, file_name ]) # Create instance of Borg class borg.setBounds( *borg_variable_range) # Set decision variable bounds borg.setEpsilons(*Borg_dict['epsilon_list']) # Set epsilon values runtime_filename = main_output_file_dir + os_fold + simulation_title + os_fold + 'sets' + os_fold + \ 'runtime_file_seed_' + str(j+1) + '.runtime' if Borg_dict['borg_type'] == 0: # Run serial Borg if Borg_dict['runtime_preferences']['runtime_choice'] == 'Yes': result = borg.solve({ "maxEvaluations": Borg_dict['num_func_evals'], "runtimeformat": 'borg', "frequency": Borg_dict['runtime_preferences']['runtime_freq'], "runtimefile": runtime_filename }) else: result = borg.solve( {"maxEvaluations": Borg_dict['num_func_evals']}) #result = borg.solve({"maxEvaluations":Borg_dict['num_func_evals']}) # Minimum is 100 in Borg elif Borg_dict['borg_type'] == 1: # Run parallel Borg if Borg_dict['runtime_preferences']['runtime_choice'] == 'Yes': result = borg.solveMPI( maxEvaluations=Borg_dict['num_func_evals'], runtime=runtime_filename, frequency=Borg_dict['runtime_preferences'] ['runtime_freq']) else: result = borg.solveMPI( maxEvaluations=Borg_dict['num_func_evals']) if result: # This particular seed is now finished being run in parallel. The result will only be returned from # one node in case running Master-Slave Borg. result.display() # Create/write objective values and decision variable values to files in folder "sets", 1 file per seed. f = open( output_location + os_fold + 'Borg_DPS_PySedSim' + str(j + 1) + '.set', 'w') f.write('#Borg Optimization Results\n') f.write('#First ' + str(Borg_dict['total_vars']) + ' are the decision variables, ' + 'last ' + str(Borg_dict['n_objs']) + ' are the ' + 'objective values\n') f.write('#List of objective names (in order of appearance): ' + ', '.join(Borg_dict['opt_dict'] ['Objective Names Ordered List']) + '\n') for solution in result: line = '' for i in range(len(solution.getVariables())): line = line + (str(solution.getVariables()[i])) + ' ' for i in range(len(solution.getObjectives())): line = line + (str(solution.getObjectives()[i])) + ' ' f.write(line[0:-1] + '\n') f.write("#") f.close() # Create/write objective values only to files in folder "sets", 1 file per seed. Purpose is so that # the file can be processed in MOEAFramework, where performance metrics may be evaluated across seeds. f2 = open( output_location + os_fold + 'Borg_DPS_PySedSim_no_vars' + str(j + 1) + '.set', 'w') for solution in result: line = '' for i in range(len(solution.getObjectives())): line = line + (str(solution.getObjectives()[i])) + ' ' f2.write(line[0:-1] + '\n') f2.write("#") f2.close() print("Seed %s complete") % j # All seeds for this scenario are complete. Now generate reference set if user desires. if Borg_dict['ref_set_yes_no'] == 'Yes': # Ensure that this reference set processing is done only once in event this is being run in parallel. #if platform.system() == 'Linux': #[start, stop, rank] = parallelize_pysedsim(1) # Distribute re-evaluation work to processors. #if rank == 0: [Borg_dict, DPS_dict] = Reference_Set(inline_optimization=[ simulation_title, Borg_dict, DPS_dict, main_output_file_dir ]) #else: #[Borg_dict, DPS_dict] = Reference_Set(inline_optimization=[simulation_title, Borg_dict, DPS_dict,main_output_file_dir]) # Re-evaluate (simulate) all of the policies from the reference set. if Borg_dict['re_eval'] == 'Yes': if Borg_dict['ref_set_yes_no'] == 'Yes': ref_set_file_name = output_location + os_fold + Borg_dict[ 'opt_output_filename'] [ref_set_array, objective_values, dec_var_values] = Policy_Reevaluation( ref_set_file_name=ref_set_file_name, post_optimization='No', file_name=file_name, dps_dict=DPS_dict, borg_dict=Borg_dict, parallel=Borg_dict['parallel_choice'], internal_opt_call=[ Borg_dict['opt_dict']['Scenario Name'] ]) else: # Do not specify a reference set file name, as user did not generate a reference set with # optimization. Instead, just use the default reference set file name from the Policy_Reevaluation # method. [ ref_set_array, objective_values, dec_var_values, dps_dict, borg_dict ] = Policy_Reevaluation( post_optimization='No', file_name=file_name, dps_dict=DPS_dict, borg_dict=Borg_dict, parallel=Borg_dict['parallel_choice'], internal_opt_call=[Borg_dict['opt_dict']['Scenario Name']]) if Borg_dict['ref_set_plot'] == 'Yes': ref_set_pref_dict = { 'num_objs': Borg_dict['n_objs'], 'num_dec_vars': DPS_dict['n_vars'], 'invert': ['No', 'No', 'No'], 'perc_conv': ['No', 'Yes', 'No'], 'unit_conv': [.365, 1, 1] } movie_dict = {'Create Movie': 'No'} plot_dict = { 'Axis Range': [[0, 6000], [0, 100], [0, 8000]], 'plot_order': [1, 0, 2], '3d_plot': 'No' } objs_to_plot = Borg_dict['opt_dict'][ 'Objective Names Ordered List'] # Plot all objectives in optimization. if Borg_dict['re_eval'] == 'Yes': # Reference set has already been processed. Store those values in the reference set preferences dict. ref_set_pref_dict['ref_set_array'] = ref_set_array ref_set_pref_dict['objective_values'] = objective_values ref_set_pref_dict['dec_var_values'] = dec_var_values # Produce/save plots. Scatter_Plot_Objectives(ref_set_pref_dict, objs_to_plot, plot_dict=plot_dict, save_fig=output_location, movie_dict=movie_dict) else: ref_set_pref_dict[ 'ref_set_file_name'] = output_location + os_fold + Borg_dict[ 'opt_output_filename'] ref_set_pref_dict['num_dec_vars'] = DPS_dict['n_vars'] ref_set_pref_dict['num_objs'] = Borg_dict['n_objs'] # Produce/save plots. [ref_set_array, objective_values, dec_var_values ] = Scatter_Plot_Objectives(ref_set_pref_dict, objs_to_plot, plot_dict=plot_dict, save_fig=output_location, movie_dict=movie_dict) if Borg_dict['borg_type'] == 1: bg.Configuration.stopMPI() # stop parallel function evaluation process return
def main(): pam = monthly_pam.prodalloc(0) n_Seeds = 25 # Number of random seeds (Borg MOEA) n_dvars = 48 # Number of decision variables n_objs = 4 # Number of objectives n_constrs = 0 # Number of constraints # Number of total simulations to run per random seed. Each simulation may be a monte carlo. n_func_evals = 1500 runtime_freq = 50 # Interval at which to print runtime details for each random seed dvar_range = {'CWUP': [[0, 120]] * 24, 'SCH': [[0, 30]] * 24} # Borg epsilon values for each objective epsilon_list = [10000, 10, 0.01, 0.01] # fix variable bounds upto current month budget_target = pam.ampl.getParameter('budget_target') bud_fix = pam.ampl.getParameter('bud_fix') if pam.MO_OFFSET > 0: for i in range(pam.MO_OFFSET): temp = budget_target['CWUP', i + 1] dvar_range['CWUP'][i] = [temp, temp] temp = budget_target['BUDSCH', i + 1] - bud_fix[i + 1] dvar_range['SCH'][i] = [temp, temp] dvar_range = dvar_range['CWUP'] + dvar_range['SCH'] ''' Short run test ''' borg = bg.Borg(n_dvars, n_objs, n_constrs, pam.solve, bounds=dvar_range, epsilons=epsilon_list) for solution in borg.solve({'maxEvaluations': 100}): solution.display() pam.ampl.close() ''' End short run test ''' # Where to save seed and runtime files # Specify location of output files for different seeds d_out = os.path.join(os.getcwd(), 'Output') d_set = os.path.join(d_out, 'sets') # Loop through seeds, calling borg.solve (serial) or borg.solveMPI (parallel) each time for j in range(n_Seeds): # Instantiate borg class, then set bounds, epsilon values, and file output locations borg = bg.Borg(n_dvars, n_objs, n_constrs, pam.solve) borg.setBounds(*dvar_range) # Set decision variable bounds borg.setEpsilons(*epsilon_list) # Set epsilon values # Runtime file path for each seed: f_runtime = os.path.join(d_out, f'seed_{j:04d}.runtime') # Run serial Borg result = borg.solve({ "maxEvaluations": n_func_evals, "runtimeformat": 'borg', "frequency": runtime_freq, "runtimefile": f_runtime }) if result: # This particular seed is now finished being run in parallel. The result will only be returned from # one node in case running Master-Slave Borg. result.display() # Create/write objective values and decision variable values to files in folder "sets", 1 file per seed. f = open(os.path.join(d_set, f'prodalloc_{j:02d}.set'), 'w') f.write('#Borg Optimization Results\n') f.write( f'#First {n_dvars} are the decision variables, last {n_objs} are the objective values\n' ) for solution in result: line = '' for i in range(len(solution.getVariables())): line = line + (str(solution.getVariables()[i])) + ' ' for i in range(len(solution.getObjectives())): line = line + (str(solution.getObjectives()[i])) + ' ' f.write(line[0:-1] + '\n') f.write("#") f.close() # Create/write only objective values to files in folder "sets", 1 file per seed. Purpose is so that # the file can be processed in MOEAFramework, where performance metrics may be evaluated across seeds. f = open(os.path.join(d_set, f'prodalloc_no_vars {j+1:04d}.set'), 'w') for solution in result: line = '' for i in range(len(solution.getObjectives())): line += (str(solution.getObjectives()[i])) + ' ' f.write(line[0:-1] + '\n') f.write("#") f.close() print(f"Seed {j} complete.") pam.ampl.close()
for j in sorted(J): for k in sorted(K): f = ownfr[i][j] * (deploy[k]['pervious'] * (1 - impfr[i]) + deploy[k]['impervious'] * impfr[i]) upper = f * area[i] u[(i, j, k)] = upper bounds.append([0, upper]) print(bounds) # Calculate Cost Coefficients cost = {} for j in sorted(J): for k in sorted(K): costCoeff = 10**(a[j] - b[j] * log10(g[k])) cost[(j, k)] = costCoeff borg = bg.Borg(nVars, nObjs, 0, swmm) borg.setBounds(*bounds) #borg.setEpsilons(*[0.01]*nObjs) epsilon1 = 1 # for total cost (obj 1) epsilon2 = 0.1 # for annual volume reduction Mgal/year borg.setEpsilons(epsilon1, epsilon2) result = borg.solve({"maxEvaluations": maxEvaluations}) solutionDict = {} solutionNumber = 1 for solution in result: solution.display() # keep this for now just in case solutionVariableList = solution.getVariables() solutionObjectiveList = solution.getObjectives() thisSolutionList = [solutionVariableList, solutionObjectiveList] solutionNumberStr = str(solutionNumber) solutionDict[solutionNumberStr] = thisSolutionList
install_cost_final = [] # Install Cost solar_fraction_final = [] # Solar Fraction client = MongoClient() # On local client dbName = 'boston' # Database name dbCollection = str(raw_input("Please enter a unique collection name for your data: ")) # DB input for collection db = client[dbName] borg_solar_opt = db[dbCollection] borg_solar_opt.delete_many({}) print("Collection cleared") runCount = 0 bounds = [str_bounds,mod_bounds,[10,45],[0,359]] # Bounds must be set for each variable maxEvaluations = int(raw_input("Please enter the maximum number of evaluations you would like BORG to run for as an integer: ")) # Max Evals borg = bg.Borg(4, 3, 0, solar_opt) # Initatie Borg Runs (vars,objs,) borg.setBounds(*bounds) # Set Boundary Limits for Variables epsilon1 = 0.01 # for total cost (obj 1) epsilon2 = 0.01 # Installed Cost epsilon3 = 0.01 # % of Satisfied Load borg.setEpsilons(epsilon1,epsilon2,epsilon3) # Set Borg Epsilons result = borg.solve({"maxEvaluations":maxEvaluations}) # Set Solutions Variable solutionDict = {} # Dictionary Form of Solution for MongoDB Input solutionNumber = 1 # Solution Index for solution in result: solution.display() # keep this for now just in case solutionVariableList = solution.getVariables() # Solution variable list solutionDict['strings'] = solutionVariableList[0] # Strings Variable solutionDict['modules'] = solutionVariableList[1] # Module Variable solutionDict['tilt'] = solutionVariableList[2] # Tilt Variable
mod_bounds = [mod_lower_bound, mod_upper_bound] solar_opt() client = MongoClient() # On local client dbName = 'test' dbCollection = 'y16m03d28_1000' db = client[dbName] runsCollection = db[dbCollection] runCount = 0 #bounds = [str_bounds,mod_Bounds,[0,90],[0,359]] # Must be set for each variable # # maxEvaluations = 100 borg = bg.Borg(4, 4, 0, solar_opt) borg.setBounds(*bounds) epsilon1 = 0.01 # for total cost (obj 1) epsilon2 = 0.01 # Installed Cost epsilon3 = 0.01 # Payback epsilon4 = 0.01 # % of Satisfied Load borg.setEpsilons(epsilon1, epsilon2) result = borg.solve({"maxEvaluations": maxEvaluations}) solutionDict = {} solutionNumber = 1 for solution in result: solution.display() # keep this for now just in case solutionVariableList = solution.getVariables() solutionObjectiveList = solution.getObjectives() thisSolutionList = [solutionVariableList, solutionObjectiveList]
client = MongoClient() # On local client dbName = 'test' dbCollection = 'y16m03d28_1000' db = client[dbName] runsCollection = db[dbCollection] runCount = 0 db.runsCollection.delete_many({}) bounds = [str_bounds,mod_bounds,[10,45],[0,359]] # Must be set for each variable # # maxEvaluations = 100 borg = bg.Borg(4, 3, 0, solar_opt) borg.setBounds(*bounds) epsilon1 = 0.01 # for total cost (obj 1) epsilon2 = 0.01 # Installed Cost epsilon3 = 0.01 # Payback epsilon4 = 0.01 # % of Satisfied Load borg.setEpsilons(epsilon1,epsilon2,epsilon3) result = borg.solve({"maxEvaluations":maxEvaluations}) solutionDict = {} solutionNumber = 1 for solution in result: solution.display() # keep this for now just in case solutionVariableList = solution.getVariables() solutionDict['strings'] = solutionVariableList[0] solutionDict['modules'] = solutionVariableList[1]