Skip to content

Commit 786beb7

Browse files
AISP 0.4.0 (#4)
* docs: adds documentation for AISP 0.4.0 * feat(docs): update docs and include example notebooks * version: 0.4.x
1 parent 4d1db22 commit 786beb7

141 files changed

Lines changed: 13746 additions & 7 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
# Display
2+
3+
Utility Functions for Displaying Algorithm Information
4+
5+
## def _supports_box_drawing()
6+
7+
```python
8+
def _supports_box_drawing() -> bool
9+
```
10+
11+
Function to check if the terminal supports boxed characters.
12+
13+
**Returns**:
14+
15+
* ***bool*** (`bool`): True if the terminal likely supports boxed characters, False otherwise.
16+
17+
---
18+
19+
## class TableFormatter
20+
21+
Class to format tabular data into strings for display in the console.
22+
23+
**Parameters**:
24+
25+
* ***headers*** (`Mapping[str, int]`): Mapping of column names to their respective widths, in the format `{column_name: column_width}`.
26+
27+
**Raises**:
28+
29+
* `ValueError`: If `headers` is empty or not a valid mapping.
30+
31+
---
32+
33+
### def _border(left, middle, right, line, new_line=True)
34+
35+
```python
36+
def _border(self, left: str, middle: str, right: str, line: str, new_line: bool = True) -> str
37+
```
38+
39+
Create a horizontal border for the table.
40+
41+
**Parameters**:
42+
43+
* ***left*** (`str`): Character on the left side of the border.
44+
* ***middle*** (`str`): Character separator between columns.
45+
* ***right*** (`str`): Character on the right side of the border.
46+
* ***line*** (`str`): Character used to fill the border.
47+
* ***new_line*** (`bool`, optional): If True, adds a line break before the border (default is True).
48+
49+
**Returns**:
50+
51+
* ***border*** (`str`): String representing the horizontal border.
52+
53+
---
54+
55+
### def get_header()
56+
57+
```python
58+
def get_header(self) -> str
59+
```
60+
61+
Generate the table header, including the top border, column headings, and separator line.
62+
63+
**Returns**:
64+
65+
* ***header*** (`str`): Formatted string of the table header.
66+
67+
---
68+
69+
### def get_row(values)
70+
71+
```python
72+
def get_row(self, values: Mapping[str, Union[str, int, float]]) -> str
73+
```
74+
75+
Generate a formatted row for the table data.
76+
77+
**Parameters**:
78+
79+
* ***values*** (`Mapping[str, Union[str, int, float]]`): Dictionary with values for each column, in the format `{column_name: value}`.
80+
81+
**Returns**:
82+
83+
* ***row*** (`str`): Formatted string of the table row.
84+
85+
---
86+
87+
### def get_bottom(new_line=False)
88+
89+
```python
90+
def get_bottom(self, new_line: bool = False) -> str
91+
```
92+
93+
Generate the table's bottom border.
94+
95+
**Parameters**:
96+
97+
* ***new_line*** (`bool`, optional): If True, adds a line break before the border (default is False).
98+
99+
**Returns**:
100+
101+
* ***bottom*** (`str`): Formatted string for the bottom border.
102+
103+
---
104+
105+
## class ProgressTable(TableFormatter)
106+
107+
Class to display a formatted table in the console to track the algorithm's progress.
108+
109+
**Parameters**:
110+
111+
* ***headers*** (`Mapping[str, int]`): Mapping `{column_name: column_width}`.
112+
* ***verbose*** (`bool`): If False, prints nothing to the terminal.
113+
114+
**Raises**:
115+
116+
* `ValueError`: If `headers` is empty or not a valid mapping.
117+
118+
---
119+
120+
### def _print_header()
121+
122+
```python
123+
def _print_header(self) -> None
124+
```
125+
126+
Print the table header.
127+
128+
---
129+
130+
### def update(values)
131+
132+
```python
133+
def update(self, values: Mapping[str, Union[str, int, float]]) -> None
134+
```
135+
136+
Add a new row of values to the table.
137+
138+
**Parameters**:
139+
140+
* ***values*** (`Mapping[str, Union[str, int, float]]`): Keys must match the columns defined in headers.
141+
142+
---
143+
144+
### def finish()
145+
146+
```python
147+
def finish(self) -> None
148+
```
149+
150+
End the table display, printing the bottom border and total time.
151+
152+
---

‎docs/advanced-guides/Utils/Sanitizers.md‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,26 @@ The function ``sanitize_param(...)``, returns the seed if it is a non-negative i
5757
* seed (``Any``): The seed value to be validated.
5858

5959
**Returns:**
60-
* ``Optional[int]``: The original seed if it is a non-negative integer, or ``None`` if it is invalid.
60+
* ``Optional[int]``: The original seed if it is a non-negative integer, or ``None`` if it is invalid.
61+
62+
---
63+
64+
## def sanitize_bounds(...)
65+
66+
```python
67+
def sanitize_bounds(
68+
bounds: Any,
69+
problem_size: int
70+
) -> Dict[str, npt.NDArray[np.float64]]
71+
```
72+
73+
The function ``sanitize_bounds(...)``, validate and normalize feature bounds.
74+
75+
**Parameters:**
76+
* ***bounds*** (``Any``): he input bounds, which must be either None or a dictionary with 'low' and 'high' keys.
77+
* ***problem_size*** (``int``): The expected length for the normalized bounds lists, corresponding to the number of features in the problem.
78+
79+
80+
**Returns:**
81+
* `Dict[str, list]`: Dictionary ``{'low': [low_1, ..., low_N], 'high': [high_1, ..., high_N]}``.
82+

‎docs/advanced-guides/base-module/Mutation.md‎

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
sidebar_position: 4
2+
sidebar_position: 5
33
title: Mutation
44
sidebar_label: Mutation
55
lastUpdatedAt: 2025/04/04
@@ -94,4 +94,32 @@ This function creates `n` clones of the input vector and applies random mutation
9494

9595
### Returns
9696

97-
* `clone_set` (`npt.NDArray[np.float64]`): Array with shape `(n, len(vector))` containing the `n` mutated clones of the original vector.
97+
* `clone_set` (`npt.NDArray[np.float64]`): Array with shape `(n, len(vector))` containing the `n` mutated clones of the original vector.
98+
99+
---
100+
101+
## clone_and_mutate_permutation
102+
103+
```python
104+
@njit([(types.int64[:], types.int64, types.float64)], cache=True)
105+
def clone___and_mutate_permutation(
106+
vector: npt.NDArray[np.int64],
107+
n: int,
108+
mutation_rate: float
109+
) -> npt.NDArray[np.int64]:
110+
```
111+
112+
Generates a set of mutated clones from a permutation vector.
113+
114+
This function creates `n` clones of the input permutation vector and applies random mutations to each one, simulating clonal expansion in artificial immune systems with discrete permutations. Each clone receives a random number of swaps according to the mutation rate.
115+
116+
### Parameters
117+
118+
* `vector` (`npt.NDArray[np.int64]`): The original immune cell with permutation values to be cloned and mutated.
119+
* `n` (`int`): Number of mutated clones to be generated.
120+
* `mutation_rate` (`float`): Probability of mutating each component ($$0 <= mutation\_rate < 1$$).
121+
122+
### Returns
123+
124+
* `clone_set` (`npt.NDArray[np.int64]`): Array with shape `(n, len(vector))` containing the `n` mutated clones of the original vector.
125+
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
---
2+
sidebar_position: 4
3+
title: Base class for optimization algorithms.
4+
sidebar_label: BaseOptimizer
5+
lastUpdatedAt: 2025/08/19
6+
author: João Paulo
7+
keywords:
8+
- BaseOptimizer
9+
- base class
10+
- optimization algorithms
11+
- abstract base class
12+
- machine learning optimization
13+
- supervised learning
14+
- optimize method
15+
- objective function
16+
- model evaluation
17+
- Python ML classes
18+
- Clonalg
19+
- metaheuristics
20+
---
21+
22+
23+
This class defines the core interface for optimization strategies and
24+
keeps track of the cost history, evaluated solutions, and the best solution found. Subclasses must implement
25+
``optimize`` and ``objective_function``.
26+
27+
---
28+
29+
### Properties
30+
31+
#### `cost_history`
32+
33+
```python
34+
@property
35+
def cost_history(self) -> List[float]
36+
```
37+
38+
Returns the history of costs during optimization.
39+
40+
---
41+
42+
#### `solution_history`
43+
44+
```python
45+
@property
46+
def solution_history(self) -> List
47+
```
48+
49+
Returns the history of evaluated solutions.
50+
51+
---
52+
53+
#### `best_solution`
54+
55+
```python
56+
@property
57+
def best_solution(self) -> Optional[Any]
58+
```
59+
60+
Returns the best solution found so far, or `None` if unavailable.
61+
62+
---
63+
64+
#### `best_cost`
65+
66+
```python
67+
@property
68+
def best_cost(self) -> Optional[float]
69+
```
70+
71+
Returns the cost of the best solution found so far, or `None` if unavailable.
72+
73+
---
74+
75+
## Functions
76+
77+
### Function _record_best(...)
78+
79+
```python
80+
def _record_best(self, cost: float, best_solution: Any) -> None
81+
```
82+
83+
Record a new cost value and update the best solution if improved.
84+
85+
**Parameters**:
86+
* ***cost***: `float` - Cost value to be added to the history.
87+
88+
---
89+
90+
### Function get_report()
91+
92+
```python
93+
def get_report(self) -> str
94+
```
95+
96+
Generate a formatted summary report of the optimization process. The report includes the best solution,
97+
its associated cost, and the evolution of cost values per iteration.
98+
99+
**Returns**:
100+
* **report**: `str` - A formatted string containing the optimization summary.
101+
102+
---
103+
104+
### Function register(...)
105+
106+
```python
107+
def register(self, alias: str, function: Callable[..., Any]) -> None
108+
```
109+
110+
Register a function dynamically in the optimizer instance.
111+
112+
**Parameters**:
113+
* ***alias***: `str` - Name used to access the function as an attribute.
114+
* ***function***: `Callable[..., Any]` - Callable to be registered.
115+
116+
**Raises**:
117+
* **TypeError**: If `function` is not callable.
118+
* **AttributeError**: If `alias` is protected and cannot be modified, or if `alias` does not exist in the
119+
optimizer class.
120+
121+
---
122+
123+
### Function reset()
124+
125+
```python
126+
def reset(self)
127+
```
128+
129+
Reset the object's internal state, clearing history and resetting values.
130+
131+
---
132+
133+
### Abstract methods
134+
135+
#### Function optimize(...)
136+
137+
```python
138+
def optimize(self, max_iters: int = 50, n_iter_no_change=10, verbose: bool = True) -> Any
139+
```
140+
141+
Execute the optimization process. This method must be implemented by the subclass to define how the optimization strategy explores the search space.
142+
143+
**Parameters**:
144+
* ***max_iters***: `int` - Maximum number of iterations.
145+
* ***n_iter_no_change***: `int`, default=10 - The maximum number of iterations without updating the best solution.
146+
* ***verbose***: `bool`, default=True - Flag to enable or disable detailed output during optimization.
147+
148+
**Implementation**:
149+
150+
* [Clonalg](/docs/aisp-techniques/clonal-selection-algorithms/clonalg#function-optimize)
151+
152+
---
153+
154+
#### Function affinity_function(...)
155+
156+
```python
157+
def affinity_function(self, solution: Any) -> float
158+
```
159+
160+
Evaluate the affinity of a candidate solution. This method must be implemented by the subclass to define the problem-specific.
161+
162+
**Parameters**:
163+
* ***solution***: `Any` - Candidate solution to be evaluated.
164+
165+
**Returns**:
166+
* **cost**: `float` - Cost value associated with the given solution.

0 commit comments

Comments
 (0)