0

在我的案例 RMHC 中,我使用启发式搜索方法为 TSP 问题创建了一个代码。我正在测试一个 txt 文件的适应度水平,但它似乎远高于预期。我期望在 40-45k 的范围内,我收到大约 100k 常数的值。我还将提供测试文件。 文件

import java.util.*;
import java.io.*;

public class YO
{
    public static void main(String[] args)
    {
        for(int i=0; i< 10; i++) {
        Double[][] read =cs.ReadArrayFile("TSP_48.txt"," ");
        
//        System.out.println(Arrays.toString(SolutionTSP(read, 100000)));
        SolutionTSP(read,100000);
        System.out.println(TSP.fitness);
        }
    }
    public static int[] SolutionTSP(Double[][] distances, int iter)
    {
        TSP sol = new TSP(distances);
        
        sol = sol.RMHC(distances, iter);
        
        ArrayList<Integer> res = sol.GetTour();
        
        int[] result = new int[res.size()];
        
        for (int i = 0; i < res.size(); i++) {
            result[i] = res.get(i);
        }
        
        return result;
    }
    
}

class TSP
{
    public static ArrayList<Integer> tour;
    public static Double fitness;
    
    public TSP(Double[][] distances)
    {
        int cities = distances[0].length;
        tour = RandPerm(cities);
        
    }

    public static TSP RMHC(Double[][] distances, int iter)
    {
        TSP sol = new TSP(distances);
        
        for(int i=0;i<iter;i++)
        {
            
            int cities = distances[0].length;
            ArrayList<Integer> RandTour = RandPerm(cities);
            
            Double oldFit = sol.fitness(cities, distances);
            ArrayList<Integer> oldTour = sol.tour;
            
            sol.tour = RandTour;
            
            Double newFit = sol.fitness(cities, distances);
            
            if (newFit > oldFit)
            {
                sol.tour = oldTour;
            }
            TSP.fitness=oldFit;
//          System.out.println("old");
//          System.out.println(oldFit);
//          System.out.println("new");
//          System.out.println(newFit);
//          System.out.println("---------");
     
        }
        return sol;
    }
    
    public Double fitness(int cities, Double[][] dist)
    {
        Double s = 0.0;

        for (int i=0; i<cities-1; i++)
        {
            int a = tour.get(i);
            int b = tour.get(i+1);
            s += dist[a-1][b-1];
        }
        
        int end = tour.get(tour.size()-1);
        int start = tour.get(0);
        
        s += dist[end-1][start-1];
        
        return s;
    }
    
    public static ArrayList<Integer> RandPerm(int cities)
    {
        ArrayList<Integer> p = new ArrayList<Integer>();
        ArrayList<Integer> t = new ArrayList<Integer>();
        
        for (int i=1; i<cities+1; i++)
        {
            p.add(i);
        }
        

        while (p.size() > 0)
        {
            int i = cs.UI(0, p.size()-1);
            t.add(p.get(i));
            p.remove(i);
        }
        
        return t;
        
    }
    
    public static ArrayList<Integer> swap(ArrayList<Integer> t)
    {
        int i = 0;
        int j = 0;
        
        while (i == j)
        {
            i = cs.UI(0, t.size()-1);
            j = cs.UI(0, t.size()-1);
        }
        
//        int temp = t.get(i);
//        t.set(i, t.get(j));
//        t.set(j, temp);
        Collections.swap(t, i, j);
        
        return t;
    }
    
    public ArrayList<Integer> GetTour()
    {
        return tour;
    }
    
}

class cs 
{
    //Shared random object
    static private Random rand;
    //Create a uniformly distributed random integer between aa and bb inclusive
    static public int UI(int aa,int bb)
    {
        int a = Math.min(aa,bb);
        int b = Math.max(aa,bb);
        if (rand == null) 
        {
            rand = new Random();
            rand.setSeed(System.nanoTime());
        }
        int d = b - a + 1;
        int x = rand.nextInt(d) + a;
        return(x);
    }
    
    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);
    }
}
4

0 回答 0