Page 125 -
P. 125

243_MasterPieces_02b.qxd  4/18/03  6:59 PM  Page 97




                                                                      The Learning Brick Sorter • Masterpiece 2  97



                        If the program does not find a cell set to KB_YES, it scans the row again for the
                    value KB_UNKNOWN. In fact, given that there are no positive values, and that negative
                    values means,“don’t use this bin,” KB_UNKNOWN is our best second choice.
                        In theory, the routine should always find at least a KB_UNKNOWN value, unless the
                    user has provided inconsistent replies. However, as I said before, good programming prac-
                    tice states we should try to anticipate any possible users’ behavior.Thus, we have to
                    handle this case too, so lacking any guiding rule, the routine simply makes a random
                    choice.This makes our Learning Brick Sorter actually more adaptable. In fact, if the user
                    changes his mind while using the Learning Brick Sorter—giving a negative feedback to a
                    right choice—the user forces the Learning Brick Sorter to find a new solution to the
                    problem: that is, a different bin for that particular color.After a few cycles, the Learning
                    Brick Sorter will be trained to the new configuration.
                        Our next step is writing the routine that updates the knowledge base according to
                    the feedback provided by the user.

                    void UpdateKnowledgeBase(int ok)
                    {
                       int r;
                       // the user said OK
                       if (ok)
                       {
                         // this bin is the right one for the given range...
                         kb[range*3+bin]=KB_YES;
                         // ...and the wrong one for the other ranges
                         for(r=0;r<RANGES;r++)
                           if (r!=range)
                             kb[r*BINS+bin]=KB_NO;
                       }


                       // the user said NO
                       else
                         // the bin is the wrong one
                         kb[range*BINS+bin]=KB_NO;
                    }
                        This needs a few comments, as it reflects what I explained earlier when explaining
                    how the knowledge base works. When the user confirms that the bin was the correct
                    one, the program sets the corresponding cell of the knowledge base to KB_YES and the
                    other cells related to the same bin to KB_NO, meaning that no other colors can go into
                    that bin. When the user provides a negative feedback, the program simply sets the cell to
                    KB_NO. Notice that the value KB_UNKNOWN is never assigned to a cell: Every feed-
                    back turns at least one unknown status into a yes or a no.
   120   121   122   123   124   125   126   127   128   129   130