BP神经网络的拓扑优化算法

'模块

Dim s1 As String

Dim i, j, t, k, g, h, z As Long '神经网络变量

Dim a(), y1(), w(), v(), o(), r(), s(), b(), l(), c(), eee(), max(), sgnrnd() As Double

Dim rf, bt As Double '学习率1,学习率2

Dim ss(), aa(), bb(), ll(), cc() As Double '回想时使用的变量

'-----------------------------------------------------------------------------

Dim fitness() As Double

Dim newfitness() As Double

Dim sumfitness As Double

Dim xdfitness() As Double

Dim pop() As Double

Dim newpop() As Double

Dim currrentpop() As Double '遗传算法变量

Dim ljgl() As Double

Dim copyindex() As Long

Dim jhgl As Double '交换率

Dim bygl As Double '变异概率

Dim changeindex() As Long

Dim varyindex() As Long

Dim changepoint As Long

Dim changenumber As Long

Dim varynumber As Long

Dim max1 As Double

Dim max2 As Double

Dim min1 As Double

Dim min2 As Double

Dim eeemin As Double

-----------------------------------------

'窗体代码

Private Sub Command1_Click()

rf = 0.6

bt = 0.6

jhgl = 0.4

bygl = 0.01

g = Val(Text2.Text) '设定学习深度

m = MSFlexGrid1.Rows - 1

n = MSFlexGrid1.Cols - 1

p = 2 * n - 1

q = 1

'变量赋初值

changenumber = (jhgl * g \ 2) * 2

varynumber = g * (n * p + p * q + p + q) * bygl

eeemin = 20

max1 = 25

min1 = 1

max2 = 0.998

min2 = 0.001

'-------------------------------------------------------------------------------

ReDim w(n, p), v(p, q), y1(m, q)

ReDim o(p), r(q), a(m, n), ss(p)

ReDim s(m, p), b(m, p), l(m, q)

ReDim c(m, q), d(m, q), cc(q)

ReDim e(m, p), bb(p), aa(n), ll(q)

ReDim eee(g)

ReDim fitness(g) '重新声明数组维数

ReDim newfitness(g)

ReDim xdfitness(g)

ReDim ljgl(g)

ReDim pop(g, n * p + p * q + p + q)

ReDim newpop(g, n * p + p * q + p + q)

ReDim copyindex(g)

ReDim changeindex(changenumber)

ReDim varyindex(varynumber)

'--------------------------------------------------------------------------------

For i = 1 To g

Randomize

For j = 1 To n * p + p * q

pop(i, j) = (max1 - min1) * Rnd() + min1

Next j

Next i

'染色体随机赋值

For i = 1 To g

Randomize

For j = n * p + p * q + 1 To n * p + p * q + p + q

pop(i, j) = (max2 - min2) * Rnd() + min2

Next j

Next i

'---------------------------------------------------------------------------------

For k = 1 To m

For i = 1 To n

a(k, i) = MSFlexGrid1.TextMatrix(k, i)

Next i

Next k

'从网格中读取训练样本

For t = 1 To m

For k = 1 To q

y1(t, k) = MSFlexGrid1.TextMatrix(t, k)

Next k

Next t

'---------------------------------------------------------------------------------

ReDim max(n) As Double

For j = 1 To n

max(j) = a(1, j)

Next j

For i = 1 To n

For j = 1 To m

If a(j, i) >= max(i) Then

max(i) = a(j, i)

End If

Next j

Next i

For i = 1 To n

For j = 1 To m

a(j, i) = a(j, i) / max(i)

' MSFlexGrid2.TextMatr

ix(j, i) = a(j, i)

Next j

Next i

Erase max()

ReDim max(q) As Double

For j = 1 To q

max(j) = y1(1, j) '数据归一化处理

Next j

For i = 1 To q

For j = 1 To m

If y1(j, i) >= max(i) Then

max(i) = y1(j, i)

End If

Next j

Next i

For i = 1 To q

For j = 1 To m

y1(j, i) = y1(j, i) / max(i)

' MSFlexGrid2.TextMatrix(j, q) = y1(j, i)

Next j

Next i

