Code Monkey home page Code Monkey logo

gaft's People

Contributors

minimada avatar pytlab avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

gaft's Issues

GAFT是否支持符号回归的遗传算法?

符号回归的遗传算法(Genetic Programming with Symbolic Regress)是一种特殊的遗传算法,这种算法中种群中的每个个体用二叉树表示,该树的每个节点是变量或者二目运算符,每个二叉树代表一个函数映射,种群的演化即函数映射的演化,我想知道有没有可能GAFT也能支持这种算法?

初始化运行

系统: win8.1
环境:Python 3.6.3 :: Anaconda, Inc. gitbash
操作环境: gitbash 下执行python

依照https://github.com/PytLab/gaft/blob/master/INSTALL.md 安装,运行测试文件时 python tests/*test.py 只能通过7个/(total ### [**[14个),可通过测试如下列表前七个:

  1. linear_ranking_selection_test.py
  2. linear_scaling_test.py
  3. mpiutil_test.py
  4. population_test.py
  5. roulette_wheel_selection_test.py
  6. tournament_selection_test.py
  7. uniform_crossover_test.py
  8. test_all.py
  9. individual_test.py
  10. flip_bit_mutation_test.py
  11. flip_bit_big_mutation_test.py
  12. exponential_ranking_selection_test.py
  13. engine_test.py
  14. dynamic_linear_scaling_test.py
    另外7个都报的import相关问题; 示例如下:
    `1. python tests/flip_bit_big_mutation_test.py
    Traceback (most recent call last):
    File "tests/flip_bit_big_mutation_test.py", line 10, in
    from .. import GAEngine
    ValueError: attempted relative import beyond top-level package
  15. python tests/exponential_ranking_selection_test.py
    Traceback (most recent call last):
    File "tests/exponential_ranking_selection_test.py", line 9, in
    from ..components import Population, BinaryIndividual
    ValueError: attempted relative import beyond top-level package测试成功结果: python tests/uniform_crossover_test.py
    test_cross (main.UniformCrossoverTest)
    Make sure individuals can be crossed correctly. ... ok

Ran 1 test in 0.000s

OK
`
另外两个例子(一维搜索和二维搜索)可以正常运行

TypeError: register_step() got an unexpected keyword argument 'g'

使用readme当中的程序示例时报错如下:
TypeError Traceback (most recent call last)
in ()
----> 1 engine.run(ng=100)

D:\Anaconda\lib\site-packages\gaft\engine.py in profiled_func(*args, **kwargs)
43 ps.dump_stats(filename)
44 else:
---> 45 result = func(*args, **kwargs)
46 return result
47 return profiled_func

D:\Anaconda\lib\site-packages\gaft\engine.py in run(self, ng)
202 msg = '{} exception is catched'.format(type(e).name)
203 self.logger.exception(msg)
--> 204 raise e
205 finally:
206 # Recover current generation number.

D:\Anaconda\lib\site-packages\gaft\engine.py in run(self, ng)
196 for a in self.analysis:
197 if g % a.interval == 0:
--> 198 a.register_step(g=g, population=self.population, engine=self)
199 except Exception as e:
200 # Log exception info.

D:\Anaconda\lib\site-packages\gaft\mpiutil.py in _call_in_master_proc(*args, **kwargs)
110 def _call_in_master_proc(*args, **kwargs):
111 if mpi.is_master:
--> 112 return func(*args, **kwargs)
113
114 return _call_in_master_proc

TypeError: register_step() got an unexpected keyword argument 'g'

fitness init will be overwritten when GAEngine init

When perform engine = GAEngine(..., fitness=fitness), the fitness function will be overwritten None at GAEngine.init line 135:
self.ori_fitness, self.fitness = None, None

I think maybe you can use
self.ori_fitness = None if self.fitness is None else self.fitness
instead of origin code.

复杂问题求解内存消耗太大

我用遗传算法来优化机器学习参数的时候,内存消耗非常大,虽然加入mpirun后速度很快,但是50个seed一轮都跑不完,内存就完全不够用了(计算了一下,十个seed一轮约占75G),请问是什么原因呢?

如何解决离散化处理

(先膜拜一下大佬)我看框架里面会根据取值范围和精确度确定基因长度,如果是离散的变量的话就不能这么做了,刚开始看还没看完框架,不知道是否可以有另一种方式,通过直接给出基因序列和长度来进行优化

运行例子报错

在win10+py3.5上安装,mpi测试用例正常
image
运行例子时,报错:
image

请问是什么原因?
谢谢!

如何获取每一步迭代后各维度变量的值?

作者您好,
其实我只想将例子中:
indv_template = BinaryIndividual(ranges=[(0, 10), (0, 10),(0, 10)], eps=0.5)
中的多维变量实时提取出来,赋给外部环境来计算FitnessValue.....

谢谢

运行例子还是报错:

C:\Users\Administrator\Envs\py3MachineLearning\Scripts\python.exe D:/personal/Mathematical_modeling/genetic_algorithm.py
Traceback (most recent call last):
File "D:/personal/Mathematical_modeling/genetic_algorithm.py", line 48, in
engine.run(ng=100)
File "C:\Users\Administrator\Envs\py3MachineLearning\lib\site-packages\gaft\engine.py", line 45, in profiled_func
result = func(*args, **kwargs)
File "C:\Users\Administrator\Envs\py3MachineLearning\lib\site-packages\gaft\engine.py", line 160, in run
a.setup(ng=ng, engine=self)
File "C:\Users\Administrator\Envs\py3MachineLearning\lib\site-packages\gaft\mpiutil.py", line 147, in _call_in_master_proc
if mpi.is_master:
NameError: name 'mpi' is not defined
麻烦您帮忙看下这是什么问题呢?

engine.py:

import cProfile
import pstats
import os

from .components import IndividualBase, Population
from .plugin_interfaces.operators import Selection, Crossover, Mutation
from .plugin_interfaces.analysis import OnTheFlyAnalysis
from .mpiutil import MPIUtil

mpi = MPIUtil()

