Skip to main content
lonkit

PyPI version Python 3.10+ Open In Colab

Local Optima Networks

lonkit is a Python library for constructing, analyzing, and visualizing Local Optima Networks (LONs) for both continuous and discrete optimization problems. LONs provide a powerful way to understand the structure of fitness landscapes, revealing how local optima are connected and how difficult it may be to find global optima.

Features

  • Basin-Hopping Sampling: Efficient exploration of continuous fitness landscapes using configurable Basin-Hopping
  • Iterated Local Search: Discrete LON construction via ILS with built-in problems (Number Partitioning, NK Landscape, OneMax)
  • LON Construction: Automatic construction of Local Optima Networks from sampling data
  • CMLON Support: Compressed Monotonic LONs for cleaner landscape analysis
  • Rich Metrics: Compute landscape metrics including funnel analysis and neutrality
  • Beautiful Visualizations: 2D and 3D plots with support for animated GIFs

Installation

pip install lonkit

Or install from source:

git clone https://github.com/helix-agh/lonkit.git
cd lonkit
pip install -e .

Quick Start

import numpy as np
from lonkit import compute_lon, LONVisualizer, BasinHoppingSamplerConfig

# Define an objective function
def rastrigin(x: np.ndarray) -> float:
    return 10 * len(x) + np.sum(x**2 - 10 * np.cos(2 * np.pi * x))

# Construct the LON
config = BasinHoppingSamplerConfig(
    n_runs=20,
    n_iter_no_change=500,
    seed=42
)
lon = compute_lon(
    rastrigin,
    dim=2,
    lower_bound=-5.12,
    upper_bound=5.12,
    config=config
)

metrics = lon.compute_metrics()
print(f"Number of funnels: {metrics['n_funnels']}")
print(f"Global funnels: {metrics['n_global_funnels']}")

# Visualize
viz = LONVisualizer()
viz.plot_2d(lon, output_path="lon_2d.png")
viz.plot_3d(lon, output_path="lon_3d.png")

Compressed Monotonic LONs (CMLONs)

CMLONs are a compressed representation where nodes with equal fitness that are connected get merged. This provides a cleaner view of the landscape's funnel structure.

# Convert LON to CMLON
cmlon = lon.to_cmlon()

# Analyze CMLON-specific metrics
cmlon_metrics = cmlon.compute_metrics()

Custom Sampling Configuration

from lonkit import BasinHoppingSampler, BasinHoppingSamplerConfig

config = BasinHoppingSamplerConfig(
    n_runs=50,                   # Number of independent runs
    n_iter_no_change=1000,       # Stop after this many consecutive non-improving perturbations
    step_size=0.05,              # Perturbation size
    step_mode="percentage",      # "percentage" or "fixed"
    coordinate_precision=4,      # Precision for identifying optima
    fitness_precision=None,      # Precision for fitness values (None = full double)
    seed=42                      # For reproducibility
)

sampler = BasinHoppingSampler(config)

# Define search domain
domain = [(-5.12, 5.12), (-5.12, 5.12)]

# Run sampling
result = sampler.sample(rastrigin, domain)
lon = sampler.sample_to_lon(result)

Discrete Optimization Problems

lonkit also supports Local Optima Networks for discrete optimization problems using Iterated Local Search (ILS).

Quick Start (Discrete)

from lonkit import NumberPartitioning, ILSSampler, ILSSamplerConfig, LONVisualizer

# Define problem instance
problem = NumberPartitioning(n=20, k=0.5, instance_seed=1)

# Configure and run ILS sampling
config = ILSSamplerConfig(n_runs=50, n_iter_no_change=100, seed=42)
sampler = ILSSampler(config)
result = sampler.sample(problem)

# Build LON and CMLON
lon = sampler.sample_to_lon(result)
cmlon = lon.to_cmlon()

# Compute metrics
print(lon.compute_metrics())
print(cmlon.compute_metrics())

# Visualize
viz = LONVisualizer()
viz.plot_2d(cmlon, output_path="npp_cmlon.png")

Custom Discrete Problems

You can define your own discrete problem by subclassing DiscreteProblem. For bitstring problems, inherit from BitstringProblem instead it provides random_solution(), local_search(), perturb(), and solution_id() out of the box. You only need to implement evaluate().

Documentation

For full documentation, visit: https://helix-agh.github.io/lonkit

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lonkit-0.4.0.tar.gz (2.7 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

lonkit-0.4.0-py3-none-any.whl (34.9 kB view details)

Uploaded Python 3

File details

Details for the file lonkit-0.4.0.tar.gz.

File metadata

  • Download URL: lonkit-0.4.0.tar.gz
  • Upload date:
  • Size: 2.7 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for lonkit-0.4.0.tar.gz
Algorithm Hash digest
SHA256 370a55f075e8647213b047c0e2ee15faf4e3f75a3e0c4d289f149e71aba8b5a9
MD5 d6dd41a0cb18f6956739e761dfa839b0
BLAKE2b-256 73373f59ce3329e53801b4256dcf2de6649dd93006314c14c12f2b7be792c8e8

See more details on using hashes here.

Provenance

The following attestation bundles were made for lonkit-0.4.0.tar.gz:

Publisher: publish.yml on helix-agh/lonkit

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file lonkit-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: lonkit-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 34.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for lonkit-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a69cc898403c5850cfdc440fd0219be7d5b61e03e252d902341d9d1c0122e516
MD5 888c569a3f44cc1e87d10c3e512ed632
BLAKE2b-256 3cd5b23551328b67ac4aa6c242c595028178bad790a61cbb822404c440ae08d9

See more details on using hashes here.

Provenance

The following attestation bundles were made for lonkit-0.4.0-py3-none-any.whl:

Publisher: publish.yml on helix-agh/lonkit

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page