Uppdatering! Nu använder jag L1 Norm och L2 norm. Här är ett exempel.
För er som är intresserad utav praktiken.
Kod: Markera allt
int main() {
// Beginning coordinates
int x_start = 1;
int y_start = 8;
// End coordinates
int x_stop = 8;
int y_stop = 8;
// Path - Our goal is to find them
int path_x[height_map * width_map];
int path_y[height_map * width_map];
// Norm modes
int norm1 = 1; // L1-Norm
int norm2 = 2; // L2-Norm
// Steps
int steps1 = 0;
int steps2 = 0;
// Map size
int map[height_map*width_map] = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, 0, 0, 0, 0, 0, 0, 0, -1,
-1, -1, 0, -1, -1, -1, -1, -1, 0, -1,
-1, -1, 0, -1, 0, 0, 0, -1, 0, -1,
-1, -1, 0, -1, 0, -1, 0, -1, 0, -1,
-1, -1, 0, -1, 0, -1, 0, -1, 0, -1,
-1, -1, 0, -1, 0, -1, 0, -1, 0, -1,
-1, 0, 0, 0, 0, -1, 0, -1, 0, -1,
-1, -1, -1, -1, -1, -1, 0, -1, 0, -1,
-1, -1, 0, 0, 0, 0, 0, -1, 0, -1,
-1, -1, 0, 0, -1, -1, -1, -1, 0, -1,
-1, -1, 0, 0, 0, 0, 0, 0, 0, -1,
-1, -1, -1, -1, -1, 0, -1, -1, 0, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
// Print the map
printf("Initial map\n");
print_int(map, height_map, width_map);
// Compute the "shortest" path
printf("Compute the coordinates\n");
clock_t start, end;
float cpu_time_used;
start = clock();
// First compute with L1-Norm and check the steps
Astar(map, path_x, path_y, x_start, y_start, x_stop, y_stop, height_map, width_map, norm1, &steps1);
// Then compute with L2-norm and check the steps
Astar(map, path_x, path_y, x_start, y_start, x_stop, y_stop, height_map, width_map, norm2, &steps2);
// Check the steps now - Which of the norms results less steps
if(steps2 > steps1){
Astar(map, path_x, path_y, x_start, y_start, x_stop, y_stop, height_map, width_map, norm1, &steps1); // Get the path again
printf("Shortest step is = %d\n", steps1);
}else{
printf("Shortest step is = %d\n", steps2);
}
end = clock();
cpu_time_used = ((float) (end - start)) / CLOCKS_PER_SEC;
printf("\nTotal speed was %f,", cpu_time_used);
// Show the path
printf("\nComputed map\n");
show_path(map, path_x, path_y, height_map, width_map);
// Show the path
for (int i = 0; i < height_map * width_map; i++)
printf("x = %d, y = %d\n", path_x[i], path_y[i]);
return EXIT_SUCCESS;
}
Nu har jag även gjort det superenkelt för er att kunna lägga till heuristik i programmet.Bara fyll på här och testa algoritmen. En god rekommendation är att använda olika former av normer.