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.0.tar.gz (10.2 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.0-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: hybridopt-0.1.0.tar.gz
  • Upload date:
  • Size: 10.2 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.0.tar.gz
Algorithm Hash digest
SHA256 eec718ceab44284f2c9c827475d0eb543518ab91323e5cbd2423f1e0e2440a68
MD5 e6577e3429ae988f06dc33edc6dfddfb
BLAKE2b-256 c3a8d74697b7a6da6a675c18b1d26fd5e1ae4aca75dabc7c1ae80384b1550f3f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: hybridopt-0.1.0-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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0c7fedfb93570aac163e169a8feb89194f479a2dfa59a14cb3ea056ae6aa6d00
MD5 4982a950a89e978397e5523ad359f07a
BLAKE2b-256 7c9051ffb63f7e8a5b1d32794749730815bfcb862de535c0caa01b459e6154d3

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