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

📄 frmmmas.frm

📁 蚂蚁算法实验室
💻 FRM
📖 第 1 页 / 共 4 页
字号:
    End If
  End If
End Sub

Private Sub chkStepByStep_Click()
  If chkStepByStep.Value = 1 Then
    SignShowStep_by_Step = True
    cmdNextStep.Enabled = False
  Else
    If chkStepByStep.Value = 0 Then
      SignShowStep_by_Step = False
      cmdNextStep.Enabled = True
    End If
  End If
End Sub

Private Sub chkUseNew1_Click()
  If chkUseNew1.Value = 1 Then
    SignUseNew = True
  Else
    If chkUseNew1.Value = 0 Then
      SignUseNew = False
    End If
  End If
End Sub

Private Sub cmdAbout_Click()
  frmAbout.Show
End Sub

Private Sub cmdCleanAll_Click()
  lstBestLenRep.Clear
  lstBestPath.Clear
  picAvgLen.Cls
  picBestLen.Cls
End Sub

Private Sub cmdCleanList_Click()
  lstBestLenRep.Clear
  lstBestPath.Clear
End Sub

Private Sub cmdCleanPicAvgLen_Click()
  picAvgLen.Cls
End Sub

Private Sub cmdClearPicBestLen_Click()
  picBestLen.Cls
End Sub

Private Sub cmdNextMove_Click()
  SignShowNextMove = True
  cmdNextMove.Enabled = False
End Sub

Private Sub cmdNextStep_Click()
  SignPause = False
  cmdNextStep.Enabled = False
  cmdShowAntMove.Enabled = False
  lstAnt_to_Show.Enabled = False
  cmdShowBestAntMove.Enabled = False
  cmdStopStep_by_Step.Enabled = True
End Sub

Private Sub cmdPause_Click()
  SignPause = True
  SignShowStep_by_Step = True
  chkStepByStep.Value = 1
  cmdStopStep_by_Step.Enabled = True
  cmdNextStep.Enabled = True
  cmdShowAntMove.Enabled = True
  cmdShowBestAntMove.Enabled = True
  cmdNextMove.Enabled = True
  lstAnt_to_Show.Enabled = True
End Sub

Private Sub cmdShowAntMove_Click()
  cmdNextMove.Enabled = True
  a = Val(lstAnt_to_Show.Text)
  If a = 0 Then
    MsgBox "必须选择一只蚂蚁!"
    Exit Sub
  End If
  Show_Ant_Move a
End Sub

Private Sub cmdShowBestAntMove_Click()
  Show_Ant_Move BestAntOfIter
End Sub

Private Sub cmdStart_Click()
  If lstCityData.ListIndex = -1 Then
    MsgBox "必须先选择一组城市数据!"
    Exit Sub
  End If
  Lock_Controls      '将计算过程中不能使用的控件锁定
  Draw_XOY           '绘制坐标轴
  r = Init_MMAS      '初始化蚁群,及TSP数据
  Init_Pic           '初始化图片框
  Draw_City_Init     '为绘制城市做准备
  Draw_City          '绘制城市
  Draw_Tao_Init      '为绘制各条路径上的信息素浓度做准备
  Show_Ant_Move_Init '为绘制蚂蚁每一条路径做准备
  Sign_Stop_Calc = False        '设置停止计算的标志为假
  If SignComputeAvg = False Then
    CalcTimes = 1     '如果没有选择计算多次去平均值,则将计算次数设置为1而不管设置计算次数的文本框中写的多少
  End If
  S_BestLen = 0
  GlobalBestLen = 0
  For time_calc_avg = 1 To CalcTimes     '循环次数为设置的求平均值的计算次数
    Init_for_Avg_Calc                    '每次计算过程需要重新初始化的数据由这个函数来完成
    If Sign_Stop_Calc = True Then        '判断用户是否按下了停止计算的按钮,如果按下了,则退出循环,由后面的代码继续处理
      Exit For
    End If
    lblTimes.Caption = time_calc_avg
    If SignAlwaysCleanResult = True Then   '如果选择了每一次计算都重新绘制或显示图形、数据,则清除个文本框和图片框的内容
      lstBestLenRep.Clear
      lstBestPath.Clear
      picAvgLen.Cls
      picBestLen.Cls
    End If
    Iteration = 1         '设置正在计算的代数为1
    Do
      lblIteration.Caption = Iteration
      r = Iteration_Init     '每次完成一代的计算后,用此函数重新初始化一些变量
      BestAntOfIter = 0
      BestLenOfIter = 0
      WorstAntOfIter = 0
      WorstLenOfIter = 0
