A genetic algorithm library in Python3

# pyind

A genetic algorithm library in Python3

pyind ONLY supports ndarray (numpy)

## Installation

`pip install pyind`

An evaluation function has the following format

```def evaluation_function(individual):  # individual is an array of gene
return fitness_of_this_individual
```

## About `conf`

`conf` has the following format

```conf_format = {
"eval": {
},
"sel": {
# See "Table Sel" below
},
"xovr": {
# See "Table Xovr" below
},
"mut": {
# See "Table Mut" below
},
}
```

Value that can be set to `conf["sel"]` are as shown in the table

In parentheses is default value

Table Sel

"sel" (elitism) "num" (10)
elitism 0–size of poplation
roulette 0–size of poplation

Value that can be set to `conf["xovr"]` are as shown in the table

In parentheses is default value

Table Xovr

"xovr" (p2) "pb" (0.875)
p2 0–1
uniform 0–1
ox 0–1

Value that can be set to `conf["mut"]` are as shown in the table

In parentheses is default value

Table Mut

"mut" (flip_bit) "pb" (0.0075) "delta" (1)
flip_bit 0–1
boundary 0–1 0–∞
swap_idx 0–1

## Future Releases

1. Fix bug
2. Add functions of selection, crossover and mutation
3. Run more faster

MIT

## Sample code

### Onemax problem

```# Onemax Problem
import numpy as np

from pyind import pyind as pi
from pyind import defaults as df

IND_LEN = 100
POP_LEN = 100

def evl(ind):
return ind.sum()

if __name__ == "__main__":
pop = np.random.randint(2, size=(POP_LEN, IND_LEN))

conf = df.CONF
conf["eval"]["func"] = evl

best = pi.Pyind(pop, conf).run()

print("best ind: ")
print(best)
```

### Traveling salesman problem (TSP)

```# Traveling salesman problem

import numpy as np
import matplotlib.pyplot as plt

from pyind import pyind as pi
from pyind import crossover as xovr
from pyind import mutation as mut
from pyind import defaults as df

CITIES_LEN = 30
POP_LEN = 300
END_GEN = 500

cities = np.random.rand(CITIES_LEN * 2).reshape((-1, 2))

def evl(ind):
total = 0
for i in range(1, len(ind)):
total += np.linalg.norm(cities[ind[i]] - cities[ind[i - 1]])
return -total

def solve(pop):
conf = df.CONF
conf["eval"]["func"] = evl
conf["xovr"]["func"] = xovr.ox
conf["mut"]["func"] = mut.swap_idx
conf["mut"]["pb"] = 0.10
return pi.Pyind(pop, conf).run(END_GEN)

if __name__ == "__main__":
t = cities.T

# Create pop
pop = np.tile(np.arange(CITIES_LEN), (POP_LEN, 1))
for e in pop:
np.random.shuffle(e)

# Plot gen 0
idx = pop
plt.plot(t[0, idx], t[1, idx], label="gen 0", marker="o")

best = solve(pop)
print("best ind: ")
print(best)

# Plot gen END_GEN
idx = best
plt.plot(t[0, idx], t[1, idx], label="gen " + str(END_GEN), marker="o")

plt.legend()
plt.show()
```

## Project details

This version 1.2 1.1 1.0