def _copy_initial_state_into_model(options:Options, 
                                   current_state:SimulationState, 
                                   md:EgretModel):
    for g, g_dict in md.elements('generator', generator_type='thermal'):
        g_dict['initial_status'] = current_state.get_initial_generator_state(g)
        g_dict['initial_p_output']  = current_state.get_initial_power_generated(g)
    for s,s_dict in md.elements('storage'):
        s_dict['initial_state_of_charge'] = current_state.get_initial_state_of_charge(s)
def create_deterministic_ruc(options,
                             data_provider:DataProvider,
                             this_date, 
                             this_hour,
                             current_state:SimulationState,
                             ruc_horizon,
                             use_next_day_in_ruc):

    ruc_every_hours = options.ruc_every_hours

    start_day = this_date
    start_time = datetime.datetime.combine(start_day, datetime.time(hour=this_hour))

    # Create a new model
    md = data_provider.get_initial_forecast_model(options, ruc_horizon, 60)

    initial_ruc = current_state is None or current_state.timestep_count == 0

    # Populate the T0 data
    if initial_ruc:
        data_provider.populate_initial_state_data(options, md)
    else:
        _copy_initial_state_into_model(options, current_state, md)

    # Populate forecasts
    infer_second_day = (not use_next_day_in_ruc)
    forecast_request_count = 24 if infer_second_day else ruc_horizon 
    data_provider.populate_with_forecast_data(options, start_time, forecast_request_count, 
                                              60, md)

    # Make some near-term forecasts more accurate
    ruc_delay = -(options.ruc_execution_hour%(-options.ruc_every_hours))
    if options.ruc_prescience_hour > ruc_delay + 1:
        improved_hour_count = options.ruc_prescience_hour - ruc_delay - 1
        for forecastable, forecast in get_forecastables(md):
            actuals = current_state.get_future_actuals(forecastable)
            for t in range(0, improved_hour_count):
                forecast_portion = (ruc_delay+t)/options.ruc_prescience_hour
                actuals_portion = 1-forecast_portion
                forecast[t] = forecast_portion*forecast[t] + actuals_portion*actuals[t]

    if infer_second_day:
        for infer_type, vals in get_forecastables_with_inferral_method(md):
            for t in range(24, ruc_horizon):
                if infer_type == InferralType.COPY_FIRST_DAY:
                    # Copy from first 24 to second 24
                    vals[t] = vals[t-24]
                else:
                    # Repeat the final value from day 1
                    vals[t] = vals[23]

    # Ensure the reserve requirement is satisfied
    _ensure_reserve_factor_honored(options, md, range(ruc_horizon))

    _ensure_contingencies_monitored(options, md, initial_ruc)

    return md
Beispiel #3
0
def create_deterministic_ruc(options,
                             data_provider:DataProvider,
                             this_date, 
                             this_hour,
                             current_state:SimulationState,
                             ruc_horizon,
                             use_next_day_in_ruc):

    ruc_every_hours = options.ruc_every_hours

    start_day = this_date
    start_time = datetime.datetime.combine(start_day, datetime.time(hour=this_hour))

    # Create a new model
    md = data_provider.get_initial_model(options, ruc_horizon, 60)

    # Populate the T0 data
    if current_state is None or current_state.timestep_count == 0:
        data_provider.populate_initial_state_data(options, start_day, md)
    else:
        _copy_initial_state_into_model(options, current_state, md)

    # Populate forecasts
    copy_first_day = (not use_next_day_in_ruc) and (this_hour != 0)
    forecast_request_count = 24 if copy_first_day else ruc_horizon 
    data_provider.populate_with_forecast_data(options, start_time, forecast_request_count, 
                                              60, md)

    # Make some near-term forecasts more accurate
    ruc_delay = -(options.ruc_execution_hour%(-options.ruc_every_hours))
    if options.ruc_prescience_hour > ruc_delay + 1:
        improved_hour_count = options.ruc_prescience_hour - ruc_delay - 1
        for forecast, actuals in zip(get_forecastables(md),
                                     current_state.get_future_actuals()):
            for t in range(0, improved_hour_count):
                forecast_portion = (ruc_delay+t)/options.ruc_prescience_hour
                actuals_portion = 1-forecast_portion
                forecast[t] = forecast_portion*forecast[t] + actuals_portion*actuals[t]


    # Ensure the reserve requirement is satisfied
    _ensure_reserve_factor_honored(options, md, range(forecast_request_count))

    # Copy from first 24 to second 24, if necessary
    if copy_first_day:
        for vals, in get_forecastables(md):
            for t in range(24, ruc_horizon):
                vals[t] = vals[t-24]

    return md