mpiutile.py

def master_only(func):
''' Decorator to limit a function to be called only in master process in MPI env.
'''
@wraps(func)
def _call_in_master_proc(*args, **kwargs):
if mpi.is_master:
return func(*args, **kwargs)

return _call_in_master_proc

代码:
#!/usr/bin/env python

-- coding: utf-8 --

'''
Find the global maximum for binary function: f(x) = ysim(2pix) + xcos(2piy)
'''

from math import sin, cos, pi

from gaft import GAEngine
from gaft.components import BinaryIndividual
from gaft.components import Population
from gaft.operators import TournamentSelection
from gaft.operators import UniformCrossover
from gaft.operators import FlipBitBigMutation

Built-in best fitness analysis.

from gaft.analysis.fitness_store import FitnessStore
from gaft.analysis.console_output import ConsoleOutput

Define population.

indv_template = BinaryIndividual(ranges=[(-2, 2), (-2, 2)], eps=0.001)
population = Population(indv_template=indv_template, size=50).init()

Create genetic operators.

#selection = RouletteWheelSelection()
selection = TournamentSelection()
crossover = UniformCrossover(pc=0.8, pe=0.5)
mutation = FlipBitBigMutation(pm=0.1, pbm=0.55, alpha=0.6)

Create genetic algorithm engine.

Here we pass all built-in analysis to engine constructor.

engine = GAEngine(population=population, selection=selection,
crossover=crossover, mutation=mutation,
analysis=[ConsoleOutput, FitnessStore])

Define fitness function.

@engine.fitness_register
def fitness(indv):
x, y = indv.solution
return ysin(2pix) + xcos(2piy)

if 'main' == name:
engine.run(ng=100)

Originally posted by @lilyef2000 in https://github.com/_render_node/MDEyOklzc3VlQ29tbWVudDQ0MjY1NzMzNA==/timeline/issue_comment#issuecomment-442657334

如何实现取值为0或1的基因?

使用GAIndividual(ranges=[(0,2),(0,2)], encoding='binary', eps=1.0)生成的永远是全0。
我看了源码,在GAIndividual的构造函数中,self.precisions = [(b - a)/(2**l-1) for l, (a, b) in zip(self.lengths, self.ranges)],这么一句,把精度重新计算了一下,因为在2**l-1中有减1的操作,无论设置何值,precisions都不可能等于1。
我这里有一个疑问,是否必须要在此减1?
我为了解决这个问题,定义了一个子类,重写了构造方法,将减1操作去掉后,能够正常工作了

ImportError: cannot import name log2

I'm getting following error using python 2.7.6:

Traceback (most recent call last):
  File "xxx", line 32, in <module>
    from gaft import GAEngine
  File "/usr/local/lib/python2.7/dist-packages/gaft/__init__.py", line 4, in <module>
    from .engine import GAEngine
  File "/usr/local/lib/python2.7/dist-packages/gaft/engine.py", line 15, in <module>
    from .components import GAIndividual, GAPopulation
  File "/usr/local/lib/python2.7/dist-packages/gaft/components/__init__.py", line 1, in <module>
    from .individual import GAIndividual
  File "/usr/local/lib/python2.7/dist-packages/gaft/components/individual.py", line 4, in <module>
    from math import log2
ImportError: cannot import name log2

I suppose that Python 2.7.6 is not supported by gaft? It would make sense to mention this (more) clearly on the front page :)

用这个框架优化SVM参数

我想用您的框架做svm调参,对python和遗传算法都不是很熟悉,想麻烦您指点一下。我调整的4个参数是class_weight,C,gamma,和特征选择,gamma本来应该在0到1调整,我定义为(1,10000),使用的时候除以10000得到实际的gamma值;
特征选择是这样做的,比如有50个特征,参数的取值范围就是1到2的49次方,转成二进制后,如果第0位是1,选择第0个特征。

  # Define  weight, C, gamma, features
  features = len(x[0])
  indv_template = GAIndividual(
      ranges=[(1, 20), (1, 1000), (1, 10000), (1, 2 ** (features - 1))],
      encoding='binary',
      eps=1)

这是fitness函数

# Define fitness function.
# @engine.fitness_register
def fitness(indv, x, y):
  weight, C, gama, features = indv.variants
  gama = gama / 10000
  # 选择特征
  features = format(np.long(features), 'b')
  selectedFetures = []
  for i, ch in enumerate(features):
    if ch == '1':
      selectedFetures.append(i)
  x = x[:, selectedFetures]
  svmModel = SVC(class_weight={1: weight}, gamma=gama, C=C)
  value = crossValidation(svmModel, x, y, scorer=scorer)
  return value

fitness文件保存的问题

您好,我想问一下怎么修改代码使得可以自定义文件保存的结果呢?现在每次都是best_fit.py,谢谢!

复杂模型套用问题

1.变量是list,或者矩阵的情况下自定义编码比较难搞。
2.有两个fitness函数的时候不易扩展

程序能否把最好的那组参数直接return?

hello小伙伴~程序能否直接把最好的那组参数,即Optimal solution直接return?或者是写成csv文件。那样的话其他程序就可以直接使用函数return的结果了,谢谢。

并行问题

您好:
我在笔记本上尝试,运行后CPU一直在25%左右。很显然没有并行,请问
1.并行需要额外配置吗?
2.GAFT的并行仅仅适用于集群?
谢谢您

最小值求解错误

问题

求解以下函数最小值时发现,fitness 解出来的完全是相反数,带入任意 $ (x, y) \in [1, 10] $所解出来的结果应该是个正数,这算是显示错误?

运行情况

$ python -u "c:\Users\Zero\untitle.py"
gaft.ConsoleOutput   INFO     Generation number: 50 Population number: 50
gaft.ConsoleOutput   INFO     Generation: 1, best fitness: -26.412, scaled fitness: -26.412
gaft.ConsoleOutput   INFO     Generation: 2, best fitness: -26.412, scaled fitness: -26.412
gaft.ConsoleOutput   INFO     Generation: 3, best fitness: -25.308, scaled fitness: -25.308
gaft.ConsoleOutput   INFO     Generation: 4, best fitness: -25.308, scaled fitness: -25.308
gaft.ConsoleOutput   INFO     Generation: 5, best fitness: -25.308, scaled fitness: -25.308
gaft.ConsoleOutput   INFO     Generation: 6, best fitness: -25.260, scaled fitness: -25.260
gaft.ConsoleOutput   INFO     Generation: 7, best fitness: -25.191, scaled fitness: -25.191
gaft.ConsoleOutput   INFO     Generation: 8, best fitness: -25.044, scaled fitness: -25.044
gaft.ConsoleOutput   INFO     Generation: 9, best fitness: -25.044, scaled fitness: -25.044
gaft.ConsoleOutput   INFO     Generation: 10, best fitness: -25.012, scaled fitness: -25.012
gaft.ConsoleOutput   INFO     Generation: 11, best fitness: -25.012, scaled fitness: -25.012
gaft.ConsoleOutput   INFO     Generation: 12, best fitness: -25.012, scaled fitness: -25.012
gaft.ConsoleOutput   INFO     Generation: 13, best fitness: -25.012, scaled fitness: -25.012
gaft.ConsoleOutput   INFO     Generation: 14, best fitness: -25.012, scaled fitness: -25.012
gaft.ConsoleOutput   INFO     Generation: 15, best fitness: -25.002, scaled fitness: -25.002
gaft.ConsoleOutput   INFO     Generation: 16, best fitness: -25.002, scaled fitness: -25.002
gaft.ConsoleOutput   INFO     Generation: 17, best fitness: -25.002, scaled fitness: -25.002
gaft.ConsoleOutput   INFO     Generation: 18, best fitness: -25.002, scaled fitness: -25.002
gaft.ConsoleOutput   INFO     Generation: 19, best fitness: -25.002, scaled fitness: -25.002
gaft.ConsoleOutput   INFO     Generation: 20, best fitness: -25.002, scaled fitness: -25.002
gaft.ConsoleOutput   INFO     Generation: 21, best fitness: -25.002, scaled fitness: -25.002
gaft.ConsoleOutput   INFO     Generation: 22, best fitness: -25.002, scaled fitness: -25.002
gaft.ConsoleOutput   INFO     Generation: 23, best fitness: -25.002, scaled fitness: -25.002
gaft.ConsoleOutput   INFO     Generation: 24, best fitness: -25.001, scaled fitness: -25.001
gaft.ConsoleOutput   INFO     Generation: 25, best fitness: -25.001, scaled fitness: -25.001
gaft.ConsoleOutput   INFO     Generation: 26, best fitness: -25.001, scaled fitness: -25.001
gaft.ConsoleOutput   INFO     Generation: 27, best fitness: -25.001, scaled fitness: -25.001
gaft.ConsoleOutput   INFO     Generation: 28, best fitness: -25.001, scaled fitness: -25.001
gaft.ConsoleOutput   INFO     Generation: 29, best fitness: -25.001, scaled fitness: -25.001
gaft.ConsoleOutput   INFO     Generation: 30, best fitness: -25.001, scaled fitness: -25.001
gaft.ConsoleOutput   INFO     Generation: 31, best fitness: -25.001, scaled fitness: -25.001
gaft.ConsoleOutput   INFO     Generation: 32, best fitness: -25.001, scaled fitness: -25.001
gaft.ConsoleOutput   INFO     Generation: 33, best fitness: -25.001, scaled fitness: -25.001
gaft.ConsoleOutput   INFO     Generation: 34, best fitness: -25.001, scaled fitness: -25.001
gaft.ConsoleOutput   INFO     Generation: 35, best fitness: -25.001, scaled fitness: -25.001
gaft.ConsoleOutput   INFO     Generation: 36, best fitness: -25.001, scaled fitness: -25.001
gaft.ConsoleOutput   INFO     Generation: 37, best fitness: -25.001, scaled fitness: -25.001
gaft.ConsoleOutput   INFO     Generation: 38, best fitness: -25.001, scaled fitness: -25.001
gaft.ConsoleOutput   INFO     Generation: 39, best fitness: -25.001, scaled fitness: -25.001
gaft.ConsoleOutput   INFO     Generation: 40, best fitness: -25.001, scaled fitness: -25.001
gaft.ConsoleOutput   INFO     Generation: 41, best fitness: -25.001, scaled fitness: -25.001
gaft.ConsoleOutput   INFO     Generation: 42, best fitness: -25.001, scaled fitness: -25.001
gaft.ConsoleOutput   INFO     Generation: 43, best fitness: -25.001, scaled fitness: -25.001
gaft.ConsoleOutput   INFO     Generation: 44, best fitness: -25.001, scaled fitness: -25.001
gaft.ConsoleOutput   INFO     Generation: 45, best fitness: -25.001, scaled fitness: -25.001
gaft.ConsoleOutput   INFO     Generation: 46, best fitness: -25.001, scaled fitness: -25.001
gaft.ConsoleOutput   INFO     Generation: 47, best fitness: -25.001, scaled fitness: -25.001
gaft.ConsoleOutput   INFO     Generation: 48, best fitness: -25.001, scaled fitness: -25.001
gaft.ConsoleOutput   INFO     Generation: 49, best fitness: -25.001, scaled fitness: -25.001
gaft.ConsoleOutput   INFO     Generation: 50, best fitness: -25.001, scaled fitness: -25.001
gaft.ConsoleOutput   INFO     Optimal solution: ([3.53125, 1.0], -25.0009765625)
gaft.FitnessStore   INFO     Best fitness values are written to best_fit.py

Source

from math import sin, cos, pi

from gaft import GAEngine
from gaft.components import BinaryIndividual
from gaft.components import Population
from gaft.operators import TournamentSelection
from gaft.operators import UniformCrossover
from gaft.operators import FlipBitBigMutation

# Built-in best fitness analysis.
from gaft.analysis.fitness_store import FitnessStore
from gaft.analysis.console_output import ConsoleOutput