''''''''''''''''''''''2002.7.14 deleted'''''''''''''''''''''''''
'      For i = 1 To MaxAnts
'        For j = 1 To MaxCities - 1
'          If j = 1 Then
'            Ant(i).Tour(j).fromCity = Ant(i).StartingCity
'            Ant(i).Visited(Ant(i).StartingCity) = True
'            Ant(i).CurrentCity = Ant(i).StartingCity
'          Else
'            Ant(i).Tour(j).fromCity = Ant(i).Tour(j - 1).toCity
'          End If
'          Ant(i).Tour(j).toCity = SelectCity(i, j)
'          Ant(i).CurrentCity = Ant(i).Tour(j).toCity
'          Ant(i).Visited(Ant(i).Tour(j).toCity) = True
'        Next j
'        Ant(i).Tour(MaxCities).fromCity = Ant(i).Tour(MaxCities - 1).toCity
'        Ant(i).Tour(MaxCities).toCity = Ant(i).StartingCity
'        Ant(i).LengthOfPath = CalcLen(i)
'        For k = 1 To MaxCities
'          Local_Update Int(Ant(i).Tour(k).fromCity), Int(Ant(i).Tour(k).toCity)
'        Next k
'      Next i
'''''''''''''''''''''2002.7.14 deleted''''''''''''''''''''''''''''
'''''''''''''''''''''2002.7.14 added'''''''''''''''''''''''''''''
      For j = 1 To MaxCities - 1    '对每个次蚂蚁的选择,执行下面的操作
        If j = 1 Then                                     '第一次选择时,
          For i = 1 To MaxAnts
            Ant(i).Tour(j).fromCity = Ant(i).StartingCity '设置每只蚂蚁的第一次选择的起始城市
            Ant(i).Visited(Ant(i).StartingCity) = True    '并设置蚂蚁已经访问这个城市的标志为“真”,
            Ant(i).CurrentCity = Ant(i).StartingCity      '设置每只蚂蚁的当前城市为这个起始程序
          Next i
        Else                                                 '如果不是蚂蚁的第一次选择
          For i = 1 To MaxAnts
            Ant(i).Tour(j).fromCity = Ant(i).Tour(j - 1).toCity  '就设置蚂蚁这次选择的起始城市为蚂蚁上次选择的目的城市
          Next i
        End If
        For i = 1 To MaxAnts
          Ant(i).Tour(j).toCity = SelectCity(i, j)     '为蚂蚁选择下一个城市
          Ant(i).CurrentCity = Ant(i).Tour(j).toCity   '设置当前城市
          Ant(i).Visited(Ant(i).Tour(j).toCity) = True '设置蚂蚁已访问过这个城市的标志为“真”
        Next i
        For i = 1 To MaxAnts
          Local_Update Int(Ant(i).Tour(j).fromCity), Int(Ant(i).Tour(j).toCity)  '对蚂蚁经过的每条路径上的信息素浓度做局部更新
        Next i
      Next j
      For i = 1 To MaxAnts
        Ant(i).Tour(MaxCities).fromCity = Ant(i).Tour(MaxCities - 1).toCity   '对蚂蚁的最后一次选择,规定必须回到起始城市
        Ant(i).Tour(MaxCities).toCity = Ant(i).StartingCity
        Ant(i).LengthOfPath = CalcLen(i)     '计算每只蚂蚁经过的路径长度
        Local_Update Int(Ant(i).Tour(MaxCities).fromCity), Int(Ant(i).Tour(j).toCity) ' 对蚂蚁经过的每条路径上的信息素浓度做局部更新
      Next i
''''''''''''''''''''2002.7.14 added'''''''''''''''''''''''''''''''''
      
      ''''''''''''评价出最好和最差的蚂蚁'''''''''''''''''''''''''''
      BestLenOfIter = Ant(1).LengthOfPath
      BestAntOfIter = 1
      WorstLenOfIter = Ant(1).LengthOfPath
      WorstAntOfIter = 1
      For i = 2 To MaxAnts
        If Ant(i).LengthOfPath <= BestLenOfIter Then
          BestAntOfIter = i
          BestLenOfIter = Ant(i).LengthOfPath
        Else
          If Ant(i).LengthOfPath > WorstLenOfIter Then
            WorstAntOfIter = i
            WorstLenOfIter = Ant(i).LengthOfPath
          End If
        End If
      Next i
      r = PhUpdate(BestAntOfIter)    '对最好的蚂蚁经过的路径上的信息素浓度做全局更新
      If SignUseNew = True Then      '如果使用新方法的标志为真
        'r = PhUpdate1(WorstAntOfIter)
        For i = 1 To MaxCities       '则,如果最差蚂蚁经过的这条路径不属于最好蚂蚁经过的路径,就对它上面的信息素浓度进行一种新的全局更新
          aa = Ant(WorstAntOfIter).Tour(j).fromCity
          bb = Ant(WorstAntOfIter).Tour(j).toCity
          If NTao(aa, bb) = False Then
            r = PhUpdate1(aa, bb, Ant(WorstAntOfIter).LengthOfPath, Ant(BestAntOfIter).LengthOfPath)
          End If
        Next i
      End If
''''''''''''''''''''''显示计算结果'''''''''''''''''''''''''''''''''
      strBestPath = Ant(BestAntOfIter).Tour(1).fromCity
      For i = 1 To MaxCities
        strBestPath = strBestPath + "->" + Str(Ant(BestAntOfIter).Tour(i).toCity)
      Next i
      lstBestPath.AddItem strBestPath
      If GlobalBestLen = 0 Then
        GlobalBestLen = BestLenOfIter
        txtGlobalBestIteration.Text = 1
        txtGlobalBestPath.Text = strBestPath
      Else
        If GlobalBestLen >= BestLenOfIter Then
          GlobalBestLen = BestLenOfIter
          txtGlobalBestIteration.Text = Iteration
          txtGlobalBestPath.Text = strBestPath
          If SignDrawPath = True Then
            Draw_Path BestAntOfIter
          End If
        End If
      End If
      S_LenOfIter = 0
      For i = 1 To MaxAnts
        S_LenOfIter = S_LenOfIter + Ant(i).LengthOfPath
      Next i
      AvgLenOfIter = S_LenOfIter / MaxAnts
      If SignDrawAvgLen = True Then
        Draw_Avg_Graph Iteration, AvgLenOfIter, False
      End If
      lstBestLenRep.AddItem BestLenOfIter
      txtGlobalBestLen.Text = GlobalBestLen
      If SignDrawBestLen = True Then
        Draw_Best_Graph Iteration, BestLenOfIter
      End If
      If SignDrawTao = True Then
        Draw_Tao
      End If
      If SignShowStep_by_Step = True Then
        cmdNextStep.Enabled = True
        cmdShowAntMove.Enabled = True
        lstAnt_to_Show.Enabled = True
        cmdShowBestAntMove.Enabled = True
        Do
          DoEvents
        Loop Until SignPause = False Or Sign_Stop_Calc = True
        SignPause = True
      End If
