DEAP源码解析

框架

DEAP架构特点为

  • 组件-流程分离
  • 模块化组件
  • 函数式编程:函数组合替代继承

类创建

通过create来创建,通过元类来创建类

python 复制代码
def create(name, base, **kargs):
	if name in globals():
        warnings.warn("A class named '{0}' has already been created and it "
                      "will be overwritten. Consider deleting previous "
                      "creation of that class or rename it.".format(name),
                      RuntimeWarning)

    # Check if the base class has to be replaced
    if base in class_replacers:
        base = class_replacers[base]
    meta_create(name, base, kargs)

def meta_create(name, base, dct):
    class_ = MetaCreator(name, base, dct)
    globals()[name] = class_
    return class_

class MetaCreator(type):
    def __new__(cls, name, base, dct):
        return super(MetaCreator, cls).__new__(cls, name, (base,), dct)

    def __init__(cls, name, base, dct):
        # A DeprecationWarning is raised when the object inherits from the
        # class "object" which leave the option of passing arguments, but
        # raise a warning stating that it will eventually stop permitting
        # this option. Usually this happens when the base class does not
        # override the __init__ method from object.
        dict_inst = {}
        dict_cls = {}
        for obj_name, obj in dct.items():
            if isinstance(obj, type):
                dict_inst[obj_name] = obj
            else:
                dict_cls[obj_name] = obj

        def init_type(self, *args, **kargs):
            """Replace the __init__ function of the new type, in order to
            add attributes that were defined with **kargs to the instance.
            """
            for obj_name, obj in dict_inst.items():
                setattr(self, obj_name, obj())
            if base.__init__ is not object.__init__:
                base.__init__(self, *args, **kargs)

        cls.__init__ = init_type
        cls.reduce_args = (name, base, dct)
        super(MetaCreator, cls).__init__(name, (base,), dict_cls)

    def __reduce__(cls):
        return (meta_create, cls.reduce_args)
相关推荐
π同学17 天前
遗传算法学习二之求TSP问题
matlab·遗传算法·tsp问题
weixin_468466852 个月前
遗传算法求解TSP旅行商问题python代码实战
python·算法·算法优化·遗传算法·旅行商问题·智能优化·np问题
Mysticbinary3 个月前
遗传算法入门
遗传算法·背包问题
rit84324993 个月前
基于二维熵阈值分割与遗传算法结合的图像分割
遗传算法
qq_340474023 个月前
10.1.1 使用python完成第一个遗传算法
遗传算法·argpartition·random.choice
青春不败 177-3266-05205 个月前
MATLAB近红外光谱分析技术及实践技术应用
随机森林·机器学习·支持向量机·matlab·卷积神经网络·遗传算法·近红外光谱
禺垣6 个月前
遗传算法的原理与实现示例
遗传算法·优化算法
软件算法开发8 个月前
基于自适应交叉概率和变异概率遗传算法的地铁车辆使用寿命和璇修次数优化matlab仿真
matlab·遗传算法·自适应交叉概率·自适应变异概率·璇修次数·使用寿命
__lost8 个月前
MATLAB退火算法和遗传算法解决旅行商问题
开发语言·算法·matlab·遗传算法·退火算法