Shortest walk and straight going possible in grid? A*-algorithm

You could also look into Dijkstra’s algorithm or the Traveling Salesman problem, maybe they are a better fit?

Here’s an old thread of mine discussing the TSP in combination with a genetic algorithm. It’s written in Python and it should be rather straightforward to add more fitness values or weights, like discussed by @laurent_delrieu above.