''''''''''''''''''''''Result''''''''''''''''''''''''''''''''
      Iteration = Iteration + 1
      Do
        DoEvents
      Loop Until SignPause = False
        
    Loop Until (Sign_Stop_Calc = True) Or (Iteration > MaxIter)
    S_BestLen = S_BestLen + GlobalBestLen
  
  Next time_calc_avg
  time_calc_avg = time_calc_avg - 1
  If Sign_Stop_Calc = False Then
    txtGlobalBestLen.Text = S_BestLen / CalcTimes
  Else
    If time_calc_avg = 1 Then
      txtGlobalBestLen.Text = GlobalBestLen
    Else
      S_BestLen = S_BestLen - GlobalBestLen
      txtGlobalBestLen.Text = S_BestLen / (time_calc_avg - 1)
    End If
  End If
  UnLock_Controls
  Sign_Stop_Calc = True
End Sub

Private Sub cmdStop_Click()
  Sign_Stop_Calc = True
  cmdNextStep.Enabled = False
  SignPause = False
End Sub

Private Sub cmdStopStep_by_Step_Click()
  SignPause = False
  SignShowStep_by_Step = False
  chkStepByStep.Value = 0
  cmdStopStep_by_Step.Enabled = False
  cmdNextStep.Enabled = False
  cmdShowAntMove.Enabled = False
  cmdShowBestAntMove.Enabled = False
  cmdNextMove.Enabled = False
  lstAnt_to_Show.Enabled = False
End Sub

Private Sub Form_Load()
  SignUseNew = False
  SignComputeAvg = False
  SignAlwaysCleanResult = True
  SignDrawBestLen = True
  SignDrawAvgLen = True
  SignDrawTogether = False
  SignDrawPath = True
  SignDrawTao = True
  SignShowStep_by_Step = False
  SignPause = False
  cmdNextStep.Enabled = False
  SignShowNextMove = False
  Sign_Stop_Calc = True
  lstAnt_to_Show.ListIndex = 0
End Sub

Private Sub Form_Unload(Cancel As Integer)
  If Sign_Stop_Calc = False Then
    MsgBox "必须先停止计算,然后才能关闭程序!"
    Cancel = -1
  End If
End Sub

Private Sub lstBestLenRep_Click()
  lstBestPath.ListIndex = lstBestLenRep.ListIndex
End Sub

Private Sub lstBestPath_Click()
  lstBestLenRep.ListIndex = lstBestPath.ListIndex
  txtBestPathAid.Text = lstBestPath.Text
End Sub

Private Sub txtMaxAnts_Change()
  lstAnt_to_Show.Clear
  For i = 1 To Val(txtMaxAnts.Text)
    lstAnt_to_Show.AddItem i
  Next i
End Sub

Private Sub txtMaxIter_Change()
  txtBestLenXMax.Text = txtMaxIter.Text
  txtAvgLenXMax.Text = txtMaxIter.Text
End Sub

⌨️ 快捷键说明

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