'-----------------------------------------------------------------------------

Dim cyclse As Long

cycles = 60

For li = 1 To cycles

sumfitness = 0

maxfitness = 0

'-----------------------------------------------------------------------------

For h = 1 To g

eee(h) = 0

For z = 1 To n * p + p * q + p + q

If z

For i = 1 To n

For j = 1 To p

w(i, j) = pop(h, (i - 1) * p + j)

Next j

Next i

End If

If z > n * p And z

For i = 1 To p

For j = 1 To q

v(i, j) = pop(h, (i - 1) * q + j + n * p)

Next j

Next i '将染色体的值逐个读入对应权和阈数组

End If

If z > n * p + p * q And z

For i = 1 To p

o(i) = pop(h, i + n * p + p * q)

Next i

End If

If z >= n * p + p * q + p And z

For i = 1 To q

r(i) = pop(h, i + n * p + p * q + p)

Next i

End If

Next z

'-----------------------------------------------------------------------------

For k = 1 To m

For j = 1 To p

s(k, j) = 0

For i = 1 To n

s(k, j) = s(k, j) + w(i, j) * a(k, i)

Next i

s(k, j) = s(k, j) - o(j)

b(k, j) = 1 / (1 + Exp(-s(k, j)))

Next j

For t = 1 To q

l(k, t) = 0

For j = 1 To p

l(k, t) = l(k, t) + v(j, t) * b(k, j)

Next j

l(k, t) = l(k, t) - r(t)

c(k, t) = 1 / (1 + Exp(-l(k, t)))

Next t

Next k '用BP求误差进而求个体适应度

For k = 1 To m

For t = 1 To q

eee(h) = eee(h) + (y1(k, t) - c(k, t)) * (y1(k, t) - c(k, t)) / 2

Next t

Next k

fitness(h) = 1 / eee(h) '适应度

sumfitness = sumfitness + fitness(h) '适应度总和

If fitness(h) > maxfitness Then maxfitness = fitness(h) '每代中种群内适应度最大个体

DoEvents

Next h

'-----------------------------------------------------------------------------

For h = 1 To g

xdfitness(h) = fitness(h) / sumfitness '相对适应度

Next h

ljgl(1) = xdfitness(1)

For h = 1 To g - 1

ljgl(h + 1) = ljgl(h) + xdfitness(h + 1) '累积适应度

'Debug.Print xdfitness(h); ljgl(h)

Next h

maxiabiao = 1

For h = 1 To g

suiji = Rnd()

For i = 1 To g

If ljgl(i) > suiji Then

copyindex(h) = i

If i > maxiabiao Then maxiabiao = i

Exit For

End If

Next i

Next h

For i = 1 To g

For j = 1 To n * p + p * q + p + q

newpop(i, j) = pop(copyindex(i), j)

Next j

Next i

For i = 1 To g

For j = 1 To n * p + p * q + p + q

pop(i, j) = newpop(i, j)

Next j

Next i

For i = 1 To g

ljgl(i) = 0

Next i

'复制算法完成,接下来是交换和变异

sumfitness = 0

For h = 1 To g

newfitness(h) = fitness(copyindex(h))

sumfitness = sumfitness + newfitness(h)

Next h

For h = 1 To g - 1

xdfitness(h) = newfitness(h) / sumfitness

Next h

'ljgl(i) = xdfitness(i)

For h = 1 To g - 1

ljgl(h + 1) = ljgl(h) + xdfitness(h + 1) '复制后的累积适应度

' Debug.Print xdfitness(h); ljgl(h)

Next h

'接下来在新种群中随机选择将要交换的染色体

For h = 1 To changenumber

suiji = Rnd()

For i = 1 To g

If ljgl(i) > suiji Then

changeindex(h) = i '要交换的染色体下标存入changeindex()

Exit For

End If

Next i

Next h

'下面接下来是交换操作

For i = 1 To changenumber - 1 Step 2

Randomize

suiji = Rnd()

'Debug.Print suiji

suiji1 = Rnd()

jhpoint = Int((n * p + p * q + p + q - 1) * suiji) + 1

