Lab Exercises - Logic in AI
Lab Exercises - Logic in AI
University of Sunderland
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.
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:
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.
With that now setup, we can run a set of further queries on the information using the relationships
we defined earlier in the code.
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.
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:
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:
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.