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

📄 tsp.java

📁 一个非常好的
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* Applet to solve the Traveling Salesman Problem
 * Copyright (C) 2002 Tobias Oetzel (Tobias.Oetzel@gmx.de)
 * This program is free software; you can redistribute it and/or modify it 
 * under the terms of the GNU General Public License as published by the 
 * Free Software Foundation; either version 2 of the License, or (at your option)
 * any later version. This program is distributed in the hope that it will be 
 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. 
 * You should have received a copy of the GNU General Public License along with this program;
 * if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 
 * Boston, MA 02111-1307, USA. 
*/



// Tsp Applet
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;

public class tsp extends JApplet 
{
 
	
  private JPanel                  ContentPanel, 
                                  OptionsPanel, 
                                  DisplayPanel,
                                  InformationPanel,
                                  DrawPanel;
  private JLabel                  AlgorithmenLabel,
                                  DelayLabel;
  private JComboBox               AlgorithmenComboBox,
                                  RandomCitiesComboBox;
  private JCheckBox               RandomCitiesCheckBox,
                                  BestSolutionsCheckBox;
  private JButton                 StartButton,
                                  AbortButton,
                                  ResetButton;  
  private simann                  MySimulatedAnnealing;
  private ant                     MyAntSystem;
  private simanngui               MySimulatedAnnealingGui;
  private antgui                  MyAntSystemGui;
  private String                  Algorithmen,
                                  RandomCitiesCount;

  protected cities                MyCities;
  protected draw                  MyDraw;      

  private boolean                 BestSolutions;
  public  int                     ActCitiesCount,
                                  SecondsRunning;
  private int                     Delay;          
  private JLabel                  StepCountLabel,
                                  TemperatureLabel,
                                  RuntimeLabel,
                                  OrgDistanceLabel,
                                  NowDistanceLabel,
                                  PerDistanceLabel,
                                  StepCountValue,
                                  TemperatureValue,
                                  RuntimeValue,
                                  OrgDistanceValue,
                                  NowDistanceValue,
                                  PerDistanceValue;
  protected JSlider               DelaySlider;
  protected Timer                 MyTimer;
  private tsp                     MyInstance;  

  // This is a hack to avoid an ugly error message in JDK 1.1.
  public tsp() 
  {
  	getRootPane().putClientProperty("defeatSystemEventQueueCheck",Boolean.TRUE);
  	MyInstance=this;
  }

  public void init() 
  {
    MyCities = new cities(MyInstance);
    setSize(800,500);
    setContentPane(makeContentPanel());
    makeOptionsContent();
    makeDisplayContent();
    makeInformationContent();
    MyTimer = new Timer(1000,new ActionListener () 
      {
        public void actionPerformed(ActionEvent e)
         {
           SecondsRunning+=1;
           String S;
           String Value;
           S=String.valueOf(SecondsRunning / 3600);
           if (S.length()==1)
           {
            S="0"+S;
           } 
           Value=S+":";
           S=String.valueOf((SecondsRunning / 60) % 60);
           if (S.length()==1)
           {
            S="0"+S;
           } 
           Value=Value+S+":";
           S=String.valueOf(SecondsRunning % 60);
           if (S.length()==1)
           {
            S="0"+S;
           } 
           Value=Value+S;
           SetRuntimeValue(Value);
         }
         
       });    
    MyTimer.stop();
    
    MyCities.GenerateTucSymbol();
    MyDraw.DrawAllCities();
    MyCities.GenerateRandomStartSolution(true);
    MyDraw.clicked = false;
  }

