Esempio n. 1
0
    def __init__(self, **kwargs):
        super(ValuationScreen, self).__init__(**kwargs)

        self.op = ValuationOptimizer()

        self.pw = ProgressWindow()
        self.pw.run_button.bind(on_release=self.execute_valuation)
Esempio n. 2
0
            def solve_pyomo_model():
                # get the current optimization solver from settings
                solver_name = App.get_running_app().config.get('optimization', 'solver')
                self.op.solver = solver_name
                self.op.solve_model()

                # post-process solve results
                anim_duration = 0.5
                self.pw.update_window('Processing solve results...\n', 100, anim_duration=anim_duration)

                sleep(anim_duration)  # let the animation play out before "finish"?

                # finish
                anim_duration = 0
                self.pw.update_window('Finished!', 100, anim_duration=anim_duration)
                self.pw.title = 'Finished!'
                self.pw.view_results_button.bind(on_release=self.activate_view_results_button)
                self.pw.view_results_button.disabled = False

                # add to finished runs
                from random import sample
                from string import ascii_letters

                load_screen = self.manager.get_screen('load_data')
                iso = load_screen.iso_select.text
                year = load_screen.year_select.text
                month = load_screen.month_select.text
                dtype = load_screen.type_select.text
                node = load_screen.node_select.text

                # random string identifier for uniqueness
                name = '-'.join([node, year, month, ''.join(sample(ascii_letters, 5))])

                self.save_run(name, iso, dtype, year, month, node)

                # create new instance of Optimizer
                self.op = ValuationOptimizer()
Esempio n. 3
0
    def process_requests(self, requests, *args):
        """Generates and solves ValuationOptimizer models based on the given requests."""
        dms = self.dms

        iso = requests['iso']
        market_type = requests['market type']
        node_id = str(requests['node id'])
        node_name = self.dms.get_node_name(node_id, iso)
        param_set = requests.get('param set', [None])

        solved_requests = []

        handler_status = True  # Set to False if any exceptions raised when building or solving ValuationOptimizer model(s).

        for month, year in requests['months']:
            param_set_iterator = iter(param_set)
            continue_param_loop = True

            while continue_param_loop:
                try:
                    params = next(param_set_iterator)
                except StopIteration:
                    break

                op = ValuationOptimizer(market_type=market_type)

                if iso == 'PJM':
                    #lmp_da, RUP, RDW, MR, RA, RD, RegCCP, RegPCP = dms.get_pjm_data(year, month, node_name)
                    lmp_da, MR, RA, RD, RegCCP, RegPCP = dms.get_pjm_data(
                        year, month, node_id)

                    op.price_electricity = lmp_da
                    op.mileage_mult = MR
                    # op.mileage_slow = RA
                    # op.mileage_fast = RD
                    op.price_regulation = RegCCP
                    op.price_reg_service = RegPCP
                    #op.fraction_reg_up = RUP
                    #op.fraction_reg_down = RDW
                elif iso == 'ERCOT':
                    lmp_da, rd, ru = dms.get_ercot_data(year, month, node_name)

                    op.price_electricity = lmp_da
                    op.price_reg_up = ru
                    op.price_reg_down = rd
                elif iso == 'MISO':
                    lmp_da, regMCP = dms.get_miso_data(year, month, node_name)

                    op.price_electricity = lmp_da
                    # op.price_reg_service = regMCP
                    op.price_regulation = regMCP
                elif iso == 'ISONE':
                    daLMP, RegCCP, RegPCP = dms.get_isone_data(
                        year, month, node_id)

                    op.price_electricity = daLMP
                    op.price_regulation = RegCCP
                    op.price_reg_service = RegPCP
                ########################################################################################################
                elif iso == 'NYISO':
                    lbmp_da, rcap_da = dms.get_nyiso_data(year, month, node_id)

                    op.price_electricity = lbmp_da
                    op.price_regulation = rcap_da
                elif iso == 'SPP':
                    lmp_da, mcpru_da, mcprd_da = dms.get_spp_data(
                        year, month, node_name)

                    op.price_electricity = lmp_da
                    op.price_reg_up = mcpru_da
                    op.price_reg_down = mcprd_da
                elif iso == 'CAISO':
                    lmp_da, aspru_da, asprd_da, asprmu_da, asprmd_da, rmu_mm, rmd_mm, rmu_pacc, rmd_pacc = dms.get_caiso_data(
                        year, month, node_name)

                    op.price_electricity = lmp_da
                    op.price_reg_up = aspru_da
                    op.price_reg_down = asprd_da
                    op.price_reg_serv_up = asprmu_da
                    op.price_reg_serv_down = asprmd_da
                    op.mileage_mult_ru = rmu_mm
                    op.mileage_mult_rd = rmd_mm
                    op.perf_score_ru = rmu_pacc  # TODO: give the option to the user to override this
                    op.perf_score_rd = rmd_pacc
                    ########################################################################################################
                else:
                    logging.error('ValOp Handler: Invalid ISO provided.')
                    raise ValueError(
                        'Invalid ISO provided to ValuationOptimizer handler.')

                if params:
                    op.set_model_parameters(**params)
                else:
                    continue_param_loop = False

                try:
                    solved_op = self._solve_model(op)
                except pyutilib.common._exceptions.ApplicationError as e:
                    logging.error(
                        'ValOp Handler: Something went wrong when solving: ({error})'
                        .format(error=e))
                    handler_status = False
                except IncompatibleDataException as e:
                    logging.error(e)
                    handler_status = False
                else:
                    solved_op = self._save_to_solved_ops(
                        solved_op, iso, market_type, node_name, year, month,
                        params)
                    solved_requests.append(solved_op)

        logging.info('ValOp Handler: Finished processing requested jobs.')
        return solved_requests, handler_status
