python3中的遗传算法库

pyind的Python项目详细描述


Pyind

python3中的遗传算法库

pyind只支持ndarray(numpy)

日本語

安装

pip install pyind

关于求值函数

求值函数的格式如下

defevaluation_function(individual):# individual is an array of genereturnfitness_of_this_individual

关于conf

conf的格式如下

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},}

可设置为conf["sel"]的值如表所示

括号中是默认值

表选择

"sel" (elitism)"num" (10)
elitism0–size of poplation
roulette0–size of poplation

可以设置为conf["xovr"]的值如表所示

括号中是默认值

表xovr

"xovr" (p2)"pb" (0.875)
p20–1
uniform0–1
ox0–1

可以设置为conf["mut"]的值如表所示

括号中是默认值

表mut

"mut" (flip_bit)"pb" (0.0075)"delta" (1)
flip_bit0–1
boundary0–10–∞
swap_idx0–1

未来版本

  1. 修复错误
  2. 增加选择、交叉和变异功能
  3. 跑得更快

许可证

麻省理工学院

示例代码

onemax问题

# Onemax ProblemimportnumpyasnpfrompyindimportpyindaspifrompyindimportdefaultsasdfIND_LEN=100POP_LEN=100defevl(ind):returnind.sum()if__name__=="__main__":pop=np.random.randint(2,size=(POP_LEN,IND_LEN))conf=df.CONFconf["eval"]["func"]=evlbest=pi.Pyind(pop,conf).run()print("best ind: ")print(best)

旅行商问题(tsp)

# Traveling salesman problemimportnumpyasnpimportmatplotlib.pyplotaspltfrompyindimportpyindaspifrompyindimportcrossoverasxovrfrompyindimportmutationasmutfrompyindimportdefaultsasdfCITIES_LEN=30POP_LEN=300END_GEN=500cities=np.random.rand(CITIES_LEN*2).reshape((-1,2))defevl(ind):total=0foriinrange(1,len(ind)):total+=np.linalg.norm(cities[ind[i]]-cities[ind[i-1]])return-totaldefsolve(pop):conf=df.CONFconf["eval"]["func"]=evlconf["xovr"]["func"]=xovr.oxconf["mut"]["func"]=mut.swap_idxconf["mut"]["pb"]=0.10returnpi.Pyind(pop,conf).run(END_GEN)if__name__=="__main__":t=cities.T# Create poppop=np.tile(np.arange(CITIES_LEN),(POP_LEN,1))foreinpop:np.random.shuffle(e)# Plot gen 0idx=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_GENidx=bestplt.plot(t[0,idx],t[1,idx],label="gen "+str(END_GEN),marker="o")plt.legend()plt.show()

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java Intellij和Eclipse无法找到库工件   java Mapbox Android Symbolayer重置筛选器   java如何在顶部显示特定的recyclerview项?   java如何在Hibernate中使用@Qualifier   我想计算特定文本webdriver java在多个页面上可用的HTML表中的数据   java捕获Spring MVC REST服务抛出的Jersey REST客户端的异常   java Hibernate flush()影响事务   密钥绑定Java密钥绑定   sonarqube java,sonar,圈复杂度   使用3DES在Java中加密,在SQL Oracle中解密   regex正则表达式在regex101上工作。com,但不是prod   JAVAsql。SQLException:ORA00600:内部错误代码,参数:[12811],[93233]   java H2数据库存储在哪里?   java如何避免在使用Jackson时从JSON字符串反序列化空JSON数组元素