首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >简单的爬山算法?

简单的爬山算法?
EN

Stack Overflow用户
提问于 2011-03-15 05:36:29
回答 2查看 26.9K关注 0票数 0

我试图用简单的爬山算法来解决旅行推销员的问题。我想要创建一个Java程序来完成这个任务。我知道这不是最好的方法,但我主要希望它能看到结果,然后将结果与我将创建的以下结果进行比较:

  • 随机爬山器
  • 随机重启山攀岩器
  • 模拟退火

无论如何,回到简单的爬山算法,我已经有了以下内容:

代码语言:javascript
复制
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);
    }
}

这就是我所需要的吗?这个密码对吗..?我在文本文档中有一系列不同的数据集,我希望程序从中读取这些数据集,然后生成结果。

我真的很感激你在这方面的帮助。

--编辑--编辑--

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

代码语言:javascript
复制
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);
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-03-15 05:42:50

我不知道你粘贴的代码和旅行推销员有什么关系。您有一个函数UR,它在间隔[a,b)中生成一个随机数。

票数 2
EN

Stack Overflow用户

发布于 2011-03-15 05:44:04

您可以将您的结果与教科书的代码存储库进行比较。

"人工智能--一种现代方法",这是aima代码库

他们的爬山实现是HillClimbingSearch.java

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5307908

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档