Skip to main content

A genetic algorithm library in Python3

Project description

pyind

A genetic algorithm library in Python3

pyind ONLY supports ndarray (numpy)

日本語

Installation

pip install pyind

About evaluation function

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": {
        "func": evaluation_function  # Required fields and it has not default value.
    },
    "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

License

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[0]
    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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyind-1.2.tar.gz (6.4 kB view details)

Uploaded Source

Built Distribution

pyind-1.2-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

Details for the file pyind-1.2.tar.gz.

File metadata

  • Download URL: pyind-1.2.tar.gz
  • Upload date:
  • Size: 6.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.9.1 tqdm/4.28.1 CPython/3.7.1

File hashes

Hashes for pyind-1.2.tar.gz
Algorithm Hash digest
SHA256 91d98c184b646791cc4990613677952baf28734cfd1043ba988d259082dc1fd5
MD5 985a066d333f73ba8ebb8f7a3d07790e
BLAKE2b-256 b6b024d48cd88cee27ebe29c180775a3b1d25316ed4c9540ac296d1a8e1d9318

See more details on using hashes here.

File details

Details for the file pyind-1.2-py3-none-any.whl.

File metadata

  • Download URL: pyind-1.2-py3-none-any.whl
  • Upload date:
  • Size: 8.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.9.1 tqdm/4.28.1 CPython/3.7.1

File hashes

Hashes for pyind-1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 7de640c54d11d260530eaed91c0a92ec1e274062a9215cc27f75b8ffe0366163
MD5 c4ed73f69080b54b827b6423428c73a1
BLAKE2b-256 740bde171ffb85255282581a00c924bc97c12a3b4dfde585ce2aa2502ef74c2b

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page