Skip to content

Commit 7f4bf66

Browse files
committed
Added first release
1 parent 03bb7a5 commit 7f4bf66

File tree

6 files changed

+616
-130
lines changed

6 files changed

+616
-130
lines changed

.gitignore

Lines changed: 1 addition & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -1,129 +1,2 @@
1-
# Byte-compiled / optimized / DLL files
21
__pycache__/
3-
*.py[cod]
4-
*$py.class
5-
6-
# C extensions
7-
*.so
8-
9-
# Distribution / packaging
10-
.Python
11-
build/
12-
develop-eggs/
13-
dist/
14-
downloads/
15-
eggs/
16-
.eggs/
17-
lib/
18-
lib64/
19-
parts/
20-
sdist/
21-
var/
22-
wheels/
23-
pip-wheel-metadata/
24-
share/python-wheels/
25-
*.egg-info/
26-
.installed.cfg
27-
*.egg
28-
MANIFEST
29-
30-
# PyInstaller
31-
# Usually these files are written by a python script from a template
32-
# before PyInstaller builds the exe, so as to inject date/other infos into it.
33-
*.manifest
34-
*.spec
35-
36-
# Installer logs
37-
pip-log.txt
38-
pip-delete-this-directory.txt
39-
40-
# Unit test / coverage reports
41-
htmlcov/
42-
.tox/
43-
.nox/
44-
.coverage
45-
.coverage.*
46-
.cache
47-
nosetests.xml
48-
coverage.xml
49-
*.cover
50-
*.py,cover
51-
.hypothesis/
52-
.pytest_cache/
53-
54-
# Translations
55-
*.mo
56-
*.pot
57-
58-
# Django stuff:
59-
*.log
60-
local_settings.py
61-
db.sqlite3
62-
db.sqlite3-journal
63-
64-
# Flask stuff:
65-
instance/
66-
.webassets-cache
67-
68-
# Scrapy stuff:
69-
.scrapy
70-
71-
# Sphinx documentation
72-
docs/_build/
73-
74-
# PyBuilder
75-
target/
76-
77-
# Jupyter Notebook
78-
.ipynb_checkpoints
79-
80-
# IPython
81-
profile_default/
82-
ipython_config.py
83-
84-
# pyenv
85-
.python-version
86-
87-
# pipenv
88-
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
89-
# However, in case of collaboration, if having platform-specific dependencies or dependencies
90-
# having no cross-platform support, pipenv may install dependencies that don't work, or not
91-
# install all needed dependencies.
92-
#Pipfile.lock
93-
94-
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
95-
__pypackages__/
96-
97-
# Celery stuff
98-
celerybeat-schedule
99-
celerybeat.pid
100-
101-
# SageMath parsed files
102-
*.sage.py
103-
104-
# Environments
105-
.env
106-
.venv
107-
env/
108-
venv/
109-
ENV/
110-
env.bak/
111-
venv.bak/
112-
113-
# Spyder project settings
114-
.spyderproject
115-
.spyproject
116-
117-
# Rope project settings
118-
.ropeproject
119-
120-
# mkdocs documentation
121-
/site
122-
123-
# mypy
124-
.mypy_cache/
125-
.dmypy.json
126-
dmypy.json
127-
128-
# Pyre type checker
129-
.pyre/
2+
.ipynb_checkpoints/

README.md