Esempio n. 4
0
    def get_solved_ops(self):
        """Returns the list of solved Optimizer objects in reverse chronological order."""
        return_list = reversed(self.solved_ops)

        return return_list


if __name__ == '__main__':
    with open('valuation_optimizer.log', 'w'):
        pass

    logging.basicConfig(filename='valuation_optimizer.log',
                        format='[%(levelname)s] %(asctime)s: %(message)s',
                        level=logging.INFO)

    op = ValuationOptimizer()

    fpath = os.path.join('data', 'PJM')
    year = 2015
    month = 3
    nodeid = '51217'

    daLMP, mr, rega, regd, RegCCP, RegPCP = read_pjm_data(
        fpath, year, month, nodeid)

    handler_requests = {}
    handler_requests['iso'] = 'PJM'
    handler_requests['market type'] = 'pjm_pfp'
    handler_requests['months'] = [
        (month, year),
    ]
Esempio n. 5
0



if __name__ == '__main__':
    from es_gui.tools.valuation.valuation_optimizer import ValuationOptimizer

    fpath = os.path.join('data', 'PJM')
    year = 2015
    month = 1
    nodeid = '1'

    for month in [12]:
        daLMP, mr, rega, regd, RegCCP, RegPCP = read_pjm_data(fpath,year,month,nodeid)

        op = ValuationOptimizer(market_type='pjm_pfp')

        op.price_electricity = daLMP
        op.mileage_ratio = mr
        op.mileage_slow = rega
        op.mileage_fast = regd
        op.price_reg_capacity = RegCCP
        op.price_reg_performance = RegPCP

        handler_requests = {}
        handler_requests['iso'] = 'PJM'
        handler_requests['market type'] = 'pjm_pfp'
        handler_requests['months'] = [(month, year),]
        handler_requests['node id'] = nodeid

        results, gross_revenue = op.run()
