A simple genetic algorithm for use in Python.
Project description
Pydarwin
A simple genetic algorithm designed to take user input for parameters such as a population size, the percentage of a population considered "elite", and the number of iterations of the algorithm to naturally find the most optimal result based on a fitness function.
Getting Pydarwin Into Your Project
To use Pydarwin, you must first install it.
pip install pydarwin
To use Pydarwin to its fullest extent, the following imports are required:
from pydarwin import GA
from pydarwin.ChromosomeSpecifications import ChromosomeSpecifications
Example Of Use - A Simple Cannon
To demonstrate the use of Pydarwin, we can look at a simple cannon using basic principles of ballistics to find the optimal shot power and angle of fire to hit a defined target.
Cannon Set-Up
Before we can implement Pydarwin's features, we must first define two basic things for this example: the target's location and the equation for projectile range. Here we define our target to be at d = 500.
# The target's location
d = 500
# Range equation
# R = ((V0)^2 * (sin(2*Theta))) / g
def rangeEquation(power, theta):
R = ((power**2) * (math.sin(math.radians(2 * theta)))) / 9.81
return R
Using Pydarwin To Hit The Target
The processes of finding the optimal target requires two steps. First, we must define what our chromosomes shall be. Second, we must define our fitness function that will analyze the different chromosomes.
Step 1:
# Step 1: Define Chromosomes (Here is the Power/Angle and their ranges)
cs = ChromosomeSpecifications()
cs.add("power", 0, 100)
cs.add("theta", 0, 90)
All chromosomes that are created have three things associated with them. When adding new chromosomes, you must define a name for the chromosome (in this case they are "power" and "theta"), a minimum value, and a maximum value. In this example, the power and angle each have their own ranges that they may operate within. These numbers can be changed however you see fit.
Step 2:
# Step 2: Define the fitness of the chromosomes
def fitness(c):
power = c["power"]
theta = c["theta"]
R = rangeEquation(power, theta)
distOff = abs(d - R)
if(distOff == 0):
return sys.float_info.max
else:
return abs(1 / distOff)
In the second step, we create our fitness function. This is the function that shall define what constitutes an "optimum" chromosome. We start this fitness function by bringing our created chromosomes (c), and giving them the names "power" and "theta" to distinguish them.
Next we call our range equation defined before we began using Pydarwin, using our chromosomes as arguments. This will allow the fitness function to see the various values for the power and angle created by our chromosomes.
In this case, we want our optimal point to be the one that hits closest to our target (d). The way Pydarwin is built, the genetic algorithm will naturally take the chromosome with the highest value as the optimal, so we must return the inverse of our distance here. This way, the chromosome that hits closest to our target shall be considered the "largest" value by the algorithm. If we happen to hit exactly where we want, we return instead the highest possible float that we are able to. In this way, nothing could ever beat it since it is already the highest possible value, and shall be taken as our optimal chromosome.
Running Pydarwin
To run Pydarwin and allow the genetic algorithm to find the optimal chromosome for you, you simply need to run GA.solve, as shown:
result = GA.solve(cs, fitness, 2000, 0.2, 2000)
The solve() method takes for parameters the chromosome specifications that you defined in Step 1 of Using Pydarwin (in this example they are cs), the fitness function that you defined in Step 2 of Using Pydarwin, the size of the population, the percentage (in decimal) of chromosomes that shall be considered "elite", and the number of iterations of the fitness function.
What is happening in the solve() method at a glance:
-
Create a population made up of chromosomes using the defining properties created until we reach the population size defined in the solve() method's parameters.
-
A for-loop is created that will loop until the number of iterations defined has been reached.
-
In the loop, we begin by sorting the population based on the fitness function, with the most optimal chromosome being placed at index 0.
-
We then take the elite chromosomes. In this case, we define the elite to be considered as the best 20% of chromosomes.
-
From the elite, we create a new population by combining elements of the elite chromosomes, until we once again reach the population size defined.
-
Repeat until the number of iterations has been reached.
-
Return our best chromosome (located in index 0 after we sort the array of chromosomes).
The solve() method and the methods it uses can be found in GA.py, located in the Pydarwin public Github repository.
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
Built Distribution
File details
Details for the file pydarwin-0.0.4.tar.gz
.
File metadata
- Download URL: pydarwin-0.0.4.tar.gz
- Upload date:
- Size: 4.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.6.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 79780b1dd33704c7f14c8513e8d48c8a67edd543c39f90f2019c0d4360451028 |
|
MD5 | 75225ccfad9de871ee3f2b711cf1f80f |
|
BLAKE2b-256 | 3307ddb676ae16823f4231ad02c42e392bbce3aa88f27f1a8d26e7bca873c4e7 |
File details
Details for the file pydarwin-0.0.4-py3-none-any.whl
.
File metadata
- Download URL: pydarwin-0.0.4-py3-none-any.whl
- Upload date:
- Size: 5.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.6.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 183729231ccc414c960e071378690b7a2f0c568398029030df51089375ad9876 |
|
MD5 | eee826d9d3a79baac3b9bcbcad048940 |
|
BLAKE2b-256 | a63d67d9566f5810dc2f295e8809d62674d6af53c91ffc917234166f474dd527 |