Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/bioma/fig4.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import matplotlib.pyplot as plt
from problems import ackley4, griewank, schwefel2_26
from utils import IMAGES_DIR, FunctionConfig, build_cmlon
from utils import IMAGES_DIR, FunctionConfig, build_cmlon, max_workers_from_env

from lonkit import LONVisualizer

Expand Down Expand Up @@ -108,7 +108,7 @@ def main() -> None:
Path(IMAGES_DIR).mkdir(parents=True, exist_ok=True)
func_names = list(FUNCTIONS.keys())

with ProcessPoolExecutor() as executor:
with ProcessPoolExecutor(max_workers=max_workers_from_env()) as executor:
futures = {
func_name: executor.submit(build_cmlon, FUNCTIONS[func_name], N_VAR)
for func_name in func_names
Expand Down
19 changes: 15 additions & 4 deletions examples/bioma/utils.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,31 @@
from __future__ import annotations

import importlib.util
from concurrent.futures import ProcessPoolExecutor
from dataclasses import dataclass, field
from pathlib import Path
from typing import TYPE_CHECKING

import matplotlib.pyplot as plt
from matplotlib.lines import Line2D

from lonkit import CMLON, BasinHoppingSampler, BasinHoppingSamplerConfig, LONVisualizer
from lonkit.visualization import COLORS

if TYPE_CHECKING:
from collections.abc import Callable
from pathlib import Path

import numpy as np

from lonkit import CMLON, BasinHoppingSampler, BasinHoppingSamplerConfig, LONVisualizer
from lonkit.visualization import COLORS
# examples/ is not a package; load shared helpers by path.
_spec = importlib.util.spec_from_file_location(
"example_utils", Path(__file__).resolve().parents[1] / "example_utils.py"
)
assert _spec is not None and _spec.loader is not None
_example_utils = importlib.util.module_from_spec(_spec)
_spec.loader.exec_module(_example_utils)
max_workers_from_env = _example_utils.max_workers_from_env


DEFAULT_N_RUNS = 100
DEFAULT_FITNESS_PRECISION = 2
Expand Down Expand Up @@ -95,7 +106,7 @@ def build_all(
]

results: dict[tuple[str, int], tuple[CMLON, dict]] = {}
with ProcessPoolExecutor() as executor:
with ProcessPoolExecutor(max_workers=max_workers_from_env()) as executor:
futures = [executor.submit(_build_one, *t) for t in tasks]
for future in futures:
func_name, n_var, cmlon, metrics = future.result()
Expand Down
10 changes: 10 additions & 0 deletions examples/example_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from __future__ import annotations

import os

LONKIT_N_JOBS_ENV = "LONKIT_N_JOBS"


def max_workers_from_env() -> int | None:
value = os.environ.get(LONKIT_N_JOBS_ENV)
return int(value) if value is not None else None
21 changes: 14 additions & 7 deletions examples/number_partitioning/plot_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
Outputs one figure: ``NPP_phase_transition_sweep.png``
"""

import os
import importlib.util
from concurrent.futures import ProcessPoolExecutor
from pathlib import Path

Expand All @@ -34,6 +34,16 @@

from lonkit import ILSSampler, ILSSamplerConfig, LONConfig, NumberPartitioning

# examples/ is not a package; load shared helpers by path.
_spec = importlib.util.spec_from_file_location(
"example_utils", Path(__file__).resolve().parents[1] / "example_utils.py"
)
assert _spec is not None and _spec.loader is not None
_example_utils = importlib.util.module_from_spec(_spec)
_spec.loader.exec_module(_example_utils)
max_workers_from_env = _example_utils.max_workers_from_env


SMOOTH_WIDTH = 3 # uniform moving-average window (points); odd integer


Expand Down Expand Up @@ -226,8 +236,7 @@ def _plot_derivative_ax(ax):
EQ_ATOL = 1e-8

K_VALUES = np.linspace(0.1, 1.0, 50)
# Cap on worker processes, so the sweep behaves on shared machines.
N_JOBS = int(os.environ.get("LONKIT_N_JOBS", "8"))
N_JOBS = max_workers_from_env()


def sweep_one(k: float) -> dict:
Expand Down Expand Up @@ -261,10 +270,8 @@ def sweep_one(k: float) -> dict:
def main():
Path(IMAGES_DIR).mkdir(parents=True, exist_ok=True)

print(
f"Sweeping k across {len(K_VALUES)} values (n={N}, n_runs={N_RUNS}, "
f"{N_JOBS} worker processes)\n"
)
workers = f"{N_JOBS} worker processes" if N_JOBS is not None else "default workers"
print(f"Sweeping k across {len(K_VALUES)} values (n={N}, n_runs={N_RUNS}, " f"{workers})\n")

with ProcessPoolExecutor(max_workers=N_JOBS) as executor:
records = list(executor.map(sweep_one, K_VALUES))
Expand Down
Binary file modified images/fig1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/fig2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/fig3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/fig4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/fig4_Ackley_4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/fig4_Griewank.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/fig4_Schwefel_2.26.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/fig5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/fig6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/number_partitioning/NPP_0.3_2d.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/number_partitioning/NPP_0.3_3d.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/number_partitioning/NPP_0.7_2d.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/number_partitioning/NPP_0.7_3d.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/number_partitioning/NPP_0.95_2d.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/number_partitioning/NPP_0.95_3d.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/number_partitioning/NPP_merged_cmlon_views.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/number_partitioning/NPP_phase_transition_sweep.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading