I have an assignment that I’m trying to do using Java, but I am confused about how to set up/what is the nodes for the following graph. Basically, it is the pen plotter problem, or more commonly known as the travelling salesman problem. Where my following input is:
Line between 4 1 and 4 4
Line between 4 4 and 4 7
Line between 2 6 and 4 4
Line between 4 4 and 6 2
Line between 6 6 and 4 4
Line between 2 2 and 4 4
and my output comes out as:
<n> nodes explored
cost = 24.61
Move from 0 0 to 2 2
Draw from 2 2 to 4 4
Draw from 4 4 to 6 6
Move from 6 6 to 4 7
Draw from 4 7 to 4 4
Draw from 4 4 to 4 1
Move from 4 1 to 6 2
Draw from 6 2 to 4 4
Draw from 4 4 to 2 6
Assuming that the bottom left hand corner of the piece of paper, would be your start (0,0) and it goes up in coordinates, are each coordinate a node, and how would I determine when to move and draw a line. I know I should be using an undirected graph with A* but I’m still quite confused about which ones are nodes (vertices) and how I’d determine when to move and when to draw lines, could someone give me some advice?
EDIT: note that the refers to the amount/number of nodes explored throughout the whole search.
To use A*, you will first need an admissible heuristic function. [explanation what it is is attached at the end of this answer].
Problem as a Graph for A:*
define G=(V,E) as your graph, such that:
V={all possible drawings prefixes}[i.e. all possible ‘snap-shots’ of each possible draw]. note you shouldn’t hold this graph in memory, but create it on the fly, withnext()which will be explained later. [note that practically, for each state you actually need to store only (1)where is the pen currently (2)which lines where already drawn]E={all possible changes from one 'snap shot' to another}You also need
w:E->R[a weight function] which will be simply:w(point1,point2)=euclidian_distance(point1,point2)You should also define
next:V->P(V):next(v)={all snap shots you can get from v, using exactly one move/drawFinally, you should also define
F: all “ending” states.F={all the prefixes which all the lines are drawn}How to run A*:
start from the snapshot where your pen is at (0,0), and no lines are drawn [this is the initial state], and keep going until you find one of the final states. when you do, if your heuristic is valid, you are guaranteed to have got the optimized solution, because A* is admissible and optimized
(*) admissible heuristic function:
let
h*(v)=real distance to targetfrom vertex v.a heuristic function h:V->R is admissible if
h(v)<=h*(v)for each v in VYour real Challenge
The hard part for TSP is finding a an admissible
h. It is so hard because you have no idea what’s the shortest path is, and if the heuristic function is not admissible, it is not guaranteed that the solution found will be optimized.Suggestion:
You might want to use some any time algorithm, When I did something similar to this, [solved TSP with multiple agents] I also used A*, but started with a non valid heuristic, and iteratively decreased it, so if I had enough time, I found optimal solution, and if not – I returned the best solution I could find.