0% found this document useful (0 votes)
103 views7 pages

Lab Exercises - Logic in AI

This document provides instructions for students on installing Python libraries and completing exercises on logic and artificial intelligence. It explains how to install the sympy and logpy libraries. It then describes two exercises - the first involves loading family relationship data from a JSON file and querying it, while the second loads state adjacency and coastline data from text files and performs queries on it. Students are asked to download necessary files, write Python code importing libraries and defining rules, load and query the data, and submit any additional work.

Uploaded by

Ham Zar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
103 views7 pages

Lab Exercises - Logic in AI

This document provides instructions for students on installing Python libraries and completing exercises on logic and artificial intelligence. It explains how to install the sympy and logpy libraries. It then describes two exercises - the first involves loading family relationship data from a JSON file and querying it, while the second loads state adjacency and coastline data from text files and performs queries on it. Students are asked to download necessary files, write Python code importing libraries and defining rules, load and query the data, and submit any additional work.

Uploaded by

Ham Zar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Faculty of Technology

University of Sunderland

Artificial Intelligence - Logic


Introduction
We have looked at some of the logic mechanisms used in AI in our lecture, now let’s see if we can
look at some examples of this in the code.

Python has a lot of libraries, but not all of them are installed with Anaconda, so from time to time
you are going to need to add libraries to your python coding environment and this can sometimes
prove to be challenging. Luckily the code we are going to look at this week relies on some obscure
libraries that are not always easy to install, so I got to experience some real pain on your behalf
setting them up – I will of course let you know the fix, so you don’t have to go through that stress.

We need to install 2 libraries, these are:

1. sympy
2. logpy

We can deal with the easy one first, run Anaconda Prompt from your start menu and you will get
this:

Type in the following command: pip install sympy and hit return. The package should install and
return to the command prompt after a few seconds.

The second library caused me some trouble, but it’s worth trying in the same way, just in case it was
just my computer. So, once you get back to the prompt, type in: pip install logpy this is what I got
when I tried to run this:

CET313 Artificial Intelligence – Kate MacFarlane


Faculty of Technology
University of Sunderland

If you don’t get this and the install completes successfully, then you are done, and you can proceed
to trying out the code exercises for this week.

If you got the same fault line as me, then from the prompt type pip install logic and hit return. Once
this completes go to canvas and download the logpy-master zip file. Once downloaded unzip it and
copy the unzipped folder to C:\users\<your username> directory.

Return to the Anaconda Prompt window and type cd logpy-master and hit return. Then type python
setup.py install and hit return.

Once this is done successfully you should have all you need to start working on todays examples. We
are going to work on two similar examples today, the first will look at family relationships and how
we would input these for interrogation in Python. The other will look at states and geography. We
will get our first look at interacting with external files of differing types and begin to see how we help
the computer to see things as we do.

Exercise 1
For the first example you need to download the JSON file relationships.json from Canvas and save it
into C:\users\<your username> directory, then run Anaconda and open a new file in Jupyter
notebook. Rename this file to family and type the following:

This will set the imports needed for us to run queries on the information in our file.

Next we will define some rules for relationships:

CET313 Artificial Intelligence – Kate MacFarlane


Faculty of Technology
University of Sunderland

Next we access our file and set parental relationships:

With that now setup, we can run a set of further queries on the information using the relationships
we defined earlier in the code.

CET313 Artificial Intelligence – Kate MacFarlane


Faculty of Technology
University of Sunderland

You can run your program now to check that all is as it should be, but there are more queries below
for you to run which will test all the definitions and relationships we have set.

You should be able to see working outputs when you run your code. Try editing the json file and
adding more defined relationships to your code. Upload any extension work to your ePortfolio.

CET313 Artificial Intelligence – Kate MacFarlane


Faculty of Technology
University of Sunderland

Exercise 2
For this example, you will need to download the 2 text files from Canvas, adjacent_states.txt and
coastal_states.txt, save them into C:\users\<your username> directory, then run Anaconda and
open a new file in Jupyter notebook. Rename this file to states and type the following import
command:

This will set the imports needed for us to run queries on the information in our file.
Then add:

To define the relationships.

Next, we specify our files:

CET313 Artificial Intelligence – Kate MacFarlane


Faculty of Technology
University of Sunderland

Then we read the file contents and add the information they contain to the fact base for our
program:

Now we can initialise the variables we need to be able to perform some queries:

We are finally ready to run some queries on our data – try the following:

CET313 Artificial Intelligence – Kate MacFarlane


Faculty of Technology
University of Sunderland

You should be able to see working outputs when you run your code. Try adding more queries to your
code. Upload any extension work to your ePortfolio.

CET313 Artificial Intelligence – Kate MacFarlane

You might also like