newpop(changeindex(i), jhpoint) = newpop(changeindex(i), jhpoint) - suiji1 * (newpop(changeindex(i), jhpoint) - newpop(changeindex(i + 1), jhpoint))

newpop(changeindex(i + 1), jhpoint) = newpop(changeindex(i + 1), jhpoint) + suiji1 * (newpop(changeindex(i), jhpoint) - newpop(changeindex(i + 1), jhpoint))

For k = jhpoint + 1 To n * p + p * q + p + q

temp = newpop(changeindex(i + 1), k)

newpop(changeindex(i + 1), k) = newpop(changeindex(i), k)

newpop(changeindex(i), k) = temp

Next k

Next i

DoEvents

'交换完成,下面是突变

For i = 1 To varynumber

temp = Int(Rnd * g * ((n * p + p * q + p + q - 1)) + 1)

varyindex(i) = temp

If i > 1 Then

For j = 1 To i - 1

If temp = varyindex(j) Then '突变点的选择避免重复

i = i - 1

End If

Next j

End If

Next i

For i = 1 To varynumber

If varyindex(i) Mod (n * p + p * q + p + q) 0 Then

hengbiao = Int(varyindex(i) \ (n * p + p * q + p + q)) + 1

shubiao = varyindex(i) Mod (n * p + p * q + p + q)

Else

hengbiao = Int(varyindex(i) \ (n * p + p * q + p + q))

shubiao = n * p + p * q + p + q

End If

If shubiao

newpop(hengbiao, shubiao) = newpop(hengbiao, shubiao) + Sgn(Rnd()) * (max1 - min1) * (1 - Exp(((1 - li / cycles) ^ 2) * Log(Rnd()))) '对权进行突变

Debug.Print Sgn(Rnd()) * (max1 - min1) * (1 - Exp(((1 - li / cycles) ^ 2) * Log(Rnd())))

Else

newpop(hengbiao, shubiao) = newpop(hengbiao, shubiao) + Sgn(Rnd()) * (max2 - min2) * (1 - Exp(((1 - li / cycles) ^ 2) * Log(Rnd()))) '对阈进行突变

End If

Next i

For i = 1 To g

For j = 1 To n * p + p * q + p + q

pop(i, j) = newpop(i, j)

Next j

Next i

Text1.Text = maxfitness

' MSFlexGrid2.TextMatrix(j, q) = newpop(i, j)

Next li

End Sub

'模块

Dim s1 As String

Dim i, j, t, k, g, h, z As Long '神经网络变量

Dim a(), y1(), w(), v(), o(), r(), s(), b(), l(), c(), eee(), max(), sgnrnd() As Double

Dim rf, bt As Double '学习率1,学习率2

Dim ss(), aa(), bb(), ll(), cc() As Double '回想时使用的变量

'-----------------------------------------------------------------------------

Dim fitness() As Double

Dim newfitness() As Double

Dim sumfitness As Double

Dim xdfitness() As Double

Dim pop() As Double

Dim newpop() As Double

Dim currrentpop() As Double '遗传算法变量

Dim ljgl() As Double

Dim copyindex() As Long

Dim jhgl As Double '交换率

Dim bygl As Double '变异概率

Dim changeindex() As Long

Dim varyindex() As Long

Dim changepoint As Long

Dim changenumber As Long

Dim varynumber As Long

Dim max1 As Double

Dim max2 As Double

Dim min1 As Double

Dim min2 As Double

Dim eeemin As Double

-----------------------------------------

'窗体代码

Private Sub Command1_Click()

rf = 0.6

bt = 0.6

jhgl = 0.4

bygl = 0.01

g = Val(Text2.Text) '设定学习深度

m = MSFlexGrid1.Rows - 1

n = MSFlexGrid1.Cols - 1

p = 2 * n - 1

q = 1

'变量赋初值

changenumber = (jhgl * g \ 2) * 2

varynumber = g * (n * p + p * q + p + q) * bygl

eeemin = 20

max1 = 25

min1 = 1

max2 = 0.998

min2 = 0.001

'-------------------------------------------------------------------------------

ReDim w(n, p), v(p, q), y1(m, q)

ReDim o(p), r(q), a(m, n), ss(p)

