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

📄 frmmmas.frm

📁 蚂蚁算法的VB优化程序
💻 FRM
📖 第 1 页 / 共 4 页
字号:
  Dim S_LenOfIter As Double

Private Sub Lock_Controls()
  cmdStart.Enabled = False
  chkCAvg.Enabled = False
  chkUseNew1.Enabled = False
  chkDrawBestLen.Enabled = False
  chkDrawAvgLen.Enabled = False
  chkAlwaysCleanResult.Enabled = False
  chkDrawTogether.Enabled = False
  lstCityData.Enabled = False
  txtCalcTime.Enabled = False
  txtMaxAnts.Enabled = False
  txtAlpha.Enabled = False
  txtBeta.Enabled = False
  txtRou.Enabled = False
  txtSigma.Enabled = False
  txtTaoMax.Enabled = False
  txtTaoMin.Enabled = False
  txtW.Enabled = False
  cmdStop.Enabled = True
End Sub
Private Sub UnLock_Controls()
  cmdStart.Enabled = True
  chkCAvg.Enabled = True
  chkUseNew1.Enabled = True
  chkDrawBestLen.Enabled = True
  chkDrawAvgLen.Enabled = True
  chkAlwaysCleanResult.Enabled = True
  chkDrawTogether.Enabled = True
  lstCityData.Enabled = True
  txtCalcTime.Enabled = True
  txtMaxAnts.Enabled = True
  txtAlpha.Enabled = True
  txtBeta.Enabled = True
  txtRou.Enabled = True
  txtSigma.Enabled = True
  txtTaoMax.Enabled = True
  txtTaoMin.Enabled = True
  txtW.Enabled = True
  cmdStop.Enabled = False
End Sub
Private Sub chkAlwaysCleanResult_Click()
  If chkAlwaysCleanResult.Value = 1 Then
    SignAlwaysCleanResult = True
  Else
    If chkAlwaysCleanResult.Value = 0 Then
      SignAlwaysCleanResult = False
    End If
  End If
End Sub

Private Sub chkCAvg_Click()
  If chkCAvg.Value = 1 Then
    SignComputeAvg = True
  Else
    If chkCAvg.Value = 0 Then
      SignComputeAvg = False
    End If
  End If
End Sub

Private Sub chkDrawAvgLen_Click()
  If chkDrawAvgLen.Value = 1 Then
    SignDrawAvgLen = True
  Else
    If chkDrawAvgLen.Value = 0 Then
      SignDrawAvgLen = False
    End If
  End If
End Sub

Private Sub chkDrawBestLen_Click()
  If chkDrawBestLen.Value = 1 Then
    SignDrawBestLen = True
  Else
    If chkDrawBestLen.Value = 0 Then
      SignDrawBestLen = False
    End If
  End If
End Sub

Private Sub chkDrawPath_Click()
  If chkDrawPath.Value = 1 Then
    SignDrawPath = True
  Else
    If chkDrawPath.Value = 0 Then
      SignDrawPath = False
    End If
  End If
End Sub

Private Sub chkDrawTao_Click()
  If chkDrawTao.Value = 1 Then
    SignDrawTao = True
  Else
    If chkDrawTao.Value = 0 Then
      SignDrawTao = False
    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 chkInitRan_Click()
  If chkInitRan.Value = 1 Then
    SignInitRan = True
  Else
    If chkInitRan.Value = 0 Then
      SignInitRan = 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 = True
  cmdNextStep.Enabled = False
  cmdShowAntMove.Enabled = False
  lstAnt_to_Show.Enabled = False
  cmdShowBestAntMove.Enabled = False
  cmdStopStep_by_Step.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
  Init_Pic
  Draw_City_Init
  Draw_City
  Draw_Tao_Init
  Show_Ant_Move_Init
  Sign_Stop_Calc = False
  If SignComputeAvg = False Then
    CalcTimes = 1
  End If
  S_BestLen = 0
  GlobalBestLen = 0
  For Time_Calc_Avg = 1 To CalcTimes
  If Sign_Stop_Calc = True Then
    Exit For
  End If
  If SignAlwaysCleanResult = True Then
    lstBestLenRep.Clear
    lstBestPath.Clear
    picAvgLen.Cls
    picBestLen.Cls
  End If
  Iteration = 1
  Do
    lblIteration.Caption = Iteration
    r = Iteration_Init
    BestAntOfIter = 0
    BestLenOfIter = 0
    WorstAntOfIter = 0
    WorstLenOfIter = 0
    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
    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
''''''''''''''''''''''Result'''''''''''''''''''''''''''''''''
    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 = True Or Sign_Stop_Calc = True
      SignPause = False
    End If
''''''''''''''''''''''Result''''''''''''''''''''''''''''''''
    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
    Iteration = Iteration + 1
    DoEvents
  Loop Until (Sign_Stop_Calc = True) Or (Iteration > MaxIter)
  S_BestLen = S_BestLen + GlobalBestLen
  lblTimes.Caption = Time_Calc_Avg
  
  Next Time_Calc_Avg
  txtGlobalBestLen.Text = S_BestLen / CalcTimes
  UnLock_Controls
End Sub

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

Private Sub cmdStopStep_by_Step_Click()
  SignPause = True
  SignShowStep_by_Step = False
  chkStepByStep.Value = 0
  cmdStopStep_by_Step.Enabled = False
  cmdNextStep.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
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 + -