import numpy as np
from ypstruct import structure
def run(problem, params):
# Problem Information
costfunc = problem.costfunc
nvar = problem.nvar
varmin = problem.varmin
varmax = problem.varmax
# Parameters
maxit = params.maxit
npop = params.npop
beta = params.beta
pc = params.pc
nc = int(np.round(pc*npop/2)*2)
gamma = params.gamma
mu = params.mu
sigma = params.sigma
# Empty Individual Template
empty_individual = structure()
empty_individual.position = None
empty_individual.cost = None
# Best Solution Ever Found
bestsol = empty_individual.deepcopy()
bestsol.cost = np.inf
# Initialize Population
pop = empty_individual.repeat(npop)
for i in range(npop):
pop[i].position = np.random.uniform(varmin, varmax, nvar)
pop[i].cost = costfunc(pop[i].position)
if pop[i].cost < bestsol.cost:
bestsol = pop[i].deepcopy()
# Best Cost of Iterations
bestcost = np.empty(maxit)
# Main Loop
for it in range(maxit):
costs = np.array([x.cost for x in pop])
avg_cost = np.mean(costs)
if avg_cost != 0:
costs = costs/avg_cost
probs = np.exp(-beta*costs)
popc = []
for _ in range(nc//2):
# Select Parents
#q = np.random.permutation(npop)
#p1 = pop[q[0]]
#p2 = pop[q[1]]
# Perform Roulette Wheel Selection
p1 = pop[roulette_wheel_selection(probs)]
p2 = pop[roulette_wheel_selection(probs)]
# Perform Crossover
c1, c2 = crossover(p1, p2, gamma)
# Perform Mutation
c1 = mutate(c1, mu, sigma)
c2 = mutate(c2, mu, sigma)
# Apply Bounds
apply_bound(c1, varmin, varmax)
apply_bound(c2, varmin, varmax)
# Evaluate First Offspring
c1.cost = costfunc(c1.position)
if c1.cost < bestsol.cost:
bestsol = c1.deepcopy()
# Evaluate Second Offspring
c2.cost = costfunc(c2.position)
if c2.cost < bestsol.cost:
bestsol = c2.deepcopy()
# Add Offsprings to popc
popc.append(c1)
popc.append(c2)
# Merge, Sort and Select
pop += popc
pop = sorted(pop, key=lambda x: x.cost)
pop = pop[0:npop]
# Store Best Cost
bestcost[it] = bestsol.cost
# Show Iteration Information
print("Iteration {}: Best Cost = {}".format(it, bestcost[it]))
# Output
out = structure()
out.pop = pop
out.bestsol = bestsol
out.bestcost = bestcost
return out
def crossover(p1, p2, gamma=0.1):
c1 = p1.deepcopy()
c2 = p1.deepcopy()
alpha = np.random.uniform(-gamma, 1+gamma, *c1.position.shape)
c1.position = alpha*p1.position + (1-alpha)*p2.position
c2.position = alpha*p2.position + (1-alpha)*p1.position
return c1, c2
def mutate(x, mu, sigma):
y = x.deepcopy()
flag = np.random.rand(*x.position.shape) <= mu
ind = np.argwhere(flag)
y.position[ind] += sigma*np.random.randn(*ind.shape)
return y
def apply_bound(x, varmin, varmax):
x.position = np.maximum(x.position, varmin)
x.position = np.minimum(x.position, varmax)
def roulette_wheel_selection(p):
c = np.cumsum(p)
r = sum(p)*np.random.rand()
ind = np.argwhere(r <= c)
return ind[0][0]
没有合适的资源?快使用搜索试试~ 我知道了~
二进制遗传算法(Binary Genetic Algorithm)是一种启发式搜索算法,用于解决优化问题。与传统的遗传算法相比,二进制遗传算法适用于解决决策变量为二进制编码的优化问题。以下是二进制遗传算法的基本描述: 初始化种群:初始时,随机生成一定数量的二进制编码的个体(也称为染色体),这些个体组成了种群。 确定适应度函数:针对具体的优化问题,需要定义一个适应度函数来评价每个个体的优劣。适应度函数通常根据问题的特性来定义,例如在投资组合优化中可以是收益、风险和相关性等指标的组合。 选择操作:通过轮盘赌选择、竞赛选择等方式,根据个体的适应度值选择父代个体用于繁殖下一代。 交叉操作:选定一定的交叉概率,对选出的父代个体进行交叉操作,生成新的个体作为下一代种群的一部分。 变异操作:选定一定的变异概率,对新生成的个体进行变异操作,以增加种群的多样性。 评估适应度:对新生成的个体,计算其适应度值。
资源推荐
资源详情
资源评论
收起资源包目录
Python.rar (2个子文件)
Python
ga.py 4KB
app.py 758B
共 2 条
- 1
资源评论
嵌入式职场
- 粉丝: 4w+
- 资源: 44
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功