# Define population.
indv_template = BinaryIndividual(ranges=[(1, 10), (1, 10)], eps=0.001)
population = Population(indv_template=indv_template, size=50).init()

# Create genetic operators.
#selection = RouletteWheelSelection()
selection = TournamentSelection()
crossover = UniformCrossover(pc=0.8, pe=0.5)
mutation = FlipBitBigMutation(pm=0.1, pbm=0.55, alpha=0.6)

# Create genetic algorithm engine.
# Here we pass all built-in analysis to engine constructor.
engine = GAEngine(
    population=population, selection=selection,
    crossover=crossover, mutation=mutation,
    analysis=[ConsoleOutput, FitnessStore]
)


def func(x, y):
    return (x-3.5)**2 + (y+4)**2


# Define fitness function.
@engine.fitness_register
@engine.minimize
def fitness(indv):
    x, y = indv.solution

    return func(x, y)


if '__main__' == __name__:
    engine.run(ng=50)

Version

$ pip show gaft
Name: gaft
Version: 0.5.7
Summary: A Genetic Algorithm Framework in Python
Home-page: https://github.com/PytLab/gaft
Author: Shao-Zheng-Jiang
Author-email: [email protected]
License: LICENSE
Location: c:\software\miniconda3\lib\site-packages
Requires:

Multi-objective GA

Does gaft support multi-objective GA? I.e. without combining all objectives into one. I'm looking for functionality such as NSGA2 that generates a clean pareto front.

Thanks!

关于 fitness 函数的返回类型

我看了下源码,fitness_register 要求返回是 float,而我在 fitness 里基本用的都是 numpy 的东西,类型是 numpy 的 float 。虽然可以转换,但有没有办法直接支持呢?

多元函数求最值优化参数约束问题

您好,我最近正在用gaft做多参数优化遇到了问题,求解的问题如下:目标函数min A=f(x,y,m,n),施加约束条件时,除了需要对x,y,m,n四个参数进行约束之外,还需要对参数L=g1(x,y,m,n)与K=g2(x,y,m,n)设定约束条件,这在gaft框架下如何实现呢?是否需要对种群初始化、交叉和变异函数进行修改?该如何修改呢?谢谢 : )

关于多进程运行的问题

您好,我在使用mpiexec -np 4 ex01.py的时候(windows上是mpiexec),发现速度比单个进程运行的速度慢了很多,我以为是进程数据广播和回收的耗时较长,就在fitness函数里面又手动添加了大量无效计算,增加单个个体的计算时间,但是还是出现并行计算时间明显大于单进程计算的时间,我想问一下这可能是什么原因哦?我的openmpi多进程编程测试没有出现问题,安装应该是没有问题的。我修改ex01.py中的fitness代码如下(只是添加了耗时计算):

# Define fitness function.
@engine.fitness_register
def fitness(indv):
    x, = indv.solution
    for ii in range(10000):
        x + 10*sin(5*x) + 7*cos(4*x)
    return x + 10*sin(5*x) + 7*cos(4*x)

希望得到您的回复,谢谢!

自定义individual类

你好,
我自定义了一个invidual类,但是在跑的过程中在crossover操作时,在clone那一步报错了。
因为我的invidual在初始化时需要传入额外的参数,具体错误看截图,请问有什么解决办法吗?谢谢
image

fitness函数没有并行

我看代码, fitness运行并没有并行啊,如果是mpi master的话会全部都算一次啊

                # The best individual in current population. 
                if mpi.is_master:
                    best_indv = self.population.best_indv(self.fitness)
                else:
                    best_indv = None
                best_indv = mpi.bcast(best_indv)

您好,运行库报错,Individual index(50) out of range

代码:
import numpy as np
from gaft import GAEngine
from gaft.components import DecimalIndividual, Population
from gaft.operators import RouletteWheelSelection, UniformCrossover, FlipBitMutation

Analysis plugin base class.

from gaft.plugin_interfaces.analysis import OnTheFlyAnalysis
from gaft.analysis.fitness_store import FitnessStore

M_pi = np.pi
Lambda = 1
NTET = 180
dtet = M_pi / 2. / NTET
DISTEW = 2
dishD = 0.1
PiDol = M_pi * dishD / Lambda
y_opti = np.zeros(361)

检测最小差值是否小于0.5

def interval_verify(gene, minimum=0.5):
for i in range(len(gene)):
for j in gene[i:]:
if abs(gene[i] - j) > minimum:
return False
return True

def lobeAMPSquare(tet):
if abs(tet) < 10e-9:
return 1.
x = tet * PiDol
y = np.sin(x) / x
return y

Step 1. 定义种群

每个基因的范围是【0~32】

indv_template = DecimalIndividual(ranges=[(-7, 8)] * 14, eps=0.005)
population = Population(indv_template=indv_template, size=50)
population.init() # Initialize population with individuals.

Step 2. 定义基因操作

selection = RouletteWheelSelection()
crossover = UniformCrossover(pc=0.8, pe=0.5)
mutation = FlipBitMutation(pm=0.1)

Step 3. 初始化引擎

engine = GAEngine(population=population, selection=selection,
crossover=crossover, mutation=mutation,
analysis=[FitnessStore])

Step 4. 定义评价函数

@engine.fitness_register
def fitness(indv):
LesPXX = np.sort(np.array(indv.solution))
# 先判断是否满足最小间距
if not interval_verify(LesPXX):
return -float('inf')
i = -180
while i <= 180:
teta = i * dtet
RepAmp = lobeAMPSquare(teta)
sumc = 0
ws = np.complex(0., 0.)
for na in range(14):
phase = 2. * M_pi * DISTEW * LesPXX[na] / Lambda * np.sin(teta)
ws += (complex(np.cos(phase), np.sin(phase)) * RepAmp)
sumc += 1
y_opti[i + NTET] = ws.real ** 2 + ws.imag ** 2
i += 1
res = np.abs(y_opti/np.max(y_opti))
return float(np.max(np.union1d(res[-180:-2], res[2:180])))
# gene = np.array(indv.solution)
# score1 = np.sin((np.pi * d * theta) / lamda) / (np.pi * d / lamda)
# score2 = np.exp(i * 2 * np.pi * theta * (d + gene) / lamda)
# score3 = score1 * score2
# # a - 实部平方 + 虚部平方
# score = np.sum((score3.real) ** 2 + (score3.imag) ** 2)
# # b - 用绝对值
# # score = np.sum(np.abs(score3))
# return float(-score) # 这个库只有最小化,我们需要最大化,因此加一个负号