def create_sced_instance(data_provider:DataProvider,
                         current_state:SimulationState,
                         options,
                         sced_horizon,
                         forecast_error_method = ForecastErrorMethod.PRESCIENT
                         ):
    ''' Create a deterministic economic dispatch instance, given current forecasts and commitments.
    '''
    assert current_state is not None

    sced_md = data_provider.get_initial_actuals_model(options, sced_horizon, current_state.minutes_per_step)

    # Set initial state
    _copy_initial_state_into_model(options, current_state, sced_md)

    ################################################################################
    # initialize the demand and renewables data, based on the forecast error model #
    ################################################################################

    if forecast_error_method is ForecastErrorMethod.PRESCIENT:
        # Warning: This method can see into the future!
        for forecastable, sced_data in get_forecastables(sced_md):
            future = current_state.get_future_actuals(forecastable)
            for t in range(sced_horizon):
                sced_data[t] = future[t]

    else:  # persistent forecast error:
        # Go through each time series that can be forecasted
        for forecastable, sced_data in get_forecastables(sced_md):
            forecast = current_state.get_forecasts(forecastable)
            # the first value is, by definition, the actual.
            sced_data[0] = current_state.get_current_actuals(forecastable)

            # Find how much the first forecast was off from the actual, as a fraction of 
            # the forecast. For all subsequent times, adjust the forecast by the same fraction.
            if forecast[0] == 0.0:
                forecast_error_ratio = 0.0
            else:
                forecast_error_ratio = sced_data[0] / forecast[0]

            for t in range(1, sced_horizon):
                sced_data[t] = forecast[t] * forecast_error_ratio

    _ensure_reserve_factor_honored(options, sced_md, range(sced_horizon))
    _ensure_contingencies_monitored(options, sced_md)

    # Set generator commitments & future state
    for g, g_dict in sced_md.elements(element_type='generator', generator_type='thermal'):
        # Start by preparing an empty array of the correct size for each generator
        fixed_commitment = [None]*sced_horizon
        g_dict['fixed_commitment'] = _time_series_dict(fixed_commitment)

        # Now fill it in with data
        for t in range(sced_horizon):
            fixed_commitment[t] = current_state.get_generator_commitment(g,t)

        # Look as far into the future as we can for future startups / shutdowns
        last_commitment = fixed_commitment[-1]
        for t in range(sced_horizon, current_state.timestep_count):
            this_commitment = current_state.get_generator_commitment(g,t)
            if (this_commitment - last_commitment) > 0.5:
                # future startup
                future_status_time_steps = ( t - sced_horizon + 1 )
                break
            elif (last_commitment - this_commitment) > 0.5:
                # future shutdown
                future_status_time_steps = -( t - sced_horizon + 1 )
                break
        else: # no break
            future_status_time_steps = 0
        g_dict['future_status'] = (current_state.minutes_per_step/60.) * future_status_time_steps

    if not options.no_startup_shutdown_curves:
        minutes_per_step = current_state.minutes_per_step
        for g, g_dict in sced_md.elements(element_type='generator', generator_type='thermal'):
            if 'startup_curve' in g_dict:
                continue
            ramp_up_rate_sced = g_dict['ramp_up_60min'] * minutes_per_step/60.
            # this rarely happens, e.g., synchronous condenser
            if ramp_up_rate_sced == 0:
                continue
            if 'startup_capacity' not in g_dict:
                sced_startup_capacity = _calculate_sced_startup_shutdown_capacity_from_none(
                                            g_dict['p_min'], ramp_up_rate_sced)
            else:
                sced_startup_capacity = _calculate_sced_startup_shutdown_capacity_from_existing(
                                            g_dict['startup_capacity'], g_dict['p_min'], minutes_per_step)

            g_dict['startup_curve'] = [ sced_startup_capacity - i*ramp_up_rate_sced \
                                        for i in range(1,int(math.ceil(sced_startup_capacity/ramp_up_rate_sced))) ]

        for g, g_dict in sced_md.elements(element_type='generator', generator_type='thermal'):
            if 'shutdown_curve' in g_dict:
                continue

            ramp_down_rate_sced = g_dict['ramp_down_60min'] * minutes_per_step/60.
            # this rarely happens, e.g., synchronous condenser
            if ramp_down_rate_sced == 0:
                continue
            # compute a new shutdown curve if we go from "on" to "off"
            if g_dict['initial_status'] > 0 and g_dict['fixed_commitment']['values'][0] == 0:
                power_t0 = g_dict['initial_p_output']
                # if we end up using a historical curve, it's important
                # for the time-horizons to match, particularly since this
                # function is also used to create long-horizon look-ahead
                # SCEDs for the unit commitment process
                create_sced_instance.shutdown_curves[g, minutes_per_step] = \
                        [ power_t0 - i*ramp_down_rate_sced for i in range(1,int(math.ceil(power_t0/ramp_down_rate_sced))) ]

            if (g,minutes_per_step) in create_sced_instance.shutdown_curves:
                g_dict['shutdown_curve'] = create_sced_instance.shutdown_curves[g,minutes_per_step]
            else:
                if 'shutdown_capacity' not in g_dict:
                    sced_shutdown_capacity = _calculate_sced_startup_shutdown_capacity_from_none(
                                                g_dict['p_min'], ramp_down_rate_sced)
                else:
                    sced_shutdown_capacity = _calculate_sced_startup_shutdown_capacity_from_existing(
                                                g_dict['shutdown_capacity'], g_dict['p_min'], minutes_per_step)

                g_dict['shutdown_curve'] = [ sced_shutdown_capacity - i*ramp_down_rate_sced \
                                             for i in range(1,int(math.ceil(sced_shutdown_capacity/ramp_down_rate_sced))) ]

    if not options.enforce_sced_shutdown_ramprate:
        for g, g_dict in sced_md.elements(element_type='generator', generator_type='thermal'):
            # make sure the generator can immediately turn off
            g_dict['shutdown_capacity'] = max(g_dict['shutdown_capacity'], (60./current_state.minutes_per_step)*g_dict['initial_p_output'] + 1.)

    return sced_md
