You are on page 1of 9

JINNAH UNIVERSITY FOR WOMEN

Department of Computer Science & Software Engineering


CSS 4871 – ARTIFICIAL INTELLIGENCE
LAB ASSIGNMENT
Max marks: 20
Instructions:
 The assignment must be submitted on JUW LMS. Email submission will not be accepted.
 Each student should solve the assignment individually.
 You are advised to go through the related topic before solving the assignment.
 Make your work clear and understandable.
 Use references where necessary.

Problem 1: Family Tree in Prolog [2 marks]


Sara and Ali got married and have two children, one is male and second is female. Ajmal is the
grandfather of both the kids and he is also the father of Sara. Whereas Najma is the grandmother
and she is the mother of Ali. Mr Waqas and his wife Naila are the uncle and aunty of the kids
and Mrs Waqas is the sister in law of Sara. Mr Abid is the brother in law of Ali and Faria is his
wife is the aunty of kids.

male(ali).
male(ajmal).
male(waqas).
male(abid).
male(child1).

female(sara).
female(najma).
female(naila).
female(faria).
female(child2).

parent(sara, ajmal).
parent(ali, najma).
parent(child1, ali).
parent(child1, sara).
parent(child2, ali).
parent(child2, sara).

married(ali, sara).
married(sara, ali).
married(waqas, naila).
married(naila, waqas).
married(abid, faria).
Page 1|9
married(faria, abid).

Also write production rules for the following queries:


 What is the relationship of Faria and Sara.

sister_in_law_of(SInLaw,SiblingInLaw):-
married(Person,SiblingInLaw),sister_of(SInLaw,Person).
sister_in_law_of(SInLaw,SiblingInLaw):-brother_of(Brother,SiblingInLaw),married(Brother,
SInLaw).
sister_in_law_of(SInLaw,SiblingInLaw):-married(Person,SiblingInLaw),
brother_of(Brother,Person), married(SInLaw,Brother).

 What is the condition for the male kid to be the grandson.

descendent_of(Descendent,Person) :- parent_of(Person,Descendent).
descendent_of(Descendent,Person):-parent_of(MaleChild,Descendent),
descendent_of(MaleChild,Person).

 Explain the relationship between Ajmal and Ali.

father_in_law_of(ChildInLaw, FInLaw) :- male(FInLaw), married(ChildInLaw, Child),


parent_of(Child, FInLaw).

 Explain the relationship between Sara and Najma.


mother_in_law_of(ChildInLaw, MInLaw) :- female(MInLaw), married(ChildInLaw, Child),
parent_of(Child, MInLaw).

Problem 2: Sorting in Prolog [2 marks]


Design a Prolog implementation of insertion sort for lists of numbers. Test your program using
various sample runs.
insert_sort(List,Sorted):-i_sort(List,[],Sorted).
i_sort([],Acc,Acc).
i_sort([H|T],Acc,Sorted):-insert(H,Acc,NAcc),i_sort(T,NAcc,Sorted).

insert(X,[Y|T],[Y|NT]):-X>Y,insert(X,T,NT).
insert(X,[Y|T],[X,Y|T]):-X=<Y.
insert(X,[],[X]).

Page 2|9
Problem 3: Recursive functions in LISP [3 marks]
This problem involves writing Lisp code recursively (what else is there?).
Write two functions, each taking two arguments. One is name BEFORE and one is named
AFTER. The first argument is an atom and the second is a list in both cases. You may assume
that the atom occurs in the list. BEFORE Returns a list containing the part of the list before the
first occurrence of the atom; AFTER returns the part of the list coming after the first occurrence
of the atom.
Here are some examples:

USER(1): (BEFORE ’here ’(thats neither here nor there))


(thats neither)
USER(2): (AFTER ’here ’(thats neither here nor there))
(nor there)
You may assume that the arguments will match these specifications. NOTE: There are NON
recursive ways to write these functions using the built-in functions of Lisp (e.g., MEMBER).
THAT’S NOT WHAT WE WANT YOU TO DO.

Problem 4: Expert System Programming [4 marks]


Write a program in which user has been asked about their temperature, their daily diet calorie amount and
workout hours and then in output show how much the person is fit (You can create your own knowledge
space)

Problem 5: Real World Planning Problem [3 marks]


There are five workers (A, B, C, D & E) and four tasks (numbered 1-4).
Page 3|9
The costs of assigning workers to tasks are shown in the following table.
1 2 3 4

A 90 80 75 70

B 35 85 55 65

C 125 95 90 95

D 45 110 95 115

E 50 100 90 100

The problem is to assign each worker to at most one task, with no two workers performing the
same task, while minimizing the total cost. Since there are more workers than tasks, one worker
will not be assigned a task.

Problem 6: Decision Trees [3 marks]


Create a decision tree with tic tac toe for 2 players.

// TicTacToeLight.java

import ch.aplu.jgamegrid.*;
import ch.aplu.tcp.*;
import java.awt.Color;
import javax.swing.JOptionPane;