Step 5. 定义动态分析, 不注释会报错

@engine.analysis_register
class ConsoleOutputAnalysis(OnTheFlyAnalysis):
interval = 1
master_only = True

def register_step(self, g, population, engine):
    best_indv = population.best_indv(engine.fitness)
    msg = 'Generation: {}, best fitness: {:.3f}'.format(g, engine.ori_fmax)
    self.logger.info(msg)

def finalize(self, population, engine):
    best_indv = population.best_indv(engine.fitness)
    x = best_indv.solution
    y = engine.ori_fmax
    msg = 'Optimal solution: ({}, {})'.format(x, y)
    self.logger.info(msg)
    print('sort:', np.sort(x))

Step 6. 运行

if 'main' == name:
engine.run(5)

报错内容:

C:\Users\liu\AppData\Local\Programs\Python\Python36\python.exe C:/Users/liu/Desktop/1.py
gaft.GAEngine ERROR IndexError exception is catched
Traceback (most recent call last):
File "C:\Users\liu\AppData\Local\Programs\Python\Python36\lib\site-packages\gaft\engine.py", line 182, in run
parents = self.selection.select(self.population, fitness=self.fitness)
File "C:\Users\liu\AppData\Local\Programs\Python\Python36\lib\site-packages\gaft\plugin_interfaces\metaclasses.py", line 157, in _wrapped_select
return select(self, population, fitness)
File "C:\Users\liu\AppData\Local\Programs\Python\Python36\lib\site-packages\gaft\operators\selection\roulette_wheel_selection.py", line 39, in select
father = population[father_idx]
File "C:\Users\liu\AppData\Local\Programs\Python\Python36\lib\site-packages\gaft\components\population.py", line 168, in getitem
raise IndexError('Individual index({}) out of range'.format(key))
IndexError: Individual index(50) out of range
gaft.FitnessStore INFO Best fitness values are written to best_fit.py
gaft.ConsoleOutputAnalysis INFO Optimal solution: ([-0.8799999999999999, 4.825000000000001, -1.5999999999999996, -1.0599999999999996, -3.55, -5.025, 5.870000000000001, 7.9399999999999995, -5.89, 5.234999999999999, -2.1799999999999997, 3.625, -6.375, -4.045], -inf)
sort: [-6.375 -5.89 -5.025 -4.045 -3.55 -2.18 -1.6 -1.06 -0.88 3.625
4.825 5.235 5.87 7.94 ]
Traceback (most recent call last):
File "C:/Users/liu/Desktop/1.py", line 106, in
engine.run(5)
File "C:\Users\liu\AppData\Local\Programs\Python\Python36\lib\site-packages\gaft\engine.py", line 45, in profiled_func
result = func(*args, **kwargs)
File "C:\Users\liu\AppData\Local\Programs\Python\Python36\lib\site-packages\gaft\engine.py", line 209, in run
raise e
File "C:\Users\liu\AppData\Local\Programs\Python\Python36\lib\site-packages\gaft\engine.py", line 182, in run
parents = self.selection.select(self.population, fitness=self.fitness)
File "C:\Users\liu\AppData\Local\Programs\Python\Python36\lib\site-packages\gaft\plugin_interfaces\metaclasses.py", line 157, in _wrapped_select
return select(self, population, fitness)
File "C:\Users\liu\AppData\Local\Programs\Python\Python36\lib\site-packages\gaft\operators\selection\roulette_wheel_selection.py", line 39, in select
father = population[father_idx]
File "C:\Users\liu\AppData\Local\Programs\Python\Python36\lib\site-packages\gaft\components\population.py", line 168, in getitem
raise IndexError('Individual index({}) out of range'.format(key))
IndexError: Individual index(50) out of range

初始化运行

系统: win8.1
环境:Python 3.6.3 :: Anaconda, Inc. gitbash
操作环境: gitbash 下执行python

依照https://github.com/PytLab/gaft/blob/master/INSTALL.md 安装,运行测试文件时 python tests/*test.py 只能通过7个/(total ### [**[14个),可通过测试如下列表前七个:

  1. linear_ranking_selection_test.py
  2. linear_scaling_test.py
  3. mpiutil_test.py
  4. population_test.py
  5. roulette_wheel_selection_test.py
  6. tournament_selection_test.py
  7. uniform_crossover_test.py
  8. test_all.py
  9. individual_test.py
  10. flip_bit_mutation_test.py
  11. flip_bit_big_mutation_test.py
  12. exponential_ranking_selection_test.py
  13. engine_test.py
  14. dynamic_linear_scaling_test.py

另外7个都报的import相关问题; 示例如下:
python tests/flip_bit_big_mutation_test.py Traceback (most recent call last): File "tests/flip_bit_big_mutation_test.py", line 10, in from .. import GAEngine ValueError: attempted relative import beyond top-level package
python tests/exponential_ranking_selection_test.py Traceback (most recent call last): File "tests/exponential_ranking_selection_test.py", line 9, in from ..components import Population, BinaryIndividual ValueError: attempted relative import beyond top-level package
测试成功结果:
python tests/uniform_crossover_test.py test_cross (main.UniformCrossoverTest) Make sure individuals can be crossed correctly. ... ok Ran 1 test in 0.000s OK
另外两个例子(一维搜索和二维搜索)可以正常运行

通过python setup.py install安装gaft不成功

