{"id":21849259,"url":"https://github.com/pasaopasen/beehivemethod","last_synced_at":"2025-04-14T14:52:37.856Z","repository":{"id":37637108,"uuid":"281138400","full_name":"PasaOpasen/BeehiveMethod","owner":"PasaOpasen","description":"Implementation of beehive method (particle swarm optimization) for global optimization of multidimentional functions","archived":false,"fork":false,"pushed_at":"2023-01-01T13:09:50.000Z","size":75151,"stargazers_count":3,"open_issues_count":2,"forks_count":2,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-04-12T19:41:12.444Z","etag":null,"topics":["algorithm","evolutionary-algorithm","function-optimization","maths","multidimentional-functions","optimization-algorithms","pypi-package","stochastic-optimization","swarm-algorithm"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/PasaOpasen.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-07-20T14:25:32.000Z","updated_at":"2023-01-25T17:03:48.000Z","dependencies_parsed_at":"2023-01-31T22:45:25.282Z","dependency_job_id":null,"html_url":"https://github.com/PasaOpasen/BeehiveMethod","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PasaOpasen%2FBeehiveMethod","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PasaOpasen%2FBeehiveMethod/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PasaOpasen%2FBeehiveMethod/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PasaOpasen%2FBeehiveMethod/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/PasaOpasen","download_url":"https://codeload.github.com/PasaOpasen/BeehiveMethod/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248900903,"owners_count":21180305,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["algorithm","evolutionary-algorithm","function-optimization","maths","multidimentional-functions","optimization-algorithms","pypi-package","stochastic-optimization","swarm-algorithm"],"created_at":"2024-11-28T00:12:03.474Z","updated_at":"2025-04-14T14:52:37.835Z","avatar_url":"https://github.com/PasaOpasen.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![PyPI\nversion](https://badge.fury.io/py/BeeHiveOptimization.svg)](https://pypi.org/project/BeeHiveOptimization/)\n[![Downloads](https://pepy.tech/badge/BeeHiveOptimization)](https://pepy.tech/project/BeeHiveOptimization)\n[![Downloads](https://pepy.tech/badge/BeeHiveOptimization/month)](https://pepy.tech/project/BeeHiveOptimization)\n[![Downloads](https://pepy.tech/badge/BeeHiveOptimization/week)](https://pepy.tech/project/BeeHiveOptimization)\n\n[![Gitter](https://badges.gitter.im/BeehiveMethod/community.svg)](https://gitter.im/BeehiveMethod/community?utm_source=badge\u0026utm_medium=badge\u0026utm_campaign=pr-badge)\n\n# Beehive method\n```\npip install BeeHiveOptimization\n```\nImplementation (from [DPEA](https://github.com/PasaOpasen/PasaOpasen.github.io/blob/master/EA_packages.md)) of **beehive method** (*particle swarm optimization*) for global optimization of multidimentional functions. It's optimized rewrite of [my C#-implementation](https://github.com/PasaOpasen/MathClasses#swarm-algorithm)\n\n- [Beehive method](#beehive-method)\n  - [Steps of algorithm](#steps-of-algorithm)\n    - [0 step: creating a function](#0-step-creating-a-function)\n    - [1st step: creating bees](#1st-step-creating-bees)\n    - [2nd step: create hive and get result](#2nd-step-create-hive-and-get-result)\n  - [Ways to get best solution](#ways-to-get-best-solution)\n  - [Animations of working](#animations-of-working)\n- [See also](#see-also)\n\n\n## Steps of algorithm\n\n### 0 step: creating a function\n\nYour target function should get a numpy array and return float number.\n\n```python\n\nf1 = lambda arr: arr[0]+arr[1]/(1+arr[0])+arr[2]*arr[3]\n\n# convertion to numpy-\u003efloat function\n\ndef target(x,y,z,q):\n  return x**2+y**2*z/q\n\nf2 = lambda arr: target(arr[0], arr[1], arr[2], arr[3])\n\n```\n\nThe method seeks the global minimum of target function. If u want to find global maximum, use this idea:\n\n```python\n\nf_tmp = lambda arr: -target(arr)\n\n#\n# ... find global min\n#\n\ntagret_result = -global_min\n\n```\n\n\n### 1st step: creating bees\n\nU should create random bees (points) on the scope of the function definition. There are several ways:\n\n```python\n\n\nfrom BeeHiveOptimization import Bees, Hive, BeeHive, TestFunctions, RandomPuts\n\nimport numpy as np\n\n# 1st step is to create bees\n\nnp.random.seed(1)\n\n# it's just numpy array with shape bees_count_x_dim\n\narr = np.random.uniform(low = -3, high = 5, size = (10,3))\n\n# width parameter means the maximum range of random begging speeds\n\nbees = Bees(arr, width = 0.2)\n\nbees.show()\n\n#Current bees' positions:\n#[[ 0.33617604  2.76259595 -2.999085  ]\n# [-0.58133942 -1.82595287 -2.26129124]\n# [-1.50991831 -0.23551418  0.17413979]\n# [ 1.31053387  0.35355612  2.481756  ]\n# [-1.364382    4.02493949 -2.78089925]\n# [ 2.36374008  0.33843842  1.46951863]\n# [-1.87690449 -1.41518809  3.40595655]\n# [ 4.74609261 -0.49260657  2.53858093]\n# [ 4.01111322  4.15685331 -2.31964631]\n# [-2.68756173 -1.64135664  4.02514003]]\n#\n#Current bees' speeds:\n#[[-0.08033063 -0.01577847  0.09157791]\n# [ 0.00663306  0.03837542 -0.03689687]\n# [ 0.03730019  0.06692513 -0.09634234]\n# [ 0.05002886  0.09777222  0.04963313]\n# [-0.0439112   0.05785587 -0.0793548 ]\n# [-0.01042129  0.0817191  -0.04127717]\n# [-0.04244493 -0.07399429 -0.09612661]\n# [ 0.03576711 -0.05767438 -0.04689067]\n# [-0.00168537 -0.08932749  0.01482352]\n# [-0.07065429  0.01786111  0.03995167]]\n\n# u aslo can create bees by random generator like () --\u003e random numpy array (point)\n\nbees = Bees.get_Bees_from_randomputs(count = 10, random_gen = RandomPuts.Normal(mean = 1, std = 0.1, size = 2), width = 0.3)\n\nbees.show()\n\n#Current bees' positions:\n#[[0.98081644 0.9112371 ]\n# [0.92528417 1.16924546]\n# [1.00508078 0.93630044]\n# [1.01909155 1.21002551]\n# [1.0120159  1.06172031]\n# [1.03001703 0.96477502]\n# [0.88574818 0.96506573]\n# [0.97911058 1.05866232]\n# [1.08389834 1.09311021]\n# [1.02855873 1.08851412]]\n#\n#Current bees' speeds:\n#[[ 0.07528273 -0.0453305 ]\n# [-0.06902163  0.11876587]\n# [-0.02157264  0.13945201]\n# [ 0.04903245  0.03650872]\n# [-0.11557621  0.13484678]\n# [-0.01502636  0.02351688]\n# [-0.02755896 -0.07889191]\n# [ 0.12101386  0.02210385]\n# [-0.1491389   0.03514347]\n# [-0.05200653  0.00811743]]\n\n```\n\n### 2nd step: create hive and get result\n\n```python\n\n\nbees = Bees(np.random.normal(loc = 2, scale = 2, size = (100,3)), width = 3)\n\nfunc = lambda arr: TestFunctions.Parabol(arr-3)\n\n    \nhive = Hive(bees, \n            func, \n            parallel = False, # use parallel evaluating of functions values for each bee? (recommented for heavy functions, f. e. integtals) \n            verbose = True) # show info about hive \n\n#total bees: 100\n#best value (at beggining): 0.45406041997965585\n\n# getting result\n\nbest_result, best_position = hive.get_result(max_step_count = 25, # maximun count of iteraions\n                      max_fall_count = 6, # maximum count of continious iterations without better result\n                      w = 0.3, fp = 2, fg = 5, # parameters of algorithm\n                      latency = 1e-9, # if new_result/old_result \u003e 1-latency then it was the iteration without better result\n                      verbose = True, # show the progress\n                      max_time_seconds = None # max seconds of working \n                      )\n\n#new best value = 0.22369081290807669 after 4 iteration\n#new best value = 0.20481778141411794 after 5 iteration\n#new best value = 0.2036698385729661 after 6 iteration\n#new best value = 0.15570778532180615 after 7 iteration\n#new best value = 0.06772538042802272 after 8 iteration\n#new best value = 0.06060365350244633 after 9 iteration\n#new best value = 0.048967304902866424 after 10 iteration\n#new best value = 0.035531597911666886 after 11 iteration\n#new best value = 0.018238258030885895 after 12 iteration\n#new best value = 0.007999184438461721 after 13 iteration\n#new best value = 0.007095161331114254 after 14 iteration\n#new best value = 0.006010424290536399 after 15 iteration\n#new best value = 0.0054592185233458875 after 16 iteration\n#new best value = 0.003526411943370142 after 17 iteration\n#new best value = 0.003212115031845861 after 18 iteration\n#new best value = 0.001246361699255375 after 19 iteration\n#new best value = 0.0011705559906451679 after 20 iteration\n#new best value = 0.0010476941319986334 after 21 iteration\n#new best value = 0.0006265651258711051 after 22 iteration\n#new best value = 0.00041912821521993736 after 23 iteration\n#new best value = 0.0003037094325696652 after 24 iteration\n#new best value = 0.0002523002561426299 after 25 iteration\n\n\n\n# u also can use this code (without creating a hive)\n\nbest_result, best_position = BeeHive.Minimize(func, bees, \n                 max_step_count = 100, max_fall_count = 30, \n                 w = 0.3, fp = 2, fg = 5, latency = 1e-9, \n                 verbose = False, parallel = False)\n\n\n```\n\n## Ways to get best solution\n\nTo get real global minimum u should use this algorithm with more bees, bigger ```max_step_count```, bigger ``` max_fall_count``` several parameters ```w, fp, fg```:\n\n```python\n\n\n# to get best solution\n\n\nfunc = lambda arr: TestFunctions.Rastrigin(arr) + TestFunctions.Shvel(arr) + 1 / (1 + np.sum(np.abs(arr)))\n\nfor w in (0.1,0.3,0.5,0.8):\n    for fp in (1, 2, 3, 4.5):\n        for fg in (3, 5, 8, 15):\n            \n            # 200 bees, 10 dimentions\n            bees = Bees(np.random.uniform(low = -100, high = 100, size = (200, 10) ))\n            \n            best_val, _ = BeeHive.Minimize(func, bees, \n                 max_step_count = 200, max_fall_count = 70, \n                 w = 2, fp = fp, fg = fg, latency = 1e-9, \n                 verbose = False, parallel = False)\n            \n            print(f'best val by w = {w}, fp = {fp}, fg = {fg} is {best_val}')\n\n\n#best val by w = 0.1, fp = 1, fg = 3 is 6428.695769232477\n#best val by w = 0.1, fp = 1, fg = 5 is 32.52712233771301\n#best val by w = 0.1, fp = 1, fg = 8 is 160.08392341144406\n#best val by w = 0.1, fp = 1, fg = 15 is 687.6266727861189\n#best val by w = 0.1, fp = 2, fg = 3 is 24.21891968781912\n#best val by w = 0.1, fp = 2, fg = 5 is 49.44330907798958\n#best val by w = 0.1, fp = 2, fg = 8 is 184.83187118199487\n#best val by w = 0.1, fp = 2, fg = 15 is 185.51770584709303\n#best val by w = 0.1, fp = 3, fg = 3 is 79.76934143653314\n#best val by w = 0.1, fp = 3, fg = 5 is 47.26493469211696\n#best val by w = 0.1, fp = 3, fg = 8 is 184.8646381092168\n#best val by w = 0.1, fp = 3, fg = 15 is 160.27503370261758\n#best val by w = 0.1, fp = 4.5, fg = 3 is 85.60089227083901\n#best val by w = 0.1, fp = 4.5, fg = 5 is 108.62418480388155\n#best val by w = 0.1, fp = 4.5, fg = 8 is 171.2798284181892\n#best val by w = 0.1, fp = 4.5, fg = 15 is 551.7224618400166\n#best val by w = 0.3, fp = 1, fg = 3 is 6483.765801924292\n#best val by w = 0.3, fp = 1, fg = 5 is 8.002321991449135\n#best val by w = 0.3, fp = 1, fg = 8 is 108.94659371003644\n#best val by w = 0.3, fp = 1, fg = 15 is 47.079602126734684\n#best val by w = 0.3, fp = 2, fg = 3 is 26.014123389174156\n#best val by w = 0.3, fp = 2, fg = 5 is 18.29385998055267\n#best val by w = 0.3, fp = 2, fg = 8 is 166.411456895688\n#best val by w = 0.3, fp = 2, fg = 15 is 250.8512847429756\n#best val by w = 0.3, fp = 3, fg = 3 is 212.89122853639924\n#best val by w = 0.3, fp = 3, fg = 5 is 282.19775766847954\n#best val by w = 0.3, fp = 3, fg = 8 is 78.26449685551408\n#best val by w = 0.3, fp = 3, fg = 15 is 201.77855256657307\n#best val by w = 0.3, fp = 4.5, fg = 3 is 101.7205867717656\n#best val by w = 0.3, fp = 4.5, fg = 5 is 39.181243373144405\n#best val by w = 0.3, fp = 4.5, fg = 8 is 31.339807579184082\n#best val by w = 0.3, fp = 4.5, fg = 15 is 136.71496136204559\n#best val by w = 0.5, fp = 1, fg = 3 is 7966.104488676415\n#best val by w = 0.5, fp = 1, fg = 5 is 31.85174765332643\n#best val by w = 0.5, fp = 1, fg = 8 is 119.51737439099381\n#best val by w = 0.5, fp = 1, fg = 15 is 343.21190707573186\n#best val by w = 0.5, fp = 2, fg = 3 is 19.69118760870259\n#best val by w = 0.5, fp = 2, fg = 5 is 355.5573149722314\n#best val by w = 0.5, fp = 2, fg = 8 is 124.35986298683065\n#best val by w = 0.5, fp = 2, fg = 15 is 159.73058691572118\n#best val by w = 0.5, fp = 3, fg = 3 is 91.73431812681028\n#best val by w = 0.5, fp = 3, fg = 5 is 348.8643367168582\n#best val by w = 0.5, fp = 3, fg = 8 is 103.67897134960793\n#best val by w = 0.5, fp = 3, fg = 15 is 239.28420706187788\n#best val by w = 0.5, fp = 4.5, fg = 3 is 128.31342837632542\n#best val by w = 0.5, fp = 4.5, fg = 5 is 184.85393346379036\n#best val by w = 0.5, fp = 4.5, fg = 8 is 24.808161917042987\n#best val by w = 0.5, fp = 4.5, fg = 15 is 151.67171420308466\n#best val by w = 0.8, fp = 1, fg = 3 is 11473.562212656598\n#best val by w = 0.8, fp = 1, fg = 5 is 60.83786609973994\n#best val by w = 0.8, fp = 1, fg = 8 is 136.37066252443586\n#best val by w = 0.8, fp = 1, fg = 15 is 384.96175871884816\n#best val by w = 0.8, fp = 2, fg = 3 is 12.05219722035246\n#best val by w = 0.8, fp = 2, fg = 5 is 157.38773631834732\n#best val by w = 0.8, fp = 2, fg = 8 is 480.3721790650163\n#best val by w = 0.8, fp = 2, fg = 15 is 122.24297005195243\n#best val by w = 0.8, fp = 3, fg = 3 is 94.49827014281807\n#best val by w = 0.8, fp = 3, fg = 5 is 227.97483895242695\n#best val by w = 0.8, fp = 3, fg = 8 is 155.60665206056973\n#best val by w = 0.8, fp = 3, fg = 15 is 253.34803362736002\n#best val by w = 0.8, fp = 4.5, fg = 3 is 64.920748391647\n#best val by w = 0.8, fp = 4.5, fg = 5 is 73.96529195451255\n#best val by w = 0.8, fp = 4.5, fg = 8 is 28.47149810229648\n#best val by w = 0.8, fp = 4.5, fg = 15 is 130.0584088063038\n\n```\n\n\n## Animations of working\n\n![1](https://github.com/PasaOpasen/BeehiveMethod/blob/master/images/Shvel%20function_movie.gif)\n\n![1](https://github.com/PasaOpasen/BeehiveMethod/blob/master/images/Shvel%20function%20with%20noise_movie.gif)\n\n![1](https://github.com/PasaOpasen/BeehiveMethod/blob/master/images/Rastrigin%20function_movie.gif)\n\n\n![1](https://github.com/PasaOpasen/BeehiveMethod/blob/master/images/Rastrigin%20function%20(2d)_movie.gif)\n\n![1](https://github.com/PasaOpasen/BeehiveMethod/blob/master/images/Rastrigin%20function%20from%20normal%20dist.%20start%20(2d)_movie.gif)\n\n![1](https://github.com/PasaOpasen/BeehiveMethod/blob/master/images/Rastrigin%20function%20with%20noise%20(2d)_movie.gif)\n\n![1](https://github.com/PasaOpasen/BeehiveMethod/blob/master/images/Shvel%20function%20(2d)_movie.gif)\n\n![1](https://github.com/PasaOpasen/BeehiveMethod/blob/master/images/Shvel%20function%20with%20noise%20(2d)_movie.gif)\n\n# See also\n\nIt can be very useful to use also:\n\n* [OppOpPopInit](https://github.com/PasaOpasen/opp-op-pop-init#population-initializers) package for initialize population and using opposition learning strategy\n* [OptimizationTestFunctions](https://github.com/PasaOpasen/OptimizationTestFunctions) package for handle several classic test functions\n\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpasaopasen%2Fbeehivemethod","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpasaopasen%2Fbeehivemethod","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpasaopasen%2Fbeehivemethod/lists"}