public class TicTacToeLight extends GameGrid
  implements GGMouseListener, TcpNodeListener
{
  private String sessionID = "ama&19td&ap";
  private final String nickname = "tic";
  private TcpNode tcpNode = new TcpNode();
  private boolean isMyMove = false;
  private char mySign;
  private char teammateSign;

  public TicTacToeLight()
  {
    super(3, 3, 80, Color.blue, null, false);
    setBgColor(Color.lightGray);
    setTitle("TicTacToe Light");
    addStatusBar(30);
    addMouseListener(this, GGMouse.lClick);
Page 4|9
    tcpNode.addTcpNodeListener(this);
    show();
    connect();
  }

  public boolean mouseEvent(GGMouse mouse)
  {
    if (!isMyMove)
      return true;

    Location loc = toLocationInGrid(mouse.getX(), mouse.getY());
    if (getOneActorAt(loc) != null)
    {
      setStatusText("Cell occupied!");
      return true;
    }

    addMark(mySign, loc);
    tcpNode.sendMessage("" + loc.x + loc.y);  // Send move
    if (isOver())
      return true;
    setStatusText("Wait for teammate's move.");
    isMyMove = false;
    return true;
  }

  private void connect()
  {
    String id = requestEntry("Enter room name (more than 3 characters):");
    sessionID = sessionID + id;
    tcpNode.connect(id, nickname);
  }

  public void messageReceived(String sender, String text)
  {
    int x = text.charAt(0) - 48; // We get ASCII code of number
    int y = text.charAt(1) - 48;

    Location loc = new Location(x, y);
    addMark(teammateSign, loc);
    if (isOver())
      return;
    isMyMove = true;
    setStatusText("It's your move.");

Page 5|9
  }

  private void addMark(char sign, Location loc)
  {
    if (sign == 'X')
      addActor(new MarkX(), loc);
    else
      addActor(new MarkO(), loc);
  }

  private boolean isBoardFull()
  {
    return getActors().size() == 9;
  }

  private void gameOver(String reason)
  {
    setStatusText(reason);
    isMyMove = false;
    addActor(new Actor("sprites/gameover.gif"), new Location(1, 1));
  }

  private char getMark(int i, int k)
  {
    Actor actor = getOneActorAt(new Location(i, k));
    if (actor == null)
      return ' ';
    else
      return (actor instanceof MarkX) ? 'X' : 'O';
  }

  private boolean isOver()
  {
    // Convert board state into string pattern
    StringBuffer pattern = new StringBuffer();
    // Horizontal
    for (int i = 0; i < 3; i++)
    {
      for (int k = 0; k < 3; k++)
      {
        pattern.append(getMark(i, k));
      }
      pattern.append(',');  // Separator
    }

Page 6|9
    // Vertical
    for (int k = 0; k < 3; k++)
    {
      for (int i = 0; i < 3; i++)
      {
        pattern.append(getMark(i, k));
      }
      pattern.append(',');
    }
    // Diagonal
    for (int i = 0; i < 3; i++)
      pattern.append(getMark(i, i));
    pattern.append(',');
    for (int i = 0; i < 3; i++)
      pattern.append(getMark(i, 2 - i));

    if (pattern.toString().contains("XXX"))
    {
      gameOver("Game over. " + (mySign == 'X' ? "You won " : "Teamate won"));
      return true;
    }
    if (pattern.toString().contains("OOO"))
    {
      gameOver("Game over. " + (mySign == 'O' ? "You won " : "Teamate won"));
      return true;
    }
    if (isBoardFull())
    {
      gameOver("Game over. It's a draw");
      return true;
    }
    return false;
  }

  public void nodeStateChanged(TcpNodeState state)
  {
  }

  public void statusReceived(String text)
  {
    if (text.contains("In session:--- (0)"))  // we are first player
    {
      setStatusText("Connected. Wait for teammate.");
      mySign = 'O';

Page 7|9
      teammateSign = 'X';
    }
    else if (text.contains("In session:--- (1)")) // we are second player
    {
      setStatusText("It's your move.");
      mySign = 'X';
      teammateSign = 'O';
      isMyMove = true;  // Second player starts
    }
    else if (text.contains("In session:--- ")) // we are next player
    {
      setStatusText("Game in progress. Terminating now...");
      TcpTools.delay(4000);
      System.exit(0);
    }
  }

  private String requestEntry(String prompt)
  {
    String entry = "";
    while (entry.length() < 3)
    {
      entry = JOptionPane.showInputDialog(null, prompt, "");
      if (entry == null)
        System.exit(0);
    }
    return entry.trim();
  }

  public static void main(String[] args)
  {
    new TicTacToeLight();
  }
}

Problem 7: Min-Max problem using Python [3 marks]


Solve the Min-Max problem for the following question:

Page 8|9
import numpy as np

print(np.max(np.min(np.array([input().split() for _ in range(int(input().split()


[0]))],int),axis=1)))

Page 9|9

You might also like