Lines changed: 170 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,170 @@
1-
# python-lanchester
2-
Python Lanchester
1+
2+
**Python 3+**, current release: **1.0.0** build 2022-07-21
3+
4+
# Python Lanchester's laws
5+
6+
**A Python module, a Jupyter notebook and a sample application for predicting the result of a battle using Lanchester differential equations. The module can predict the result with 3 different models: `linear law`, `square law` and `modernized model`. An example included in the repository allows you to predict the result using one of these 3 models and display the result and a plot with the course of the battle over time. The module can be easily used in any Python application.**
7+
8+
## What are Lanchester's laws?
9+
from: https://en.wikipedia.org/wiki/Lanchester%27s_laws
10+
> 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.
11+
In 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).
12+
13+
![lanchester](https://user-images.githubusercontent.com/61396542/180256468-4fec137e-121a-416b-86ce-ed96f4a27ad2.png)
14+
15+
## Repository contents:
16+
17+
- `lanchester.py` - Python module with functions that solve the Lanchester equations in time
18+
- `app.py` - sample example application that uses the module
19+
- `notebook.ipynb` - Jupyter notebook with an example of how it works in real-time
20+
21+
22+
### Example of use:
23+
24+
The module can predict the result using 3 different models: `linear law`, `square law` and `modernized model`.
25+
26+
**Required libraries**
27+
28+
- `numpy`
29+
- `matplotlib`
30+
31+
32+
**Method #1: linear law**
33+
34+
```python
35+
# app.py
36+
37+
import numpy as np
38+
import matplotlib.pyplot as plt
39+
import lanchester
40+
41+
# base parameters:
42+
R0 = 8000 # number of RED units
43+
B0 = 10000 # number of BLUE units
44+
T = 100 # total number of steps in the simulation
45+
dt = 1 # time interval
46+
47+
48+
# parameters for "linear" and "modernized" models:
49+
r_l = 0.00001 # combat efficiency of RED units
50+
b_l = 0.00002 # combat efficiency of BLUE units
51+
52+
R, B = lanchester.linear(R0, B0, r_l, b_l, T, dt) # result
53+
54+
```
55+
56+
**Method #2: square law**
57+
58+
```python
59+
# app.py
60+
61+
import numpy as np
62+
import matplotlib.pyplot as plt
63+
import lanchester
64+
65+
# base parameters:
66+
R0 = 8000 # number of RED units
67+
B0 = 10000 # number of BLUE units
68+
T = 100 # total number of steps in the simulation
69+
dt = 1 # time interval
70+
71+
# parameters for "square" and "modernized" models:
72+
r_s = 0.2 # average number of RED units that damage each other per unit of time
73+
b_s = 0.1 # average number of BLUE units that damage each other per unit of time
74+
75+
R, B = lanchester.square(R0, B0, r_s, b_s, T, dt) # result
76+
77+
```
78+
79+
**Method #3: modernized model**
80+
81+
```python
82+
# app.py
83+
84+
import numpy as np
85+
import matplotlib.pyplot as plt
86+
import lanchester
87+
88+
# base parameters:
89+
R0 = 8000 # number of RED units
90+
B0 = 10000 # number of BLUE units
91+
T = 100 # total number of steps in the simulation
92+
dt = 1 # time interval
93+
94+
# parameters for "linear" and "modernized" models:
95+
r_l = 0.00001 # combat efficiency of RED units
96+
b_l = 0.00002 # combat efficiency of BLUE units
97+
98+
99+
# parameters for "square" and "modernized" models:
100+
r_s = 0.2 # average number of RED units that damage each other per unit of time
101+
b_s = 0.1 # average number of BLUE units that damage each other per unit of time
102+
103+
104+
# parameters for "modernized" model only:
105+
r_f = 0.6 # RED units camouflage ability factor
106+
b_f = 0.2 # BLUE units camouflage ability factor
107+
108+
r_a = 0.6 # RED units ability to recognize
109+
b_a = 0.2 # BLUE units ability to recognize
110+
111+
r_i = 4 # RED units information warfare ability coefficient
112+
b_i = 4 # BLUE units information warfare ability coefficient
113+
114+
R, 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
115+
```
116+
117+
**Displaying result and plot from obtained predictions**
118+
119+
```python
120+
121+
# display result
122+
print("Predicted result of the battle:\n")
123+
124+
if R[-1] > B[-1]:
125+
print("Winner: RED")
126+
else:
127+
print("Winner: BLUE")
128+
129+
# display remaining units info
130+
print("Remaining RED units [", R[-1], "]")
131+
print("Remaining BLUE units [", B[-1], "]")
132+
133+
# display result on plot
134+
t = np.arange(0, len(R) * dt, dt)
135+
136+
plt.figure(1)
137+
plt.plot(t, R, '--r', label='RED units')
138+
plt.plot(t, B, 'b', label='BLUE units')
139+
plt.xlabel("Time (round)")
140+
plt.ylabel("Number of units")
141+
plt.title("Lanchester's model simulation")
142+
plt.legend()
143+
plt.show()
144+
```
145+
146+
**Result:**
147+
148+
![lanchester](https://user-images.githubusercontent.com/61396542/180256468-4fec137e-121a-416b-86ce-ed96f4a27ad2.png)
149+
150+
151+
## Changelog
152+
**- 1.0.0** - published first release (2022-07-21)
153+
154+
## Credits
155+
156+
### Python Lanchester is free to use but if you liked then you can donate project via BTC:
157+
158+
**14X6zSCbkU5wojcXZMgT9a4EnJNcieTrcr**
159+
160+
or by PayPal:
161+
**[https://www.paypal.me/szczyglinski](https://www.paypal.me/szczyglinski)**
162+
163+
164+
**Enjoy!**
165+
166+
MIT License | 2022 Marcin 'szczyglis' Szczygliński
167+
168+
https://github.com/szczyglis-dev/python-lanchester
169+
170+

__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# (c) 2022 Marcin "szczyglis" Szczygliński
2+
# GitHub page: https://github.com/szczyglis-dev/python-lanchester
3+
4+
# Version: 1.0.0
5+
# This package is licensed under the MIT License.
6+
# License text available at https://opensource.org/licenses/MIT
7+
8+
__version__ = "1.0.0"

0 commit comments

Comments
 (0)