Esempio n. 6
0
    def process_requests(self, requests, *args):
        """Generates and solves ValuationOptimizer models based on the given requests."""
        dms = self.dms

        iso = requests['iso']
        market_type = requests['market type']
        node_id = str(requests['node id'])
        node_name = self.dms.get_node_name(node_id, iso)
        param_set = requests.get('param set', [None])

        solved_requests = []

        handler_status = set()

        for month, year in requests['months']:
            param_set_iterator = iter(param_set)
            continue_param_loop = True

            while continue_param_loop:
                try:
                    params = next(param_set_iterator)
                except StopIteration:
                    break

                op = ValuationOptimizer(market_type=market_type)

                if iso == 'PJM':
                    #lmp_da, RUP, RDW, MR, RA, RD, RegCCP, RegPCP = dms.get_pjm_data(year, month, node_name)
                    lmp_da, MR, RA, RD, RegCCP, RegPCP = dms.get_pjm_data(year, month, node_id)

                    op.price_electricity = lmp_da
                    op.mileage_mult = MR
                    # op.mileage_slow = RA
                    # op.mileage_fast = RD
                    op.price_regulation = RegCCP
                    op.price_reg_service = RegPCP
                    #op.fraction_reg_up = RUP
                    #op.fraction_reg_down = RDW
                elif iso == 'ERCOT':
                    lmp_da, rd, ru = dms.get_ercot_data(year, month, node_name)

                    op.price_electricity = lmp_da
                    op.price_reg_up = ru
                    op.price_reg_down = rd
                elif iso == 'MISO':
                    lmp_da, regMCP = dms.get_miso_data(year, month, node_name)

                    op.price_electricity = lmp_da
                    # op.price_reg_service = regMCP
                    op.price_regulation = regMCP
                elif iso == 'ISONE':
                    daLMP, RegCCP, RegPCP, miMULT = dms.get_isone_data(year, month, node_id)

                    op.price_electricity = daLMP
                    op.price_regulation = RegCCP
                    op.price_reg_service = RegPCP
                    op.mileage_mult = miMULT
                ########################################################################################################
                elif iso == 'NYISO':
                    lbmp_da, rcap_da = dms.get_nyiso_data(year, month, node_id)

                    op.price_electricity = lbmp_da
                    op.price_regulation = rcap_da
                elif iso == 'SPP':
                    lmp_da, mcpru_da, mcprd_da = dms.get_spp_data(year, month, node_name)

                    op.price_electricity = lmp_da
                    op.price_reg_up = mcpru_da
                    op.price_reg_down = mcprd_da
                elif iso == 'CAISO':
                    lmp_da, aspru_da, asprd_da, asprmu_da, asprmd_da, rmu_mm, rmd_mm, rmu_pacc, rmd_pacc = dms.get_caiso_data(year, month, node_name)

                    op.price_electricity = lmp_da
                    op.price_reg_up = aspru_da
                    op.price_reg_down = asprd_da
                    op.price_reg_serv_up = asprmu_da
                    op.price_reg_serv_down = asprmd_da
                    op.mileage_mult_ru = rmu_mm
                    op.mileage_mult_rd = rmd_mm
                    op.perf_score_ru = rmu_pacc # TODO: give the option to the user to override this
                    op.perf_score_rd = rmd_pacc
                else:
                    logging.error('ValOp Handler: Invalid ISO provided.')
                    raise ValueError('Invalid ISO provided to ValuationOptimizer handler.')

                if params:
                    op.set_model_parameters(**params)
                else:
                    continue_param_loop = False

                try:
                    solved_op = self._solve_model(op)
                except pyutilib.common._exceptions.ApplicationError as e:
                    logging.error('Op Handler: {error}'.format(error=e))

                    if 'No executable found' in e.args[0]:
                        # Could not locate solver executable
                        handler_status.add('* The executable for the selected solver could not be found; please check your installation.')
                    else:
                        handler_status.add('* ({0} {1}) {2}. The problem may be infeasible.'.format(month, year, e.args[0]))
                except IncompatibleDataException as e:
                    # Data exception raised by ValuationOptimizer
                    logging.error(e)
                    handler_status.add('* ({0} {1}) The time series data has mismatched sizes.'.format(month, year))
                except AssertionError as e:
                    # An optimal solution could not be found as reported by the solver
                    logging.error('Op Handler: {error}'.format(error=e))
                    handler_status.add('* ({0} {1}) An optimal solution could not be found; the problem may be infeasible.'.format(month, year))
                else:
                    solved_op = self._save_to_solved_ops(solved_op, iso, market_type, node_name,
                                                        year, month, params)
                    solved_requests.append(solved_op)

        logging.info('Op Handler: Finished processing requested jobs.')
        return solved_requests, handler_status
