Java 用遗传算法求解旅行商问题并在谷歌地图上显示该路线

Java 用遗传算法求解旅行商问题并在谷歌地图上显示该路线,java,android,maps,traveling-salesman,Java,Android,Maps,Traveling Salesman,我想用遗传算法解决旅行推销员问题,并在谷歌地图上显示这条路线。我想在logcat中打印出合适的路线,但我无法在地图上按顺序显示要去的城市。你能帮助我吗? logcat的输出如下:大小:5,适应值:1.0737363764271219E9,等位基因:[整基因(0,4)=0,整基因(0,4)=4,整基因(0,4)=2,整基因(0,4)=3,整基因(0,4)=1],应用数据:null Kurye.java: public class Kurye extends Salesman { pub

我想用遗传算法解决旅行推销员问题,并在谷歌地图上显示这条路线。我想在logcat中打印出合适的路线,但我无法在地图上按顺序显示要去的城市。你能帮助我吗? logcat的输出如下:大小:5,适应值:1.0737363764271219E9,等位基因:[整基因(0,4)=0,整基因(0,4)=4,整基因(0,4)=2,整基因(0,4)=3,整基因(0,4)=1],应用数据:null

Kurye.java:
public class Kurye extends Salesman {


    public static final int CITIES = 5;
    // private static double[][] CITYARRAY;
    public static double[][] CITYARRAY = new double[][]{
           {58.71514145151117,25.82912027835846},{39.96919780592407,32.890751492232084},{40.73748614216524,29.3393293954432},{39.980275889023986,32.842243406921625},{49.84060536176288,12.234100140631199},
           /* {19.776266, -99.119753}, {19.767437, -99.123009},
            {19.746360, -99.097878},{19.776283, -99.119924},
            {19.752176, -99.093930}, {19.753761, -99.099530}, {19.760044, -99.091653},
            {19.791929, -99.082839}, {19.827808, -99.078333}, {19.824172, -99.116228}*/

    };
   
   
    public double[][] getCITYARRAY() {
        return CITYARRAY;
    }

    public void setCITYARRAY(double[][] CITYARRAY) {
        this.CITYARRAY = CITYARRAY;
    }
    @Override
    public double distance(Gene a_from, Gene a_to) {

        //Formula de haversine
     /*
        }*/



        IntegerGene geneB = (IntegerGene) a_to;
        IntegerGene geneA = (IntegerGene) a_from;
        //IntegerGene geneB = (IntegerGene) a_to;
        int a = geneA.intValue();
        int b = geneB.intValue();
        double x1 = CITYARRAY[a][0];
        //System.out.println("X1: "+x1);
        double y1 = CITYARRAY[a][1];
        double x2 = CITYARRAY[b][0];
        double y2 = CITYARRAY[b][1];
        //return Math.sqrt( (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));

        //public static double distanciaCoord(double lat1, double lng1, double lat2, double lng2)
        //double radioTierra = 3958.75;//en millas
        double radioTierra = 6371;//en kilómetros
        double dLat = Math.toRadians(x2 - x1);
        double dLng = Math.toRadians(y2 - y1);
        double sindLat = Math.sin(dLat / 2);
        double sindLng = Math.sin(dLng / 2);
        double va1 = Math.pow(sindLat, 2) + Math.pow(sindLng, 2)
                * Math.cos(Math.toRadians(x1)) * Math.cos(Math.toRadians(x2));
        double va2 = 2 * Math.atan2(Math.sqrt(va1), Math.sqrt(1 - va1));
        double distancia = radioTierra * va2;

        return distancia;
    }


    @Override
    public IChromosome createSampleChromosome(Object a_initial_data) {
        try {
            Gene[] genes = new Gene[CITIES];
            for (int i = 0; i < genes.length; i++) {
                genes[i] = new IntegerGene(getConfiguration(), 0, CITIES - 1);
                genes[i].setAllele(new Integer(i));
            }
            IChromosome sample = new Chromosome(getConfiguration(), genes);
            return sample;
        }
        catch (InvalidConfigurationException iex) {
            throw new IllegalStateException(iex.getMessage());
        }
    }

}

MapsActivity.java:
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback ,GoogleMap.OnPolylineClickListener{
    FileOutputStream fileOutputStream;

    private GoogleMap mMap;
    public static double[][] CITY = new double[][]{};
    




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }

    /**
     * Manipulates the map once available.
     * This callback is triggered when the map is ready to be used.
     * This is where we can add markers or lines, add listeners or move the camera. In this case,
     * we just add a marker near Sydney, Australia.
     * If Google Play services is not installed on the device, the user will be prompted to install
     * it inside the SupportMapFragment. This method will only be triggered once the user has
     * installed Google Play services and returned to the app.
     */
    @Override
    public void onMapReady(GoogleMap googleMap) {
        try {
            mMap = googleMap;
            Kurye kur = new Kurye();

            IChromosome optimal = kur.findOptimalPath(null);

            System.out.println("Solution: ");
            System.out.println(optimal);
           




            //  System.out.println(optimal.toString());
            System.out.println("Score " +
                    (Integer.MAX_VALUE / 2 - optimal.getFitnessValue()));

            // Add a marker in Sydney and move the camera
            LatLng sydney = new LatLng(-34, 151);
            mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));

           /* mMap.addMarker(new MarkerOptions().position(new LatLng(kur.getCITYARRAY()[0][0], kur.getCITYARRAY()[0][1])).title("1"));



            mMap.addMarker(new MarkerOptions().position(new LatLng(kur.getCITYARRAY()[1][0], kur.getCITYARRAY()[1][1])).title("2"+optimal.getGene(1)));
            mMap.addMarker(new MarkerOptions().position(new LatLng(kur.getCITYARRAY()[2][0], kur.getCITYARRAY()[2][1])).title("3"));
            mMap.addMarker(new MarkerOptions().position(new LatLng(kur.getCITYARRAY()[3][0], kur.getCITYARRAY()[3][1])).title("4"));
            mMap.addMarker(new MarkerOptions().position(new LatLng(kur.getCITYARRAY()[4][0], kur.getCITYARRAY()[4][1])).title("5"));*/
            mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));

            for (int i = 0; i < kur.getCITYARRAY().length; i++) {
                mMap.addMarker(new MarkerOptions().position(new LatLng(kur.getCITYARRAY()[i][0], kur.getCITYARRAY()[i][1])).title(String.valueOf(optimal.getGene(i))));
            }


            Configuration.reset();






        } catch (Exception e) {
            e.printStackTrace();
        }
    }
