Skip to main content

Local Optima Networks (LONs) for continuous and discrete optimization - Python implementation

Project description

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, 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.

Project details


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.3.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.3.0-py3-none-any.whl (33.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: lonkit-0.3.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.7

File hashes

Hashes for lonkit-0.3.0.tar.gz
Algorithm Hash digest
SHA256 109679bcd497947ebecbd2c411738fd73f4e9ff8f4b75def67837bef801ca1ae
MD5 7d3c7a74d0c34533a655c0da035b512a
BLAKE2b-256 e86828aa8498341d74cd620790a9cd0319078d506be77b08169956fce8850539

See more details on using hashes here.

Provenance

The following attestation bundles were made for lonkit-0.3.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.3.0-py3-none-any.whl.

File metadata

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

File hashes

Hashes for lonkit-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 702a76b68925b7ca8b8a4192f85c2b06f9761441d514316832cfbbb51081f322
MD5 4a4fd3a348e8a5782891d8e27c707a10
BLAKE2b-256 d37940130bdcb690fd88ee43c9bb20bc506b56654c597043e2fd5f1693551372

See more details on using hashes here.

Provenance

The following attestation bundles were made for lonkit-0.3.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 Pingdom Monitoring Sentry Error logging StatusPage Status page