错误信息如下:(希望得到指点,多谢!期待您的回复~)
Installed /usr/local/lib/python3.5/dist-packages/gaft-0.5.4-py3.5.egg
Processing dependencies for gaft==0.5.4
Searching for mpi4py
Reading https://pypi.org/simple/mpi4py/
Downloading https://files.pythonhosted.org/packages/31/27/1288918ac230cc9abc0da17d84d66f3db477757d90b3d6b070d709391a15/mpi4py-3.0.0.tar.gz#sha256=b457b02d85bdd9a4775a097fac5234a20397b43e073f14d9e29b6cd78c68efd7
Best match: mpi4py 3.0.0
Processing mpi4py-3.0.0.tar.gz
Writing /tmp/easy_install-y80fvzfj/mpi4py-3.0.0/setup.cfg
Running mpi4py-3.0.0/setup.py -q bdist_egg --dist-dir /tmp/easy_install-y80fvzfj/mpi4py-3.0.0/egg-dist-tmp-c4x3ge3x
/usr/bin/ld: cannot find -llmpe
collect2: error: ld returned 1 exit status
/usr/bin/ld: cannot find -lvt-mpi
collect2: error: ld returned 1 exit status
/usr/bin/ld: cannot find -lvt.mpi
collect2: error: ld returned 1 exit status
/usr/bin/ld: cannot find -lvt-mpi
collect2: error: ld returned 1 exit status
/usr/bin/ld: cannot find -lvt.mpi
collect2: error: ld returned 1 exit status
/usr/bin/ld: cannot find -lvt-hyb
collect2: error: ld returned 1 exit status
/usr/bin/ld: cannot find -lvt.ompi
collect2: error: ld returned 1 exit status
_configtest.c:2:17: fatal error: mpi.h: No such file or directory
compilation terminated.
error: Setup script exited with error: Cannot compile MPI programs. Check your configuration!!!

mpiexec 問題

請問為什麼我的電腦會運行 2 組? 而不是1組data 分派給2個核心?

C:\test>mpiexec -np 2 "C:\ProgramData\Anaconda3\python.exe" ex01.py
gaft.ConsoleOutputAnalysis INFO Generation: 0, best fitness: 19.883
gaft.ConsoleOutputAnalysis INFO Generation: 0, best fitness: 24.853
gaft.ConsoleOutputAnalysis INFO Generation: 1, best fitness: 19.883
gaft.ConsoleOutputAnalysis INFO Generation: 1, best fitness: 24.854
gaft.ConsoleOutputAnalysis INFO Generation: 2, best fitness: 19.883
gaft.ConsoleOutputAnalysis INFO Generation: 2, best fitness: 24.854
gaft.ConsoleOutputAnalysis INFO Generation: 3, best fitness: 19.883
gaft.ConsoleOutputAnalysis INFO Generation: 3, best fitness: 24.854
gaft.ConsoleOutputAnalysis INFO Generation: 4, best fitness: 20.600
gaft.ConsoleOutputAnalysis INFO Generation: 4, best fitness: 24.854
gaft.ConsoleOutputAnalysis INFO Generation: 5, best fitness: 20.600
gaft.ConsoleOutputAnalysis INFO Generation: 5, best fitness: 24.854
gaft.ConsoleOutputAnalysis INFO Generation: 6, best fitness: 20.600
gaft.ConsoleOutputAnalysis INFO Generation: 6, best fitness: 24.855
gaft.ConsoleOutputAnalysis INFO Generation: 7, best fitness: 20.677
gaft.ConsoleOutputAnalysis INFO Generation: 7, best fitness: 24.855
gaft.ConsoleOutputAnalysis INFO Generation: 8, best fitness: 20.677
gaft.ConsoleOutputAnalysis INFO Generation: 9, best fitness: 20.677
gaft.ConsoleOutputAnalysis INFO Generation: 8, best fitness: 24.855
gaft.FitnessStore INFO Best fitness values are written to best_fit.py
gaft.ConsoleOutputAnalysis INFO Optimal solution: ([9.3212890625], 20.6766
55455888568)
gaft.ConsoleOutputAnalysis INFO Generation: 9, best fitness: 24.855
gaft.FitnessStore INFO Best fitness values are written to best_fit.py
gaft.ConsoleOutputAnalysis INFO Optimal solution: ([7.855224609375], 24.85
494496737466)

How to use it right

