def run_world(num_steps=number_of_steps): world = World(initialization_year=2018, market_time_splices=MARKET_TIME_SPLICES, data_folder="test_new", number_of_steps=number_of_steps, scenario_file=scenario_RL_few_agents, total_demand=5000, number_of_agents=3, client_rl=client) for i in range(num_steps): world.step()
def eval_world(individual): # time_start = time.time() # for i in range(1000): # pass # t1 = time.time() # # time_taken = t1-time_start # return [1], time_taken # return ([1]), # individual = np.array([0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) prices_individual = np.array(individual[:-3]).reshape(-1, 2).tolist() MARKET_TIME_SPLICES = 8 YEARS_TO_RUN = 18 number_of_steps = YEARS_TO_RUN * MARKET_TIME_SPLICES scenario_2018 = "{}/../run/beis_case_study/scenario/reference_scenario_2018.py".format(ROOT_DIR) world = World(initialization_year=2018, scenario_file=scenario_2018, market_time_splices=MARKET_TIME_SPLICES, data_folder="best_run_beis_comparison", number_of_steps=number_of_steps, long_term_fitting_params=prices_individual, highest_demand=63910, nuclear_subsidy=individual[-3], future_price_uncertainty_m=individual[-2], future_price_uncertainty_c=individual[-1]) time_start = time.perf_counter() timestamp_start = time.time() for _ in range(YEARS_TO_RUN): for i in range(MARKET_TIME_SPLICES): try: results_df, over_invested = world.step() except: return [[99999999], 0, 0, 0, 0] if over_invested: return [[99999999], 0, 0, 0, 0] _, cumulative_diff = get_projection_difference_sum(results_df, world.year_number) # print("cumulative diff: {}".format(cumulative_diff)) if cumulative_diff > 1.5: joined, total_difference = get_projection_difference_sum(results_df) else: pass time_end = time.perf_counter() timestamp_end = time.time() time_taken = time_end-time_start joined, total_difference = get_projection_difference_sum(results_df) print("max_demand : dif: {} :x {}".format(individual, total_difference)) # print(joined.simulated) # print("input: {} {}, returns: {}, {}, {}".format(individual[0], individual[1], [total_difference], time_taken, joined.simulated)) # print("input: {} {}, returns: {}, {}, {}".format(individual[0], individual[1], [total_difference], time_taken, timestamp_start, timestamp_end, joined.simulated)) try: return [total_difference], time_taken, timestamp_start, timestamp_end, joined.simulated except AttributeError: return [total_difference], time_taken, timestamp_start, timestamp_end, 0
def __init__(self, config): print("trying to init") self.step_number_env = 0 self.action = None self.config = config self.max_number_of_steps = config['max_number_of_steps'] self.world = World(initialization_year=2018, scenario_file=config['scenario_file'], data_folder=config['data_folder'], number_of_steps=self.max_number_of_steps) self.action_space = Box( 0.0, 250.0, shape=(1, ), dtype=np.float32) self.observation_space = Box( -10000.0, 0.0, shape=(2, ), dtype=np.float32)
def reset(self): self.step_number_env=0 self.world = World(initialization_year=2018, scenario_file=self.config['scenario_file'], data_folder=self.config['data_folder'], number_of_steps=self.max_number_of_steps) self.world.step_number -= 1 resultant_observation = self.world.step(17) mean_electricity_price = resultant_observation[0] carbon_emitted = resultant_observation[1] obs = np.array([mean_electricity_price, carbon_emitted]) # obs = -abs(mean_electricity_price)-abs(carbon_emitted) # obs = np.random.uniform(low=-100, high=1, size=(2,)) obs = np.array(obs) # return np.expand_dims(obs, axis=0) return obs
class WorldEnvironment(gym.Env): # def __init__(self, scenario_file=None, max_number_of_steps=32, data_folder="reinforcement_learning"): def __init__(self, config): print("trying to init") self.step_number_env = 0 self.action = None self.config = config self.max_number_of_steps = config['max_number_of_steps'] self.world = World(initialization_year=2018, scenario_file=config['scenario_file'], data_folder=config['data_folder'], number_of_steps=self.max_number_of_steps) self.action_space = Box( 0.0, 250.0, shape=(1, ), dtype=np.float32) self.observation_space = Box( -10000.0, 0.0, shape=(2, ), dtype=np.float32) def reset(self): self.step_number_env=0 self.world = World(initialization_year=2018, scenario_file=self.config['scenario_file'], data_folder=self.config['data_folder'], number_of_steps=self.max_number_of_steps) self.world.step_number -= 1 resultant_observation = self.world.step(17) mean_electricity_price = resultant_observation[0] carbon_emitted = resultant_observation[1] obs = np.array([mean_electricity_price, carbon_emitted]) # obs = -abs(mean_electricity_price)-abs(carbon_emitted) # obs = np.random.uniform(low=-100, high=1, size=(2,)) obs = np.array(obs) # return np.expand_dims(obs, axis=0) return obs def step(self, action): self.action = action self.step_number_env += 1 resultant_observation = self.world.step(action) mean_electricity_price = resultant_observation[0] carbon_emitted = resultant_observation[1] ob = -abs(mean_electricity_price)-abs(carbon_emitted) reward = ob done = self.world.step_number > self.max_number_of_steps print("step number: {}, action: {}, reward: {}, mean_electricity_price: {}, carbon_emitted: {}".format(self.world.step_number,action, reward, mean_electricity_price, carbon_emitted)) ob = np.array([mean_electricity_price, carbon_emitted]) return ob, reward, done, {} def render(self): return False
def run_scenario(gencos_rl_bidding, port_number): print("Running scenario with: {}".format(gencos_rl_bidding)) time.sleep(60) beis_params = [0.00121256259168, 46.850377392563864, 0.0029982421515, 28.9229765616468, 0.00106156336814, 18.370337670063762, 0.00228312539654, 0.0, 0.0024046471141100003, 34.43480109190594, 0.0, -20.88014916953091, 0.0, 8.15032953348701, 0.00200271495761, -12.546185375581802, 0.00155518243668, 39.791132970522796, 0.00027449937576, 8.42878689508516, 0.00111989525697, 19.81640207212787, 0.00224091998324, 5.26288570922149, 0.00209189353332, -5.9117317131295195, 0.00240696026847, -5.0144941135222, 0.00021183142492999999, -1.29658413335784, 0.00039441444392000004, -11.41659250225168, 0.00039441444392000004, -11.41659250225168, 120.21276910611674, 0.0, 0.00059945111227] prices_individual = np.array(beis_params[:-3]).reshape(-1, 2).tolist() MARKET_TIME_SPLICES = 8 YEARS_TO_RUN = 1 number_of_steps = YEARS_TO_RUN * MARKET_TIME_SPLICES scenario_2018 = "../scenario/reference_scenario_2018.py".format(ROOT_DIR) carbon_df = pd.read_csv('linear_data_exploded.csv'.format(ROOT_DIR)) carbon_list = carbon_df.x.tolist() # result_distributions_object = pickle.load(open( # "{}/run/market_forecasting_comparison/run/Compare_worlds/result_distributions_object.p".format(ROOT_DIR), # "rb")) # # resultant_dist = '{}' # # dist_class = eval(list(result_distributions_object[resultant_dist].fitted_param.keys())[0] + ".rvs") # dist_object = dist_class(*list(result_distributions_object[resultant_dist].fitted_param.values())[0], # size=50000).tolist() while True: world = World(carbon_price_scenario=carbon_list, initialization_year=2018, scenario_file=scenario_2018, market_time_splices=MARKET_TIME_SPLICES, data_folder="compare_ml_accuracy", number_of_steps=number_of_steps, long_term_fitting_params=prices_individual, highest_demand=63910, nuclear_subsidy=beis_params[-3], future_price_uncertainty_m=beis_params[-2], future_price_uncertainty_c=beis_params[-1], dropbox=False, gencos_rl=gencos_rl_bidding, write_data_to_file=True, rl_port_number=port_number) for _ in range(YEARS_TO_RUN): for i in range(MARKET_TIME_SPLICES): # try: if i / 8 == 0: print('end of year') world.step()
def world_eval(individual, carbon_policy): beis_params = [0.00121256259168, 46.850377392563864, 0.0029982421515, 28.9229765616468, 0.00106156336814, 18.370337670063762, 0.00228312539654, 0.0, 0.0024046471141100003, 34.43480109190594, 0.0, -20.88014916953091, 0.0, 8.15032953348701, 0.00200271495761, -12.546185375581802, 0.00155518243668, 39.791132970522796, 0.00027449937576, 8.42878689508516, 0.00111989525697, 19.81640207212787, 0.00224091998324, 5.26288570922149, 0.00209189353332, -5.9117317131295195, 0.00240696026847, -5.0144941135222, 0.00021183142492999999, -1.29658413335784, 0.00039441444392000004, -11.41659250225168, 0.00039441444392000004, -11.41659250225168, 120.21276910611674, 0.0, 0.00059945111227] # beis_params = [0.00121256259168, 46.850377392563864, 0.0029982421515, 28.9229765616468, 0.00106156336814, 18.370337670063762, 0.00228312539654, 0.0, 0.0024046471141100003, 34.43480109190594, 0.0, -20.88014916953091, 0.0, 8.15032953348701, 0.00200271495761, -12.546185375581802, 0.00155518243668, 39.791132970522796, 0.00027449937576, 8.42878689508516, 0.00111989525697, 19.81640207212787, 0.00224091998324, 5.26288570922149, 0.00209189353332, -5.9117317131295195, 0.00240696026847, -5.0144941135222, 0.00021183142492999999, -1.29658413335784, 0.00039441444392000004, -11.41659250225168, 0.0021988838824299997, 12.633572943294599, 120.21276910611674, 0.0, 0.00059945111227] prices_individual = np.array(beis_params[:-3]).reshape(-1, 2).tolist() MARKET_TIME_SPLICES = 8 YEARS_TO_RUN = 17 number_of_steps = YEARS_TO_RUN * MARKET_TIME_SPLICES scenario_2018 = "{}/../run/beis_case_study/scenario/reference_scenario_2018.py".format(ROOT_DIR) # if individual[0] == 1: # individual = [individual[1]*i + individual[2] for i in range(1, 20)] # else: # individual = [individual[3]*i ** individual[4] + individual[2] for i in range(1, 20)] individual = [individual[0]*i + individual[1] for i in range(1, 20)] print(individual) world = World(carbon_price_scenario=individual[:-1], initialization_year=2018, scenario_file=scenario_2018, market_time_splices=MARKET_TIME_SPLICES, data_folder="{}_data".format(carbon_policy), number_of_steps=number_of_steps, long_term_fitting_params=prices_individual, highest_demand=63910, nuclear_subsidy=beis_params[-3], future_price_uncertainty_m=beis_params[-2], future_price_uncertainty_c=beis_params[-1]) for _ in range(YEARS_TO_RUN): for i in range(MARKET_TIME_SPLICES): # try: if i/8 == 0: print('end of year') average_electricity_price, carbon_emitted = world.step() # except Exception as e: # print("try catch error: \n{}".format(e)) # return 889999999999999999, 889999999999999999 # if carbon_emitted is bool: if isinstance(carbon_emitted, bool): print("carbon emitted error") return 999999999999999999, 999999999999999999 if not isinstance(average_electricity_price, float): print("average_electricity_price {}, type {}".format(average_electricity_price, type(average_electricity_price))) average_electricity_price = 779999999999999999 if not isinstance(carbon_emitted, float): print("carbon_emitted {}, type {}".format(carbon_emitted, type(carbon_emitted))) carbon_emitted = 779999999999999999 print("average_electricity_price: {}, carbon_emitted: {}".format(average_electricity_price, carbon_emitted)) return average_electricity_price, carbon_emitted
def eval_world_parallel(individual): prices_individual = np.array(individual[:-3]).reshape(-1, 2).tolist() # print(prices_individual) MARKET_TIME_SPLICES = 8 YEARS_TO_RUN = 18 number_of_steps = YEARS_TO_RUN * MARKET_TIME_SPLICES scenario_2018 = "{}/../run/beis_case_study/scenario/reference_scenario_2018.py".format(ROOT_DIR) world = World(initialization_year=2018, scenario_file=scenario_2018, market_time_splices=MARKET_TIME_SPLICES, data_folder="best_run_beis_comparison", number_of_steps=number_of_steps, long_term_fitting_params=prices_individual, highest_demand=63910, nuclear_subsidy=individual[-3], future_price_uncertainty_m=individual[-2], future_price_uncertainty_c=individual[-1]) for j in range(YEARS_TO_RUN): for i in range(MARKET_TIME_SPLICES): try: # print("j:{}, i: {}".format(j, i)) results_df, over_invested = world.step() except: return 99999, 0 del world return individual, results_df
def run_scenario(ignore): print(ignore) world = World(carbon_price_scenario=individual[:-1], initialization_year=2018, scenario_file=scenario_2018, market_time_splices=MARKET_TIME_SPLICES, data_folder="UK_carbon_scenario_data", number_of_steps=number_of_steps, long_term_fitting_params=prices_individual, highest_demand=63910, nuclear_subsidy=beis_params[-3], future_price_uncertainty_m=beis_params[-2], future_price_uncertainty_c=beis_params[-1]) for _ in range(YEARS_TO_RUN): for i in range(MARKET_TIME_SPLICES): # try: if i / 8 == 0: print('end of year') average_electricity_price, carbon_emitted = world.step() # except Exception as e: # print("try catch error: \n{}".format(e)) # return 889999999999999999, 889999999999999999 # if carbon_emitted is bool: if isinstance(carbon_emitted, bool): print("carbon emitted error") # return 999999999999999999, 999999999999999999 if not isinstance(average_electricity_price, float): # print("average_electricity_price {}, type {}".format(average_electricity_price, type(average_electricity_price))) print("average_electricity_price") average_electricity_price = 779999999999999999 if not isinstance(carbon_emitted, float): print("carbon_emitted {}, type {}".format( carbon_emitted, type(carbon_emitted))) carbon_emitted = 779999999999999999 print("average_electricity_price: {}, carbon_emitted: {}".format( average_electricity_price, carbon_emitted))
def eval_world(individual): # time_start = time.time() # for i in range(1000): # pass # t1 = time.time() # # time_taken = t1-time_start # return [1], time_taken # return ([1]), # MARKET_TIME_SPLICES = 8 MARKET_TIME_SPLICES = 8 YEARS_TO_RUN = 6 number_of_steps = YEARS_TO_RUN * MARKET_TIME_SPLICES scenario_2013 = "{}/../run/validation-optimisation/scenario_file/scenario_2013.py".format( ROOT_DIR) world = World( initialization_year=2013, scenario_file=scenario_2013, market_time_splices=MARKET_TIME_SPLICES, data_folder="best_run_all_dat_yearly_time_step_retired_plants", number_of_steps=number_of_steps, fitting_params=[individual[0], individual[1]], highest_demand=63910) time_start = time.perf_counter() timestamp_start = time.time() for i in range(number_of_steps): results_df = world.step() time_end = time.perf_counter() timestamp_end = time.time() time_taken = time_end - time_start # # contributed_results = results_df.filter(regex='contributed_').tail(MARKET_TIME_SPLICES) # contributed_results *= 1/24 # # # print("contributed_results: {}".format(contributed_results)) # contributed_results = contributed_results.rename(columns={'contributed_PV': "contributed_solar"}) # cluster_size = pd.Series([22.0, 30.0, 32.0, 35.0, 43.0, 53.0, 68.0, 82.0]) # # # contributed_results['cluster_size'] = [22.0, 30.0, 32.0, 35.0, 43.0, 53.0, 68.0, 82.0] # # results_wa = contributed_results.apply(lambda x: np.average(x, weights=cluster_size.values)).to_frame() # # results_wa.index = results_wa.index.str.split("_").str[1].str.lower() # # print("results_wa: {}".format(results_wa)) # offshore = results_wa.loc["offshore"].iloc[0] # onshore = results_wa.loc["onshore"].iloc[0] # # print("offshotre: {}".format(offshore)) # # results_wa = results_wa.append(pd.DataFrame({"wind", offshore+onshore})) # results_wa.loc['wind'] = [offshore+onshore] # # print("results_wa: {}".format(results_wa)) # # electricity_mix = pd.read_csv("{}/data/processed/electricity_mix/energy_mix_historical.csv".format(ROOT_DIR)) # actual_mix_2018 = electricity_mix[electricity_mix.year == 2018] # # # actual_mix_2018 = actual_mix_2018.set_index("variable") # # print(actual_mix_2018) # # joined = actual_mix_2018[['value']].join(results_wa, how='inner') # # print("joined: \n{}".format(joined)) # # joined = joined.rename(columns={'value':'actual', 0:'simulated'}) # # joined = joined.loc[~joined.index.str.contains('biomass')] # # # print("joined: \n{}".format(joined)) # # joined['actual_perc'] = joined['actual']/joined['actual'].sum() # joined['simulated_perc'] = joined['simulated']/joined['simulated'].sum() # # # print("joined: \n{}".format(joined)) # # total_difference_col = joined['actual_perc'] - joined['simulated_perc'] # print(total_difference_col) # total_difference = total_difference_col.abs().sum() # # print("max_demand : dif: {} :x {}".format(individual, total_difference)) # # print(joined.simulated) # # print("input: {} {}, returns: {}, {}, {}".format(individual[0], individual[1], [total_difference], time_taken, joined.simulated)) # # print("input: {} {}, returns: {}, {}, {}".format(individual[0], individual[1], [total_difference], time_taken, timestamp_start, timestamp_end, joined.simulated)) # return [total_difference], time_taken, timestamp_start, timestamp_end, joined.simulated return 0, 0, 0, 0, 0
# # replace "/path/to/module/file.py" with "module/file.py" # filename = os.sep.join(frame.filename.split(os.sep)[-2:]) # print("#%s: %s:%s: %.1f KiB" # % (index, filename, frame.lineno, stat.size / 1024)) # line = linecache.getline(frame.filename, frame.lineno).strip() # if line: # print(' %s' % line) # # other = top_stats[limit:] # if other: # size = sum(stat.size for stat in other) # print("%s other: %.1f KiB" % (len(other), size / 1024)) # total = sum(stat.size for stat in top_stats) # print("Total allocated size: %.1f KiB" % (total / 1024)) # # class TestWorld: # def test_world_initialization(self): # with PyCallGraph(output=GraphvizOutput()): MARKET_TIME_SPLICES = 8 YEARS_TO_RUN = 40 number_of_steps = YEARS_TO_RUN * MARKET_TIME_SPLICES for _ in range(1000): world = World(initialization_year=2018, market_time_splices=MARKET_TIME_SPLICES, data_folder="test_new", number_of_steps=number_of_steps) for i in range(number_of_steps): world.step()
import sys # import scenario_file # sys.modules['elecsim'].scenario.scenario_data=scenario_file from elecsim.model.world import World import logging # logging.basicConfig(level=logging.INFO) if __name__ == "__main__": world = World(2018, log_level="info", market_time_splices=8, number_of_steps=8) for day in range(8): world.step()
""" File name: run_reference_scenario.py Date created: 22/08/2019 Feature: #Enter feature description here """ __author__ = "Alexander Kell" __copyright__ = "Copyright 2018, Alexander Kell" __license__ = "MIT" __email__ = "*****@*****.**" # MARKET_TIME_SPLICES = 8 MARKET_TIME_SPLICES = 8 YEARS_TO_RUN = 17 number_of_steps = YEARS_TO_RUN * MARKET_TIME_SPLICES scenario_2013 = "{}/../run/beis_case_study/scenario/reference_scenario_2018.py".format( ROOT_DIR) for _ in range(100): world = World(initialization_year=2018, scenario_file=scenario_2013, market_time_splices=MARKET_TIME_SPLICES, data_folder="best_run_beis_comparison", number_of_steps=number_of_steps, fitting_params=[0.001644, 11.04157], highest_demand=63910) for _ in range(number_of_steps): world.step()
from elecsim.constants import ROOT_DIR import logging logger = logging.getLogger(__name__) """ File name: run_2013 Date created: 09/07/2019 Feature: #Enter feature description here """ __author__ = "Alexander Kell" __copyright__ = "Copyright 2018, Alexander Kell" __license__ = "MIT" __email__ = "*****@*****.**" pd.set_option('display.max_rows', 4000) logging.basicConfig(level=logging.INFO) MARKET_TIME_SPLICES = 8 YEARS_TO_RUN = 8 number_of_steps = YEARS_TO_RUN * MARKET_TIME_SPLICES scenario_2013 = "{}/../run/validation-optimisation/scenario_file/scenario_2013.py".format(ROOT_DIR) world = World(initialization_year=2013, scenario_file=scenario_2013, market_time_splices=MARKET_TIME_SPLICES, data_folder="runs_2013", number_of_steps=number_of_steps, fitting_params=[0.002, 0]) for i in range(number_of_steps): world.step()