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.

Metadata

Release files for lonkit 0.8.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for lonkit 0.8.0
File Size Uploaded
lonkit-0.8.0.tar.gz 13.8 MB Details

Built distribution (wheel)

Table of built distributions (wheels) for lonkit 0.8.0
File Interpreter ABI Platform
lonkit-0.8.0-py3-none-any.whl Python 3 none any Details

Total release size: 13.8 MB

Release files / lonkit-0.8.0.tar.gz

Download URL lonkit-0.8.0.tar.gz
Size 13.8 MB
Tags Source
SHA-256 checksum
How to use checksums
2866fe156b3211524c980bdbf3c2e72f5cada9a32ac40aa913b2e396fe2c4e52
BLAKE2b-256 checksum
How to use checksums
dbaa4d124db963b45fff94cf214cde2843d3ae4c14581293f0f6288ede08c601
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 11, 2026.

Transparency log

Release files / lonkit-0.8.0-py3-none-any.whl

Download URL lonkit-0.8.0-py3-none-any.whl
Size 38.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
538bcd332c1f7bd2f67c0eb031a2fa487c32a77162da83b78c69ea33c9c89d50
BLAKE2b-256 checksum
How to use checksums
f6330fa03289163aaa15f5839535fdd8a2d0a918b9c3d05195631e5c5d173b8f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 11, 2026.

Transparency log

Release history Release notifications | RSS feed

0.9.0

2 release files

This release

0.8.0 This release

2 release files

0.7.0

2 release files

0.6.0

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page