  private Container makeContentPanel()
  {
    ContentPanel = new JPanel();
    ContentPanel.setBackground(new Color(204,204,255));
    ContentPanel.setLayout(null);
    ContentPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
    ContentPanel.setBounds(0,0,800,500);

    Border myBorder = BorderFactory.createMatteBorder(1,1,1,1,Color.blue);
    Font   myFont   = new Font("System",0,12);
    OptionsPanel    = new JPanel();
    OptionsPanel.setBackground(new Color(204,204,255));
    OptionsPanel.setBorder(BorderFactory.createTitledBorder(myBorder,"Optionen",0,0,myFont,Color.blue));
    OptionsPanel.setBounds(10,10,250,480);
    OptionsPanel.setLayout(null);
    	
    DisplayPanel = new JPanel ();
    DisplayPanel.setBackground(new Color(204,204,255));
    DisplayPanel.setBorder(BorderFactory.createTitledBorder(myBorder,"Anzeige",0,0,myFont,Color.blue));
    DisplayPanel.setBounds(270,10,520,405);
    DisplayPanel.setLayout(null);
    
    InformationPanel = new JPanel ();
    InformationPanel.setBackground(new Color(204,204,255));
    InformationPanel.setBorder(BorderFactory.createTitledBorder(myBorder,"Information",0,0,myFont,Color.blue));
    InformationPanel.setBounds(270,420,520,70);
    InformationPanel.setLayout(null);
    
    ContentPanel.add(OptionsPanel);
    ContentPanel.add(DisplayPanel);
    ContentPanel.add(InformationPanel);
    	
    return ContentPanel;
  }
    
