我想用遗传算法解决旅行商问题并在googleMap上显示这条路线我想在logcat中打印出合适的路线,但我不能在Map上按顺序显示要去的城市。你能帮助我吗?logcat的输出如下follows:size:5,健身value:1.0737363764271219e9,等位基因:[整合基因(0,4)=0,整合基因(0,4)=4,整合基因(0,4)=2,整合基因(0,4)=3,整合基因(0,4)=1],应用data: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();
}
}
暂无答案!
目前还没有任何答案,快来回答吧!