Analyzing geography
Let's use logic programming to build a solver to analyze geography. In this problem, we will specify information about the location of various states in the US and then query our program to answer various questions based on those facts and rules. The following is a map of the US:
You have been provided with two text files named adjacent_states.txt
and coastal_states.txt
. These files contain the details about which states are adjacent to each other and which states are coastal. Based on this, we can get interesting information like What states are adjacent to both Oklahoma and Texas? or Which coastal state is adjacent to both New Mexico and Louisiana?
Create a new Python file and import the following:
from logpy import run, fact, eq, Relation, var
Initialize the relations:
adjacent = Relation() coastal = Relation()
Define the input files to load the data from:
file_coastal = 'coastal_states.txt' file_adjacent = 'adjacent_states.txt'
Load the data:
# Read the file containing...