  private void makeOptionsContent()
  {
    
    Border myBorder = BorderFactory.createMatteBorder(1,1,1,1,Color.blue);
    Font MyFont      = new Font("System",0,12);
    AlgorithmenLabel = new JLabel();
    AlgorithmenLabel.setText("Algorithmen");
    AlgorithmenLabel.setBounds(25,20,100,15);
    AlgorithmenLabel.setFont(MyFont);
    
    AlgorithmenComboBox = new JComboBox();
    AlgorithmenComboBox.setBackground(Color.white);
    AlgorithmenComboBox.setEditable(false);
    AlgorithmenComboBox.setBounds(25,36,200,21);
    AlgorithmenComboBox.setFont(MyFont);
    AlgorithmenComboBox.addItem("Simulated Annealing");
    AlgorithmenComboBox.addItem("Ant Colony Optimization");
    AlgorithmenComboBox.setSelectedIndex(1);
    Algorithmen=(String)AlgorithmenComboBox.getSelectedItem();
    AlgorithmenComboBox.addActionListener(new ActionListener () 
      {
        public void actionPerformed(ActionEvent e)
         {
           Algorithmen=(String)AlgorithmenComboBox.getSelectedItem();
           if (Algorithmen=="Simulated Annealing")
           {
             MySimulatedAnnealingGui.ShowControls(true);
             TemperatureValue.setVisible(true);
             TemperatureLabel.setVisible(true);
             MyAntSystemGui.ShowControls(false);
           }
           else if (Algorithmen=="Ant Colony Optimization")
           {
             MySimulatedAnnealingGui.ShowControls(false);
             TemperatureValue.setVisible(false);
             TemperatureLabel.setVisible(false);
             MyAntSystemGui.ShowControls(true);
           } 
         }
       });    
    
   
    RandomCitiesCheckBox = new JCheckBox();
    RandomCitiesCheckBox.setText("Zuf鋖lige St鋎te erzeugen");
    RandomCitiesCheckBox.setBounds(22,308,200,21);// 212
    RandomCitiesCheckBox.setBackground(new Color(204,204,255));
    RandomCitiesCheckBox.setFont(MyFont);
    RandomCitiesCheckBox.addActionListener(new ActionListener () 
      {
        public void actionPerformed(ActionEvent e)
         {
           if (RandomCitiesCheckBox.isSelected())
           {
             MyCities.RemoveAllCities();
             MyDraw.ClearAll();
             RandomCitiesCount=(String)RandomCitiesComboBox.getSelectedItem();
             int RCount=Integer.parseInt(RandomCitiesCount);
           	 MyCities.GenerateRandomCities(RCount);
             MyDraw.DrawAllCities();
             MyCities.GenerateRandomStartSolution(true);
             MyDraw.clicked = false;
             
           }
           else if (!RandomCitiesCheckBox.isSelected())
           {
             MyCities.RemoveAllCities(); 
             MyDraw.ClearAll();
           }   
           	
         }
       });    
    
    RandomCitiesComboBox = new JComboBox();
    RandomCitiesComboBox.setBackground(Color.white);
    RandomCitiesComboBox.setEditable(true);
    RandomCitiesComboBox.setBounds(25,330,200,21);
    RandomCitiesComboBox.setFont(MyFont);
    RandomCitiesComboBox.addItem("10");
    RandomCitiesComboBox.addItem("30");
    RandomCitiesComboBox.addItem("50");
    RandomCitiesComboBox.addItem("100");
    RandomCitiesComboBox.addItem("200");
    RandomCitiesComboBox.addItem("300");
    RandomCitiesComboBox.addActionListener(new ActionListener () 
      {
        public void actionPerformed(ActionEvent e)
         {
           if (RandomCitiesCheckBox.isSelected())
           {
             MyCities.RemoveAllCities();
             MyDraw.ClearAll();
             RandomCitiesCount=(String)RandomCitiesComboBox.getSelectedItem();
             int RCount=Integer.parseInt(RandomCitiesCount);
             if (RCount>2700) 
             {
               RandomCitiesComboBox.setSelectedIndex(0);
               RCount=10;
             } 
             MyCities.GenerateRandomCities(RCount);
             MyDraw.DrawAllCities();
             MyCities.GenerateRandomStartSolution(true);
             MyDraw.clicked = false;
           }
         }
       });    
  
    BestSolutionsCheckBox = new JCheckBox();
    BestSolutionsCheckBox.setText("bisher beste L鰏ung zeigen");
    BestSolutions=false;
    BestSolutionsCheckBox.setSelected(false);
    BestSolutionsCheckBox.setBounds(22,362,200,21);//268
    BestSolutionsCheckBox.setBackground(new Color(204,204,255));
    BestSolutionsCheckBox.setFont(MyFont);
    BestSolutionsCheckBox.addActionListener(new ActionListener () 
      {
        public void actionPerformed(ActionEvent e)
         {
           if (BestSolutionsCheckBox.isSelected())
           {
            BestSolutions=true;
           }
           else if (!BestSolutionsCheckBox.isSelected())
           {
            BestSolutions=false;
           }   
           if (MySimulatedAnnealing!=null)
           {
             if (MySimulatedAnnealing.IsRunning())
             {
               MySimulatedAnnealing.SetBestSolutions(BestSolutions);
               MySimulatedAnnealing.Repaint();
             }
           }
           if (MyAntSystem!=null)
           {  
             if (MyAntSystem.IsRunning())
             {
               MyAntSystem.SetBestSolutions(BestSolutions);
               MyAntSystem.Repaint();
             } 
           }  
         }
       });    
       
    DelayLabel = new JLabel();
    DelayLabel.setText("Pause zwischen Schritten");
    DelayLabel.setBounds(25,390,200,15);
    DelayLabel.setFont(MyFont);
    
    DelaySlider = new JSlider();
    DelaySlider.setBounds(18,406,214,20);
    DelaySlider.setBackground(new Color(204,204,255));
    DelaySlider.setMajorTickSpacing(100);
    DelaySlider.setMaximum(2000);
    DelaySlider.setMinimum(0);
    DelaySlider.setValue(0);
    DelaySlider.addChangeListener(new ChangeListener () 
      {
        public void stateChanged(ChangeEvent e)
         {
           Delay=DelaySlider.getValue();
           if (MySimulatedAnnealing!=null)
           {
             if (MySimulatedAnnealing.IsRunning())
             {
               MySimulatedAnnealing.SetDelayTime(Delay);
             }
           }
           if (MyAntSystem!=null)
           {  
             if (MyAntSystem.IsRunning())
             {
               MyAntSystem.SetDelayTime(Delay);
             } 
           }  
         }
       });    

    StartButton = new JButton();
    StartButton.setText("Start");
    StartButton.setBounds(10,440,70,25);
    StartButton.setEnabled(false);
    StartButton.setFont(MyFont);
    StartButton.addActionListener(new ActionListener () 
      {
        public void actionPerformed(ActionEvent e)
         {
	    if( MyDraw.clicked ){
	      MyCities.GenerateRandomStartSolution(true);
	      MyDraw.clicked = false;
	    }
          
           MyDraw.allowClick=false;
           HandleControls(false);
           StartButton.setEnabled(false);

⌨️ 快捷键说明

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