项目作者: bigzhao

项目描述 :
Binary genetic algorithm in python.
高级语言: Python
项目地址: git://github.com/bigzhao/Binary-Genetic-Algorithm.git
创建时间: 2017-11-22T02:30:07Z
项目社区:https://github.com/bigzhao/Binary-Genetic-Algorithm

开源协议:

下载


Binary Genetic algorithm in Python

Status : under development

What’s New

version 0.0.1 : intial version.

Presentation

In computer science and operations research, a genetic algorithm (GA) is a metaheuristic inspired by the process of natural selection that belongs to the larger class of evolutionary algorithms (EA). Genetic algorithms are commonly used to generate high-quality solutions to optimization and search problems by relying on bio-inspired operators such as mutation, crossover and selection.

Prerequisites

The present code has been developed under python3.x. You will need to have the following installed on your computer to make it work :

  • Python 3.x
  • Numpy

Using Binary Genetic algorithm (BGA)

The first thing you should do is to define your own evaluation function for your own optimization problem. For example, for 0-1 pakages problems i will define the evaluation function as follows:

  1. # arr is the individual vector
  2. # w for weights
  3. # v for values
  4. # 1000 is the maximum weight.
  5. def values(arr):
  6. w = np.array([71, 34, 82, 23, 1, 88, 12, 57, 10, 68, 5, 33, 37, 69, 98, 24, 26, 83, 16, 26, 18, 43, 52, 71, 22, 65, 68, 8, 40, 40, 24, 72, 16, 34, 10, 19, 28, 13, 34, 98, 29, 31, 79, 33, 60, 74, 44, 56, 54, 17, 63, 83, 100, 54, 10, 5, 79, 42, 65, 93, 52, 64, 85, 68, 54, 62, 29, 40, 35, 90, 47, 77, 87, 75, 39, 18, 38, 25, 61, 13, 36, 53, 46, 28, 44, 34, 39, 69, 42, 97, 34, 83, 8, 74, 38, 74, 22, 40, 7, 94])
  7. v = np.array([26, 59, 30, 19, 66, 85, 94, 8, 3, 44, 5, 1, 41, 82, 76, 1, 12, 81, 73, 32, 74, 54, 62, 41, 19, 10, 65, 53, 56, 53, 70, 66, 58, 22, 72, 33, 96, 88, 68, 45, 44, 61, 78, 78, 6, 66, 11, 59, 83, 48, 52, 7, 51, 37, 89, 72, 23, 52, 55, 44, 57, 45, 11, 90, 31, 38, 48, 75, 56, 64, 73, 66, 35, 50, 16, 51, 33, 58, 85, 77, 71, 87, 69, 52, 10, 13, 39, 75, 38, 13, 90, 35, 83, 93, 61, 62, 95, 73, 26, 85])
  8. w_ = np.sum(w * arr)
  9. if w_ > 1000:
  10. # print(np.sum(w * arr))
  11. return 1.0 / (w_ - 1000)
  12. else:
  13. return np.sum(v * arr)

After defining the evalution function, you should initialize the BGA class and use .run to find the opetimal solution.

  1. from bga import BGA
  2. num_pop = 30
  3. problem_dimentions = 10
  4. test = BGA(pop_shape=(num_pop, problem_dimentions), method=values, p_c=0.8, p_m=0.2, max_round = 1000, early_stop_rounds=None, verbose = None, maximum=True)
  5. best_solution, best_fitness = test.run()

The whole test code is shown as follows:

  1. import numpy as np
  2. from bga import BGA
  3. def values(arr):
  4. w = np.array([71, 34, 82, 23, 1, 88, 12, 57, 10, 68, 5, 33, 37, 69, 98, 24, 26, 83, 16, 26, 18, 43, 52, 71, 22, 65, 68, 8, 40, 40, 24, 72, 16, 34, 10, 19, 28, 13, 34, 98, 29, 31, 79, 33, 60, 74, 44, 56, 54, 17, 63, 83, 100, 54, 10, 5, 79, 42, 65, 93, 52, 64, 85, 68, 54, 62, 29, 40, 35, 90, 47, 77, 87, 75, 39, 18, 38, 25, 61, 13, 36, 53, 46, 28, 44, 34, 39, 69, 42, 97, 34, 83, 8, 74, 38, 74, 22, 40, 7, 94])
  5. v = np.array([26, 59, 30, 19, 66, 85, 94, 8, 3, 44, 5, 1, 41, 82, 76, 1, 12, 81, 73, 32, 74, 54, 62, 41, 19, 10, 65, 53, 56, 53, 70, 66, 58, 22, 72, 33, 96, 88, 68, 45, 44, 61, 78, 78, 6, 66, 11, 59, 83, 48, 52, 7, 51, 37, 89, 72, 23, 52, 55, 44, 57, 45, 11, 90, 31, 38, 48, 75, 56, 64, 73, 66, 35, 50, 16, 51, 33, 58, 85, 77, 71, 87, 69, 52, 10, 13, 39, 75, 38, 13, 90, 35, 83, 93, 61, 62, 95, 73, 26, 85])
  6. w_ = np.sum(w * arr)
  7. if w_ > 1000:
  8. # print(np.sum(w * arr))
  9. return 1.0 / (w_ - 1000)
  10. else:
  11. return np.sum(v * arr)
  12. num_pop = 30
  13. problem_dimentions = 10
  14. test = BGA(pop_shape=(num_pop, problem_dimentions), method=values, p_c=0.8, p_m=0.2, max_round = 1000, early_stop_rounds=None, verbose = None, maximum=True)
  15. best_solution, best_fitness = test.run()

The output for test case is shown as follows:

  1. Did not improved within 100 rounds. Break.
  2. Solution: [0 0 0 1 1 0 1 0 0 0 1 0 0 1 0 0 0 0 1 0 1 0 0 0 1 0 0 1 0 0 1 1 1 0 1 1 1
  3. 1 0 0 1 1 0 1 0 0 0 0 1 1 0 0 0 0 1 1 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0
  4. 1 1 1 0 0 1 1 0 0 1 0 0 0 0 0 0 1 0 1 0 1 1 1 0 1 0]
  5. Fitness: 2325
  6. Evaluation times: 20730