ReDim s(m, p), b(m, p), l(m, q)

ReDim c(m, q), d(m, q), cc(q)

ReDim e(m, p), bb(p), aa(n), ll(q)

ReDim eee(g)

ReDim fitness(g) '重新声明数组维数

ReDim newfitness(g)

ReDim xdfitness(g)

ReDim ljgl(g)

ReDim pop(g, n * p + p * q + p + q)

ReDim newpop(g, n * p + p * q + p + q)

ReDim copyindex(g)

ReDim changeindex(changenumber)

ReDim varyindex(varynumber)

'--------------------------------------------------------------------------------

For i = 1 To g

Randomize

For j = 1 To n * p + p * q

pop(i, j) = (max1 - min1) * Rnd() + min1

Next j

Next i

'染色体随机赋值

For i = 1 To g

Randomize

For j = n * p + p * q + 1 To n * p + p * q + p + q

pop(i, j) = (max2 - min2) * Rnd() + min2

Next j

Next i

'---------------------------------------------------------------------------------

For k = 1 To m

For i = 1 To n

a(k, i) = MSFlexGrid1.TextMatrix(k, i)

Next i

Next k

'从网格中读取训练样本

For t = 1 To m

For k = 1 To q

y1(t, k) = MSFlexGrid1.TextMatrix(t, k)

Next k

Next t

'---------------------------------------------------------------------------------

ReDim max(n) As Double

For j = 1 To n

max(j) = a(1, j)

Next j

For i = 1 To n

For j = 1 To m

If a(j, i) >= max(i) Then

max(i) = a(j, i)

End If

Next j

Next i

For i = 1 To n

For j = 1 To m

a(j, i) = a(j, i) / max(i)

' MSFlexGrid2.TextMatr

ix(j, i) = a(j, i)

Next j

Next i

Erase max()

ReDim max(q) As Double

For j = 1 To q

max(j) = y1(1, j) '数据归一化处理

Next j

For i = 1 To q

For j = 1 To m

If y1(j, i) >= max(i) Then

max(i) = y1(j, i)

End If

Next j

Next i

For i = 1 To q

For j = 1 To m

y1(j, i) = y1(j, i) / max(i)

' MSFlexGrid2.TextMatrix(j, q) = y1(j, i)

Next j

Next i

'-----------------------------------------------------------------------------

Dim cyclse As Long

cycles = 60

For li = 1 To cycles

sumfitness = 0

maxfitness = 0

'-----------------------------------------------------------------------------

For h = 1 To g

eee(h) = 0

For z = 1 To n * p + p * q + p + q

If z

For i = 1 To n

For j = 1 To p

w(i, j) = pop(h, (i - 1) * p + j)

Next j

Next i

End If

If z > n * p And z

For i = 1 To p

For j = 1 To q

v(i, j) = pop(h, (i - 1) * q + j + n * p)

Next j

Next i '将染色体的值逐个读入对应权和阈数组

End If

If z > n * p + p * q And z

For i = 1 To p

o(i) = pop(h, i + n * p + p * q)

Next i

End If

If z >= n * p + p * q + p And z

For i = 1 To q

r(i) = pop(h, i + n * p + p * q + p)

Next i

End If

Next z

'-----------------------------------------------------------------------------

For k = 1 To m

For j = 1 To p

s(k, j) = 0

For i = 1 To n

s(k, j) = s(k, j) + w(i, j) * a(k, i)

Next i

s(k, j) = s(k, j) - o(j)

b(k, j) = 1 / (1 + Exp(-s(k, j)))

Next j

For t = 1 To q

l(k, t) = 0

For j = 1 To p

l(k, t) = l(k, t) + v(j, t) * b(k, j)

Next j

l(k, t) = l(k, t) - r(t)

c(k, t) = 1 / (1 + Exp(-l(k, t)))

Next t

Next k '用BP求误差进而求个体适应度

For k = 1 To m

For t = 1 To q

eee(h) = eee(h) + (y1(k, t) - c(k, t)) * (y1(k, t) - c(k, t)) / 2

Next t

Next k

fitness(h) = 1 / eee(h) '适应度

