⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 run.frm

📁 Hopfield 网——擅长于联想记忆与解迷路 实现H网联想记忆的关键
💻 FRM
字号:
VERSION 5.00
Begin VB.Form run 
   Caption         =   "运行机制"
   ClientHeight    =   6570
   ClientLeft      =   60
   ClientTop       =   345
   ClientWidth     =   7815
   LinkTopic       =   "Form1"
   ScaleHeight     =   6570
   ScaleWidth      =   7815
   StartUpPosition =   3  '窗口缺省
   Begin VB.Frame Frame3 
      Caption         =   "显示随机权重"
      Height          =   2535
      Left            =   4440
      TabIndex        =   27
      Top             =   3480
      Width           =   3015
      Begin VB.Label Label10 
         Height          =   1815
         Left            =   120
         TabIndex        =   28
         Top             =   360
         Width           =   2655
      End
   End
   Begin VB.Frame Frame2 
      Caption         =   "显示随机域值"
      Height          =   1455
      Left            =   4440
      TabIndex        =   25
      Top             =   1800
      Width           =   2895
      Begin VB.Label Label9 
         Height          =   975
         Left            =   120
         TabIndex        =   26
         Top             =   360
         Width           =   2535
      End
   End
   Begin VB.CommandButton Command2 
      Caption         =   "退出"
      Height          =   495
      Left            =   6960
      TabIndex        =   6
      Top             =   6120
      Width           =   735
   End
   Begin VB.Frame Frame1 
      Caption         =   "Frame1"
      Height          =   1455
      Left            =   120
      TabIndex        =   0
      Top             =   120
      Width           =   7695
      Begin VB.CommandButton Command5 
         Caption         =   "设置神经元初始状态"
         Height          =   855
         Left            =   3120
         TabIndex        =   16
         Top             =   240
         Width           =   735
      End
      Begin VB.CommandButton Command4 
         Caption         =   "计算能量"
         Height          =   375
         Left            =   4680
         TabIndex        =   15
         Top             =   960
         Width           =   855
      End
      Begin VB.CommandButton Command1 
         Caption         =   "1"
         Height          =   375
         Index           =   5
         Left            =   6960
         TabIndex        =   11
         Top             =   360
         Visible         =   0   'False
         Width           =   615
      End
      Begin VB.CommandButton Command1 
         Caption         =   "1"
         Height          =   375
         Index           =   4
         Left            =   6360
         TabIndex        =   10
         Top             =   360
         Visible         =   0   'False
         Width           =   615
      End
      Begin VB.CommandButton Command1 
         Caption         =   "1"
         Height          =   375
         Index           =   3
         Left            =   5760
         TabIndex        =   9
         Top             =   360
         Visible         =   0   'False
         Width           =   615
      End
      Begin VB.CommandButton Command1 
         Caption         =   "1"
         Height          =   375
         Index           =   2
         Left            =   5160
         TabIndex        =   8
         Top             =   360
         Visible         =   0   'False
         Width           =   615
      End
      Begin VB.CommandButton Command1 
         Caption         =   "1"
         Height          =   375
         Index           =   1
         Left            =   4560
         TabIndex        =   7
         Top             =   360
         Visible         =   0   'False
         Width           =   615
      End
      Begin VB.CommandButton Command1 
         Caption         =   "1"
         Height          =   375
         Index           =   0
         Left            =   3960
         TabIndex        =   5
         Top             =   360
         Visible         =   0   'False
         Width           =   615
      End
      Begin VB.TextBox Text2 
         Height          =   375
         Left            =   2160
         TabIndex        =   4
         Text            =   "20"
         Top             =   240
         Width           =   735
      End
      Begin VB.TextBox Text1 
         Height          =   375
         Left            =   960
         TabIndex        =   2
         Text            =   "4"
         Top             =   240
         Width           =   615
      End
      Begin VB.Label Label4 
         Caption         =   "产生随机的域值;产生随机的初始权值"
         Height          =   375
         Left            =   120
         TabIndex        =   12
         Top             =   840
         Width           =   1455
      End
      Begin VB.Label Label2 
         Caption         =   "运行次数"
         Height          =   495
         Left            =   1680
         TabIndex        =   3
         Top             =   240
         Width           =   495
      End
      Begin VB.Label Label1 
         Caption         =   "神经元个数"
         Height          =   495
         Left            =   120
         TabIndex        =   1
         Top             =   240
         Width           =   615
      End
   End
   Begin VB.Label Label8 
      Height          =   375
      Left            =   4920
      TabIndex        =   24
      Top             =   5760
      Width           =   1095
   End
   Begin VB.Label Label7 
      Height          =   4095
      Left            =   2520
      TabIndex        =   23
      Top             =   2040
      Width           =   1695
   End
   Begin VB.Label Label3 
      Height          =   3975
      Index           =   5
      Left            =   1800
      TabIndex        =   22
      Top             =   2160
      Width           =   255
   End
   Begin VB.Label Label3 
      Height          =   3975
      Index           =   4
      Left            =   1440
      TabIndex        =   21
      Top             =   2160
      Width           =   255
   End
   Begin VB.Label Label3 
      Height          =   3975
      Index           =   3
      Left            =   1200
      TabIndex        =   20
      Top             =   2040
      Width           =   255
   End
   Begin VB.Label Label3 
      Height          =   3975
      Index           =   2
      Left            =   840
      TabIndex        =   19
      Top             =   2040
      Width           =   255
   End
   Begin VB.Label Label3 
      Height          =   3975
      Index           =   1
      Left            =   480
      TabIndex        =   18
      Top             =   2040
      Width           =   255
   End
   Begin VB.Label Label3 
      Height          =   3975
      Index           =   0
      Left            =   120
      TabIndex        =   17
      Top             =   2040
      Width           =   255
   End
   Begin VB.Label Label6 
      Caption         =   "能量"
      Height          =   375
      Left            =   2880
      TabIndex        =   14
      Top             =   1680
      Width           =   855
   End
   Begin VB.Label Label5 
      Caption         =   "状态"
      Height          =   255
      Left            =   240
      TabIndex        =   13
      Top             =   1680
      Width           =   975
   End
End
Attribute VB_Name = "run"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Public neuralnumber As Integer             '
Public runnumber As Integer             '模式数

Private Sub Command1_Click(Index As Integer)
If Command1(Index).Caption = 1 Then
    Command1(Index).Caption = -1
ElseIf Command1(Index).Caption = 0 Then
    Command1(Index).Caption = 1
End If

End Sub

Private Sub Command2_Click()
Unload Me

End Sub




Private Sub Command4_Click()              '计算能量显示状态及相应能量


Dim threshold(6) As Double              '
Dim weights(6, 6) As Double             '
Dim output(6) As Double
Dim energyweight As Double
Dim energythreshold As Double
Dim energytotal As Double
Dim i, j, k, h As Integer
k = 0
j = 0
i = 0

'产生随机的域值;
Randomize
For i = 0 To neuralnumber        '为nn个神经元设置随机域值threshold(i) = Rnd
threshold(i) = Rnd
Next i

'产生随机的初始权值
For i = 0 To neuralnumber - 2
  For j = i + 1 To neuralnumber - 1
  weights(i, j) = Rnd
  weights(j, i) = weights(i, j)
  Next j
Next i
For i = 0 To neuralnumber - 1
 weights(i, i) = 0
Next i

'将输入的初始状态存入output()中
For i = 0 To neuralnumber - 1
   If Command1(i).Caption = 1 Then
   output(i) = 1
   Else: output(i) = -1
   End If
Next i

'-------------------------------------计算能量---------------------------------


'考虑到神经元发火的阈值,H网的整体能量: E =  - 0.5∑i∑jwijxixj + ∑iθixi********
energyweight = 0
energythreshold = 0
For i = 0 To neuralnumber - 1
  For j = 0 To neuralnumber - 1
  energyweight = energyweight + weights(i, j) * output(i) * output(j)     '计算∑i∑jwijxixj
  Next j
  energythreshold = energythreshold + threshold(i) * output(i)                '计算∑iθixi
Next i
energytotal = -0.5 * energyweight + energythreshold

'Print Energy, b(1), b(2), b(3), b(4)----------------- 打印出网络能量-------------,
  For j = 0 To neuralnumber - 1
    Label8.Caption = output(j)
    Label3(j).Caption = Label8.Caption + Chr(10) & Chr(13)
  Next j
Label8.Caption = energytotal
Label7.Caption = Label8.Caption + Chr(10) & Chr(13)

'*****************************************************************************
'                                   运行网络
'H网工作运行规则:
'1)从网中随机选取一个神经元i
'2)求所选中的神经元i的所有输入加权和:
'     Hi = ∑jwijxj-θi ,j=1…n, j≠i
'3)计算i 的第 t+1 时刻的输出值:
'    IF   Hi(t)≥0, xi(t+1)= 1
'           Hi(t)<0,  xi(t+1)= -1
'4)i以外,所有神经元输出保持不变:
'    xj(t+1)=  xj(t),j=1,2,…n,j≠i
'5)返回到第1)步,直到进入稳定状态。
i = 0
runnumber = Text2.Text
For k = 0 To runnumber - 1

 i = Int(neuralnumber * Rnd)                 '1)随机产生i
 h = 0
 
 For j = 0 To neuralnumber - 1
 h = h + weights(i, j) * output(j)
 Next j
 h = h - threshold(i)                      '2)计算 Hi = ∑jwijxj-θi
 
 If h >= 0 Then                            '3)计算i 的第 t+1 时刻的输出值
 output(i) = 1
 Else
 output(i) = -1
 End If

'--------------------------------------计算能量-----------------------------
energythreshold = 0
energyweight = 0

'E =  - 0.5∑i∑jwijxixj + ∑iθixi

For i = 0 To neuralnumber - 1
  For j = 0 To neuralnumber - 1
  energyweight = energyweight + weights(i, j) * output(i) * output(j)
  Next j
  energythreshold = energythreshold + threshold(i) * output(i)
Next i
energytotal = -0.5 * energyweight + energythreshold

'Print Energy, b(1), b(2), b(3), b(4)   '打印出能量,输出过程
  For j = 0 To neuralnumber - 1
Label8.Caption = output(j)
Label3(j).Caption = Label3(j).Caption + Label8.Caption + Chr(10) & Chr(13)
  Next j

Label8.Caption = energytotal
Label7.Caption = Label7.Caption + Label8.Caption + Chr(10) & Chr(13)

Next k

'显示随机域值
j = 0
While j < neuralnumber                   '对每个i计算它到另一个元素j的权重
Label8.Caption = threshold(j)
    Label9.Caption = Label9.Caption + Label8.Caption + Chr(10) & Chr(13)
      j = j + 1
    Wend

'显示随机权重
i = 0
j = 0
While i < neuralnumber                   '对每个i计算它到另一个元素j的权重
    j = 0
    While j < neuralnumber
    Label8.Caption = weights(i, j)
    Label10.Caption = Label10.Caption + " :" + Label8.Caption
      j = j + 1
    Wend
      Label10.Caption = Label10.Caption + Chr(10) & Chr(13)                   '两元素间的权重

i = i + 1
Wend


End Sub

Private Sub Command5_Click()
Dim i As Integer
neuralnumber = Text1.Text
For i = 0 To neuralnumber - 1
   Command1(i).Visible = True
Next i

End Sub

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -