|  | 
|  | 1 | +from ast import literal_eval | 
|  | 2 | +import configparser | 
|  | 3 | +import os | 
|  | 4 | +import sys | 
| 1 | 5 | from typing import Any, Dict, List, Tuple, Optional | 
| 2 | 6 | 
 | 
|  | 7 | +Kwargs = Dict[str, Any] | 
|  | 8 | + | 
| 3 | 9 | import gurobipy | 
| 4 | 10 | from gurobipy import GRB | 
| 5 | 11 | 
 | 
|  | 
| 11 | 17 | 
 | 
| 12 | 18 | UNBOUNDED = (-GRB.INFINITY, +GRB.INFINITY) | 
| 13 | 19 | 
 | 
|  | 20 | +# *********************************************************************** | 
|  | 21 | +# CONFIG FILE MANAGEMENT | 
|  | 22 | +#  | 
|  | 23 | +# - read config files from file path | 
|  | 24 | +# - extract experiment settings | 
|  | 25 | +# - instantiate planner | 
|  | 26 | +# | 
|  | 27 | +# *********************************************************************** | 
|  | 28 | + | 
|  | 29 | + | 
|  | 30 | +def _parse_config_file(path: str): | 
|  | 31 | +    if not os.path.isfile(path): | 
|  | 32 | +        raise FileNotFoundError(f'File {path} does not exist.') | 
|  | 33 | +    config = configparser.RawConfigParser() | 
|  | 34 | +    config.optionxform = str  | 
|  | 35 | +    config.read(path) | 
|  | 36 | +    args = {k: literal_eval(v)  | 
|  | 37 | +            for section in config.sections() | 
|  | 38 | +            for (k, v) in config.items(section)} | 
|  | 39 | +    return config, args | 
|  | 40 | + | 
|  | 41 | + | 
|  | 42 | +def _parse_config_string(value: str): | 
|  | 43 | +    config = configparser.RawConfigParser() | 
|  | 44 | +    config.optionxform = str  | 
|  | 45 | +    config.read_string(value) | 
|  | 46 | +    args = {k: literal_eval(v)  | 
|  | 47 | +            for section in config.sections() | 
|  | 48 | +            for (k, v) in config.items(section)} | 
|  | 49 | +    return config, args | 
|  | 50 | + | 
|  | 51 | + | 
|  | 52 | +def _getattr_any(packages, item): | 
|  | 53 | +    for package in packages: | 
|  | 54 | +        loaded = getattr(package, item, None) | 
|  | 55 | +        if loaded is not None: | 
|  | 56 | +            return loaded | 
|  | 57 | +    return None | 
|  | 58 | + | 
|  | 59 | + | 
|  | 60 | +def _load_config(config, args): | 
|  | 61 | +    gurobi_args = {k: args[k] for (k, _) in config.items('Gurobi')} | 
|  | 62 | +    compiler_args = {k: args[k] for (k, _) in config.items('Optimizer')} | 
|  | 63 | +     | 
|  | 64 | +    # policy class | 
|  | 65 | +    plan_method = compiler_args.pop('method') | 
|  | 66 | +    plan_kwargs = compiler_args.pop('method_kwargs', {}) | 
|  | 67 | +    compiler_args['plan'] = getattr(sys.modules[__name__], plan_method)(**plan_kwargs) | 
|  | 68 | +    compiler_args['model_params'] = gurobi_args | 
|  | 69 | +     | 
|  | 70 | +    return compiler_args | 
|  | 71 | + | 
|  | 72 | + | 
|  | 73 | +def load_config(path: str) -> Kwargs: | 
|  | 74 | +    '''Loads a config file at the specified file path.''' | 
|  | 75 | +    config, args = _parse_config_file(path) | 
|  | 76 | +    return _load_config(config, args) | 
|  | 77 | + | 
|  | 78 | + | 
|  | 79 | +def load_config_from_string(value: str) -> Kwargs: | 
|  | 80 | +    '''Loads config file contents specified explicitly as a string value.''' | 
|  | 81 | +    config, args = _parse_config_string(value) | 
|  | 82 | +    return _load_config(config, args) | 
|  | 83 | + | 
| 14 | 84 | 
 | 
| 15 | 85 | # *********************************************************************** | 
| 16 | 86 | # ALL VERSIONS OF GUROBI PLANS | 
| @@ -237,7 +307,7 @@ def params(self, compiled: GurobiRDDLCompiler, | 
| 237 | 307 |                     lb_name = f'lb__{action}__{k}' | 
| 238 | 308 |                     ub_name = f'ub__{action}__{k}'                     | 
| 239 | 309 |                     if values is None: | 
| 240 |  | -                        lb, ub = self.state_bounds[states[0]] | 
|  | 310 | +                        lb, ub = self.state_bounds.get(states[0], UNBOUNDED) | 
| 241 | 311 |                         var_bounds = UNBOUNDED if is_linear else (lb - 1, ub + 1) | 
| 242 | 312 |                         lb_var = compiled._add_var(model, vtype, *var_bounds)                         | 
| 243 | 313 |                         ub_var = compiled._add_var(model, vtype, *var_bounds) | 
|  | 
0 commit comments