sumfitness = sumfitness + fitness(h) '适应度总和

If fitness(h) > maxfitness Then maxfitness = fitness(h) '每代中种群内适应度最大个体

DoEvents

Next h

'-----------------------------------------------------------------------------

For h = 1 To g

xdfitness(h) = fitness(h) / sumfitness '相对适应度

Next h

ljgl(1) = xdfitness(1)

For h = 1 To g - 1

ljgl(h + 1) = ljgl(h) + xdfitness(h + 1) '累积适应度

'Debug.Print xdfitness(h); ljgl(h)

Next h

maxiabiao = 1

For h = 1 To g

suiji = Rnd()

For i = 1 To g

If ljgl(i) > suiji Then

copyindex(h) = i

If i > maxiabiao Then maxiabiao = i

Exit For

End If

Next i

Next h

For i = 1 To g

For j = 1 To n * p + p * q + p + q

newpop(i, j) = pop(copyindex(i), j)

Next j

Next i

For i = 1 To g

For j = 1 To n * p + p * q + p + q

pop(i, j) = newpop(i, j)

Next j

Next i

For i = 1 To g

ljgl(i) = 0

Next i

'复制算法完成,接下来是交换和变异

sumfitness = 0

For h = 1 To g

newfitness(h) = fitness(copyindex(h))

sumfitness = sumfitness + newfitness(h)

Next h

For h = 1 To g - 1

xdfitness(h) = newfitness(h) / sumfitness

Next h

'ljgl(i) = xdfitness(i)

For h = 1 To g - 1

ljgl(h + 1) = ljgl(h) + xdfitness(h + 1) '复制后的累积适应度

' Debug.Print xdfitness(h); ljgl(h)

Next h

'接下来在新种群中随机选择将要交换的染色体

For h = 1 To changenumber

suiji = Rnd()

For i = 1 To g

If ljgl(i) > suiji Then

changeindex(h) = i '要交换的染色体下标存入changeindex()

Exit For

End If

Next i

Next h

'下面接下来是交换操作

For i = 1 To changenumber - 1 Step 2

Randomize

suiji = Rnd()

'Debug.Print suiji

suiji1 = Rnd()

jhpoint = Int((n * p + p * q + p + q - 1) * suiji) + 1

newpop(changeindex(i), jhpoint) = newpop(changeindex(i), jhpoint) - suiji1 * (newpop(changeindex(i), jhpoint) - newpop(changeindex(i + 1), jhpoint))

newpop(changeindex(i + 1), jhpoint) = newpop(changeindex(i + 1), jhpoint) + suiji1 * (newpop(changeindex(i), jhpoint) - newpop(changeindex(i + 1), jhpoint))

For k = jhpoint + 1 To n * p + p * q + p + q

temp = newpop(changeindex(i + 1), k)

newpop(changeindex(i + 1), k) = newpop(changeindex(i), k)

newpop(changeindex(i), k) = temp

Next k

Next i

DoEvents

'交换完成,下面是突变

For i = 1 To varynumber

temp = Int(Rnd * g * ((n * p + p * q + p + q - 1)) + 1)

varyindex(i) = temp

If i > 1 Then

For j = 1 To i - 1

If temp = varyindex(j) Then '突变点的选择避免重复

i = i - 1

End If

Next j

End If

Next i

For i = 1 To varynumber

If varyindex(i) Mod (n * p + p * q + p + q) 0 Then

hengbiao = Int(varyindex(i) \ (n * p + p * q + p + q)) + 1

shubiao = varyindex(i) Mod (n * p + p * q + p + q)

Else

hengbiao = Int(varyindex(i) \ (n * p + p * q + p + q))

shubiao = n * p + p * q + p + q

End If

If shubiao

newpop(hengbiao, shubiao) = newpop(hengbiao, shubiao) + Sgn(Rnd()) * (max1 - min1) * (1 - Exp(((1 - li / cycles) ^ 2) * Log(Rnd()))) '对权进行突变

Debug.Print Sgn(Rnd()) * (max1 - min1) * (1 - Exp(((1 - li / cycles) ^ 2) * Log(Rnd())))

