moofs 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- moofs-0.1.0/LICENSE +21 -0
- moofs-0.1.0/PKG-INFO +122 -0
- moofs-0.1.0/README.md +92 -0
- moofs-0.1.0/pyproject.toml +39 -0
- moofs-0.1.0/setup.cfg +4 -0
- moofs-0.1.0/src/moofs/__init__.py +64 -0
- moofs-0.1.0/src/moofs/algorithms/__init__.py +4 -0
- moofs-0.1.0/src/moofs/algorithms/mofs_rfga.py +141 -0
- moofs-0.1.0/src/moofs/algorithms/nsga2.py +70 -0
- moofs-0.1.0/src/moofs/core/__init__.py +17 -0
- moofs-0.1.0/src/moofs/core/algorithm.py +94 -0
- moofs-0.1.0/src/moofs/core/dominance.py +165 -0
- moofs-0.1.0/src/moofs/core/operators.py +113 -0
- moofs-0.1.0/src/moofs/core/problem.py +107 -0
- moofs-0.1.0/src/moofs/core/solution.py +46 -0
- moofs-0.1.0/src/moofs/filters.py +67 -0
- moofs-0.1.0/src/moofs/metrics.py +189 -0
- moofs-0.1.0/src/moofs/plotting.py +103 -0
- moofs-0.1.0/src/moofs/selection.py +149 -0
- moofs-0.1.0/src/moofs.egg-info/PKG-INFO +122 -0
- moofs-0.1.0/src/moofs.egg-info/SOURCES.txt +23 -0
- moofs-0.1.0/src/moofs.egg-info/dependency_links.txt +1 -0
- moofs-0.1.0/src/moofs.egg-info/requires.txt +14 -0
- moofs-0.1.0/src/moofs.egg-info/top_level.txt +1 -0
- moofs-0.1.0/tests/test_moofs.py +176 -0
moofs-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Herman Motcheyo
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
moofs-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: moofs
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Multi-objective feature selection with a scikit-learn API: MOFS-RFGA, NSGA-II, Pareto-front visualization and PlatEMO-compatible metrics
|
|
5
|
+
Author: Herman Motcheyo
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/Herman-Motcheyo/moofs
|
|
8
|
+
Keywords: feature-selection,multi-objective,nsga2,spea2,moead,sparseea,pso,relieff,benchmark
|
|
9
|
+
Classifier: Development Status :: 4 - Beta
|
|
10
|
+
Classifier: Intended Audience :: Science/Research
|
|
11
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
14
|
+
Requires-Python: >=3.9
|
|
15
|
+
Description-Content-Type: text/markdown
|
|
16
|
+
License-File: LICENSE
|
|
17
|
+
Requires-Dist: numpy>=1.21
|
|
18
|
+
Requires-Dist: pandas>=1.3
|
|
19
|
+
Requires-Dist: scikit-learn>=1.0
|
|
20
|
+
Requires-Dist: matplotlib>=3.5
|
|
21
|
+
Provides-Extra: dev
|
|
22
|
+
Requires-Dist: pytest>=7.0; extra == "dev"
|
|
23
|
+
Requires-Dist: build; extra == "dev"
|
|
24
|
+
Requires-Dist: twine; extra == "dev"
|
|
25
|
+
Provides-Extra: docs
|
|
26
|
+
Requires-Dist: mkdocs>=1.6; extra == "docs"
|
|
27
|
+
Requires-Dist: mkdocs-material>=9.5; extra == "docs"
|
|
28
|
+
Requires-Dist: mkdocstrings[python]>=0.25; extra == "docs"
|
|
29
|
+
Dynamic: license-file
|
|
30
|
+
|
|
31
|
+
# moofs
|
|
32
|
+
|
|
33
|
+
**Multi-Objective Optimization for Feature Selection**
|
|
34
|
+
|
|
35
|
+
A Python library for multi-objective feature selection with a unified, scikit-learn compatible API. `moofs` searches for the best trade-offs between **classification error** and **number of selected features**, returns the full Pareto front, and lets you pick the subset that fits your needs.
|
|
36
|
+
|
|
37
|
+
## Installation
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
pip install moofs
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Quick start
|
|
44
|
+
|
|
45
|
+
```python
|
|
46
|
+
from moofs import MOFSSelector
|
|
47
|
+
|
|
48
|
+
selector = MOFSSelector(algorithm="mofs-rfga", max_evals=5000, random_state=0)
|
|
49
|
+
X_reduced = selector.fit_transform(X, y)
|
|
50
|
+
|
|
51
|
+
selector.pareto_front_ # (n_solutions, 2): [error %, subset size]
|
|
52
|
+
selector.support_ # boolean mask of the chosen subset
|
|
53
|
+
selector.get_feature_names_out()
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
It drops into any scikit-learn pipeline:
|
|
57
|
+
|
|
58
|
+
```python
|
|
59
|
+
from sklearn.pipeline import Pipeline
|
|
60
|
+
from sklearn.neighbors import KNeighborsClassifier
|
|
61
|
+
|
|
62
|
+
pipe = Pipeline([
|
|
63
|
+
("fs", MOFSSelector(max_evals=5000, random_state=0)),
|
|
64
|
+
("clf", KNeighborsClassifier(n_neighbors=3)),
|
|
65
|
+
]).fit(X_train, y_train)
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## Available algorithms
|
|
69
|
+
|
|
70
|
+
| Algorithm | Key | Authors | Reference |
|
|
71
|
+
|-----------|-----|---------|-----------|
|
|
72
|
+
| **MOFS-RFGA** | `mofs-rfga` | Xue, Zhu & Neri (2023) | [paper](https://doi.org/10.1016/j.asoc.2023.109987) |
|
|
73
|
+
| **NSGA-II** | `nsga2` | Deb, Pratap, Agarwal & Meyarivan (2002) | [paper](https://doi.org/10.1109/4235.996017) |
|
|
74
|
+
|
|
75
|
+
More algorithms from the MOFS literature (SparseEA, NSGA-II/SDR, SPEA2, MOEA/D, NSPSOFS, CMDPSOFS) are planned for upcoming releases — see the [CHANGELOG](CHANGELOG.md).
|
|
76
|
+
|
|
77
|
+
## Visualizing Pareto fronts
|
|
78
|
+
|
|
79
|
+
```python
|
|
80
|
+
from moofs import plot_selector, plot_fronts
|
|
81
|
+
|
|
82
|
+
plot_selector(selector) # front + highlighted chosen subset
|
|
83
|
+
plot_fronts({"MOFS-RFGA": r1, "NSGA-II": r2}, reference=True)
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
## Metrics
|
|
87
|
+
|
|
88
|
+
Quality indicators follow the **PlatEMO definitions** used in the MOFS literature, so values are directly comparable with published tables: `igd`, `hv` (normalized, reference point (1,1)), `coverage` (weak dominance), `nfs`, `spacing`.
|
|
89
|
+
|
|
90
|
+
```python
|
|
91
|
+
from moofs import compare
|
|
92
|
+
|
|
93
|
+
table = compare({"MOFS-RFGA": r1, "NSGA-II": r2})
|
|
94
|
+
# algorithm IGD HV NFS best_error_% min_subset_size
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
## Research-style API
|
|
98
|
+
|
|
99
|
+
For experiments and full control over the search:
|
|
100
|
+
|
|
101
|
+
```python
|
|
102
|
+
from moofs import FeatureSelectionProblem, MOFSRFGA, NSGA2
|
|
103
|
+
|
|
104
|
+
problem = FeatureSelectionProblem(X, y) # KNN k=3, 3-fold CV, cached
|
|
105
|
+
result = MOFSRFGA(problem, pop_size=100, max_evals=300_000, seed=0).run()
|
|
106
|
+
result.F # objective matrix of the Pareto front
|
|
107
|
+
result.front # solutions with binary masks (.x)
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
The evaluation protocol follows the reference paper: k-NN (k=3) classifier, 3-fold cross-validation, objectives = (classification error %, subset size). Evaluations are memoized; cache hits still count toward `max_evals` so budgets stay comparable.
|
|
111
|
+
|
|
112
|
+
## Faithfulness notes
|
|
113
|
+
|
|
114
|
+
Implementations are traceable to their source papers, and ambiguities are documented rather than silently resolved. Notably, the MOFS-RFGA paper's Fig. 1 and its Algorithm 3 disagree on the crossover semantics; `moofs` defaults to the Fig. 1 reading (consistent with the mutation operator) and exposes `interpretation="pseudocode"` for the literal alternative. See the documentation for details.
|
|
115
|
+
|
|
116
|
+
## License
|
|
117
|
+
|
|
118
|
+
MIT — see [LICENSE](LICENSE).
|
|
119
|
+
|
|
120
|
+
## Citing
|
|
121
|
+
|
|
122
|
+
If you use `moofs` in academic work, please cite the underlying algorithm papers (see the table above). A citable software DOI is planned.
|
moofs-0.1.0/README.md
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
# moofs
|
|
2
|
+
|
|
3
|
+
**Multi-Objective Optimization for Feature Selection**
|
|
4
|
+
|
|
5
|
+
A Python library for multi-objective feature selection with a unified, scikit-learn compatible API. `moofs` searches for the best trade-offs between **classification error** and **number of selected features**, returns the full Pareto front, and lets you pick the subset that fits your needs.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pip install moofs
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Quick start
|
|
14
|
+
|
|
15
|
+
```python
|
|
16
|
+
from moofs import MOFSSelector
|
|
17
|
+
|
|
18
|
+
selector = MOFSSelector(algorithm="mofs-rfga", max_evals=5000, random_state=0)
|
|
19
|
+
X_reduced = selector.fit_transform(X, y)
|
|
20
|
+
|
|
21
|
+
selector.pareto_front_ # (n_solutions, 2): [error %, subset size]
|
|
22
|
+
selector.support_ # boolean mask of the chosen subset
|
|
23
|
+
selector.get_feature_names_out()
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
It drops into any scikit-learn pipeline:
|
|
27
|
+
|
|
28
|
+
```python
|
|
29
|
+
from sklearn.pipeline import Pipeline
|
|
30
|
+
from sklearn.neighbors import KNeighborsClassifier
|
|
31
|
+
|
|
32
|
+
pipe = Pipeline([
|
|
33
|
+
("fs", MOFSSelector(max_evals=5000, random_state=0)),
|
|
34
|
+
("clf", KNeighborsClassifier(n_neighbors=3)),
|
|
35
|
+
]).fit(X_train, y_train)
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Available algorithms
|
|
39
|
+
|
|
40
|
+
| Algorithm | Key | Authors | Reference |
|
|
41
|
+
|-----------|-----|---------|-----------|
|
|
42
|
+
| **MOFS-RFGA** | `mofs-rfga` | Xue, Zhu & Neri (2023) | [paper](https://doi.org/10.1016/j.asoc.2023.109987) |
|
|
43
|
+
| **NSGA-II** | `nsga2` | Deb, Pratap, Agarwal & Meyarivan (2002) | [paper](https://doi.org/10.1109/4235.996017) |
|
|
44
|
+
|
|
45
|
+
More algorithms from the MOFS literature (SparseEA, NSGA-II/SDR, SPEA2, MOEA/D, NSPSOFS, CMDPSOFS) are planned for upcoming releases — see the [CHANGELOG](CHANGELOG.md).
|
|
46
|
+
|
|
47
|
+
## Visualizing Pareto fronts
|
|
48
|
+
|
|
49
|
+
```python
|
|
50
|
+
from moofs import plot_selector, plot_fronts
|
|
51
|
+
|
|
52
|
+
plot_selector(selector) # front + highlighted chosen subset
|
|
53
|
+
plot_fronts({"MOFS-RFGA": r1, "NSGA-II": r2}, reference=True)
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## Metrics
|
|
57
|
+
|
|
58
|
+
Quality indicators follow the **PlatEMO definitions** used in the MOFS literature, so values are directly comparable with published tables: `igd`, `hv` (normalized, reference point (1,1)), `coverage` (weak dominance), `nfs`, `spacing`.
|
|
59
|
+
|
|
60
|
+
```python
|
|
61
|
+
from moofs import compare
|
|
62
|
+
|
|
63
|
+
table = compare({"MOFS-RFGA": r1, "NSGA-II": r2})
|
|
64
|
+
# algorithm IGD HV NFS best_error_% min_subset_size
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Research-style API
|
|
68
|
+
|
|
69
|
+
For experiments and full control over the search:
|
|
70
|
+
|
|
71
|
+
```python
|
|
72
|
+
from moofs import FeatureSelectionProblem, MOFSRFGA, NSGA2
|
|
73
|
+
|
|
74
|
+
problem = FeatureSelectionProblem(X, y) # KNN k=3, 3-fold CV, cached
|
|
75
|
+
result = MOFSRFGA(problem, pop_size=100, max_evals=300_000, seed=0).run()
|
|
76
|
+
result.F # objective matrix of the Pareto front
|
|
77
|
+
result.front # solutions with binary masks (.x)
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
The evaluation protocol follows the reference paper: k-NN (k=3) classifier, 3-fold cross-validation, objectives = (classification error %, subset size). Evaluations are memoized; cache hits still count toward `max_evals` so budgets stay comparable.
|
|
81
|
+
|
|
82
|
+
## Faithfulness notes
|
|
83
|
+
|
|
84
|
+
Implementations are traceable to their source papers, and ambiguities are documented rather than silently resolved. Notably, the MOFS-RFGA paper's Fig. 1 and its Algorithm 3 disagree on the crossover semantics; `moofs` defaults to the Fig. 1 reading (consistent with the mutation operator) and exposes `interpretation="pseudocode"` for the literal alternative. See the documentation for details.
|
|
85
|
+
|
|
86
|
+
## License
|
|
87
|
+
|
|
88
|
+
MIT — see [LICENSE](LICENSE).
|
|
89
|
+
|
|
90
|
+
## Citing
|
|
91
|
+
|
|
92
|
+
If you use `moofs` in academic work, please cite the underlying algorithm papers (see the table above). A citable software DOI is planned.
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61.0", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "moofs"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Multi-objective feature selection with a scikit-learn API: MOFS-RFGA, NSGA-II, Pareto-front visualization and PlatEMO-compatible metrics"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
authors = [{ name = "Herman Motcheyo" }]
|
|
11
|
+
license = { text = "MIT" }
|
|
12
|
+
requires-python = ">=3.9"
|
|
13
|
+
keywords = ["feature-selection", "multi-objective", "nsga2", "spea2", "moead", "sparseea", "pso", "relieff", "benchmark"]
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Development Status :: 4 - Beta",
|
|
16
|
+
"Intended Audience :: Science/Research",
|
|
17
|
+
"License :: OSI Approved :: MIT License",
|
|
18
|
+
"Programming Language :: Python :: 3",
|
|
19
|
+
"Topic :: Scientific/Engineering :: Artificial Intelligence",
|
|
20
|
+
]
|
|
21
|
+
dependencies = [
|
|
22
|
+
"numpy>=1.21",
|
|
23
|
+
"pandas>=1.3",
|
|
24
|
+
"scikit-learn>=1.0",
|
|
25
|
+
"matplotlib>=3.5",
|
|
26
|
+
]
|
|
27
|
+
|
|
28
|
+
[project.optional-dependencies]
|
|
29
|
+
dev = ["pytest>=7.0", "build", "twine"]
|
|
30
|
+
docs = ["mkdocs>=1.6", "mkdocs-material>=9.5", "mkdocstrings[python]>=0.25"]
|
|
31
|
+
|
|
32
|
+
[project.urls]
|
|
33
|
+
Homepage = "https://github.com/Herman-Motcheyo/moofs"
|
|
34
|
+
|
|
35
|
+
[tool.setuptools.packages.find]
|
|
36
|
+
where = ["src"]
|
|
37
|
+
|
|
38
|
+
[tool.pytest.ini_options]
|
|
39
|
+
testpaths = ["tests"]
|
moofs-0.1.0/setup.cfg
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"""moofs — Multi-Objective Optimization for Feature Selection.
|
|
2
|
+
|
|
3
|
+
A reference library of multi-objective feature-selection algorithms with a
|
|
4
|
+
scikit-learn compatible API, Pareto-front visualization, and
|
|
5
|
+
PlatEMO-compatible quality indicators.
|
|
6
|
+
|
|
7
|
+
v0.1 algorithms: MOFS-RFGA (Xue, Zhu & Neri, 2023) and NSGA-II (Deb et al.,
|
|
8
|
+
2002).
|
|
9
|
+
|
|
10
|
+
Quick start
|
|
11
|
+
-----------
|
|
12
|
+
>>> from moofs import MOFSSelector
|
|
13
|
+
>>> selector = MOFSSelector(algorithm="mofs-rfga", max_evals=2000,
|
|
14
|
+
... random_state=0)
|
|
15
|
+
>>> X_reduced = selector.fit_transform(X, y)
|
|
16
|
+
|
|
17
|
+
Research-style API
|
|
18
|
+
------------------
|
|
19
|
+
>>> from moofs import FeatureSelectionProblem, MOFSRFGA
|
|
20
|
+
>>> problem = FeatureSelectionProblem(X, y)
|
|
21
|
+
>>> result = MOFSRFGA(problem, pop_size=60, max_evals=2000, seed=0).run()
|
|
22
|
+
>>> result.F
|
|
23
|
+
"""
|
|
24
|
+
|
|
25
|
+
from .algorithms import MOFSRFGA, NSGA2
|
|
26
|
+
from .core import (
|
|
27
|
+
Algorithm,
|
|
28
|
+
FeatureSelectionProblem,
|
|
29
|
+
Problem,
|
|
30
|
+
Result,
|
|
31
|
+
Solution,
|
|
32
|
+
)
|
|
33
|
+
from .filters import mutual_information, relieff
|
|
34
|
+
from .metrics import (
|
|
35
|
+
compare,
|
|
36
|
+
coverage,
|
|
37
|
+
gd,
|
|
38
|
+
hv,
|
|
39
|
+
hv_raw,
|
|
40
|
+
igd,
|
|
41
|
+
merge_reference_front,
|
|
42
|
+
nfs,
|
|
43
|
+
spacing,
|
|
44
|
+
)
|
|
45
|
+
from .plotting import plot_fronts, plot_pareto_front, plot_selector
|
|
46
|
+
from .selection import MOFSSelector
|
|
47
|
+
|
|
48
|
+
__version__ = "0.1.0"
|
|
49
|
+
|
|
50
|
+
ALGORITHMS = {
|
|
51
|
+
"mofs-rfga": MOFSRFGA,
|
|
52
|
+
"nsga2": NSGA2,
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
__all__ = [
|
|
56
|
+
"MOFSSelector",
|
|
57
|
+
"MOFSRFGA", "NSGA2", "ALGORITHMS",
|
|
58
|
+
"Problem", "FeatureSelectionProblem", "Solution", "Algorithm", "Result",
|
|
59
|
+
"relieff", "mutual_information",
|
|
60
|
+
"igd", "gd", "hv", "hv_raw", "coverage", "nfs", "spacing",
|
|
61
|
+
"merge_reference_front", "compare",
|
|
62
|
+
"plot_pareto_front", "plot_fronts", "plot_selector",
|
|
63
|
+
"__version__",
|
|
64
|
+
]
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
"""MOFS-RFGA: Multi-Objective Feature Selection with ReliefF and Genetic
|
|
2
|
+
Algorithm.
|
|
3
|
+
|
|
4
|
+
Reference: Y. Xue, H. Zhu, F. Neri, "A feature selection approach based on
|
|
5
|
+
NSGA-II with ReliefF", Applied Soft Computing 134 (2023) 109987.
|
|
6
|
+
https://doi.org/10.1016/j.asoc.2023.109987
|
|
7
|
+
|
|
8
|
+
Hybrid filter/wrapper method: a ReliefF (or any filter) score vector guides
|
|
9
|
+
the initialisation, a 3-parent crossover and the mutation; NSGA-II
|
|
10
|
+
environmental selection drives the multi-objective search.
|
|
11
|
+
|
|
12
|
+
Note on the crossover semantics: the paper's Fig. 1 and its Algorithm 3
|
|
13
|
+
disagree on which gene is removed from S2 / added from S1. The default here
|
|
14
|
+
follows Fig. 1 (remove the *worse*-scored gene from S2, add the
|
|
15
|
+
*better*-scored gene from S1), which is consistent with the mutation
|
|
16
|
+
operator's prose and the score-guided philosophy of the method. Pass
|
|
17
|
+
``interpretation="pseudocode"`` for the literal Algorithm 3 reading.
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
import numpy as np
|
|
21
|
+
|
|
22
|
+
from ..core.algorithm import Algorithm
|
|
23
|
+
from ..core.dominance import dominates, environmental_selection
|
|
24
|
+
from ..core.operators import ensure_nonempty
|
|
25
|
+
from ..filters import relieff
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class MOFSRFGA(Algorithm):
|
|
29
|
+
"""MOFS-RFGA in the unified API.
|
|
30
|
+
|
|
31
|
+
Parameters
|
|
32
|
+
----------
|
|
33
|
+
sc : array-like of float, optional
|
|
34
|
+
Feature-score vector (higher = better). If None, ReliefF scores are
|
|
35
|
+
computed automatically from ``problem.X`` and ``problem.y``.
|
|
36
|
+
D_init : int, optional
|
|
37
|
+
Upper bound on the number of features selected at initialisation;
|
|
38
|
+
defaults to ``n_var``.
|
|
39
|
+
"""
|
|
40
|
+
|
|
41
|
+
name = "MOFS-RFGA"
|
|
42
|
+
|
|
43
|
+
def __init__(self, problem, pop_size=60, max_evals=20000, sc=None,
|
|
44
|
+
D_init=None, interpretation="figure", seed=None,
|
|
45
|
+
verbose=False):
|
|
46
|
+
super().__init__(problem, pop_size, max_evals, seed, verbose)
|
|
47
|
+
if sc is None:
|
|
48
|
+
sc = relieff(problem.X, problem.y)
|
|
49
|
+
self.sc = np.asarray(sc, dtype=float)
|
|
50
|
+
self.D_init = D_init if D_init is not None else problem.n_var
|
|
51
|
+
if interpretation not in ("figure", "pseudocode"):
|
|
52
|
+
raise ValueError("interpretation must be 'figure' or 'pseudocode'")
|
|
53
|
+
self.interpretation = interpretation
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def _initial_population(self):
|
|
57
|
+
pop = []
|
|
58
|
+
for _ in range(self.pop_size):
|
|
59
|
+
x = np.zeros(self.problem.n_var, dtype=int)
|
|
60
|
+
R = self.rng.integers(1, max(2, self.D_init))
|
|
61
|
+
for _ in range(R):
|
|
62
|
+
i, j = self.rng.choice(len(self.sc), size=2, replace=False)
|
|
63
|
+
x[i if self.sc[i] >= self.sc[j] else j] = 1
|
|
64
|
+
x = ensure_nonempty(x, self.rng)
|
|
65
|
+
pop.append(self.evaluate(x))
|
|
66
|
+
return pop
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def _tournament_parents(self, pop, k=3):
|
|
70
|
+
parents = []
|
|
71
|
+
for _ in range(k):
|
|
72
|
+
i, j = self.rng.choice(len(pop), size=2, replace=False)
|
|
73
|
+
a, b = pop[i], pop[j]
|
|
74
|
+
if dominates(a.F, b.F):
|
|
75
|
+
parents.append(a)
|
|
76
|
+
elif dominates(b.F, a.F):
|
|
77
|
+
parents.append(b)
|
|
78
|
+
else:
|
|
79
|
+
parents.append(a if self.rng.random() < 0.5 else b)
|
|
80
|
+
return parents
|
|
81
|
+
|
|
82
|
+
def _crossover_3_to_1(self, p1, p2, p3):
|
|
83
|
+
L1, L2, L3 = p1 & p2, p1 & p3, p2 & p3
|
|
84
|
+
O = np.logical_or(L1, np.logical_or(L2, L3)).astype(int)
|
|
85
|
+
S3 = L1 & L2 & L3 # genes selected 3 times
|
|
86
|
+
S2 = O ^ S3 # genes selected exactly twice
|
|
87
|
+
S1 = np.logical_or(p1, np.logical_or(p2, p3)).astype(int) ^ S3 ^ S2
|
|
88
|
+
remove_better = self.interpretation == "pseudocode"
|
|
89
|
+
if self.rng.random() < 0.5:
|
|
90
|
+
cand = np.where(S2 == 1)[0]
|
|
91
|
+
if len(cand) > 0:
|
|
92
|
+
picks = self.rng.choice(cand, size=min(2, len(cand)),
|
|
93
|
+
replace=False)
|
|
94
|
+
key = max if remove_better else min
|
|
95
|
+
target = key(picks, key=lambda d: self.sc[d])
|
|
96
|
+
O[target] = 0
|
|
97
|
+
else:
|
|
98
|
+
cand = np.where(S1 == 1)[0]
|
|
99
|
+
if len(cand) > 0:
|
|
100
|
+
picks = self.rng.choice(cand, size=min(2, len(cand)),
|
|
101
|
+
replace=False)
|
|
102
|
+
key = min if remove_better else max
|
|
103
|
+
target = key(picks, key=lambda d: self.sc[d])
|
|
104
|
+
O[target] = 1
|
|
105
|
+
return O
|
|
106
|
+
|
|
107
|
+
def _mutation(self, o):
|
|
108
|
+
o = o.copy()
|
|
109
|
+
if self.rng.random() < 0.5:
|
|
110
|
+
cand = np.where(o == 1)[0]
|
|
111
|
+
if len(cand) > 0:
|
|
112
|
+
picks = self.rng.choice(cand, size=min(2, len(cand)),
|
|
113
|
+
replace=False)
|
|
114
|
+
worst = min(picks, key=lambda d: self.sc[d])
|
|
115
|
+
o[worst] = 0
|
|
116
|
+
else:
|
|
117
|
+
cand = np.where(o == 0)[0]
|
|
118
|
+
if len(cand) > 0:
|
|
119
|
+
picks = self.rng.choice(cand, size=min(2, len(cand)),
|
|
120
|
+
replace=False)
|
|
121
|
+
best = max(picks, key=lambda d: self.sc[d])
|
|
122
|
+
o[best] = 1
|
|
123
|
+
return o
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
def _run(self):
|
|
127
|
+
pop = self._initial_population()
|
|
128
|
+
pop = environmental_selection(pop, self.pop_size)
|
|
129
|
+
gen = 0
|
|
130
|
+
while self.budget_left():
|
|
131
|
+
offspring = []
|
|
132
|
+
while len(offspring) < self.pop_size and self.budget_left():
|
|
133
|
+
p1, p2, p3 = self._tournament_parents(pop)
|
|
134
|
+
c = self._crossover_3_to_1(p1.x, p2.x, p3.x)
|
|
135
|
+
c = self._mutation(c)
|
|
136
|
+
c = ensure_nonempty(c, self.rng)
|
|
137
|
+
offspring.append(self.evaluate(c))
|
|
138
|
+
pop = environmental_selection(pop + offspring, self.pop_size)
|
|
139
|
+
gen += 1
|
|
140
|
+
self.log(f"gen={gen} evals={self.n_evals}")
|
|
141
|
+
return self._result(pop)
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"""NSGA-II (Deb et al., 2002) with binary encoding for feature selection.
|
|
2
|
+
|
|
3
|
+
Default parameters follow the MOFS literature: Pc = 0.9, Pm = 1/D.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from ..core.algorithm import Algorithm
|
|
7
|
+
from ..core.dominance import environmental_selection
|
|
8
|
+
from ..core.operators import (
|
|
9
|
+
binary_tournament,
|
|
10
|
+
bitflip_mutation,
|
|
11
|
+
ensure_nonempty,
|
|
12
|
+
single_point_crossover,
|
|
13
|
+
uniform_crossover,
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class NSGA2(Algorithm):
|
|
18
|
+
"""NSGA-II for binary multi-objective feature selection.
|
|
19
|
+
|
|
20
|
+
Parameters
|
|
21
|
+
----------
|
|
22
|
+
pc : float, default=0.9
|
|
23
|
+
Crossover probability.
|
|
24
|
+
pm : float, optional
|
|
25
|
+
Per-gene mutation probability; defaults to 1/D.
|
|
26
|
+
crossover : {"single_point", "uniform"}, default="single_point"
|
|
27
|
+
"""
|
|
28
|
+
|
|
29
|
+
name = "NSGA-II"
|
|
30
|
+
|
|
31
|
+
def __init__(self, problem, pop_size=60, max_evals=20000, pc=0.9, pm=None,
|
|
32
|
+
crossover="single_point", seed=None, verbose=False):
|
|
33
|
+
super().__init__(problem, pop_size, max_evals, seed, verbose)
|
|
34
|
+
self.pc = pc
|
|
35
|
+
self.pm = pm if pm is not None else 1.0 / problem.n_var
|
|
36
|
+
self._crossover = (uniform_crossover if crossover == "uniform"
|
|
37
|
+
else single_point_crossover)
|
|
38
|
+
|
|
39
|
+
def _initial_population(self):
|
|
40
|
+
pop = []
|
|
41
|
+
for _ in range(self.pop_size):
|
|
42
|
+
x = (self.rng.random(self.problem.n_var) < 0.5).astype(int)
|
|
43
|
+
x = ensure_nonempty(x, self.rng)
|
|
44
|
+
pop.append(self.evaluate(x))
|
|
45
|
+
return pop
|
|
46
|
+
|
|
47
|
+
def _sort_population(self, population):
|
|
48
|
+
"""Hook overridden by NSGA-II/SDR."""
|
|
49
|
+
return environmental_selection(population, self.pop_size)
|
|
50
|
+
|
|
51
|
+
def _run(self):
|
|
52
|
+
pop = self._initial_population()
|
|
53
|
+
pop = self._sort_population(pop)
|
|
54
|
+
gen = 0
|
|
55
|
+
while self.budget_left():
|
|
56
|
+
offspring = []
|
|
57
|
+
while len(offspring) < self.pop_size and self.budget_left():
|
|
58
|
+
p1 = binary_tournament(pop, self.rng)
|
|
59
|
+
p2 = binary_tournament(pop, self.rng)
|
|
60
|
+
c1, c2 = self._crossover(p1.x, p2.x, self.rng, pc=self.pc)
|
|
61
|
+
for c in (c1, c2):
|
|
62
|
+
if len(offspring) >= self.pop_size or not self.budget_left():
|
|
63
|
+
break
|
|
64
|
+
c = bitflip_mutation(c, self.rng, pm=self.pm)
|
|
65
|
+
c = ensure_nonempty(c, self.rng)
|
|
66
|
+
offspring.append(self.evaluate(c))
|
|
67
|
+
pop = self._sort_population(pop + offspring)
|
|
68
|
+
gen += 1
|
|
69
|
+
self.log(f"gen={gen} evals={self.n_evals}")
|
|
70
|
+
return self._result(pop)
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
from .algorithm import Algorithm, Result
|
|
2
|
+
from .dominance import (
|
|
3
|
+
crowding_distance,
|
|
4
|
+
dominates,
|
|
5
|
+
environmental_selection,
|
|
6
|
+
fast_non_dominated_sort,
|
|
7
|
+
nondominated,
|
|
8
|
+
sdr_sort,
|
|
9
|
+
)
|
|
10
|
+
from .problem import FeatureSelectionProblem, Problem
|
|
11
|
+
from .solution import Solution, population_F
|
|
12
|
+
|
|
13
|
+
__all__ = [
|
|
14
|
+
"Algorithm", "Result", "Problem", "FeatureSelectionProblem",
|
|
15
|
+
"Solution", "population_F", "dominates", "fast_non_dominated_sort",
|
|
16
|
+
"crowding_distance", "environmental_selection", "nondominated", "sdr_sort",
|
|
17
|
+
]
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
"""Base class shared by every algorithm, and the Result container."""
|
|
2
|
+
|
|
3
|
+
import numpy as np
|
|
4
|
+
|
|
5
|
+
from .dominance import nondominated
|
|
6
|
+
from .solution import Solution, population_F
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class Result:
|
|
10
|
+
"""Outcome of a run.
|
|
11
|
+
|
|
12
|
+
Attributes
|
|
13
|
+
----------
|
|
14
|
+
front : list of Solution
|
|
15
|
+
Non-dominated solutions found.
|
|
16
|
+
population : list of Solution
|
|
17
|
+
Final population (or archive).
|
|
18
|
+
n_evals : int
|
|
19
|
+
Number of objective-function evaluations consumed.
|
|
20
|
+
algorithm : str
|
|
21
|
+
Name of the algorithm.
|
|
22
|
+
"""
|
|
23
|
+
|
|
24
|
+
def __init__(self, front, population, n_evals, algorithm):
|
|
25
|
+
self.front = front
|
|
26
|
+
self.population = population
|
|
27
|
+
self.n_evals = n_evals
|
|
28
|
+
self.algorithm = algorithm
|
|
29
|
+
|
|
30
|
+
@property
|
|
31
|
+
def F(self):
|
|
32
|
+
"""Objective matrix of the front (n_solutions x n_obj)."""
|
|
33
|
+
return population_F(self.front)
|
|
34
|
+
|
|
35
|
+
def __repr__(self):
|
|
36
|
+
return (f"Result(algorithm={self.algorithm!r}, "
|
|
37
|
+
f"front={len(self.front)}, n_evals={self.n_evals})")
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
class Algorithm:
|
|
41
|
+
"""Base class for all algorithms.
|
|
42
|
+
|
|
43
|
+
Parameters
|
|
44
|
+
----------
|
|
45
|
+
problem : Problem
|
|
46
|
+
pop_size : int, default=60
|
|
47
|
+
Population (or swarm/archive) size N.
|
|
48
|
+
max_evals : int, default=20000
|
|
49
|
+
Maximum number of objective-function evaluations (maxFES).
|
|
50
|
+
seed : int, optional
|
|
51
|
+
Random seed for reproducibility.
|
|
52
|
+
verbose : bool, default=False
|
|
53
|
+
"""
|
|
54
|
+
|
|
55
|
+
name = "algorithm"
|
|
56
|
+
|
|
57
|
+
def __init__(self, problem, pop_size=60, max_evals=20000, seed=None,
|
|
58
|
+
verbose=False):
|
|
59
|
+
self.problem = problem
|
|
60
|
+
self.pop_size = pop_size
|
|
61
|
+
self.max_evals = max_evals
|
|
62
|
+
self.rng = np.random.default_rng(seed)
|
|
63
|
+
self.verbose = verbose
|
|
64
|
+
self._start_evals = 0
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def evaluate(self, x):
|
|
68
|
+
"""Evaluate a decision vector and wrap it in a Solution."""
|
|
69
|
+
return Solution(x, self.problem.evaluate(x))
|
|
70
|
+
|
|
71
|
+
@property
|
|
72
|
+
def n_evals(self):
|
|
73
|
+
return self.problem.n_evals - self._start_evals
|
|
74
|
+
|
|
75
|
+
def budget_left(self):
|
|
76
|
+
return self.n_evals < self.max_evals
|
|
77
|
+
|
|
78
|
+
def log(self, *args):
|
|
79
|
+
if self.verbose:
|
|
80
|
+
print(f"[{self.name}]", *args)
|
|
81
|
+
|
|
82
|
+
def _result(self, population):
|
|
83
|
+
front = nondominated([s for s in population if s.F is not None])
|
|
84
|
+
return Result(front, population, self.n_evals, self.name)
|
|
85
|
+
|
|
86
|
+
# -- interface ---------------------------------------------------------
|
|
87
|
+
|
|
88
|
+
def run(self):
|
|
89
|
+
"""Run the algorithm until the FE budget is exhausted."""
|
|
90
|
+
self._start_evals = self.problem.n_evals
|
|
91
|
+
return self._run()
|
|
92
|
+
|
|
93
|
+
def _run(self):
|
|
94
|
+
raise NotImplementedError
|