def solve(self, model='MESSAGE', solve_options={}, **kwargs): """Solve the Scenario. Parameters ---------- model : string the type of model to solve (e.g., MESSAGE or MESSAGE-MACRO) solve_options : dict name, value pairs to use for GAMS solver optfile, see `message_ix.DEFAULT_SOLVE_OPTIONS` for defaults and see https://www.gams.com/latest/docs/S_CPLEX.html for possible arguments By default, :meth:`ixmp.Scenario.solve` is called with "MESSAGE" as the *model* argument; see the documentation of that method for other arguments. *model* may also be overwritten, e.g.: >>> s.solve(model='MESSAGE-MACRO') """ # TODO: we generate the cplex.opt file on the fly. this is *not* safe # agaisnt race conditions. It is possible to generate opt files with # random names (see # https://www.gams.com/latest/docs/UG_GamsCall.html#GAMSAOoptfile); # however, we need to clean up the code in ixmp that passes arguments # to gams to do so. fname = os.path.join(default_paths.model_path(), 'cplex.opt') opts = copy.deepcopy(DEFAULT_SOLVE_OPTIONS) opts.update(solve_options) lines = '\n'.join('{} = {}'.format(k, v) for k, v in opts.items()) with open(fname, 'w') as f: f.writelines(lines) ret = super(Scenario, self).solve(model=model, **kwargs) os.remove(fname) return ret
import os import re import ixmp.model_settings as model_settings from message_ix import default_paths from message_ix.core import * model_file = os.path.join(default_paths.model_path(), '{model}_run.gms') in_file = os.path.join(default_paths.data_path(), 'MsgData_{case}.gdx') out_file = os.path.join(default_paths.output_path(), 'MsgOutput_{case}.gdx') iter_file = os.path.join(default_paths.output_path(), 'MsgIterationReport_{case}.gdx') solve_args = ['--in="{inp}"', '--out="{outp}"', '--iter="' + iter_file + '"'] for msg in ['MESSAGE', 'MESSAGE-MACRO']: model_settings.register_model( msg, model_settings.ModelConfig(model_file='"{}"'.format(model_file), inp=in_file, outp=out_file, args=solve_args)) # retrieve MESSAGEix version number from model/version.gms fname = os.path.join(default_paths.model_path(), 'version.gms') fname = fname if os.path.exists(fname) else \ os.path.join(os.path.dirname(os.path.realpath(__file__)), 'model', 'version.gms') # only exists here on install with open(fname) as f: s = str(f.readlines())