|
8 | 8 |
|
9 | 9 | def s2mpj_load(problem_name, *args): |
10 | 10 | """ |
11 | | - Load the S2MPJ problem. |
| 11 | + Convert an S2MPJ problem name to a `Problem` instance. |
12 | 12 |
|
13 | 13 | Parameters |
14 | 14 | ---------- |
15 | 15 | problem_name : str |
16 | | - Name of the problem in S2MPJ. |
17 | | - *args : tuple |
18 | | - Additional arguments to pass to the problem class constructor |
| 16 | + Name of the problem in the S2MPJ collection. More details about |
| 17 | + S2MPJ can be found at |
| 18 | + `the official repository <https://github.com/GrattonToint/S2MPJ>`_. |
19 | 19 |
|
20 | 20 | Returns |
21 | 21 | ------- |
22 | | - `Problem` |
23 | | - An instance of the Problem class. |
| 22 | + optiprofiler.Problem |
| 23 | + A ``Problem`` instance corresponding to the named problem. |
| 24 | +
|
| 25 | + Notes |
| 26 | + ----- |
| 27 | + There are two ways to obtain the ``problem_name`` you want: |
| 28 | +
|
| 29 | + 1. Use `s2mpj_select` to get the problem names that satisfy your |
| 30 | + criteria. |
| 31 | + 2. Look for the CSV file ``probinfo_python.csv`` in the same directory |
| 32 | + as this module. It contains information about all problems in S2MPJ. |
| 33 | +
|
| 34 | + The problem name may appear in the form ``'PROBLEMNAME_n_m'`` where |
| 35 | + ``n`` is the dimension and ``m`` is the number of linear and nonlinear |
| 36 | + constraints. This happens when a problem accepts extra arguments to |
| 37 | + change its dimension or number of constraints. This information is |
| 38 | + stored in the ``probinfo_python.csv`` file. |
| 39 | +
|
| 40 | + See Also |
| 41 | + -------- |
| 42 | + s2mpj_select : Select problems from S2MPJ by criteria. |
| 43 | +
|
| 44 | + Examples |
| 45 | + -------- |
| 46 | + .. code-block:: python |
| 47 | +
|
| 48 | + from optiprofiler.problem_libs.s2mpj import s2mpj_load |
| 49 | +
|
| 50 | + problem = s2mpj_load('ROSENBR') |
| 51 | + print(problem.n) # 2 |
24 | 52 | """ |
25 | 53 |
|
26 | 54 | # Add the path of the problem to the system path. |
@@ -243,53 +271,103 @@ def jcub(x): |
243 | 271 |
|
244 | 272 | def s2mpj_select(options): |
245 | 273 | """ |
246 | | - Select problems from the S2MPJ collection that satisfy given criteria. |
247 | | - |
| 274 | + Select problems from S2MPJ that satisfy given criteria. |
| 275 | +
|
248 | 276 | Parameters |
249 | 277 | ---------- |
250 | 278 | options : dict |
251 | | - A dictionary containing selection criteria: |
252 | | - - ptype: problem type, string containing any of 'u', 'b', 'l', 'n' |
253 | | - (default: 'ubln') |
254 | | - - mindim: minimum dimension (default: 1) |
255 | | - - maxdim: maximum dimension (default: inf) |
256 | | - - minb: minimum number of bound constraints (default: 0) |
257 | | - - maxb: maximum number of bound constraints (default: inf) |
258 | | - - minlcon: minimum number of linear constraints (default: 0) |
259 | | - - maxlcon: maximum number of linear constraints (default: inf) |
260 | | - - minnlcon: minimum number of nonlinear constraints (default: 0) |
261 | | - - maxnlcon: maximum number of nonlinear constraints (default: inf) |
262 | | - - mincon: minimum total number of constraints (default: 0) |
263 | | - - maxcon: maximum total number of constraints (default: inf) |
264 | | - - oracle: oracle level, 0=zeroth-order, 1=first-order, 2=second-order |
265 | | - (default: 0) |
266 | | - - excludelist: list of problems to exclude (default: []) |
267 | | - |
| 279 | + A dictionary containing selection criteria. More details about |
| 280 | + S2MPJ can be found at |
| 281 | + `the official repository <https://github.com/GrattonToint/S2MPJ>`_. |
| 282 | + Supported keys: |
| 283 | +
|
| 284 | + - **ptype** (*str*) -- Type of problems to select. A string |
| 285 | + consisting of any combination of ``'u'`` (unconstrained), |
| 286 | + ``'b'`` (bound constrained), ``'l'`` (linearly constrained), |
| 287 | + and ``'n'`` (nonlinearly constrained), such as ``'b'``, |
| 288 | + ``'ul'``, ``'ubn'``. Default is ``'ubln'``. |
| 289 | + - **mindim** (*int*) -- Minimum dimension. Default is ``1``. |
| 290 | + - **maxdim** (*int*) -- Maximum dimension. Default is ``inf``. |
| 291 | + - **minb** (*int*) -- Minimum number of bound constraints. |
| 292 | + Default is ``0``. |
| 293 | + - **maxb** (*int*) -- Maximum number of bound constraints. |
| 294 | + Default is ``inf``. |
| 295 | + - **minlcon** (*int*) -- Minimum number of linear constraints. |
| 296 | + Default is ``0``. |
| 297 | + - **maxlcon** (*int*) -- Maximum number of linear constraints. |
| 298 | + Default is ``inf``. |
| 299 | + - **minnlcon** (*int*) -- Minimum number of nonlinear constraints. |
| 300 | + Default is ``0``. |
| 301 | + - **maxnlcon** (*int*) -- Maximum number of nonlinear constraints. |
| 302 | + Default is ``inf``. |
| 303 | + - **mincon** (*int*) -- Minimum total number of linear and |
| 304 | + nonlinear constraints. Default is ``0``. |
| 305 | + - **maxcon** (*int*) -- Maximum total number of linear and |
| 306 | + nonlinear constraints. Default is ``inf``. |
| 307 | + - **oracle** (*int*) -- Oracle provided by the problem. ``0`` |
| 308 | + means zeroth-order, ``1`` means first-order, ``2`` means |
| 309 | + second-order. Default is ``0``. |
| 310 | + - **excludelist** (*list of str*) -- List of problem names to |
| 311 | + exclude. Default is ``[]``. |
| 312 | +
|
268 | 313 | Returns |
269 | 314 | ------- |
270 | | - list |
271 | | - A list of problem names that satisfy the criteria. |
| 315 | + list of str |
| 316 | + Problem names that satisfy the given criteria. |
| 317 | +
|
| 318 | + Notes |
| 319 | + ----- |
| 320 | + 1. All information about the problems can be found in the CSV file |
| 321 | + ``probinfo_python.csv`` in the same directory as this module. |
| 322 | +
|
| 323 | + 2. The problem name may appear in the form ``'PROBLEMNAME_n_m'`` |
| 324 | + where ``n`` is the dimension and ``m`` is the number of |
| 325 | + constraints. This happens when a problem accepts extra arguments |
| 326 | + to change its dimension or number of constraints. |
| 327 | +
|
| 328 | + 3. There is a file ``config.txt`` in the same directory as this |
| 329 | + module. It can be used to set the options ``variable_size`` and |
| 330 | + ``test_feasibility_problems``. See the comments in ``config.txt`` |
| 331 | + for details. You can also override these options at runtime using |
| 332 | + `set_plib_config` or by setting environment variables |
| 333 | + ``S2MPJ_VARIABLE_SIZE`` and ``S2MPJ_TEST_FEASIBILITY_PROBLEMS``. |
| 334 | + Environment variables take precedence over ``config.txt``. |
| 335 | +
|
| 336 | + See Also |
| 337 | + -------- |
| 338 | + s2mpj_load : Load a problem from S2MPJ. |
| 339 | + optiprofiler.get_plib_config : Read the current configuration. |
| 340 | + optiprofiler.set_plib_config : Override configuration at runtime. |
| 341 | +
|
| 342 | + Examples |
| 343 | + -------- |
| 344 | + .. code-block:: python |
| 345 | +
|
| 346 | + from optiprofiler.problem_libs.s2mpj import s2mpj_select |
| 347 | +
|
| 348 | + names = s2mpj_select({'ptype': 'u', 'maxdim': 2}) |
272 | 349 | """ |
273 | | - # Set default options in the config file if not provided. |
| 350 | + # Read config: environment variables (set via set_plib_config) take |
| 351 | + # precedence over the values in config.txt. |
274 | 352 | variable_size = 'default' |
275 | 353 | test_feasibility_problems = 0 |
276 | | - # Check if 'config.txt' exists under the same directory as this script |
277 | 354 | current_dir = os.path.dirname(os.path.abspath(__file__)) |
278 | 355 | config_path = os.path.join(current_dir, 'config.txt') |
279 | 356 | if os.path.exists(config_path): |
280 | 357 | try: |
281 | 358 | with open(config_path, 'r') as f: |
282 | | - # Find the line starting with 'variable_size=' and 'test_feasibility_problems=' |
283 | | - lines = f.readlines() |
284 | | - for line in lines: |
285 | | - if line.strip().startswith('variable_size='): |
286 | | - variable_size = line.strip().split('=')[1].strip() |
287 | | - variable_size = variable_size.split('#')[0].split('%')[0].strip() |
288 | | - elif line.strip().startswith('test_feasibility_problems='): |
289 | | - test_feasibility_problems = line.strip().split('=')[1].strip() |
290 | | - test_feasibility_problems = int(test_feasibility_problems.split('#')[0].split('%')[0].strip()) |
291 | | - except: |
| 359 | + for line in f: |
| 360 | + stripped = line.strip() |
| 361 | + if stripped.startswith('variable_size='): |
| 362 | + variable_size = stripped.split('=')[1].split('#')[0].split('%')[0].strip() |
| 363 | + elif stripped.startswith('test_feasibility_problems='): |
| 364 | + test_feasibility_problems = int(stripped.split('=')[1].split('#')[0].split('%')[0].strip()) |
| 365 | + except Exception: |
292 | 366 | pass |
| 367 | + if 'S2MPJ_VARIABLE_SIZE' in os.environ: |
| 368 | + variable_size = os.environ['S2MPJ_VARIABLE_SIZE'] |
| 369 | + if 'S2MPJ_TEST_FEASIBILITY_PROBLEMS' in os.environ: |
| 370 | + test_feasibility_problems = int(os.environ['S2MPJ_TEST_FEASIBILITY_PROBLEMS']) |
293 | 371 |
|
294 | 372 | if variable_size not in ['default', 'min', 'max', 'all']: |
295 | 373 | raise ValueError("Invalid `variable_size` in the file `config.txt`. Please set it to 'default', 'min', 'max', or 'all'.") |
|
0 commit comments