Else

newpop(hengbiao, shubiao) = newpop(hengbiao, shubiao) + Sgn(Rnd()) * (max2 - min2) * (1 - Exp(((1 - li / cycles) ^ 2) * Log(Rnd()))) '对阈进行突变

End If

Next i

For i = 1 To g

For j = 1 To n * p + p * q + p + q

pop(i, j) = newpop(i, j)

Next j

Next i

Text1.Text = maxfitness

' MSFlexGrid2.TextMatrix(j, q) = newpop(i, j)

Next li

End Sub


相关文章

  • 结构拓扑优化综述
  • 专题论坛ForumonSpecialTopic 结构拓扑优化综述 谢涛, 刘静, 刘军考 (哈尔滨工业大学机电工程学院,哈尔滨150001) 摘要:回顾了结构拓扑优化的发展过程,总结了离散变量结构和连续体结构拓扑优化的一些常用方法,并对结构 ...查看


  • 结构拓扑优化综述_谢涛
  • 专题论坛ForumonSpecialTopic 结构拓扑优化综述 谢涛, 刘静, 刘军考 (哈尔滨工业大学机电工程学院,哈尔滨150001) 摘要:回顾了结构拓扑优化的发展过程,总结了离散变量结构和连续体结构拓扑优化的一些常用方法,并对结构 ...查看


  • 结构拓扑优化设计的发展_现状及展望
  • 第24卷第1期2004年2月 辽宁工学院学报 JOURNAL OF LIAONING INST IT UTE OF TECHNOLOGY V ol. 24 N o. 1F eb. 2004 结构拓扑优化设计的发展.现状及展望 赵丽红1, 郭 ...查看


  • 车用自组织网络分层优化策略研究
  • 2008年7月系统工程理论与实践第7期 文章编号:100026788(2008) 0720119206 车用自组织网络分层优化策略研究 刘鸿飞1,2, 黄席樾, 李丽君, 张仔兵132 (11重庆大学自动化学院, 重庆400044;21重庆 ...查看


  • 非线性规划的粒子群算法
  • XX 大学 智能优化算法课内实验报告书 院系名称 : 学生姓名 : 专业名称 : 班 级 : 学 时号 : 间 : 非线性规划问题的粒子群算法 1.1 背景介绍 1.1.1 非线性规划简介 具有非线性约束条件或目标函数的数学规划,是运筹学的 ...查看


  • 智能机器人路径规划及算法研究
  • 机器人技术 文章编号:1008-帖70(2咖6)11-2-0244-03 中文核心期刊'微计算机信息>(嵌入式与SOC)2006年第22卷第11-2期 智能机器人路径规划及算法研究 Research on PathP|anningan ...查看


  • 关于智能机器人的认识
  • 关于智能机器人路径规划的认识 樊阳阳 仪器仪表工程 学号2013704008 摘 要 智能机器人是人工智能的理想研究平台,是一个在感知.思维.效应方面全面模拟人的机器系统,它是人工智能技术的综合试验场,可以全面地考察人工智能各个领域的技术. ...查看


  • 试析机械结构优化设计的应用及趋势
  • [摘  要]机械结构优化主要是指借助计算机的前提下对机械产品性能.效率.寿命等提高,以此改变机械产品形状.尺寸.动态性能及拓扑结构优化的过程.是发展机械行业的必然要求,也是顺应信息时代发展的需求. [关键词]机械结构;优化设计;趋势 中图分 ...查看


  • 图论 第四讲 经典算法
  • 图论的经典算法 图的经典算法是必须掌握的,背也要背过.联赛中考图论也是在经典算法的基础上,考的是灵活应用.所以经典算法是必要的工具.图的经典算法有求单源最短路径的,,求负权回路的,多源最短路径的,,最小生成树的prim 和kruskal , ...查看


  • 主动配电网文献综述
  • 主动配电网文献综述 摘要:分布式电源( distributed generation, DG)和电动汽车的大量接入.智能家居的普及.需求侧响应的全面实施等显著增强了配电系统规划与运行的复杂性,同时,未来的配电网对规划与运行的优化策略提出了更 ...查看


热门内容