I try it as the tutorial,but I get wrong answer.
I print every individual in every generation.
the result like this:
0.093015549423
2535.6098254620283
2535.6098254620283
2544.9132766384128
2499.4233745285956
2528.402614792907
2509.3861930920684
2507.066317062549
2530.2371658559478
2530.2371658559478
2535.6098254620283
gaft.ConsoleOutput INFO Generation number: 10 Population number: 10
[7.96875, 10.6875, 0.625, 2.40625, 6.40625, 40.05859375, 2.0, -1.9375, 0.875, 7.96875, 5.40625]
[4.53125, 9.75, 0.625, 1.0, 6.71875, 39.671875, 0.375, -1.9375, 0.5, 9.0625, 4.46875]
[6.875, 9.59375, 0.75, 2.875, 7.03125, 20.529296875, 1.5, -2.09375, 0.375, 5.625, 7.59375]
[7.1875, 2.5625, 0.375, 5.0625, 9.375, 42.572265625, 2.125, -5.375, 1.125, 6.875, 7.59375]
[5.78125, 10.375, 0.125, 5.53125, 9.375, 5.25390625, 0.0, -3.5, 2.875, 7.96875, 5.25]
[8.4375, 9.75, 0.5, 1.3125, 6.25, 57.07421875, 0.125, -5.375, 2.75, 7.96875, 5.875]
[4.0625, 10.375, 0.5, 4.59375, 7.34375, 97.486328125, 1.25, -5.375, 2.0, 9.53125, 5.40625]
[4.53125, 10.53125, 0.625, 5.53125, 7.34375, 26.5234375, 3.0, -3.03125, 1.25, 9.0625, 5.875]
[4.84375, 9.28125, 0.875, 3.96875, 8.4375, 85.884765625, 2.125, -3.96875, 1.375, 9.84375, 4.46875]
[9.84375, 2.71875, 0.75, 3.34375, 7.03125, 79.890625, 0.75, -5.84375, 2.875, 8.125, 4.0]
2544.9132766384128
2528.402614792907
2502.350522767732
2535.6098254620283
2546.7478277014534
2544.9132766384128
2530.2371658559478
2530.2371658559478
2540.1647364377413
2516.84281309958
gaft.ConsoleOutput INFO Generation: 1, best fitness: 2546.748, scaled fitness: 2546.748
[8.28125, 5.0625, 0.75, 2.71875, 8.4375, 75.443359375, 2.625, -2.71875, 0.5, 7.5, 4.46875]
[4.53125, 10.53125, 0.625, 5.53125, 7.34375, 26.5234375, 3.0, -3.03125, 1.25, 9.0625, 5.875]
[8.4375, 9.75, 0.5, 1.3125, 6.25, 57.07421875, 0.125, -5.375, 2.75, 7.96875, 5.875]
[8.59375, 5.84375, 0.875, 5.0625, 7.5, 26.91015625, 2.25, -4.4375, 3.375, 7.96875, 8.84375]
[6.25, 4.28125, 0.25, 5.375, 8.4375, 43.92578125, 0.5, -2.875, 1.0, 5.0, 7.4375]
[8.4375, 9.75, 0.5, 1.3125, 6.25, 57.07421875, 0.125, -5.375, 2.75, 7.96875, 5.875]
[5.3125, 1.78125, 0.75, 2.71875, 9.0625, 30.390625, 0.25, -4.28125, 1.625, 5.78125, 6.1875]
[4.6875, 1.15625, 0.375, 3.03125, 8.125, 21.49609375, 0.75, -4.75, 2.125, 8.28125, 4.15625]
[5.78125, 10.375, 0.125, 5.53125, 9.375, 5.25390625, 0.0, -3.5, 2.875, 7.96875, 5.25]
[5.78125, 10.375, 0.125, 5.53125, 9.375, 5.25390625, 0.0, -3.5, 2.875, 7.96875, 5.25]
2546.7478277014534
2530.2371658559478
2544.9132766384128
2532.6826772228915
2535.6098254620283
2544.9132766384128
2540.707688290086
2546.7478277014534
2546.7478277014534
2546.7478277014534
gaft.ConsoleOutput INFO Generation: 2, best fitness: 2546.748, scaled fitness: 2546.748
[8.59375, 4.59375, 0.625, 3.34375, 8.4375, 12.794921875, 0.875, -5.53125, 3.25, 7.03125, 5.71875]
[2.96875, 4.4375, 0.375, 1.46875, 5.78125, 20.72265625, 2.875, -3.03125, 2.75, 8.4375, 6.96875]
[9.0625, 10.6875, 0.875, 5.6875, 5.15625, 28.650390625, 0.375, -3.1875, 0.125, 9.375, 5.09375]
[8.125, 9.75, 0.625, 1.625, 9.375, 1.38671875, 0.125, -3.5, 2.875, 7.96875, 5.25]
[0.15625, 4.59375, 0.375, 4.59375, 8.4375, 45.666015625, 0.375, -4.125, 1.125, 9.0625, 7.28125]
[3.4375, 3.5, 0.875, 2.09375, 9.375, 21.49609375, 0.375, -4.125, 2.875, 7.96875, 6.03125]
[5.9375, 4.28125, 0.875, 5.84375, 8.75, 6.4140625, 0.0, -4.4375, 1.625, 5.78125, 5.5625]
[5.15625, 7.875, 0.0, 2.40625, 9.6875, 29.23046875, 0.25, -3.34375, 2.875, 7.96875, 5.875]
[0.46875, 5.53125, 0.75, 2.09375, 8.90625, 91.87890625, 1.375, -2.5625, 1.5, 6.40625, 4.78125]
[8.28125, 10.375, 0.625, 5.0625, 6.25, 51.66015625, 0.125, -2.875, 2.75, 7.96875, 5.875]
2546.7478277014534
2517.2646125534816
2520.252725197512
2540.707688290086
2519.0991636165218
2540.707688290086
2546.7478277014534
2540.707688290086
2540.707688290086
2516.84281309958
gaft.ConsoleOutput INFO Generation: 3, best fitness: 2546.748, scaled fitness: 2546.748
[3.4375, 4.75, 0.625, 2.09375, 9.375, 2.93359375, 0.125, -1.625, 2.875, 7.96875, 5.25]
[2.8125, 6.15625, 0.5, 1.625, 6.71875, 29.810546875, 3.0, -5.84375, 3.75, 7.34375, 4.9375]
[3.75, 10.6875, 0.125, 1.3125, 9.53125, 58.427734375, 2.0, -3.8125, 0.625, 9.53125, 5.25]
[4.6875, 5.21875, 0.625, 3.96875, 8.28125, 79.697265625, 2.25, -4.59375, 1.125, 8.90625, 8.21875]
[8.59375, 6.3125, 0.25, 4.28125, 5.46875, 65.1953125, 0.0, -3.96875, 2.0, 9.53125, 5.09375]
[6.71875, 5.53125, 0.75, 1.9375, 5.15625, 92.072265625, 1.375, -2.5625, 0.0, 8.75, 4.78125]
[6.09375, 10.53125, 0.375, 5.0625, 6.875, 65.1953125, 3.875, -1.15625, 1.5, 9.84375, 6.65625]
[3.28125, 3.96875, 0.875, 4.75, 7.1875, 9.5078125, 1.75, -3.8125, 0.625, 8.125, 4.15625]
[5.9375, 8.5, 0.625, 4.59375, 9.375, 4.48046875, 0.125, -6.0, 2.875, 7.96875, 5.875]
[3.28125, 5.375, 0.375, 3.03125, 9.375, 22.26953125, 0.25, -1.625, 2.875, 7.96875, 5.40625]
2546.7478277014534
2513.4885250071575
2555.621778075836
2519.0991636165218
2500.3321512540733
2516.89843710509
2501.602478945066
2516.84281309958
2546.7478277014534
2546.7478277014534
gaft.ConsoleOutput INFO Generation: 4, best fitness: 2555.622, scaled fitness: 2555.622
[3.28125, 5.84375, 0.875, 2.40625, 6.875, 21.8828125, 1.25, -1.625, 0.625, 8.28125, 4.15625]
[4.375, 2.875, 0.875, 2.875, 7.03125, 91.87890625, 0.0, -3.1875, 1.0, 5.78125, 4.15625]
[3.28125, 6.625, 0.125, 1.3125, 8.4375, 82.404296875, 3.0, -1.78125, 3.125, 5.625, 8.21875]
[4.53125, 5.375, 0.875, 3.5, 5.46875, 59.974609375, 2.625, -2.09375, 2.875, 6.71875, 5.5625]
[7.03125, 4.90625, 0.75, 4.4375, 5.78125, 2.16015625, 1.875, -1.3125, 0.0, 8.125, 4.15625]
[5.625, 7.71875, 0.0, 5.0625, 5.625, 21.8828125, 2.5, -3.5, 2.25, 9.53125, 4.78125]
[9.53125, 3.03125, 0.875, 3.03125, 9.84375, 54.560546875, 3.875, -4.125, 0.875, 5.0, 8.6875]
[3.28125, 5.375, 0.375, 3.03125, 9.375, 22.26953125, 0.25, -1.625, 2.875, 7.96875, 5.40625]
[8.75, 2.5625, 0.625, 3.1875, 5.9375, 87.044921875, 3.5, -2.25, 2.5, 8.4375, 6.8125]
[0.0, 10.0625, 0.875, 1.15625, 5.46875, 56.494140625, 0.5, -1.46875, 2.25, 9.53125, 5.71875]