Kurye.java:
公共类Kurye扩展{
公共静态最终国际城市=5;
//私有静态双[][]城市阵列;
公共静态双精度[][]城市光线=新双精度[][]{
{58.71514145151117,25.82912027835846},{39.96919780592407,32.890751492232084},{40.73748614216524,29.3393293954432},{39.980275889023986,32.842243406921625},{49.84060536176288,12.234100140631199},
/* {19.776266, -99.119753}, {19.767437, -99.123009},
{19.746360, -99.097878},{19.776283, -99.119924},
{19.752176, -99.093930}, {19.753761, -99.099530}, {19.760044, -99.091653},
{19.791929, -99.082839}, {19.827808, -99.078333}, {19.824172, -99.116228}*/
};
public double[]getCITYARRAY(){
返回城市射线;
}
公共void setCITYARRAY(双[][]城市阵列){
this.CITYARRAY=CITYARRAY;
}
@凌驾
公共双距离(基因a_-from,基因a_-to){
//哈弗森公式
/*
}*/
整合子基因b=(整合子基因)a_to;
整合素基因a=(整合素)a_来自;
//整合子基因b=(整合子基因)a_to;
inta=geneA.intValue();
intb=geneB.intValue();
双x1=城市射线[a][0];
//System.out.println(“X1:+X1”);
双y1=城市雷[a][1];
双x2=城市射线[b][0];
双y2=城市射线[b][1];
//返回数学sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
//公共静态双距离命令(双lat1、双lng1、双lat2、双lng2)
//双放射性铁=3958.75;//n millas
双无线电tierra=6371;//en kilómetros
双dLat=数学托拉迪安(x2-x1);
双dLng=数学环面(y2-y1);
双正弦=数学正弦(dLat/2);
双正弦=数学正弦(dLng/2);
double va1=数学功率(sindLat,2)+数学功率(sindLng,2)
*Math.cos(Math.toRadians(x1))*Math.cos(Math.toRadians(x2));
double-va2=2*Math.atan2(Math.sqrt(va1),Math.sqrt(1-va1));
双倍距离=无线电TIERRA*va2;
返回距离;
}
@凌驾
公共IChromosome createSampleChromosome(对象a_初始数据){
试一试{
基因[]基因=新基因[城市];
for(int i=0;i