{"id":20339048,"url":"https://github.com/szczyglis-dev/python-lanchester","last_synced_at":"2025-10-13T18:07:29.710Z","repository":{"id":47953834,"uuid":"516434083","full_name":"szczyglis-dev/python-lanchester","owner":"szczyglis-dev","description":"[Python] A module, notebook, and sample application for predicting the outcome of a battle using Lanchester's differential equations. The module can forecast results using three different models: the linear law, the square law, and a modernized model.","archived":false,"fork":false,"pushed_at":"2024-08-26T15:48:56.000Z","size":49,"stargazers_count":9,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-11T23:15:43.794Z","etag":null,"topics":["algorithm","battle","differential-equations","equation","lanchester","law","linear","notebook","notebook-jupyter","plot","prediction-model","python","python-module","python3","simulation","square"],"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/szczyglis-dev.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"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,"zenodo":null}},"created_at":"2022-07-21T15:55:03.000Z","updated_at":"2025-03-20T15:12:46.000Z","dependencies_parsed_at":"2025-04-11T23:23:57.058Z","dependency_job_id":null,"html_url":"https://github.com/szczyglis-dev/python-lanchester","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/szczyglis-dev/python-lanchester","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/szczyglis-dev%2Fpython-lanchester","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/szczyglis-dev%2Fpython-lanchester/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/szczyglis-dev%2Fpython-lanchester/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/szczyglis-dev%2Fpython-lanchester/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/szczyglis-dev","download_url":"https://codeload.github.com/szczyglis-dev/python-lanchester/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/szczyglis-dev%2Fpython-lanchester/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279016551,"owners_count":26085850,"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","status":"online","status_checked_at":"2025-10-13T02:00:06.723Z","response_time":61,"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":["algorithm","battle","differential-equations","equation","lanchester","law","linear","notebook","notebook-jupyter","plot","prediction-model","python","python-module","python3","simulation","square"],"created_at":"2024-11-14T21:15:14.246Z","updated_at":"2025-10-13T18:07:29.694Z","avatar_url":"https://github.com/szczyglis-dev.png","language":"Python","readme":"Release: **1.0.1** | build: **2024.08.26** | Python: **\u003e=3.7**\n\n# Python Lanchester's laws\n\n**A Python module, Jupyter notebook, and sample application for predicting the outcome of battles using Lanchester's differential equations. The module can forecast results using three different models: the `linear law`, the `square law`, and a `modernized model`. An example included in the repository demonstrates how to predict outcomes using any of these three models and display both the result and a plot showing the progress of the battle over time. The module can be easily integrated into any Python application.**\n\n## What are Lanchester's laws?\n\nfrom: https://en.wikipedia.org/wiki/Lanchester%27s_laws\n\u003e Lanchester's laws are mathematical formulae for calculating the relative strengths of military forces. The Lanchester equations are differential equations describing the time dependence of two armies' strengths A and B as a function of time, with the function depending only on A and B.\nIn 1915 and 1916, during World War I, M. Osipov and Frederick Lanchester independently devised a series of differential equations to demonstrate the power relationships between opposing forces. Among these are what is known as Lanchester's linear law (for ancient combat) and Lanchester's square law (for modern combat with long-range weapons such as firearms).\n\n**Screenshot from included example application**\n\n![lanchester](https://user-images.githubusercontent.com/61396542/180256468-4fec137e-121a-416b-86ce-ed96f4a27ad2.png)\n\n## Repository contents\n\n- `lanchester.py` - A Python module containing functions that solve the Lanchester equations over time.\n- `app.py` - An example application demonstrating the use of the module.\n- `notebook.ipynb` - A Jupyter notebook showcasing an example of how the module works in real time.\n\n\n### Example of Use\n\nThe module can predict outcomes using three different models: the `linear law`, the `square law`, and a `modernized model`.\n\n**Required packages**\n\n- `numpy`\n- `matplotlib`\n\n\n**Method #1: linear law**\n\n```python\n# app.py\n\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport lanchester\n\n# base parameters:\nR0 = 8000 # number of RED units\nB0 = 10000 # number of BLUE units\nT = 100 # total number of steps in the simulation\ndt = 1 # time interval \n\n\n# parameters for \"linear\" and \"modernized\" models:\nr_l = 0.00001 # combat efficiency of RED units\nb_l = 0.00002 # combat efficiency of BLUE units\n\nR, B = lanchester.linear(R0, B0, r_l, b_l, T, dt) # result\n\n```\n\n**Method #2: square law**\n\n```python\n# app.py\n\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport lanchester\n\n# base parameters:\nR0 = 8000 # number of RED units\nB0 = 10000 # number of BLUE units\nT = 100 # total number of steps in the simulation\ndt = 1 # time interval \n\n# parameters for \"square\" and \"modernized\"  models:\nr_s = 0.2 # average number of RED units that damage each other per unit of time\nb_s = 0.1 # average number of BLUE units that damage each other per unit of time\n\nR, B = lanchester.square(R0, B0, r_s, b_s, T, dt) # result\n\n```\n\n**Method #3: modernized model**\n\n```python\n# app.py\n\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport lanchester\n\n# base parameters:\nR0 = 8000 # number of RED units\nB0 = 10000 # number of BLUE units\nT = 100 # total number of steps in the simulation\ndt = 1 # time interval \n\n# parameters for \"linear\" and \"modernized\" models:\nr_l = 0.00001 # combat efficiency of RED units\nb_l = 0.00002 # combat efficiency of BLUE units\n\n\n# parameters for \"square\" and \"modernized\"  models:\nr_s = 0.2 # average number of RED units that damage each other per unit of time\nb_s = 0.1 # average number of BLUE units that damage each other per unit of time\n\n\n# parameters for \"modernized\" model only:\nr_f = 0.6 # RED units camouflage ability factor\nb_f = 0.2 # BLUE units camouflage ability factor\n\nr_a = 0.6 # RED units ability to recognize\nb_a = 0.2 # BLUE units ability to recognize\n\nr_i = 4 # RED units information warfare ability coefficient\nb_i = 4 # BLUE units information warfare ability coefficient\n\nR, B = lanchester.modernized(R0, B0, r_l, b_l, r_s, b_s, r_f, r_a, b_f, b_a, r_i, b_i, T, dt) # result\n```\n\n**Displaying result and plot from obtained predictions**\n\n```python\n\n# display result\nprint(\"Predicted result of the battle：\\n\")\n\nif R[-1] \u003e B[-1]:\n    print(\"Winner: RED\")\nelse:\n    print(\"Winner: BLUE\")\n\n# display remaining units info  \nprint(\"Remaining RED units [\", R[-1], \"]\")\nprint(\"Remaining BLUE units [\", B[-1], \"]\")\n\n# display result on plot\nt = np.arange(0, len(R) * dt, dt)\n\nplt.figure(1)\nplt.plot(t, R, '--r', label='RED units')\nplt.plot(t, B, 'b', label='BLUE units')\nplt.xlabel(\"Time (round)\")\nplt.ylabel(\"Number of units\")\nplt.title(\"Lanchester's model simulation\")\nplt.legend()\nplt.show()\n```\n\n**Result of the battle**\n\n![lanchester](https://user-images.githubusercontent.com/61396542/180256468-4fec137e-121a-416b-86ce-ed96f4a27ad2.png)\n\n\n## Changelog\n\n**1.0.0** - Initial release (2022-07-21)\n\n**1.0.1** - Updated documentation (2024-08-26)\n\n--- \n**Python Lanchester is free to use, but if you like it, you can support my work by buying me a coffee ;)**\n\nhttps://www.buymeacoffee.com/szczyglis\n\n**Enjoy!**\n\nMIT License | 2022 Marcin 'szczyglis' Szczygliński\n\nhttps://github.com/szczyglis-dev/python-lanchester\n\nContact: szczyglis@protonmail.com\n","funding_links":["https://www.buymeacoffee.com/szczyglis"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fszczyglis-dev%2Fpython-lanchester","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fszczyglis-dev%2Fpython-lanchester","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fszczyglis-dev%2Fpython-lanchester/lists"}