Skip to main content

Hybrid optimization library with LHS, BO, GA, DE, Local, PSO support

Project description

HybridOpt

HybridOpt は Python で多様な最適化アルゴリズムを組み合わせて、連続・離散変数の混合、制約付き最適化、スケジュール探索の自動比較を行えるハイブリッド最適化ライブラリです。

特徴

  • LHS(Latin Hypercube Sampling)、Bayesian Optimization、局所探索、Differential Evolution、GA、PSO に対応
  • 連続・整数変数の混合に対応
  • 制約付き目的関数の探索が可能
  • 複数スケジュールの再現性評価、時間制限評価が可能
  • CSV に履歴を追記しながら探索可能

インストール

pip install hybridopt pyDOE scikit-optimize scipy

※ PSO を利用する場合は pyswarms を別途インストールしてください。

pip install pyswarms

使用例

目的関数例(多峰性)

import numpy as np
from hybridopt.optimizer import HybridOptimizer

def my_func(x, y):
    """多峰性関数: 最大値は (0.1, 4.0)"""
    return np.sin(5*x) * (1-np.tanh(y**2)) + 2*np.exp(-((x-0.1)**2 + (y-4.0)**2))

変数定義

variables = [
    {"label": "x", "bounds": (0, 1), "type": float},
    {"label": "y", "bounds": (0, 5), "type": float}
]

1. 通常の最適化

optimizer = HybridOptimizer(variables, target_label="obj", maximize=True)

X_all, Y_all, best_x, best_y = optimizer.run_schedule(
    func=my_func,
    csv_filename="history_normal.csv",
    schedule=[("lhs", 10), ("bo", 15), ("local", 20)]
)
print("Best x:", best_x)
print("Best y:", best_y)

出力例

Best x: [0.092, 4.0]
Best y: 2.625

探索アニメーション

2. 制約付き最適化

constraint_func = lambda y: y > 0
optimizer_constrained = HybridOptimizer(
    variables,
    target_label="obj",
    maximize=True,
    constraint=constraint_func
)

X_all, Y_all, best_x, best_y = optimizer_constrained.run_schedule(
    func=my_func,
    csv_filename="history_constrained.csv",
    schedule=[("lhs", 10), ("bo", 10), ("local", 20)]
)
print("Best x:", best_x)
print("Best y:", best_y)

出力例

Best x: [0.092, 4.0]
Best y: 2.625

3. 複数スケジュールの検証

schedules = [
    [("lhs", 10), ("de", 20), ("local", 20)],
    [("lhs", 15), ("ga", 25), ("local", 15)],
    [("lhs", 20), ("bo", 10), ("local", 20)]
]

best_result = optimizer.run_multiple_schedules(
    func=my_func,
    csv_prefix="history_schedule",
    schedules=schedules,
    n_runs=3
)
print("Overall best schedule:", best_result["schedule"])
print("Best y:", best_result["best_y"])
print("Best x:", best_result["best_x"])

出力例

Overall best schedule: [('lhs', 20), ('bo', 10), ('local', 20)]
Best y: 2.625
Best x: [0.092, 4.0]

4. 時間制限ベースで検証

best_result_time = optimizer.run_multiple_schedules_time_based(
    func=my_func,
    csv_prefix="history_time_schedule",
    schedules=schedules,
    time_limit_per_schedule=5.0,
    n_runs=3
)
print("Overall best schedule:", best_result_time["schedule"])
print("Best y:", best_result_time["best_y"])
print("Best x:", best_result_time["best_x"])

出力例

Overall best schedule: [('lhs', 20), ('bo', 10), ('local', 20)]
Best y: 2.625
Best x: [0.092, 4.0]

探索アルゴリズム

モード 説明
lhs Latin Hypercube Sampling
bo Bayesian Optimization
local 局所探索(L-BFGS-B)
de Differential Evolution
ga Genetic Algorithm
pso Particle Swarm Optimization(pyswarms が必要)

制約の指定

constraint_func = lambda y: y > 0  # 目的関数 y が正の値のみ許可
optimizer = HybridOptimizer(variables, target_label="obj", constraint=constraint_func)

制約を満たさない点は探索対象外として扱われます。

スケジュール探索

複数のアルゴリズムを組み合わせた探索スケジュールを検証可能。

  • run_multiple_schedules は最良結果を繰り返し評価し、平均値で比較。
  • run_multiple_schedules_time_based は各スケジュールの実行時間で公平に比較。

ライセンス

HybridOpt は MIT ライセンスの下で提供されています。詳細は LICENSE ファイルを参照してください。

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

hybridopt-0.1.1.tar.gz (10.3 kB view details)

Uploaded Source

Built Distribution

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

hybridopt-0.1.1-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

Details for the file hybridopt-0.1.1.tar.gz.

File metadata

  • Download URL: hybridopt-0.1.1.tar.gz
  • Upload date:
  • Size: 10.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for hybridopt-0.1.1.tar.gz
Algorithm Hash digest
SHA256 c453b420cc5f8a041ab0f991b5c14a5e59ac232487b1bbc675ae4f344d3254fb
MD5 8fd7f211869c3f522491ecbd18dd94ac
BLAKE2b-256 21b704f0207f2fc25395d4ab9ad374bac6e17bb5f4ed668f95f3411700e345a5

See more details on using hashes here.

File details

Details for the file hybridopt-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: hybridopt-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 9.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for hybridopt-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1896be6a4447f5785da2c59b0dfd891ca44655dee348a0a7660cb54e4f093357
MD5 f27f9488850fafb6b20627dc72ec5b38
BLAKE2b-256 2f7614cab6211a7ec5865f6e2517d5be2a12c69b82876d5de305514ed3fbc5d8

See more details on using hashes here.

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