Esempio n. 7
0
class ValuationScreen(Screen):
    """The home screen for the 'Valuation - Advanced' section."""
    def __init__(self, **kwargs):
        super(ValuationScreen, self).__init__(**kwargs)

        self.op = ValuationOptimizer()

        self.pw = ProgressWindow()
        self.pw.run_button.bind(on_release=self.execute_valuation)

    def on_enter(self):
        ab = self.manager.nav_bar
        ab.build_valuation_advanced_nav_bar()
        ab.set_title('Valuation - Advanced')

    def _generate_requests(self):
        data_screen = self.manager.get_screen('load_data')
        params_screen = self.manager.get_screen('set_parameters')

        iso_selected, market_formulation_selected, node_selected, year_selected, month_selected = data_screen.get_inputs()

        param_settings = params_screen.get_inputs()

        requests = {'iso': iso_selected,
                    'market type': market_formulation_selected,
                    'months': [(month_selected, year_selected)],
                    'node id': node_selected,
                    }

        if param_settings:
            requests['param set'] = param_settings

        return requests

    def execute_valuation(self, *args):
        try:
            requests = self._generate_requests()
        except InputError as e:
            logging.error('ValuationExecute: ' + str(e))

            popup = WarningPopup()
            popup.popup_text.text = str(e)
            popup.open()
        else:
            self.pw.title = 'Running optimization'
            self.pw.run_button.disabled = True

            solver_name = App.get_running_app().config.get('optimization', 'solver')
            handler = self.manager.get_screen('valuation_home').handler
            handler.solver_name = solver_name
            handler.process_requests(requests)

            self.pw.update_window('Finished!', 100, anim_duration=0)
            self.pw.title = 'Finished!'

            self.pw.view_results_button.bind(on_release=self.activate_view_results_button)
            self.pw.view_results_button.disabled = False

    def open_valuation_run_menu(self, *args):
        """
        Opens the popup/screen for executing a valuation optimization routine.
        """
        #if self.op.price_electricity is not None:
        if True:
            self.pw.open()
        else:
            # Optimizer LMP attribute is still its initialized None value.
            # TODO: Better way to check if data has been loaded?

            popup = WarningPopup()
            popup.popup_text.text = 'No data currently loaded. Please load data first!'
            popup.open()

    def save_run(self, name, iso, dtype, year, month, node, op=None):
        from datetime import datetime

        time_finished = datetime.now().strftime('%A, %B %d, %Y %H:%M:%S')

        results_dict = {}
        results_dict['name'] = name
        if op is None:
            results_dict['op'] = self.op
        else:
            results_dict['op'] = op
        results_dict['iso'] = iso
        results_dict['data_type'] = dtype
        results_dict['year'] = year
        results_dict['month'] = month
        results_dict['node'] = node
        results_dict['time'] = time_finished

        plot_screen = self.manager.get_screen('valuation_results_viewer')
        rv = plot_screen.run_selector.rv
        rv.data.append(results_dict)

    def execute_valuation_run(self, *args):
        """
        Executes the valuation run in a separate thread when possible.
        """
        self.pw.title = 'Running optimization'
        self.pw.run_button.disabled = True

        def _execute_valuation_run(*largs):
            self.pw.progress_label.text = ''

            # instantiate Pyomo model
            anim_duration = 0.3
            self.pw.update_window('Instantiating Pyomo model...\n', 10, anim_duration=anim_duration)
            self.op.instantiate_model()

            # populate Pyomo model
            anim_duration = 2.5  # this duration is empirically based
            self.pw.update_window('Populating Pyomo model...\n', 80, anim_duration=anim_duration)
            self.op.populate_model()

            # solve and process results
            anim_duration = 0.5
            self.pw.update_window('Solving model...\n', 90)
            sleep(0.5)  # hack to force pb to update before the solve clogs the main loop/thread

            @mainthread
            def solve_pyomo_model():
                # get the current optimization solver from settings
                solver_name = App.get_running_app().config.get('optimization', 'solver')
                self.op.solver = solver_name
                self.op.solve_model()

                # post-process solve results
                anim_duration = 0.5
                self.pw.update_window('Processing solve results...\n', 100, anim_duration=anim_duration)

                sleep(anim_duration)  # let the animation play out before "finish"?

                # finish
                anim_duration = 0
                self.pw.update_window('Finished!', 100, anim_duration=anim_duration)
                self.pw.title = 'Finished!'
                self.pw.view_results_button.bind(on_release=self.activate_view_results_button)
                self.pw.view_results_button.disabled = False

                # add to finished runs
                from random import sample
                from string import ascii_letters

                load_screen = self.manager.get_screen('load_data')
                iso = load_screen.iso_select.text
                year = load_screen.year_select.text
                month = load_screen.month_select.text
                dtype = load_screen.type_select.text
                node = load_screen.node_select.text

                # random string identifier for uniqueness
                name = '-'.join([node, year, month, ''.join(sample(ascii_letters, 5))])

                self.save_run(name, iso, dtype, year, month, node)

                # create new instance of Optimizer
                self.op = ValuationOptimizer()

            solve_pyomo_model()

        thread = threading.Thread(target=_execute_valuation_run, args=())
        thread.start()

    def activate_view_results_button(self, *args):
        """
        Enables the "View Results" button after the optimization routine is complete.
        """
        self.manager.nav_bar.go_to_screen('valuation_results_viewer')
        self.pw.dismiss()