My requirement is that there are ten variables, four of which are enumerated types, looking for global optimal.
Who can give me example?

ImportError: cannot import name 'GAEngine'

我的操作系统是Windows10
我通过pip install gaft安装了。

但是在Spider下写个语句
from gaft import GAEngine
会出现如下错误
ImportError: cannot import name 'GAEngine'
后来我又装了Microsoft MPI,仍然出现以上错误,请问怎么解决。

并行化有一些疑问

首先感谢作者能提供这么优秀的开源库供大家学习!我在看readme的时候,没有看到如何进行并行化的内容,翻看了example的代码也没有找到,看了关于mip的相关内容后,我猜测是用
mpiexec -n 10 python code.py
这样来开启10个进程来运行,不知道是否如此?

Come in and have a look! 作为一名老用户,中肯地对此框架作出一些评价,走过路过的也希望来看一下。

因为不方便用平时用的号来评论,以免因为言语过激而引发人肉搜索。但当我看到这个做得不怎么专业的框架至今还没更新得比较完善,而且又看到那么多小号来点赞,我觉得我必须跟作者反映个基本事实了:这个框架若还不更新,只会误人子弟!

我猜想作者应该不是这个领域的专家。否则会清楚地知道一个基本事实:尽快更新框架。以正确引导大家。

我还是非常感谢作者利用自己的知识写下了一个还算能跑的基本遗传算法框架。但随着学习的深入,我发现这个框架并不如作者所说的直接往里面添加东西就好了。比如多目标优化、动态优化问题、约束处理等,这些并不能够简单地通过往里面添加代码来解决。

如果作者不想继续研发的话,我建议去用pymoo。功能强大,支持遗传算法、差分进化、遗传策略、约束优化、多目标优化等,而且是由专业团队开发的,所有代码都有论文支撑。如果其他人看到也可以考虑一下我的建议。

但我更希望作者能尽快更新,最好是和专业的人士一起合作。不然这个框架是在继续误人子弟。

语言有些偏激,谨此见谅!

How to run gaft using mpi ?

Hi,
first, thanks a lot for your library that is very easy to use.

My optimisation problem is working well but now I would like to speed up using parallel processing with mpi.
However, I am a newbie on that things and I don't know the programming rules in the python program itself and the command to run in parralle my script.

Should it be possible to have a full example of running example1 (for example) using mpi ?

Best regards

How to initial population with a specified solution

Hi.
I want to add an specific solution in first population.
for example in this population:

# Define population.
indv_template = BinaryIndividual(ranges=[(-2, 2), (-2, 2)], eps=0.001)
population = Population(indv_template=indv_template, size=50).init()

i want to add a solution like [-1, 1] in first population.
I tried this but i got error:

def init(self, chromsome=None, solution=None):

how can i do this??
thanks

变异或交叉后数值溢出问题

以整数为例:
假设chrom1 = '1000',ranges = [0,10],chrom1变异为1100则outrange,此处在库里面没看到相关处理;
同理,假设chrom2 = '0101',与chrom1在第2位交叉,也会出现outrange;
当然,outrange异常可以在fitness中进行惩罚,但这点不得不提一下。

how to solve a Polynomial

Hi.

I have a database with 200 sample with 4 feature for example "a, b, c, d".
i want to put it into a polynomial like "ax + by + cz + dw" that have a different answer for each data.
i want to use Genetic to find best Coefficients for this problem.
can i use this framework for my problem??
can you help me how i can generate my population and fitness function ???

Edit:
in #13 you create 3 variable.
the ranges that you set like (-10,10) is for first generate or it kept in all the algorithm????
what if i want to define an elite chromosome for beginning???

Edit 2:
i have this operators in my Genetic in matlab. what is the Equivalent of this options??

options=gaoptimset('InitialPopulation',population,'SelectionFcn',@selectiontournament,'CrossoverFcn',@crossoversinglepoint, 'CrossoverFraction',0.8,'MutationFcn',{@mutationgaussian,0.4},'MigrationFraction',0.5,'EliteCount',5,'StallGenLimit',50,'TolFun',0,'PlotFcns',{ @gaplotbestf,@gaplotscores},'populationsize',200,'Generations',200,'TimeLimit',400,'Display','iter')

sorry for my English.

best regard.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.