Skip to content

Commit ae44310

Browse files
committed
Update s2mpj tools: add env to config
1 parent e1747a0 commit ae44310

2 files changed

Lines changed: 135 additions & 39 deletions

File tree

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,24 @@ This repository preserves only the files relevant to Python users from the origi
1010
- **`src/list_of_python_problems`**: A listing of all available problems.
1111
- **`src/s2mpjlib.py`**: Supporting library script.
1212

13+
## Configuration
14+
15+
The file `config.txt` in this directory controls how `s2mpj_select` filters problems (e.g., `variable_size` and `test_feasibility_problems`). See the comments in `config.txt` for a full description of each option.
16+
17+
When used through **OptiProfiler**, you can override these options at runtime without editing `config.txt`:
18+
19+
```python
20+
from optiprofiler import set_plib_config, get_plib_config
21+
22+
# View the current effective configuration
23+
print(get_plib_config('s2mpj'))
24+
25+
# Override at runtime (persists for the current Python process)
26+
set_plib_config('s2mpj', variable_size='all', test_feasibility_problems=2)
27+
```
28+
29+
You can also set the environment variables `S2MPJ_VARIABLE_SIZE` and `S2MPJ_TEST_FEASIBILITY_PROBLEMS` directly. Environment variables take precedence over `config.txt`.
30+
1331
## Maintenance
1432

1533
This repository is **automatically synchronized** with the upstream `GrattonToint/S2MPJ` repository via GitHub Actions. It checks for updates daily to ensure the problem set remains current.

s2mpj_tools.py

Lines changed: 117 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,47 @@
88

99
def s2mpj_load(problem_name, *args):
1010
"""
11-
Load the S2MPJ problem.
11+
Convert an S2MPJ problem name to a `Problem` instance.
1212
1313
Parameters
1414
----------
1515
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>`_.
1919
2020
Returns
2121
-------
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
2452
"""
2553

2654
# Add the path of the problem to the system path.
@@ -243,53 +271,103 @@ def jcub(x):
243271

244272
def s2mpj_select(options):
245273
"""
246-
Select problems from the S2MPJ collection that satisfy given criteria.
247-
274+
Select problems from S2MPJ that satisfy given criteria.
275+
248276
Parameters
249277
----------
250278
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+
268313
Returns
269314
-------
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})
272349
"""
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.
274352
variable_size = 'default'
275353
test_feasibility_problems = 0
276-
# Check if 'config.txt' exists under the same directory as this script
277354
current_dir = os.path.dirname(os.path.abspath(__file__))
278355
config_path = os.path.join(current_dir, 'config.txt')
279356
if os.path.exists(config_path):
280357
try:
281358
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:
292366
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'])
293371

294372
if variable_size not in ['default', 'min', 'max', 'all']:
295373
raise ValueError("Invalid `variable_size` in the file `config.txt`. Please set it to 'default', 'min', 'max', or 'all'.")

0 commit comments

Comments
 (0)