CS 460. Artificial Intelligence


Syllabus

Student presentations record

Prolog is a programming language based on logic. It consists almost entirely of subprogram calls. It lacks (almost) assignment statements and the standard conditional and looping control structures. Yet it is an extremely powerful and elegant language. It includes two features not found in most programming languages: backtracking (in which the program backs up to a previous state when it determines that the approach it had been following will not work out) and logic variables with unification (in which variables and data structures match each other and take on values in simple yet powerful ways).

Search is at the core of artificial intelligence (AI). Since Prolog includes within itself a built-in search capability (backtracking), it is sometimes considered the ultimate AI programming language. 

Homework. All numbered homework problems are from the text.

  1. Due Wednesday 10/1: 1.3 and 1.5 (page 13).

  2. Due Monday 10/6: 3.1 - 3.9 (pages 67, 73, 74).

  3. Due Wednesday 10/8: Write the following prolog predicates.

    1. makeSet(L, S).  If L is a list, which may contain duplicates, S is a list of the same elements but without duplicates.
    2. add(E, S0, S1). Add the element E to the set S0 to produce the set S1.
    3. intersection(S0, S1, S2).  S2 is the set intersection of S0 and S1.
    4. union(S0, S1, S2). S2 is the set union of S0 and S1.
    5. difference(S0, S1, S2). S2 is the set difference: S0 - S1.
    6. equal(S0, S1). Succeeds is S0 and S1 are the same set, i.e., have the same elements but not necessarily in the same order.
    7. subsequence(L0, L1). L0 is a list; L1 is also a list whose elements all occur in L0 in the same order as in L1.
    8. 3.11 (page 74).


  4. Due Monday 10/13: 3.16 - 3.21 (pages 85 - 86).  Also: range(N1, N2, List), where List is a list of the numbers between N1 and N2. (Similar to 3.21.)

  5. Due Monday 10/20: Logic Puzzles. See: http://www.enchantedlearning.com/math/logic/puzzles/. Also see http://abbott.calstatela.edu/courses/cs_460/weightLogicPuzzle.pl and http://abbott.calstatela.edu/courses/cs_460/birthMonth.pl.

    1. Pets.  Define fact predicates: canFly/1, hasHair/1, and barks/1.  E.g. hasHair(dog). hasHair(cat).
    2. Last names. Use built-in predicate atom_chars/2. e.g., atom_chars(abcd, [a,b,c,d]).
    3. Sports. Use negation in clues. E.g., clue1 would would include something like:
      \+ likes(audrey, soccer).  Also, define gender/2, e.g., gender(audrey, female).
    4. Favorite animal.  Create a list of animals ordered by size.  Use technique similar to that used in Birth Month puzzle to compare the sizes of two animals.
    5. A more complicated one (with two fields for each person instead of one): http://www.alphajade.com/mishmash/entertainment/puzzles/logic/logic2.aspx
    6. (Not a homework: n-queens problem: http://abbott.calstatela.edu/courses/CS_460/NQueens.pl.)
    7. For a more challenging set of puzzles, look at http://homepages.paradise.net.nz/borborygmus/.  You may substitute any of these for any of a - e.

       
  6. We will be working on Chapter 11. Please bring your book with you to class. Here is a version of Figure 11.7: http://abbott.calstatela.edu/courses/CS_460/towers.pl

    Due Monday 10/27: Write programs to solve the following classic problems.
    1. Wolf-goat-cabbage e.g., http://perso.wanadoo.fr/jeux.lulu/html/anglais/loupChe/loupChe1.htm.
    2. Missionaries and cannibals, e.g., http://www.plastelina.net/examples/games/game2.html.
    3. Three jugs, e.g., http://www.maa.org/editorial/knot/water.html.
    4. N-Queens. (Same problem we did earlier.  This time find a sequence of queen placements. The state can be something like a pair:
                              [unplaced queens] - [placed  queens].  
      You would start in the state: 
                              [1, 2, 3, 4] - [ ] 
      and end, for example, in the state 
                              [ ] - [1-2, 2-4, 3-1, 4-3].
     
  7. Due Monday 11/3.

    1. Extend the predicates depthFirst/2 and breadthFirst/2 in http://abbott.calstatela.edu/courses/cs_460/trees.pl so that the second argument is the complete path (in reverse order) from Start to End rather than just the End node.

      That is, define depthFirst(+Start, [?End | ?Path]) and breadthFirst(+Start, [?End | ?Path]).

      See the paths/2 predicate in  http://abbott.calstatela.edu/courses/CS_460/towers.pl for an example.

      To watch programs execute, look at http://abbott.calstatela.edu/courses/CS_460/trace.pl.

    2. Run the problems from last week under iterDep/3 in http://abbott.calstatela.edu/courses/CS_460/towers.pl. You should get the same solutions as before, but they should appear ordered by length, shortest first.


  8. Due Monday 11/10. Extend the towers problem as follows.

    1. Allow there to be an arbitrary number of blocks.

    2. Allow state transitions that move not only the top block, but the top n blocks, for any n, from one tower to another or to the table.
       
    3. Assume each block has a cost when it is moved, perhaps based on its weight.

      One way to do this is to attach a block's cost to the block. So instead of a tower looking like this a : b : c, it would look like this a-3 : b-2 : c-5. This would indicate that block a costs 3 units to move, block b costs 2 units to move, and block c costs 5 units to move.
       
    4. Also allow the cost of moves to be adjusted for the size of the move as allowed by (b) above.  For example, if you move 3 blocks at a time, the cost might be not just the sum of the costs of the blocks being moved, but 3 times that sum.  Or perhaps, you want to give a discount for moving multiple blocks.  So moving 3 blocks at a time might cost the sum of the costs of the blocks divided by 3.

      A simple way to do this is to include in the top level call an arithmetic expression that gives the cost of a move in terms of two parameters: the sum of the costs of the individual blocks and the number of blocks in the move.

      For example, instead of defining bestFirst(Start, Goal, Solution), you might define

                                  bestFirst(Start, Goal, cost(Sum, Count) = Cost, Solution)

      where the third argument is a function that expresses Cost in terms of the variables Sum and Count.  It might be something like Sum*Count.  In this case, the cost of a move is the sum of the costs of the blocks times the number of blocks moved. Note that cost( Sum, Count) = Cost is a single argument to the function bestFirst/4.

    5. So that this approach is reasonably general, modify your step(Node, NextNode)  predicates to have 4 arguments:

                              step(Node, cost(Sum, Count) = Cost, NextNode, Cost).

      The second argument is the expression for the cost function. The final argument is the cost value.  The first and third arguments are the state input and output.


      See CostFunctionExample.pl for an example of how the computation might work if the state data structure were a list of towers. The step/4 function in that file removes the top few blocks (different numbers of blocks on backtracking) from one of the blocks and makes a new tower from the removed blocks.. It also computes a cost. (It uses the convention we discussed in class of attaching a cost to each tower.)

      ?- step([a-3:b-5:c-2, d-5:e-6], cost(Sum, Count) = Sum/Count, Next, Value).

      Sum = 3
      Count = 1
      Next = [a-3, b-5:c-2, d-5:e-6]
      Value = 3 ;

      Sum = 8
      Count = 2
      Next = [a-3:b-5, c-2, d-5:e-6]
      Value = 4 ;

      Sum = 5
      Count = 1
      Next = [d-5, e-6, a-3:b-5:c-2]
      Value = 5 ;

      No
      ?- 


       

    6. Write a best-first search algorithm that uses cost information to find the best (lowest cost) path from an input state to a goal state. Run tests to illustrate how the result depends on cost.

    7. If you haven't already done so, generalize the jugs problem so that you can have any number of jugs. Provide cost values for the step transitions in this problem and run tests to illustrate how the result depends on cost.



  9. Due Monday 11/17. In minimax.pl, we represented a directed graph as a list of graph edges. Thus [a-b, a-c, b-d, b-e, e-a, c-b] may be understood as representing the graph shown below. 

    Develop the following two components of a game playing program. (At this point, these two program components don't interact.)
     
    1. Write a program that will keep track of the positions of two players on such a graph. The players take turns moving from node to node.  Allow the program to consult settings that determine, for each player, whether it is a human or the computer. 
      1. If a player is a human, tell the human what the possible moves are and then accept as keyboard input one of those as the player's next move. 
      2. If the player is the computer, let the computer select its next move at random.
         
    2. Optional. Write a program that will take a graph of this sort and produce a list of all pairs of nodes along with the length of the shortest path between them.  For example, for this graph, the output might be represented as the following. [a/b-1, a/c-1, a/d-2, a/e-2, b/a-2, b/c-3, b/d-1, b/e-1, c/a-3, c/b-1, c/d-2, c/e-2, d/a-n, d/b-n, d/c-n, d/e-n, e/a-1, e-b, 2, e/c-2, e/d-3].
     
  10. Due Monday 11/24.

    1. Revise minimax so that it applies to this game.
    2. Revise minimax so that it returns not only a recommended next move, but also the projected sequence of moves that follow that move.
    3. Have your computer players use the revised minimax it to determine their next move.

    You may start with my revised version of minimax, which is in http://abbott.calstatela.edu/Courses/CS_460/Network%20Game along with a framework for the game.. If you use my version, you should be able to explain it as if you had written it.

    Game Rules.

    1. If one player lands on a node occupied by another player, the second player's piece (the one whose node was invaded) is removed from the game.
    2. The game is over when one player loses all his pieces.

     
  11. Due Monday 12/1. (Final assignment.)

    1. Modify the game so that each player may have an arbitrary number of pieces.  As the rules indicate, a player who loses all his pieces loses the game.

      In the extended game, since multiple pieces may be able to move to the same node, a player may have to specify the edge over which he wants to move and not just the node to which he is moving. That is, a move is specified by specifying an edge over which the player wants to move a piece.

      In the extended game, a player may have multiple pieces on a single node. Since a player's pieces are indistinguishable, when moving a piece from such a node, it is not necessary to indicate which of these is moving.

      If a player lands on a node that contains multiple pieces belonging to the other player, all the other player's pieces are removed from the game.

    2. Modify the game so that there is a separate graph for each player. Presumably the nodes in the two graphs will overlap, but there may be nodes in each player's graph that are not in the other player's graph.  It may also be the case that two nodes are joined in one direction in one player's graph and in the other direction in the other player's graph.

    3. Write an intelligent static evaluation function.  Here are a number of things you may consider.

      1. (Easy) Look not just at whether the board position indicates a win for one side or another but how many pieces each side has on the board. The value of a board position is the difference between these numbers.

      2. (Harder) Analyze the game before you start playing and look for traps. A trap is a position involving one piece on each side. If it is the turn of Player B to move, and if Player A is guaranteed a way to take Player B's piece after some finite number of moves, then Player A has established a trap for Player B.  For example, in the graph above, if Player A is on a, and Player B is on c, and it is Player B's turn to move, Player A has established a trap. Another trap in the game above is if it is Player B's turn to move, and Player B is on d.  It doesn't matter where Player A is. Player A is guaranteed to take player B's piece.

        Once all the traps in the game are known (and you would find them before starting to play), the static evaluation function scores a board position higher for each trap.

      3. (Even harder) Once you have found a trap, evaluate the trap in the context of the actual game. It may be that other pieces in the actual game prevent a player from carrying a trap out. (His piece may be taken in the process.) For example, in the trap above [Player A on a, Player B on c, Player B to move], if Player B also has a piece on e, he can take Player A's piece on a instead of moving his piece from c to b.

      4. (Even harder still) Look for more sophisticated traps involving multiple pieces. For example, are there board positions in which two of Player A's pieces can be guaranteed to take one of Player B's pieces? Presumably, ganging up like that allows Player A to corner Player B. But what if Player B goes after another of Player A's pieces in the mean time? Can you execute a 2-on-1 trap quickly enough so that the other player can't take one of your pieces while you are executing it?

        To evaluate a multi-piece trap for Player A, (a) compute the number of moves it takes to execute the trap, and (b) determine if all of Player A's pieces are more than that number of moves away from all of Player B's pieces. To determine (b), use the shortest-path information described in a previous homework.

         





My home page