【问题标题】:Simple hill climbing algorithm? [closed]简单的爬山算法? [关闭]
【发布时间】:2011-03-15 05:36:29
【问题描述】:

我正在尝试使用简单的爬山算法来解决旅行商问题。我想创建一个 Java 程序来做到这一点。我知道这不是最好的,但我主要希望它查看结果,然后将结果与我还将创建的以下结果进行比较:

  • 随机爬山者
  • 随机重启爬山者
  • 模拟退火。

不管怎样,回到简单的爬山算法,我已经有了这个:

import java.util.*;
public class HCSA 
{
    static private Random rand; 
    static public void main(String args[])
    {
        for(int i=0;i<10;++i) System.out.println(UR(3,4));
    }
    static public double UR(double a,double b)
    {
        if (rand == null) 
        {
            rand = new Random();
            rand.setSeed(System.nanoTime());
        }
        return((b-a)*rand.nextDouble()+a);
    }
}

这就是我所需要的吗?这段代码是否正确..?我希望程序从中读取并生成结果的文本文档中有一系列不同的数据集。

非常感谢您对此的任何帮助。

----- 编辑----

我是个白痴,当我应该先在记事本中打开它时,我直接在 Eclipse 中打开了 Java 文件。这是我现在得到的代码。

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.Reader;
import java.io.StreamTokenizer;
import java.util.ArrayList;

{
    //Print a 2D double array to the console Window
    static public void PrintArray(double x[][])
    {
        for(int i=0;i<x.length;++i)
        {
            for(int j=0;j<x[i].length;++j)
            {
                System.out.print(x[i][j]);
                System.out.print(" ");
            }
            System.out.println();
        }
    }
    //reads in a text file and parses all of the numbers in it
    //is for reading in a square 2D numeric array from a text file
    //This code is not very good and can be improved!
    //But it should work!!!
    //'sep' is the separator between columns
    static public double[][] ReadArrayFile(String filename,String sep)
    {
        double res[][] = null;
        try
        {
            BufferedReader input = null;
            input = new BufferedReader(new FileReader(filename));
            String line = null;
            int ncol = 0;
            int nrow = 0;

            while ((line = input.readLine()) != null) 
            {
                ++nrow;
                String[] columns = line.split(sep);
                ncol = Math.max(ncol,columns.length);
            }
            res = new double[nrow][ncol];
            input = new BufferedReader(new FileReader(filename));
            int i=0,j=0;
            while ((line = input.readLine()) != null) 
            {

                String[] columns = line.split(sep);
                for(j=0;j<columns.length;++j)
                {
                    res[i][j] = Double.parseDouble(columns[j]);
                }
                ++i;
            }
        }
        catch(Exception E)
        {
            System.out.println("+++ReadArrayFile: "+E.getMessage());
        }
        return(res);
    }
    //This method reads in a text file and parses all of the numbers in it
    //This code is not very good and can be improved!
    //But it should work!!!
    //It takes in as input a string filename and returns an array list of Integers
    static public ArrayList<Integer> ReadIntegerFile(String filename)
    {
        ArrayList<Integer> res = new ArrayList<Integer>();
        Reader r;
        try
        {
            r = new BufferedReader(new FileReader(filename));
            StreamTokenizer stok = new StreamTokenizer(r);
            stok.parseNumbers();
            stok.nextToken();
            while (stok.ttype != StreamTokenizer.TT_EOF) 
            {
                if (stok.ttype == StreamTokenizer.TT_NUMBER)
                {
                    res.add((int)(stok.nval));
                }
                stok.nextToken();
            }
        }
        catch(Exception E)
        {
            System.out.println("+++ReadIntegerFile: "+E.getMessage());
        }
        return(res);
    }
}

【问题讨论】:

  • “这段代码是否正确.. ?” -- 正确吗?我没有看到任何爬山算法。另外,我只需在main() 中初始化rand 并在UR() 中删除测试。
  • 嗯,适合使用爬山算法遍历几个数据集。数据集是旅游距离。看起来它不像我想象的那样。现在有点迷茫。感谢您的其他建议。
  • 刚刚找到上面贴的java的sn-p的描述“TSP java类中的三个方法,用于读取数组(城市距离文件),打印数组和读取旅游文件(整数列表文件)。”我错过了什么吗?
  • 模拟退火将很难正确(而且很容易出错)。看看Drools Plannersource code(java,开源)
  • 我上面的内容实际上是一个简单的爬山算法吗?稍后我将不得不查看模拟退火,但我什至无法得到这个..

标签: java algorithm artificial-intelligence traveling-salesman hill-climbing


【解决方案1】:

您可以将结果与教科书的代码库进行比较
Artificial Intelligence a Modern Approach”,这里是aima code repository
他们的爬山实现是HillClimbingSearch.java

【讨论】:

  • 我怎样才能运行这个程序!有没有教程可以运行这个源代码..
  • 教程为本代码附带的教材,教材首页aima.cs.berkeley.edu
  • 感谢 Crowne ...我已经在运行它了 :)
【解决方案2】:

我不确定您粘贴的代码与旅行推销员有什么关系。您有一个函数UR,它在[a,b) 区间内生成一个随机数。

【讨论】:

  • 哦..这令人失望。我现在完全糊涂了。我的印象是代码目前使用了设定的数字 (3,4),我需要做的就是更改它以便读取数据集。
  • 好的,我刚刚找到上面发布的java的sn-p的描述:“TSP java类中的三个方法,用于读取数组(城市距离文件),打印数组和读取在游览文件中(整数列表文件)”
猜你喜欢
  • 1970-01-01
  • 2016-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多