{"id":13487004,"url":"https://github.com/guofei9987/scikit-opt","last_synced_at":"2026-10-18T23:08:49.209Z","repository":{"id":37550223,"uuid":"113166974","full_name":"guofei9987/scikit-opt","owner":"guofei9987","description":"Genetic Algorithm, Particle Swarm Optimization, Simulated Annealing, Ant Colony Optimization Algorithm,Immune Algorithm, Artificial Fish Swarm Algorithm, Differential Evolution and TSP(Traveling salesman) ","archived":false,"fork":false,"pushed_at":"2026-03-25T09:12:13.000Z","size":389,"stargazers_count":6718,"open_issues_count":73,"forks_count":1108,"subscribers_count":53,"default_branch":"master","last_synced_at":"2026-10-10T18:41:02.636Z","etag":null,"topics":["ant-colony-algorithm","artificial-intelligence","fish-swarms","genetic-algorithm","heuristic-algorithms","immune","immune-algorithm","optimization","particle-swarm-optimization","pso","simulated-annealing","travelling-salesman-problem","tsp"],"latest_commit_sha":null,"homepage":"https://scikit-opt.github.io/scikit-opt/#/en/","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/guofei9987.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"custom":["https://www.guofei.site/微信打赏.jpg"],"open_collective":"scikit-opt"}},"created_at":"2017-12-05T10:20:41.000Z","updated_at":"2026-10-10T08:23:57.000Z","dependencies_parsed_at":"2023-11-23T06:38:19.360Z","dependency_job_id":"5543116a-e677-4a87-952c-20e83be1cf0a","html_url":"https://github.com/guofei9987/scikit-opt","commit_stats":{"total_commits":298,"total_committers":27,"mean_commits":"11.037037037037036","dds":0.261744966442953,"last_synced_commit":"adfd8e5a719074d7980af8663ce0e95745cc40ad"},"previous_names":[],"tags_count":24,"template":false,"template_full_name":null,"purl":"pkg:github/guofei9987/scikit-opt","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/guofei9987%2Fscikit-opt","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/guofei9987%2Fscikit-opt/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/guofei9987%2Fscikit-opt/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/guofei9987%2Fscikit-opt/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/guofei9987","download_url":"https://codeload.github.com/guofei9987/scikit-opt/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/guofei9987%2Fscikit-opt/sbom","scorecard":{"id":449559,"data":{"date":"2025-08-11","repo":{"name":"github.com/guofei9987/scikit-opt","commit":"adfd8e5a719074d7980af8663ce0e95745cc40ad"},"scorecard":{"version":"v5.2.1-40-gf6ed084d","commit":"f6ed084d17c9236477efd66e5b258b9d4cc7b389"},"score":2.5,"checks":[{"name":"Dangerous-Workflow","score":-1,"reason":"no workflows found","details":null,"documentation":{"short":"Determines if the project's GitHub Action workflows avoid dangerous patterns.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#dangerous-workflow"}},{"name":"Maintained","score":0,"reason":"0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project is \"actively maintained\".","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#maintained"}},{"name":"Code-Review","score":6,"reason":"Found 10/16 approved changesets -- score normalized to 6","details":null,"documentation":{"short":"Determines if the project requires human code review before pull requests (aka merge requests) are merged.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#code-review"}},{"name":"Packaging","score":-1,"reason":"packaging workflow not detected","details":["Warn: no GitHub/GitLab publishing workflow detected."],"documentation":{"short":"Determines if the project is published as a package that others can easily download, install, easily update, and uninstall.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#packaging"}},{"name":"Token-Permissions","score":-1,"reason":"No tokens found","details":null,"documentation":{"short":"Determines if the project's workflows follow the principle of least privilege.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#token-permissions"}},{"name":"CII-Best-Practices","score":0,"reason":"no effort to earn an OpenSSF best practices badge detected","details":null,"documentation":{"short":"Determines if the project has an OpenSSF (formerly CII) Best Practices Badge.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#cii-best-practices"}},{"name":"Binary-Artifacts","score":10,"reason":"no binaries found in the repo","details":null,"documentation":{"short":"Determines if the project has generated executable (binary) artifacts in the source repository.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#binary-artifacts"}},{"name":"Pinned-Dependencies","score":-1,"reason":"no dependencies found","details":null,"documentation":{"short":"Determines if the project has declared and pinned the dependencies of its build process.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#pinned-dependencies"}},{"name":"Security-Policy","score":0,"reason":"security policy file not detected","details":["Warn: no security policy file detected","Warn: no security file to analyze","Warn: no security file to analyze","Warn: no security file to analyze"],"documentation":{"short":"Determines if the project has published a security policy.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#security-policy"}},{"name":"License","score":10,"reason":"license file detected","details":["Info: project has a license file: LICENSE:0","Info: FSF or OSI recognized license: MIT License: LICENSE:0"],"documentation":{"short":"Determines if the project has defined a license.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#license"}},{"name":"Fuzzing","score":0,"reason":"project is not fuzzed","details":["Warn: no fuzzer integrations found"],"documentation":{"short":"Determines if the project uses fuzzing.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#fuzzing"}},{"name":"Signed-Releases","score":-1,"reason":"no releases found","details":null,"documentation":{"short":"Determines if the project cryptographically signs release artifacts.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#signed-releases"}},{"name":"Branch-Protection","score":0,"reason":"branch protection not enabled on development/release branches","details":["Warn: branch protection not enabled for branch 'master'"],"documentation":{"short":"Determines if the default and release branches are protected with GitHub's branch protection settings.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#branch-protection"}},{"name":"SAST","score":0,"reason":"SAST tool is not run on all commits -- score normalized to 0","details":["Warn: 0 commits out of 24 are checked with a SAST tool"],"documentation":{"short":"Determines if the project uses static code analysis.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#sast"}},{"name":"Vulnerabilities","score":0,"reason":"11 existing vulnerabilities detected","details":["Warn: Project is vulnerable to: PYSEC-2018-34 / GHSA-2fc2-6r4j-p65h","Warn: Project is vulnerable to: PYSEC-2021-856 / GHSA-5545-2q6w-2gh6","Warn: Project is vulnerable to: PYSEC-2019-108 / GHSA-9fq2-x9r6-wfmf","Warn: Project is vulnerable to: PYSEC-2018-33 / GHSA-cw6w-4rcx-xphc","Warn: Project is vulnerable to: PYSEC-2021-857 / GHSA-f7c7-j99h-c22f","Warn: Project is vulnerable to: GHSA-fpfv-jqm9-f5jm","Warn: Project is vulnerable to: PYSEC-2017-1 / GHSA-frgw-fgh6-9g52","Warn: Project is vulnerable to: PYSEC-2020-73","Warn: Project is vulnerable to: PYSEC-2019-156 / GHSA-xp76-357g-9wqq","Warn: Project is vulnerable to: PYSEC-2023-102","Warn: Project is vulnerable to: PYSEC-2023-114"],"documentation":{"short":"Determines if the project has open, known unfixed vulnerabilities.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#vulnerabilities"}}]},"last_synced_at":"2025-08-19T07:49:49.080Z","repository_id":37550223,"created_at":"2025-08-19T07:49:49.080Z","updated_at":"2025-08-19T07:49:49.080Z"},"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":344872684,"owners_count":38343411,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-10-03T21:59:58.778Z","status":"online","status_checked_at":"2026-10-18T02:00:06.448Z","response_time":63,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["ant-colony-algorithm","artificial-intelligence","fish-swarms","genetic-algorithm","heuristic-algorithms","immune","immune-algorithm","optimization","particle-swarm-optimization","pso","simulated-annealing","travelling-salesman-problem","tsp"],"created_at":"2024-07-31T18:00:54.265Z","updated_at":"2026-10-18T23:08:49.202Z","avatar_url":"https://github.com/guofei9987.png","language":"Python","funding_links":["https://www.guofei.site/微信打赏.jpg","https://opencollective.com/scikit-opt"],"categories":["The Data Science Toolbox","Python","Optimization","\u003cspan id=\"head24\"\u003e3.2. Optimization\u003c/span\u003e","Machine Learning Framework","Sklearn实用程序","Repos","Uncategorized"],"sub_categories":["General Machine Learning Packages","Others","\u003cspan id=\"head25\"\u003e3.2.1. Metaheuristic\u003c/span\u003e","General Purpose Framework","General-Purpose Machine Learning","NLP","Uncategorized"],"readme":"\n# [scikit-opt](https://github.com/guofei9987/scikit-opt)\n\n[![PyPI](https://img.shields.io/pypi/v/scikit-opt)](https://pypi.org/project/scikit-opt/)\n[![Build Status](https://travis-ci.com/guofei9987/scikit-opt.svg?branch=master)](https://travis-ci.com/guofei9987/scikit-opt)\n[![codecov](https://codecov.io/gh/guofei9987/scikit-opt/branch/master/graph/badge.svg)](https://codecov.io/gh/guofei9987/scikit-opt)\n[![License](https://img.shields.io/pypi/l/scikit-opt.svg)](https://github.com/guofei9987/scikit-opt/blob/master/LICENSE)\n![Python](https://img.shields.io/badge/python-\u003e=3.5-green.svg)\n![Platform](https://img.shields.io/badge/platform-windows%20|%20linux%20|%20macos-green.svg)\n[![fork](https://img.shields.io/github/forks/guofei9987/scikit-opt?style=social)](https://github.com/guofei9987/scikit-opt/fork)\n[![Downloads](https://pepy.tech/badge/scikit-opt)](https://pepy.tech/project/scikit-opt)\n[![Discussions](https://img.shields.io/badge/discussions-green.svg)](https://github.com/guofei9987/scikit-opt/discussions)\n\u003ca href=\"https://hellogithub.com/repository/guofei9987/scikit-opt\" target=\"_blank\"\u003e\u003cimg src=\"https://abroad.hellogithub.com/v1/widgets/recommend.svg?rid=6763d615842e4449a02f024f3e2e345c\u0026claim_uid=se0WHo8cbiLv2w1\u0026theme=small\" alt=\"Featured｜HelloGitHub\" /\u003e\u003c/a\u003e\n\n\nSwarm Intelligence in Python  \n(Genetic Algorithm, Particle Swarm Optimization, Simulated Annealing, Ant Colony Algorithm, Immune Algorithm, Artificial Fish Swarm Algorithm in Python)\n\n\n- **Documentation:** [https://scikit-opt.github.io/scikit-opt/#/en/](https://scikit-opt.github.io/scikit-opt/#/en/)\n- **文档：** [https://scikit-opt.github.io/scikit-opt/#/zh/](https://scikit-opt.github.io/scikit-opt/#/zh/)  \n- **Source code:** [https://github.com/guofei9987/scikit-opt](https://github.com/guofei9987/scikit-opt)\n- **Help us improve scikit-opt** [https://www.wjx.cn/jq/50964691.aspx](https://www.wjx.cn/jq/50964691.aspx)\n\n# install\n```bash\npip install scikit-opt\n```\n\nFor the current developer version:\n```bach\ngit clone git@github.com:guofei9987/scikit-opt.git\ncd scikit-opt\npip install .\n```\n\n# Features\n## Feature1: UDF\n\n**UDF** (user defined function) is available now!\n\nFor example, you just worked out a new type of `selection` function.  \nNow, your `selection` function is like this:  \n-\u003e Demo code: [examples/demo_ga_udf.py#s1](https://github.com/guofei9987/scikit-opt/blob/master/examples/demo_ga_udf.py#L1)\n```python\n# step1: define your own operator:\ndef selection_tournament(algorithm, tourn_size):\n    FitV = algorithm.FitV\n    sel_index = []\n    for i in range(algorithm.size_pop):\n        aspirants_index = np.random.choice(range(algorithm.size_pop), size=tourn_size)\n        sel_index.append(max(aspirants_index, key=lambda i: FitV[i]))\n    algorithm.Chrom = algorithm.Chrom[sel_index, :]  # next generation\n    return algorithm.Chrom\n\n\n```\n\nImport and build ga  \n-\u003e Demo code: [examples/demo_ga_udf.py#s2](https://github.com/guofei9987/scikit-opt/blob/master/examples/demo_ga_udf.py#L12)\n```python\nimport numpy as np\nfrom sko.GA import GA, GA_TSP\n\ndemo_func = lambda x: x[0] ** 2 + (x[1] - 0.05) ** 2 + (x[2] - 0.5) ** 2\nga = GA(func=demo_func, n_dim=3, size_pop=100, max_iter=500, prob_mut=0.001,\n        lb=[-1, -10, -5], ub=[2, 10, 2], precision=[1e-7, 1e-7, 1])\n\n```\nRegist your udf to GA  \n-\u003e Demo code: [examples/demo_ga_udf.py#s3](https://github.com/guofei9987/scikit-opt/blob/master/examples/demo_ga_udf.py#L20)\n```python\nga.register(operator_name='selection', operator=selection_tournament, tourn_size=3)\n```\n\nscikit-opt also provide some operators  \n-\u003e Demo code: [examples/demo_ga_udf.py#s4](https://github.com/guofei9987/scikit-opt/blob/master/examples/demo_ga_udf.py#L22)\n```python\nfrom sko.operators import ranking, selection, crossover, mutation\n\nga.register(operator_name='ranking', operator=ranking.ranking). \\\n    register(operator_name='crossover', operator=crossover.crossover_2point). \\\n    register(operator_name='mutation', operator=mutation.mutation)\n```\nNow do GA as usual  \n-\u003e Demo code: [examples/demo_ga_udf.py#s5](https://github.com/guofei9987/scikit-opt/blob/master/examples/demo_ga_udf.py#L28)\n```python\nbest_x, best_y = ga.run()\nprint('best_x:', best_x, '\\n', 'best_y:', best_y)\n```\n\n\u003e Until Now, the **udf** surport `crossover`, `mutation`, `selection`, `ranking` of GA\n\u003e scikit-opt provide a dozen of operators, see [here](https://github.com/guofei9987/scikit-opt/tree/master/sko/operators)\n\nFor advanced users:\n\n-\u003e Demo code: [examples/demo_ga_udf.py#s6](https://github.com/guofei9987/scikit-opt/blob/master/examples/demo_ga_udf.py#L31)\n```python\nclass MyGA(GA):\n    def selection(self, tourn_size=3):\n        FitV = self.FitV\n        sel_index = []\n        for i in range(self.size_pop):\n            aspirants_index = np.random.choice(range(self.size_pop), size=tourn_size)\n            sel_index.append(max(aspirants_index, key=lambda i: FitV[i]))\n        self.Chrom = self.Chrom[sel_index, :]  # next generation\n        return self.Chrom\n\n    ranking = ranking.ranking\n\n\ndemo_func = lambda x: x[0] ** 2 + (x[1] - 0.05) ** 2 + (x[2] - 0.5) ** 2\nmy_ga = MyGA(func=demo_func, n_dim=3, size_pop=100, max_iter=500, lb=[-1, -10, -5], ub=[2, 10, 2],\n             precision=[1e-7, 1e-7, 1])\nbest_x, best_y = my_ga.run()\nprint('best_x:', best_x, '\\n', 'best_y:', best_y)\n```\n\n##  feature2: continue to run\n(New in version 0.3.6)  \nRun an algorithm for 10 iterations, and then run another 20 iterations base on the 10 iterations before:\n```python\nfrom sko.GA import GA\n\nfunc = lambda x: x[0] ** 2\nga = GA(func=func, n_dim=1)\nga.run(10)\nga.run(20)\n```\n\n## feature3: 4-ways to accelerate\n- vectorization\n- multithreading\n- multiprocessing\n- cached\n\nsee [https://github.com/guofei9987/scikit-opt/blob/master/examples/example_function_modes.py](https://github.com/guofei9987/scikit-opt/blob/master/examples/example_function_modes.py)\n\n\n\n## feature4: GPU computation\n We are developing GPU computation, which will be stable on version 1.0.0  \nAn example is already available: [https://github.com/guofei9987/scikit-opt/blob/master/examples/demo_ga_gpu.py](https://github.com/guofei9987/scikit-opt/blob/master/examples/demo_ga_gpu.py)\n\n\n# Quick start\n\n## 1. Differential Evolution\n**Step1**：define your problem  \n-\u003e Demo code: [examples/demo_de.py#s1](https://github.com/guofei9987/scikit-opt/blob/master/examples/demo_de.py#L1)\n```python\n'''\nmin f(x1, x2, x3) = x1^2 + x2^2 + x3^2\ns.t.\n    x1*x2 \u003e= 1\n    x1*x2 \u003c= 5\n    x2 + x3 = 1\n    0 \u003c= x1, x2, x3 \u003c= 5\n'''\n\n\ndef obj_func(p):\n    x1, x2, x3 = p\n    return x1 ** 2 + x2 ** 2 + x3 ** 2\n\n\nconstraint_eq = [\n    lambda x: 1 - x[1] - x[2]\n]\n\nconstraint_ueq = [\n    lambda x: 1 - x[0] * x[1],\n    lambda x: x[0] * x[1] - 5\n]\n\n```\n\n**Step2**: do Differential Evolution  \n-\u003e Demo code: [examples/demo_de.py#s2](https://github.com/guofei9987/scikit-opt/blob/master/examples/demo_de.py#L25)\n```python\nfrom sko.DE import DE\n\nde = DE(func=obj_func, n_dim=3, size_pop=50, max_iter=800, lb=[0, 0, 0], ub=[5, 5, 5],\n        constraint_eq=constraint_eq, constraint_ueq=constraint_ueq)\n\nbest_x, best_y = de.run()\nprint('best_x:', best_x, '\\n', 'best_y:', best_y)\n\n```\n\n## 2. Genetic Algorithm\n\n**Step1**：define your problem  \n-\u003e Demo code: [examples/demo_ga.py#s1](https://github.com/guofei9987/scikit-opt/blob/master/examples/demo_ga.py#L1)\n```python\nimport numpy as np\n\n\ndef schaffer(p):\n    '''\n    This function has plenty of local minimum, with strong shocks\n    global minimum at (0,0) with value 0\n    https://en.wikipedia.org/wiki/Test_functions_for_optimization\n    '''\n    x1, x2 = p\n    part1 = np.square(x1) - np.square(x2)\n    part2 = np.square(x1) + np.square(x2)\n    return 0.5 + (np.square(np.sin(part1)) - 0.5) / np.square(1 + 0.001 * part2)\n\n\n```\n\n**Step2**: do Genetic Algorithm  \n-\u003e Demo code: [examples/demo_ga.py#s2](https://github.com/guofei9987/scikit-opt/blob/master/examples/demo_ga.py#L16)\n```python\nfrom sko.GA import GA\n\nga = GA(func=schaffer, n_dim=2, size_pop=50, max_iter=800, prob_mut=0.001, lb=[-1, -1], ub=[1, 1], precision=1e-7)\nbest_x, best_y = ga.run()\nprint('best_x:', best_x, '\\n', 'best_y:', best_y)\n```\n\n-\u003e Demo code: [examples/demo_ga.py#s3](https://github.com/guofei9987/scikit-opt/blob/master/examples/demo_ga.py#L22)\n```python\nimport pandas as pd\nimport matplotlib.pyplot as plt\n\nY_history = pd.DataFrame(ga.all_history_Y)\nfig, ax = plt.subplots(2, 1)\nax[0].plot(Y_history.index, Y_history.values, '.', color='red')\nY_history.min(axis=1).cummin().plot(kind='line')\nplt.show()\n```\n\n![Figure_1-1](https://img1.github.io/heuristic_algorithm/ga_1.png)\n\n### 2.2 Genetic Algorithm for TSP(Travelling Salesman Problem)\nJust import the `GA_TSP`, it overloads the `crossover`, `mutation` to solve the TSP\n\n**Step1**: define your problem. Prepare your points coordinate and the distance matrix.  \nHere I generate the data randomly as a demo:  \n-\u003e Demo code: [examples/demo_ga_tsp.py#s1](https://github.com/guofei9987/scikit-opt/blob/master/examples/demo_ga_tsp.py#L1)\n```python\nimport numpy as np\nfrom scipy import spatial\nimport matplotlib.pyplot as plt\n\nnum_points = 50\n\npoints_coordinate = np.random.rand(num_points, 2)  # generate coordinate of points\ndistance_matrix = spatial.distance.cdist(points_coordinate, points_coordinate, metric='euclidean')\n\n\ndef cal_total_distance(routine):\n    '''The objective function. input routine, return total distance.\n    cal_total_distance(np.arange(num_points))\n    '''\n    num_points, = routine.shape\n    return sum([distance_matrix[routine[i % num_points], routine[(i + 1) % num_points]] for i in range(num_points)])\n\n\n```\n\n**Step2**: do GA  \n-\u003e Demo code: [examples/demo_ga_tsp.py#s2](https://github.com/guofei9987/scikit-opt/blob/master/examples/demo_ga_tsp.py#L19)\n```python\n\nfrom sko.GA import GA_TSP\n\nga_tsp = GA_TSP(func=cal_total_distance, n_dim=num_points, size_pop=50, max_iter=500, prob_mut=1)\nbest_points, best_distance = ga_tsp.run()\n\n```\n\n**Step3**: Plot the result:  \n-\u003e Demo code: [examples/demo_ga_tsp.py#s3](https://github.com/guofei9987/scikit-opt/blob/master/examples/demo_ga_tsp.py#L26)\n```python\nfig, ax = plt.subplots(1, 2)\nbest_points_ = np.concatenate([best_points, [best_points[0]]])\nbest_points_coordinate = points_coordinate[best_points_, :]\nax[0].plot(best_points_coordinate[:, 0], best_points_coordinate[:, 1], 'o-r')\nax[1].plot(ga_tsp.generation_best_Y)\nplt.show()\n```\n\n![GA_TPS](https://img1.github.io/heuristic_algorithm/ga_tsp.png)\n\n\n## 3. PSO(Particle swarm optimization)\n\n### 3.1 PSO\n**Step1**: define your problem:  \n-\u003e Demo code: [examples/demo_pso.py#s1](https://github.com/guofei9987/scikit-opt/blob/master/examples/demo_pso.py#L1)\n```python\ndef demo_func(x):\n    x1, x2, x3 = x\n    return x1 ** 2 + (x2 - 0.05) ** 2 + x3 ** 2\n\n\n```\n\n**Step2**: do PSO  \n-\u003e Demo code: [examples/demo_pso.py#s2](https://github.com/guofei9987/scikit-opt/blob/master/examples/demo_pso.py#L6)\n```python\nfrom sko.PSO import PSO\n\npso = PSO(func=demo_func, n_dim=3, pop=40, max_iter=150, lb=[0, -1, 0.5], ub=[1, 1, 1], w=0.8, c1=0.5, c2=0.5)\npso.run()\nprint('best_x is ', pso.gbest_x, 'best_y is', pso.gbest_y)\n\n```\n\n**Step3**: Plot the result  \n-\u003e Demo code: [examples/demo_pso.py#s3](https://github.com/guofei9987/scikit-opt/blob/master/examples/demo_pso.py#L13)\n```python\nimport matplotlib.pyplot as plt\n\nplt.plot(pso.gbest_y_hist)\nplt.show()\n```\n\n\n![PSO_TPS](https://img1.github.io/heuristic_algorithm/pso.png)\n\n### 3.2 PSO with nonlinear constraint\n\nIf you need nolinear constraint like `(x[0] - 1) ** 2 + (x[1] - 0) ** 2 - 0.5 ** 2\u003c=0`  \nCodes are like this:\n```python\nconstraint_ueq = (\n    lambda x: (x[0] - 1) ** 2 + (x[1] - 0) ** 2 - 0.5 ** 2\n    ,\n)\npso = PSO(func=demo_func, n_dim=2, pop=40, max_iter=max_iter, lb=[-2, -2], ub=[2, 2]\n          , constraint_ueq=constraint_ueq)\n```\n\nNote that, you can add more then one nonlinear constraint. Just add it to `constraint_ueq`\n\nMore over, we have an animation:  \n![pso_ani](https://img1.github.io/heuristic_algorithm/pso.gif)  \n↑**see [examples/demo_pso_ani.py](https://github.com/guofei9987/scikit-opt/blob/master/examples/demo_pso_ani.py)**\n\n\n## 4. SA(Simulated Annealing)\n### 4.1 SA for multiple function\n**Step1**: define your problem  \n-\u003e Demo code: [examples/demo_sa.py#s1](https://github.com/guofei9987/scikit-opt/blob/master/examples/demo_sa.py#L1)\n```python\ndemo_func = lambda x: x[0] ** 2 + (x[1] - 0.05) ** 2 + x[2] ** 2\n\n```\n**Step2**: do SA  \n-\u003e Demo code: [examples/demo_sa.py#s2](https://github.com/guofei9987/scikit-opt/blob/master/examples/demo_sa.py#L3)\n```python\nfrom sko.SA import SA\n\nsa = SA(func=demo_func, x0=[1, 1, 1], T_max=1, T_min=1e-9, L=300, max_stay_counter=150)\nbest_x, best_y = sa.run()\nprint('best_x:', best_x, 'best_y', best_y)\n\n```\n\n**Step3**: Plot the result  \n-\u003e Demo code: [examples/demo_sa.py#s3](https://github.com/guofei9987/scikit-opt/blob/master/examples/demo_sa.py#L10)\n```python\nimport matplotlib.pyplot as plt\nimport pandas as pd\n\nplt.plot(pd.DataFrame(sa.best_y_history).cummin(axis=0))\nplt.show()\n\n```\n![sa](https://img1.github.io/heuristic_algorithm/sa.png)\n\n\nMoreover, scikit-opt provide 3 types of Simulated Annealing: Fast, Boltzmann, Cauchy. See [more sa](https://scikit-opt.github.io/scikit-opt/#/en/more_sa)\n### 4.2 SA for TSP\n**Step1**: oh, yes, define your problems. To boring to copy this step.  \n\n**Step2**: DO SA for TSP  \n-\u003e Demo code: [examples/demo_sa_tsp.py#s2](https://github.com/guofei9987/scikit-opt/blob/master/examples/demo_sa_tsp.py#L21)\n```python\nfrom sko.SA import SA_TSP\n\nsa_tsp = SA_TSP(func=cal_total_distance, x0=range(num_points), T_max=100, T_min=1, L=10 * num_points)\n\nbest_points, best_distance = sa_tsp.run()\nprint(best_points, best_distance, cal_total_distance(best_points))\n```\n\n**Step3**: plot the result  \n-\u003e Demo code: [examples/demo_sa_tsp.py#s3](https://github.com/guofei9987/scikit-opt/blob/master/examples/demo_sa_tsp.py#L28)\n```python\nfrom matplotlib.ticker import FormatStrFormatter\n\nfig, ax = plt.subplots(1, 2)\n\nbest_points_ = np.concatenate([best_points, [best_points[0]]])\nbest_points_coordinate = points_coordinate[best_points_, :]\nax[0].plot(sa_tsp.best_y_history)\nax[0].set_xlabel(\"Iteration\")\nax[0].set_ylabel(\"Distance\")\nax[1].plot(best_points_coordinate[:, 0], best_points_coordinate[:, 1],\n           marker='o', markerfacecolor='b', color='c', linestyle='-')\nax[1].xaxis.set_major_formatter(FormatStrFormatter('%.3f'))\nax[1].yaxis.set_major_formatter(FormatStrFormatter('%.3f'))\nax[1].set_xlabel(\"Longitude\")\nax[1].set_ylabel(\"Latitude\")\nplt.show()\n\n```\n![sa](https://img1.github.io/heuristic_algorithm/sa_tsp.png)\n\n\nMore: Plot the animation:  \n\n![sa](https://img1.github.io/heuristic_algorithm/sa_tsp1.gif)  \n↑**see [examples/demo_sa_tsp.py](https://github.com/guofei9987/scikit-opt/blob/master/examples/demo_sa_tsp.py)**\n\n\n\n\n## 5. ACA (Ant Colony Algorithm) for tsp\n-\u003e Demo code: [examples/demo_aca_tsp.py#s2](https://github.com/guofei9987/scikit-opt/blob/master/examples/demo_aca_tsp.py#L17)\n```python\nfrom sko.ACA import ACA_TSP\n\naca = ACA_TSP(func=cal_total_distance, n_dim=num_points,\n              size_pop=50, max_iter=200,\n              distance_matrix=distance_matrix)\n\nbest_x, best_y = aca.run()\n\n```\n\n![ACA](https://img1.github.io/heuristic_algorithm/aca_tsp.png)\n\n\n## 6. immune algorithm (IA)\n-\u003e Demo code: [examples/demo_ia.py#s2](https://github.com/guofei9987/scikit-opt/blob/master/examples/demo_ia.py#L6)\n```python\n\nfrom sko.IA import IA_TSP\n\nia_tsp = IA_TSP(func=cal_total_distance, n_dim=num_points, size_pop=500, max_iter=800, prob_mut=0.2,\n                T=0.7, alpha=0.95)\nbest_points, best_distance = ia_tsp.run()\nprint('best routine:', best_points, 'best_distance:', best_distance)\n\n```\n\n![IA](https://img1.github.io/heuristic_algorithm/ia2.png)\n\n## 7. Artificial Fish Swarm Algorithm (AFSA)\n-\u003e Demo code: [examples/demo_afsa.py#s1](https://github.com/guofei9987/scikit-opt/blob/master/examples/demo_afsa.py#L1)\n```python\ndef func(x):\n    x1, x2 = x\n    return 1 / x1 ** 2 + x1 ** 2 + 1 / x2 ** 2 + x2 ** 2\n\n\nfrom sko.AFSA import AFSA\n\nafsa = AFSA(func, n_dim=2, size_pop=50, max_iter=300,\n            max_try_num=100, step=0.5, visual=0.3,\n            q=0.98, delta=0.5)\nbest_x, best_y = afsa.run()\nprint(best_x, best_y)\n```\n\n\n\n# Projects using scikit-opt\n\n- [Yu, J., He, Y., Yan, Q., \u0026 Kang, X. (2021). SpecView: Malware Spectrum Visualization Framework With Singular Spectrum Transformation. IEEE Transactions on Information Forensics and Security, 16, 5093-5107.](https://ieeexplore.ieee.org/abstract/document/9607026/)\n- [Zhen, H., Zhai, H., Ma, W., Zhao, L., Weng, Y., Xu, Y., ... \u0026 He, X. (2021). Design and tests of reinforcement-learning-based optimal power flow solution generator. Energy Reports.](https://www.sciencedirect.com/science/article/pii/S2352484721012737)\n- [Heinrich, K., Zschech, P., Janiesch, C., \u0026 Bonin, M. (2021). Process data properties matter: Introducing gated convolutional neural networks (GCNN) and key-value-predict attention networks (KVP) for next event prediction with deep learning. Decision Support Systems, 143, 113494.](https://www.sciencedirect.com/science/article/pii/S016792362100004X)\n- [Tang, H. K., \u0026 Goh, S. K. (2021). A Novel Non-population-based Meta-heuristic Optimizer Inspired by the Philosophy of Yi Jing. arXiv preprint arXiv:2104.08564.](https://arxiv.org/abs/2104.08564)\n- [Wu, G., Li, L., Li, X., Chen, Y., Chen, Z., Qiao, B., ... \u0026 Xia, L. (2021). Graph embedding based real-time social event matching for EBSNs recommendation. World Wide Web, 1-22.](https://link.springer.com/article/10.1007/s11280-021-00934-y)\n- [Pan, X., Zhang, Z., Zhang, H., Wen, Z., Ye, W., Yang, Y., ... \u0026 Zhao, X. (2021). A fast and robust mixture gases identification and concentration detection algorithm based on attention mechanism equipped recurrent neural network with double loss function. Sensors and Actuators B: Chemical, 342, 129982.](https://www.sciencedirect.com/science/article/abs/pii/S0925400521005517)\n- [Castella Balcell, M. (2021). Optimization of the station keeping system for the WindCrete floating offshore wind turbine.](https://upcommons.upc.edu/handle/2117/350262)\n- [Zhai, B., Wang, Y., Wang, W., \u0026 Wu, B. (2021). Optimal Variable Speed Limit Control Strategy on Freeway Segments under Fog Conditions. arXiv preprint arXiv:2107.14406.](https://arxiv.org/abs/2107.14406)\n- [Yap, X. H. (2021). Multi-label classification on locally-linear data: Application to chemical toxicity prediction.](https://etd.ohiolink.edu/apexprod/rws_olink/r/1501/10?clear=10\u0026p10_accession_num=wright162901936395651)\n- [Gebhard, L. (2021). Expansion Planning of Low-Voltage Grids Using Ant Colony Optimization Ausbauplanung von Niederspannungsnetzen mithilfe eines Ameisenalgorithmus.](https://ad-publications.cs.uni-freiburg.de/theses/Master_Lukas_Gebhard_2021.pdf)\n- [Ma, X., Zhou, H., \u0026 Li, Z. (2021). Optimal Design for Interdependencies between Hydrogen and Power Systems. IEEE Transactions on Industry Applications.](https://ieeexplore.ieee.org/abstract/document/9585654)\n- [de Curso, T. D. C. (2021). Estudo do modelo Johansen-Ledoit-Sornette de bolhas financeiras.](https://d1wqtxts1xzle7.cloudfront.net/67649721/TCC_Thibor_Final-with-cover-page-v2.pdf?Expires=1639140872\u0026Signature=LDZoVsAGO0mLMlVsQjnzpLlRhLyt5wdIDmBjm1yWog5bsx6apyRE9aHuwfnFnc96uvam573wiHMeV08QlK2vhRcQS1d0buenBT5fwoRuq6PTDoMsXmpBb-lGtu9ETiMb4sBYvcQb-X3C7Hh0Ec1FoJZ040gXJPWdAli3e1TdOcGrnOaBZMgNiYX6aKFIZaaXmiQeV3418~870bH4IOQXOapIE6-23lcOL-32T~FSjsOrENoLUkcosv6UHPourKgsRufAY-C2HBUWP36iJ7CoH0jSTo1e45dVgvqNDvsHz7tmeI~0UPGH-A8MWzQ9h2ElCbCN~UNQ8ycxOa4TUKfpCw__\u0026Key-Pair-Id=APKAJLOHF5GGSLRBV4ZA)\n- [Wu, T., Liu, J., Liu, J., Huang, Z., Wu, H., Zhang, C., ... \u0026 Zhang, G. (2021). A Novel AI-based Framework for AoI-optimal Trajectory Planning in UAV-assisted Wireless Sensor Networks. IEEE Transactions on Wireless Communications.](https://ieeexplore.ieee.org/abstract/document/9543607)\n- [Liu, H., Wen, Z., \u0026 Cai, W. (2021, August). FastPSO: Towards Efficient Swarm Intelligence Algorithm on GPUs. In 50th International Conference on Parallel Processing (pp. 1-10).](https://dl.acm.org/doi/abs/10.1145/3472456.3472474)\n- [Mahbub, R. (2020). Algorithms and Optimization Techniques for Solving TSP.](https://raiyanmahbub.com/images/Research_Paper.pdf)\n- [Li, J., Chen, T., Lim, K., Chen, L., Khan, S. A., Xie, J., \u0026 Wang, X. (2019). Deep learning accelerated gold nanocluster synthesis. Advanced Intelligent Systems, 1(3), 1900029.](https://onlinelibrary.wiley.com/doi/full/10.1002/aisy.201900029)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fguofei9987%2Fscikit-opt","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fguofei9987%2Fscikit-opt","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fguofei9987%2Fscikit-opt/lists"}