0

My problem is sorting strings given in three lists using implementation of A* search,so I need to develop a heuristic function that will make solving various instances of this problem efficient.

The state can be represented s a list of 3 lists, example: [ [C B], [D], [H G F A E] ]

I pick up the top portion of any stack and move it to any other. For example, the H above could be moved on top of the C or the D, and the [H G] could be moved onto the second stack to create [H G D], etc. In this domain, there are unit operator costs, so each move costs 1, regardless of how many words are being moved.

The goal is to take an initial, configuration of blocks and move them all to the left stack in sorted order from top down. For example, given the 8 blocks in the above example, the goal would be [[A B C D E F G H] [] []].

I need to develop a heuristic that can be used with A* search to make the search efficient. I should try to keep the heuristics you develop admissible. For that you may try to have your heuristics to approximate the number of steps remaining to reach the goal state.

I thinking about a heuristic depends on the sorted words in each stack and the sum of points of each stack is the heuristic for the state, but this is not efficient, I think that I need to include the ascii code of each letter to my calculations, any ideas?

rene
  • 41,474
  • 78
  • 114
  • 152
  • So you want to sort a list of those problems by the number of steps to solve the problem? Hint: If you have pairwise comparison, like in Java, make sure that the search function is called only once for each element in the list, and not once for each combination. E.g., use a hashmap to cache the results of the search. Maybe this alone is enough of a speed-up. – tobias_k Nov 14 '14 at 14:22
  • Also, your problem closely resembles the Towers of Hanoi problem, just with random initial configuration. Maybe this helps you with your search for a good A* heuristic. – tobias_k Nov 14 '14 at 14:24
  • yes, I am programming with java, the only thing I need is the value of the heuristic for each state, so I need an algorithm can calculate heuristic value for each possible state then I can choose the better one. what do you mean exactly by search function? – faris hamarshi Nov 14 '14 at 14:28
  • yes I could see that it is very similar to tower of hanoi,I can give a weight for each word and try to apply tower of hanoi algorithm but I don't think it will be very efficient for sorting words. – faris hamarshi Nov 14 '14 at 14:33
  • Well, your A* search function of course. I meant that there are two things you can optimize: How fast your search is (determined by the heuristic) and how often you _call_ the search. If you are using a Java `Comparator` for the sorting, just make sure that you do not call the search function inside the comparator, as this will call the function for each combination of list entries to be compared, i.e. in the region of n^2 times instead of just n times. PS.: If you already have some code, you might want to show it to get better help. – tobias_k Nov 14 '14 at 14:33
  • Oh I see now, yes you are right my code does that – faris hamarshi Nov 14 '14 at 14:35
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/64963/discussion-between-faris-hamarshi-and-tobias-k). – faris hamarshi Nov 14 '14 at 18:55

1 Answers1

0

At the beginning, try a simple or naive heuristic. For example, "h" will be the sum of non-ascending order of two letters. Lets say when you develop a new child you got something like this [ACBED] here take first couple [AC] they are in ascending order, do nothing, but for the next couple [CB] it is not in ascending order then add one to "h". [BE] is good. For [ED] add one to "h". So "h == 2". This Heuristic looks simple but I believe it is better than blind search (i.e. breadth/depth search). From this idea you can add more rules to enhance the heuristic based on your analysis of outcomes. I hope that is useful.