def solve_multistage_standalone(power_system, times, scenario_tree, data): stage_times = times.subdivide(user_config.hours_commitment, user_config.hours_overlap) pid = user_config.pid if user_config.pid else os.getpid() has_pid = user_config.output_prefix or user_config.pid if user_config.standalone_restart: # get the last stage in storage storage = get_storage() stg_start = len(storage['solve_time'].dropna()) logging.info('Restarting on stage {}'.format(stg_start)) stage_times_remaining = stage_times[stg_start:] else: storage = init_store(power_system, stage_times, data) stage_times_remaining = stage_times stg_start = 0 for stg, t_stage in enumerate(stage_times_remaining): logging.info('Stage starting at {}'.format(t_stage.Start.date())) # store current stage times storage = store_times(t_stage, storage) storage.close() storage = None command = 'standalone_minpower {dir} {stg} {pid} {db}'.format( dir=user_config.directory, stg=stg + stg_start, pid='--pid {}'.format(pid) if has_pid else '', db='--debugger' if user_config.debugger else '') try: subprocess.check_call(command, shell=True, stdout=sys.stdout) except AttributeError: # HACK - avoid error when nose hijacks sys.stdout subprocess.check_call(command, shell=True) repack_storage() storage = get_storage() return storage, stage_times
def solve_multistage_standalone(power_system, times, scenario_tree, data): stage_times = times.subdivide( user_config.hours_commitment, user_config.hours_overlap) pid = user_config.pid if user_config.pid else os.getpid() has_pid = user_config.output_prefix or user_config.pid if user_config.standalone_restart: # get the last stage in storage storage = get_storage() stg_start = len(storage['solve_time'].dropna()) logging.info('Restarting on stage {}'.format(stg_start)) stage_times_remaining = stage_times[stg_start:] else: storage = init_store(power_system, stage_times, data) stage_times_remaining = stage_times stg_start = 0 for stg, t_stage in enumerate(stage_times_remaining): logging.info('Stage starting at {}'.format(t_stage.Start.date())) # store current stage times storage = store_times(t_stage, storage) storage.close() storage = None command = 'standalone_minpower {dir} {stg} {pid} {db}'.format( dir=user_config.directory, stg=stg + stg_start, pid='--pid {}'.format(pid) if has_pid else '', db='--debugger' if user_config.debugger else '') try: subprocess.check_call(command, shell=True, stdout=sys.stdout) except AttributeError: # HACK - avoid error when nose hijacks sys.stdout subprocess.check_call(command, shell=True) repack_storage() storage = get_storage() return storage, stage_times
def solve_problem( datadir='.', shell=True, problemfile=False, csv=True, ): """ Solve a optimization problem specified by spreadsheets in a directory. Read in data, create and solve the problem, and return solution. The problem type is determined by the data. All options are set within `user_config`. """ user_config.directory = datadir pid = user_config.pid if user_config.pid else os.getpid() _set_store_filename(pid) _setup_logging(pid) if user_config.standalone_restart: store = get_storage() debugger = user_config.debugger # preserve debugger state user_config.update(store['configuration']) user_config.debugger = debugger user_config.standalone_restart = True # preserve restart state store.close() logging.debug(dict(user_config)) start_time = timer.time() logging.debug('Minpower reading {}'.format(datadir)) generators, loads, lines, times, scenario_tree, data = get_data.parsedir() logging.debug('data read') power_system = powersystems.PowerSystem(generators, loads, lines) logging.debug('power system initialized') if times.spanhrs <= user_config.hours_commitment + user_config.hours_overlap: solution = create_solve_problem(power_system, times, scenario_tree) else: # split into multiple stages and solve if user_config.standalone: stage_solutions, stage_times = solve_multistage_standalone( power_system, times, scenario_tree, data) else: stage_solutions, stage_times = solve_multistage( power_system, times, scenario_tree, data) solution = results.make_multistage_solution(power_system, stage_times, stage_solutions) if shell: if user_config.output_prefix or user_config.pid: stdout = sys.stdout sys.stdout = StreamToLogger() solution.show() sys.stdout = stdout solution.show() if csv and not user_config.standalone: solution.saveCSV() if user_config.visualization: solution.visualization() logging.info('total time: {}s'.format(timer.time() - start_time)) if user_config.on_complete_script: os.system(user_config.on_complete_script) return solution
def solve_problem(datadir='.', shell=True, problemfile=False, csv=True, ): """ Solve a optimization problem specified by spreadsheets in a directory. Read in data, create and solve the problem, and return solution. The problem type is determined by the data. All options are set within `user_config`. """ user_config.directory = datadir pid = user_config.pid if user_config.pid else os.getpid() _set_store_filename(pid) _setup_logging(pid) if user_config.standalone_restart: store = get_storage() debugger = user_config.debugger # preserve debugger state user_config.update(store['configuration']) user_config.debugger = debugger user_config.standalone_restart = True # preserve restart state store.close() logging.debug(dict(user_config)) start_time = timer.time() logging.debug('Minpower reading {}'.format(datadir)) generators, loads, lines, times, scenario_tree, data = get_data.parsedir() logging.debug('data read') power_system = powersystems.PowerSystem(generators,loads,lines) logging.debug('power system initialized') if times.spanhrs <= user_config.hours_commitment + user_config.hours_overlap: solution = create_solve_problem(power_system, times, scenario_tree) else: #split into multiple stages and solve if user_config.standalone: stage_solutions, stage_times = solve_multistage_standalone( power_system, times, scenario_tree, data) else: stage_solutions, stage_times = solve_multistage( power_system, times, scenario_tree, data) solution = results.make_multistage_solution( power_system, stage_times, stage_solutions) if shell: if user_config.output_prefix or user_config.pid: stdout = sys.stdout sys.stdout = StreamToLogger() solution.show() sys.stdout = stdout solution.show() if csv and not user_config.standalone: solution.saveCSV() if user_config.visualization: solution.visualization() logging.info('total time: {}s'.format(timer.time()-start_time)) if user_config.on_complete_script: os.system(user_config.on_complete_script) return solution