Beispiel #5
0
def create_sced_instance(data_provider:DataProvider,
                         current_state:SimulationState,
                         options,
                         sced_horizon,
                         forecast_error_method = ForecastErrorMethod.PRESCIENT
                         ):
    ''' Create an hourly deterministic economic dispatch instance, given current forecasts and commitments.
    '''
    assert current_state != None

    sced_md = data_provider.get_initial_model(options, sced_horizon, current_state.minutes_per_step)

    # Set initial state
    _copy_initial_state_into_model(options, current_state, sced_md)

    ################################################################################
    # initialize the demand and renewables data, based on the forecast error model #
    ################################################################################

    if forecast_error_method is ForecastErrorMethod.PRESCIENT:
        # Warning: This method can see into the future!
        future_actuals = current_state.get_future_actuals()
        sced_forecastables, = get_forecastables(sced_md)
        for future,sced_data in zip(future_actuals, sced_actuals):
            for t in range(sced_horizon):
                sced_data[t] = future[t]

    else:  # persistent forecast error:
        current_actuals = current_state.get_current_actuals()
        forecasts = current_state.get_forecasts()
        sced_forecastables = get_forecastables(sced_md)
        # Go through each time series that can be forecasted
        for current_actual, forecast, (sced_data,) in zip(current_actuals, forecasts, sced_forecastables):
            # the first value is, by definition, the actual.
            sced_data[0] = current_actual

            # Find how much the first forecast was off from the actual, as a fraction of 
            # the forecast. For all subsequent times, adjust the forecast by the same fraction.
            current_forecast = forecast[0]
            if current_forecast == 0.0:
                forecast_error_ratio = 0.0
            else:
                forecast_error_ratio = current_actual / forecast[0]

            for t in range(1, sced_horizon):
                sced_data[t] = forecast[t] * forecast_error_ratio

    _ensure_reserve_factor_honored(options, sced_md, range(sced_horizon))

    ## TODO: propogate relax_t0_ramping_initial_day into this function
    ## if relaxing initial ramping, we need to relax it in the first SCED as well
    assert options.relax_t0_ramping_initial_day is False

    # Set generator commitments
    for g, g_dict in sced_md.elements(element_type='generator', generator_type='thermal'):
        # Start by preparing an empty array of the correct size for each generator
        fixed_commitment = [None]*sced_horizon
        g_dict['fixed_commitment'] = _time_series_dict(fixed_commitment)

        # Now fill it in with data
        for t in range(sced_horizon):
            fixed_commitment[t] = current_state.get_generator_commitment(g,t)

    return sced_md