The deep search algorithm
 

     In order to gauge  the performance of  the heuristic algorithm,  an algorithm that deduced the best possible average performance had to be developed.  This entails finding a strategy 's' such that when all codes are deduced using 's', the sum of guesses made is minimized.  In order to develop such a strategy, a deep search procedure has to be applied to each code set, that is: an algorithm that searches all the possible paths and finds the shortest one.  As for Mastermind, this entails trying all possible codes as the first guess, and then choosing the one that leads to guessing the rest of the codes the quickest. To better understand what this involves, a detailed study of the 4 color and 3 positions game is given in Figure 4.  For this example we have chosen 112 as the initial code.

     Unfortunately, the deep search algorithm has only a very poor time performance. In fact, the above results of  the {4,3} case was achieved only after the following optimization were implemented.

Optimization of the deep search algorithm

     After beginning to run the above algorithm on the {4,3} case it became clear that doing a pure depth search (i.e. searching every possible path) was an unfeasible task with any larger case. Below, we outline some of the optimizations made.
 
     The first optimization made concerned the top level of the search tree. It was clear that several first guesses led to equal results and therefore, testing each one was wasteful.  With any color - position combination one can come up with the minimum set of codes that have the essential characteristics of all the others.  In the 4x3 case these are 111, 112, 123. All other codes are just permutations of these three.  By permutations we mean a switch of color and/or position. Therefore, by reducing the first level search space from 64 to 3, we improved our performance drastically.  This however was not enough. While reducing the number of  guesses, the computer had to try from the topmost level down, and  it did not allow us to follow the same procedure for lower levels.  At lower levels one cannot assume that all codes of a certain structure (ones that can be converted into others by a set of permutations)  achieve the same minimum of guesses

     The second optimization we made involved stopping a search on a branch as soon as the optimum was found. For example: in the 4x3 case outlined in Figure 1, we can follow the path: 112->(2,0)->132->(1,1).  This leaves us with three remaining possibilities, namely 212, 113, 412.
If we now try 212 we distinguish the rest as shown below.
        212       113      412
212  (3,0)     (1,0)     (2,0)

     The fact that each answer we received appears only once ensures that this guess (212) gives the shortest path.  We do not need to consider the other two (113 and 412).  We have already discovered the optimum path at this point and can disregard the rest.
     This optimization significantly reduces the number of times that the deep search function is called.
 
     The optimization allowed us to do deep searches for standard cases up to {6,4}, for which the algorithm took approximately 15 hours.  Below is a table of the achieved results.
 

    A Big O analysis of the deep search algorithm gave a Big O of c2pp4.  As it becomes obvious that going further using the present algorithm is unrealistic, we are looking into calculating a theoretical minimum.  This can be done by calculating the size of the sets corresponding to each answer instead of its elements.  Already the exit conditions of the algorithm are based on size so just calculating the lengths from the beginning would not degrade the accuracy of our results. This work is still in progress.
 
Top
Back - Introduction   Definitions  The heuristic algorithm
Forward - Conclusion