Skip to main content

A multi-objective optimization algorithm library implemented in Python, containing some basic operators and commonly used multi-objective optimization algorithms.

Project description

算法模板

重写一个算法模板需要继承算法基类AlgorithmBase以规范算法调用接口:

from darwin.ea.base.algorithm import AlgorithmBase

以MOEAD为例:

注意:

每一个算法都需要更具一个具体的配置项来运行,这个配置项提供了算法运行的必要参数,框架已经提供了默认的Settings对象,该对象默认包含如下信息:

class Settings:
    def __init__(self,
                 population_num=100, # 种群大小
                 objectives_num=3, # 目标空间维度
                 dec_num=30, # 决策空间维度
                 evaluation=500, # 进化代数
                 problem=DTLZ1, # 测试问题
                 encoding="real", # 种群编码类型
                 visualization=True): # 可视化开关

        self.population_num = population_num

        self.objectives_num = objectives_num

        self.dec_num = dec_num

        self.evaluation = evaluation

        self.problem = problem

        self.encoding = encoding

        self.visualization = visualization
class MOEAD(AlgorithmBase):
    def __init__(self,
                 settings=None,
                 n_neighbors=15,
                 crossover=None,
                 mutation=None,
                 prob_neighbor_mating=1,
                 **kwargs):
        # settings必须要定义,如果用户定义为空则获取框架默认配置
        if settings is None:
            settings = Settings()
        # 父类__init__接口,必须调用
        super().__init__(settings=settings,
                         name="MOEAD",
                         crossover=crossover,
                         mutation=mutation)

        self.prob_neighbor_mating = prob_neighbor_mating # 骚操作
        self.n_var = self.settings.dec_num

        self.n_obj = self.settings.objectives_num

        self.problem = settings.problem(self.n_var, self.n_obj)

        self.ref_dirs = UniformReferenceDirection(n_dim=self.n_obj, n_points=self.settings.population_num).do()

        self.pop_size = self.ref_dirs.shape[0] # 由于权重的生成不能满足需求的点,更具绑定的规则个体数量和参考向量的数量要一一对应
        self.settings.population_num = self.ref_dirs.shape[0] # 因为种群大小改变了,所以需要更新配置中的种群大小

        self.pop = Population(random_sampling.float_random_sampling(self.problem.bounds, self.pop_size, self.n_var)) # 初始话种群
        self.pop.refresh(self.problem) # 根据测试问题刷新种群的性状,说白了也就是目标值
        self.ideal_point = np.min(self.pop.F, axis=0) # 设置参考点
        self.n_neighbors = n_neighbors # 设置领域大小
        self.neighbors = np.argsort(cdist(self.ref_dirs, self.ref_dirs), axis=1, kind='quicksort')[:, :self.n_neighbors] # 获得每一个点对应的邻居,注意np.argsort自己百度
        self.decomposition = PBI() # 设置用到的分解方法
 
    # 种群迭代接口,每调用一次就相当于进化一次
    def next(self):
        # moead核心操作,自己理解,该操作目的是更新种群,不需要返回值
        for i in np.random.permutation(self.pop_size):
            N = self.neighbors[i, :]
            if np.random.random() < self.prob_neighbor_mating:
                parents = N[np.random.permutation(self.n_neighbors)][:2]
            else:
                parents = np.random.permutation(self.pop.population_num)[:2]

            off_x = self.crossover.do(self.problem.bounds, genes=self.pop.X[parents])
            off_x = self.mutation.do(self.problem.bounds, genes=off_x)
            off_x = off_x[np.random.randint(0, len(off_x)), None]
            off_pop = Population(off_x)
            off_pop.refresh(self.problem)

            off_f = off_pop.F
            self.ideal_point = np.min(np.vstack([self.ideal_point, off_f]), axis=0)

            FV = self.decomposition.do(self.pop.F[N, :], weights=self.ref_dirs[N, :], utopian_point=self.ideal_point)
            FV_off = self.decomposition.do(off_f, weights=self.ref_dirs[N, :], utopian_point=self.ideal_point)
            I = np.where(FV_off < FV)[0]
            self.pop[N[I]] = off_pop

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

darwin_moea-0.0.4.tar.gz (30.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

darwin_moea-0.0.4-py3-none-any.whl (50.1 kB view details)

Uploaded Python 3

File details

Details for the file darwin_moea-0.0.4.tar.gz.

File metadata

  • Download URL: darwin_moea-0.0.4.tar.gz
  • Upload date:
  • Size: 30.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.2

File hashes

Hashes for darwin_moea-0.0.4.tar.gz
Algorithm Hash digest
SHA256 f9aee77cd4b230dd33e2b702dc7fa4d89618279513f67a4b5fda53cab2d77c07
MD5 7e0756c99044f47170fdfb223919e1d6
BLAKE2b-256 448400483dc6de058fa171446f45bd7c98ac28190b00eec32bd74976d93fcf26

See more details on using hashes here.

File details

Details for the file darwin_moea-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: darwin_moea-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 50.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.2

File hashes

Hashes for darwin_moea-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 fdd935a14f3c58ccc63543fa266077ec98e2051b7e762fd39c86a516af4407f6
MD5 2894ad8e6972e725852ed4147d36edf4
BLAKE2b-256 6b974b3d8b17f61fa6901ed1e5b4760bf4aa740641e1c247d902b8a2d16d38a0

See more details on using hashes here.

Supported by

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