Source code for dispaset.solve

# -*- coding: utf-8 -*-"""This worksheet contains the two main functions to solve the DispaSET optimization problem using PYOMO or GAMS.Solve with GAMS and the high level API--------------------------------------The high level interface is recommended for Linux users because it solvesthe "whitespace in the simulation folder" issue.Installation: To install the high-level API in Python 2.x:: cd gams24.4_linux_x64_64_sfx/apifiles/Python/api python gamssetup.py install To install the high-level API in Python 3.x:: cd gams24.6_linux_x64_64_sfx/apifiles/Python/api_34 python setup.py install Solve with GAMS and the low level APIs--------------------------------------Use lower level apis to run GAMS. BAsed on GAMS xpexample2.pyThe advantage of the low level API is that it can easily be installed from pip:: pip install gdxcc pip install gamsxcc pip install optccSolve with PYOMO----------------The Pyomo version of Dispa-SET is currently not up-to-date. Use at your own risk."""################################################################################################################################################################### Dispa-SET: main model ###########################################################################################################################################################################importosimportshutilimportloggingimporttimefrom.misc.gdx_handlerimportget_gams_path,import_local_lib,package_existsfrom.misc.gms_handlerimportsolve_high_level,solve_low_levelfrom.commonimportcommons

[docs]defis_sim_folder_ok(sim_folder):''' Function that checks if the provided path is a valid Dispa-SET simulation folder. The following files are required: - Inputs.gdx - UCM_h.gms :param sim_folder: path (absolute or relative) to the simulation folder '''ifnotos.path.exists(sim_folder):logging.error('The provided DispaSET simulation environment folder ('+sim_folder+') does not exist')returnFalseifnotos.path.exists(os.path.join(sim_folder,u'Inputs.gdx')):logging.error('There is no Inputs.gdx file within the specified DispaSET simulation environment folder ('+sim_folder+'). Check that the GDX output is activated in the option file and that no error stated during the pre-processing')returnFalseifnotos.path.exists(os.path.join(sim_folder,u'UCM_h.gms')):logging.error('There is no UCM_h.gms file within the specified DispaSET simulation environment folder ('+sim_folder+')')returnFalsereturnTrue

[docs]defsolve_GAMS(sim_folder,gams_folder=None,output_lst=False):''' Function used to run the optimization using the GAMS engine. :param sim_folder: path to a valid Dispa-SET simulation folder :param gams_folder: path to the gams folder. If not provided, the script will try to find it automatically :param work_dir: path to the working directory (does not need to be provided) :param output_lst: Set to True to conserve a copy of the GAMS lst file in the simulation folder '''ifpackage_exists('gamsxcc')andpackage_exists('optcc'):solv_func=solve_low_levellogging.info('Using the low-level gams api')elifpackage_exists('gams'):solv_func=solve_high_levellogging.info('Using the high-level gams api')else:solv_func=solve_low_levellogging.warning('Could not find the GAMS APIs. Trying to locate local version')ifnotimport_local_lib('lowlevel'):returnFalsegams_folder=get_gams_path(gams_folder)ifnotgams_folder:# couldn't locatelogging.error('GAMS path cannot be located. Simulation is stopped')returnFalsesim_folder=os.path.abspath(sim_folder)gams_folder=os.path.abspath(gams_folder)ifis_sim_folder_ok(sim_folder):#Temporary warning for Spyder users:ifany(['SPY_'innamefornameinos.environ]):# check if spyderlogging.info("\nIf the script seems stuck at this place \n(gams is optimizing but not output is displayed), \nit is preferable to run Dispa-SET in a \nseparate terminal (in Spyder: Preferences - Run - \nExecute in an external system terminal)")ret=solv_func(gams_folder,sim_folder,output_lst=output_lst)ifos.path.isfile(os.path.join(sim_folder,'debug.gdx')):logging.warning('A debug file was created. There has probably been an optimization error')ifos.path.isfile(commons['logfile']):shutil.copy(commons['logfile'],os.path.join(sim_folder,'warn_solve.log'))returnretelse:returnFalse

[docs]defsolve_pyomo(sim_folder):''' Function used to run the optimization using the PYOMO engine. :param sim_folder: path to a valid Dispa-SET simulation folder '''importpicklefrom.pyomo.modelimportDispaSolveraiseNotImplementedError('Pyomo is currently outdated and does not work with the latest features.''Please use an older version of dispaset.')withopen(os.path.join(sim_folder,'Inputs.p'),'rb')aspyomo_input_file:SimData=pickle.load(pyomo_input_file)ifSimData['config']['SimulationType']=='MILP':LPFormulation=Falseelse:LPFormulation=Trueifos.path.isfile(SimData['config']['cplex_path']):path_cplex=SimData['config']['cplex_path']else:path_cplex=''iflen(SimData['config']['cplex_path'])>2:logging.warning('The specified path for cplex ('+SimData['config']['cplex_path']+') is not valid. It will be ignored')time0=time.time()results=DispaSolve(SimData['sets'],SimData['parameters'],LPFormulation=LPFormulation,path_cplex=path_cplex)logging.info('Completed simulation in {0:.2f} seconds'.format(time.time()-time0))ifos.path.isfile(commons['logfile']):shutil.copy(commons['logfile'],os.path.join(sim_folder,'warn_solve.log'))returnresults