简单爬山算法?

我正在尝试使用简单爬山算法来解决旅行商问题。我想创建一个 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);    }}

这就是我所需要的全部吗?这段代码甚至对吗?我有一系列不同的文本文件数据集,我希望程序从中读取并生成结果。

非常感谢任何帮助。

—– 编辑 —-

我太蠢了,应该先在记事本中打开 Java 文件,然后再直接在 Eclipse 中打开它…… 这是我现在得到的代码。

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);    }}

回答:

我不确定你粘贴的代码与旅行商有什么关系。你有一个函数 UR,它生成区间 [a,b) 中的一个随机数。

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注