def moo_optimization(locator, weather_file, gv, config): ''' This function optimizes the conversion, storage and distribution systems of a heating distribution for the case study. It requires that the energy demand, technology potential and thermal networks are simulated, as follows: - energy demand simulation: run cea/demand/demand_main.py - PV potential: run cea/technologies/solar/photovoltaic.py - PVT potential: run cea/technologies/solar/photovoltaic_thermal.py - flat plate solar collector potential: run cea/technologies/solar/solar_collector.py with config.solar.type_scpanel = 'FP' - evacuated tube solar collector potential: run cea/technologies/solar/solar_collector.py with config.solar.type_scpanel = 'ET' - waste water heat recovery: run cea/resources/sewage_heat_exchanger.py - lake water potential: run cea/resources/lake_potential.py - thermal network simulation: run cea/technologies/thermal_network/thermal_network_matrix.py if no network is currently present in the case study, consider running network_layout/main.py first - decentralized building simulation: run cea/optimization/preprocessing/decentralized_building_main.py :param locator: path to input locator :param weather_file: path to weather file :param gv: global variables class :type locator: string :type weather_file: string :type gv: class :returns: None :rtype: Nonetype ''' # read total demand file and names and number of all buildings total_demand = pd.read_csv(locator.get_total_demand()) building_names = total_demand.Name.values gv.num_tot_buildings = total_demand.Name.count() lca = lca_calculations(locator, config) prices = Prices(locator, config) # pre-process information regarding resources and technologies (they are treated before the optimization) # optimize best systems for every individual building (they will compete against a district distribution solution) print "PRE-PROCESSING" extra_costs, extra_CO2, extra_primary_energy, solar_features = preproccessing( locator, total_demand, building_names, weather_file, gv, config, prices, lca) # optimize the distribution and linearize the results(at the moment, there is only a linearization of values in Zug) print "NETWORK OPTIMIZATION" network_features = network_opt_main.network_opt_main(config, locator) # optimize conversion systems print "CONVERSION AND STORAGE OPTIMIZATION" master_main.non_dominated_sorting_genetic_algorithm( locator, building_names, extra_costs, extra_CO2, extra_primary_energy, solar_features, network_features, gv, config, prices, lca)
def moo_optimization(locator, weather_file, gv, config): ''' This function optimizes the conversion, storage and distribution systems of a heating distribution for the case study. It requires that solar technologies be calculated in advance and nodes of a distribution should have been already generated. :param locator: path to input locator :param weather_file: path to weather file :param gv: global variables class :type locator: string :type weather_file: string :type gv: class :returns: None :rtype: Nonetype ''' # read total demand file and names and number of all buildings total_demand = pd.read_csv(locator.get_total_demand()) building_names = total_demand.Name.values gv.num_tot_buildings = total_demand.Name.count() lca = lca_calculations(locator, config) prices = Prices(locator, config) # pre-process information regarding resources and technologies (they are treated before the optimization) # optimize best systems for every individual building (they will compete against a district distribution solution) print "PRE-PROCESSING" extra_costs, extra_CO2, extra_primary_energy, solarFeat = preproccessing( locator, total_demand, building_names, weather_file, gv, config, prices, lca) # optimize the distribution and linearize the results(at the moment, there is only a linearization of values in Zug) print "NETWORK OPTIMIZATION" network_features = network_opt.network_opt_main(config, locator) # optimize conversion systems print "CONVERSION AND STORAGE OPTIMIZATION" master.evolutionary_algo_main(locator, building_names, extra_costs, extra_CO2, extra_primary_energy, solarFeat, network_features, gv, config, prices, lca)
def run_as_script(scenario_path=None): import cea.globalvar import pandas as pd import cea.optimization.distribution.network_opt_main as network_opt from cea.optimization.preprocessing.preprocessing_main import preproccessing gv = cea.globalvar.GlobalVariables() if scenario_path is None: scenario_path = gv.scenario_reference locator = cea.inputlocator.InputLocator(scenario_path=scenario_path) total_demand = pd.read_csv(locator.get_total_demand()) building_names = total_demand.Name.values gv.num_tot_buildings = total_demand.Name.count() weather_file = locator.get_default_weather() extraCosts, extraCO2, extraPrim, solarFeat = preproccessing( locator, total_demand, building_names, weather_file, gv) ntwFeat = network_opt.network_opt_main() sensAnalysis(locator, extraCosts, extraCO2, extraPrim, solarFeat, ntwFeat, gen) print 'sensitivity analysis succeeded'
def moo_optimization(locator, weather_file, config): ''' This function optimizes the conversion, storage and distribution systems of a heating distribution for the case study. It requires that the energy demand, technology potential and thermal networks are simulated, as follows: - energy demand simulation: run cea/demand/demand_main.py - PV potential: run cea/technologies/solar/photovoltaic.py - PVT potential: run cea/technologies/solar/photovoltaic_thermal.py - flat plate solar collector potential: run cea/technologies/solar/solar_collector.py with config.solar.type_scpanel = 'FP' - evacuated tube solar collector potential: run cea/technologies/solar/solar_collector.py with config.solar.type_scpanel = 'ET' - waste water heat recovery: run cea/resources/sewage_heat_exchanger.py - lake water potential: run cea/resources/water_body_potential.py - thermal network simulation: run cea/technologies/thermal_network/thermal_network.py if no network is currently present in the case study, consider running network_layout/main.py first - decentralized building simulation: run cea/optimization/preprocessing/decentralized_building_main.py :param locator: path to input locator :param weather_file: path to weather file :type locator: cea.inputlocator.InputLocator :type weather_file: string :returns: None :rtype: Nonetype ''' t0 = time.perf_counter() # read total demand file and names and number of all buildings total_demand = pd.read_csv(locator.get_total_demand()) building_names_all = list( total_demand.Name.values) # needs to be a list to avoid errors # local flags if config.optimization.network_type == DH_ACRONYM: district_heating_network = True district_cooling_network = False elif config.optimization.network_type == DC_ACRONYM: district_heating_network = False district_cooling_network = True else: raise Exception("no valid values for 'network-type' input parameter") # GET NAMES_OF BUILDINGS THAT HAVE HEATING, COOLING AND ELECTRICITY LOAD SEPARATELY buildings_heating_demand = get_building_names_with_load( total_demand, load_name='QH_sys_MWhyr') buildings_cooling_demand = get_building_names_with_load( total_demand, load_name='QC_sys_MWhyr') buildings_electricity_demand = get_building_names_with_load( total_demand, load_name='E_sys_MWhyr') # pre-process information regarding resources and technologies (they are treated before the optimization) # optimize best systems for every individual building (they will compete against a district distribution solution) print("PRE-PROCESSING") weather_features, network_features, prices, lca = preproccessing( locator, total_demand, buildings_heating_demand, buildings_cooling_demand, weather_file, district_heating_network, district_cooling_network) # optimize conversion systems print("SUPPLY SYSTEMS OPTIMIZATION") master_main.non_dominated_sorting_genetic_algorithm( locator, building_names_all, district_heating_network, district_cooling_network, buildings_heating_demand, buildings_cooling_demand, buildings_electricity_demand, network_features, weather_features, config, prices, lca) t1 = time.perf_counter() print('Centralized Optimization succeeded after %s seconds' % (t1 - t0))
print ('combined euclidean distance = ' + str(combined_euclidean_distance)) print ('spread = ' + str(spread_final)) return combined_euclidean_distance, spread_final if __name__ == "__main__": config = cea.config.Configuration() gv = cea.globalvar.GlobalVariables() locator = cea.inputlocator.InputLocator(scenario=config.scenario) weather_file = config.weather total_demand = pd.read_csv(locator.get_total_demand()) building_names = total_demand.Name.values gv.num_tot_buildings = total_demand.Name.count() lca = lca_calculations(locator, config) prices = Prices(locator, config) extra_costs, extra_CO2, extra_primary_energy, solar_features = preproccessing(locator, total_demand, building_names, weather_file, gv, config, prices, lca) # optimize the distribution and linearize the results(at the moment, there is only a linearization of values in Zug) print "NETWORK OPTIMIZATION" nBuildings = len(building_names) network_features = network_opt_main.network_opt_main(config, locator) non_dominated_sorting_genetic_algorithm(locator, building_names, extra_costs, extra_CO2, extra_primary_energy, solar_features, network_features, gv, config, prices, lca)
def main(config): """ run the whole optimization routine """ gv = cea.globalvar.GlobalVariables() locator = cea.inputlocator.InputLocator(scenario=config.scenario) weather_file = config.weather try: if not demand_files_exist(config, locator): raise ValueError( "Missing demand data of the scenario. Consider running demand script first" ) if not os.path.exists(locator.get_total_demand()): raise ValueError( "Missing total demand of the scenario. Consider running demand script first" ) if not os.path.exists(locator.PV_totals()): raise ValueError( "Missing PV potential of the scenario. Consider running photovoltaic script first" ) if config.district_heating_network: if not os.path.exists(locator.PVT_totals()): raise ValueError( "Missing PVT potential of the scenario. Consider running photovoltaic-thermal script first" ) if not os.path.exists(locator.SC_totals(panel_type='FP')): raise ValueError( "Missing SC potential of panel type 'FP' of the scenario. Consider running solar-collector script first with panel_type as SC1 and t-in-SC as 75" ) if not os.path.exists(locator.SC_totals(panel_type='ET')): raise ValueError( "Missing SC potential of panel type 'ET' of the scenario. Consider running solar-collector script first with panel_type as SC2 and t-in-SC as 150" ) if not os.path.exists(locator.get_sewage_heat_potential()): raise ValueError( "Missing sewage potential of the scenario. Consider running sewage heat exchanger script first" ) if not os.path.exists( locator.get_optimization_network_edge_list_file( config.thermal_network.network_type, '')): raise ValueError( "Missing network edge list. Consider running thermal network script first" ) except ValueError as err: import sys print(err.message) sys.exit(1) # read total demand file and names and number of all buildings total_demand = pd.read_csv(locator.get_total_demand()) building_names = total_demand.Name.values gv.num_tot_buildings = total_demand.Name.count() prices = Prices(locator, config) lca = lca_calculations(locator, config) # pre-process information regarding resources and technologies (they are treated before the optimization) # optimize best systems for every individual building (they will compete against a district distribution solution) extra_costs, extra_CO2, extra_primary_energy, solarFeat = preproccessing( locator, total_demand, building_names, weather_file, gv, config, prices, lca) # optimize the distribution and linearize the results(at the moment, there is only a linearization of values in Zug) network_features = network_opt_main.network_opt_main(config, locator) ## generate individual from config # heating technologies at the centralized plant heating_block = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 90.0, 6 ] # FIXME: connect PV to config # cooling technologies at the centralized plant centralized_vcc_size = config.supply_system_simulation.centralized_vcc centralized_ach_size = config.supply_system_simulation.centralized_ach centralized_storage_size = config.supply_system_simulation.centralized_storage cooling_block = [0, 0, 1, 0.3, 1, 0.4, 1, 0.2, 6, 7] cooling_block[2:4] = [1, centralized_vcc_size ] if (centralized_vcc_size != 0) else [0, 0] cooling_block[4:6] = [1, centralized_ach_size ] if (centralized_ach_size != 0) else [0, 0] cooling_block[6:8] = [1, centralized_storage_size ] if (centralized_storage_size != 0) else [0, 0] total_demand = pd.read_csv(locator.get_total_demand()) building_names = total_demand.Name.values # read list of buildings connected to DC from config if len(config.supply_system_simulation.dc_connected_buildings) == 0: dc_connected_buildings = building_names # default, all connected else: dc_connected_buildings = config.supply_system_simulation.dc_connected_buildings # dc_connected_buildings = building_names # default, all connected # buildings connected to networks heating_network = [0] * building_names.size cooling_network = [0] * building_names.size for building in dc_connected_buildings: index = np.where(building_names == building)[0][0] cooling_network[index] = 1 individual = heating_block + cooling_block + heating_network + cooling_network # individual = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0.01,1,0.535812211,0,0,0,0,10,7,1,0,1,1,0,1,0,0,0,0,1,1,1,1,0,1,1,0,1,1] supply_calculation(individual, building_names, total_demand, locator, extra_costs, extra_CO2, extra_primary_energy, solarFeat, network_features, gv, config, prices, lca) print 'Buildings connected to thermal network:', dc_connected_buildings print 'Centralized systems:', centralized_vcc_size, 'VCC', centralized_ach_size, 'ACH', centralized_storage_size print 'Decentralized systems:', config.supply_system_simulation.decentralized_systems print 'supply calculation succeeded!'
def sensOneFactor(obj, factor_name, mini, maxi, colNumber): iniValue = getattr(obj, factor_name) index = 0 for delta in np.arange(mini, maxi + 1E-5, (maxi-mini)/step): FactorResults[index][colNumber + 0] = delta if abs(delta) > 1E-10: setattr(obj, factor_name, iniValue * (1+delta)) newpop = [] for ind in pop: newInd = toolbox.clone(ind) newpop.append(newInd) (costs, CO2, prim) = eI.evaluation_main(newInd, buildList, locator, solarFeat, ntwFeat, obj,, newInd.fitness.values = (costs, CO2, prim) index += 1 setattr(obj, factor_name, iniValue) sensOneFactor(gV, 'ELEC_PRICE', bandwidth.minElec, bandwidth.maxElec, 0) sensOneFactor(gV, 'NG_PRICE', bandwidth.minNG, bandwidth.maxNG, 2) sensOneFactor(gV, 'BG_PRICE', bandwidth.minBG, bandwidth.maxBG, 4) indexSensible = FactorResults.argmax()%(2*bandwidth.nFactors) if indexSensible == 1: mostSensitive = 'Electricity price' elif indexSensible == 3: mostSensitive = 'NG price' else: mostSensitive = 'BG price' print FactorResults print mostSensitive return mostSensitive gen = 4 def run_as_script(scenario_path=None): import cea.globalvar import pandas as pd import cea.optimization.distribution.network_opt_main as network_opt from cea.optimization.preprocessing.preprocessing_main import preproccessing gv = cea.globalvar.GlobalVariables() if scenario_path is None: scenario_path = gv.scenario_reference locator = cea.inputlocator.InputLocator(scenario_path=scenario_path) total_demand = pd.read_csv(locator.get_total_demand()) building_names = total_demand.Name.values gv.num_tot_buildings = total_demand.Name.count() weather_file = locator.get_default_weather() extraCosts, extraCO2, extraPrim, solarFeat = preproccessing(locator, total_demand, building_names, weather_file, gv) ntwFeat = network_opt.network_opt_main() sensAnalysis(locator, extraCosts, extraCO2, extraPrim, solarFeat, ntwFeat, gen) print 'sensitivity analysis succeeded' if __name__ == '__main__': run_as_script(r'C:\reference-case-zug\baseline')
def individual_evaluation(generation, level, size, variable_groups): """ :param generation: Generation of the optimization in which the individual evaluation is to be done :type generation: int :param level: Number of the uncertain scenario. For each scenario, the objectives are calculated :type level: int :param size: Total uncertain scenarios developed. See 'uncertainty.csv' :type size: int :return: Function saves the new objectives in a json file """ from cea.optimization.preprocessing.preprocessing_main import preproccessing gv = cea.globalvar.GlobalVariables() scenario_path = gv.scenario_reference locator = cea.inputlocator.InputLocator(scenario_path) config = cea.config.Configuration() weather_file = locator.get_default_weather() with open( locator.get_optimization_master_results_folder() + "\CheckPoint_" + str(generation), "rb") as fp: data = json.load(fp) pop = data['population'] ntwList = data['networkList'] # # Uncertainty Part row = [] with open(locator.get_uncertainty_results_folder() + '\uncertainty.csv') as f: reader = csv.reader(f) for i in xrange(size + 1): row.append(next(reader)) j = level + 1 for i in xrange(len(row[0]) - 1): setattr(gv, row[0][i + 1], float(row[j][i + 1])) total_demand = pd.read_csv(locator.get_total_demand()) building_names = total_demand.Name.values gv.num_tot_buildings = total_demand.Name.count() lca = lca_calculations(locator, config) prices = Prices(locator, config) extra_costs, extra_CO2, extra_primary_energy, solarFeat = preproccessing( locator, total_demand, building_names, weather_file, gv) network_features = network_opt.network_opt_main() def objective_function(ind, ind_num): (costs, CO2, prim) = evaluation.evaluation_main( ind, building_names, locator, solarFeat, network_features, gv, config, prices, lca, ind_num, generation) # print (costs, CO2, prim) return (costs, CO2, prim) fitness = [] for i in xrange(gv.initialInd): evaluation.checkNtw(pop[i], ntwList, locator, gv) fitness.append(objective_function(pop[i], i)) with open(locator.get_uncertainty_checkpoint(level), "wb") as fp: cp = dict(population=pop, uncertainty_level=level, population_fitness=fitness) json.dump(cp, fp)