






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
This assignment is for Artificial Intelligence course. It was assigned by Madam Amrita Ahuja at Central University of Jammu and Kashmir. Its main points are: Binary, Constraints, Satisfaction, Csp, Impleme, Ntation, Dfs, Singleton, Propagation, Reduction, Algorithm, Domain
Typology: Exercises
1 / 10
This page cannot be seen from the preview
Don't miss anything!







To work on this problem set, you will need to get the code.
This lab has two parts. The first part is on CSPs and the second part is on learning algorithms, specifically KNN and decision trees.
In this portion of Lab 4, you are to complete the implementation of a general constraint satisfaction problem solver. You'll test it on problems we've worked out by hand in class.
We have provided you a basic CSP implementation in csp.py. The implementation has the Depth-first-search already completed. It even has a basic built in constraint checker. So it will produce the search trees of the kind for DFS w/ back tracking with basic constraint checking.
However, it doesn't do forward checking or forward checking + singleton propagation!
So your job is to complete:
forward_checking(state):
and
forward_checking_prop_singleton(state):
in the file lab4.py. Here state is an instance of CSPState an object that keep track of the current variable assignments and domains. These functions are called by the Search algorithm at every node in the search tree. These functions should return False at points at which the Domain Reduction Algorithm would backtrack, and True otherwise (i.e. continue extending).
As a hint, here is the (unrefined) pseudocode for the two algorithms.
If you get a state with no current variable assignment (at the Root of the search tree) then you should just True, since forward checking could only be applied when there is some variable assignment.
These are some useful functions defined in csp.py that you should use in your code to implement the above algorithms:
CSPState: representation of one of the many possible search states in the CSP problem.
Variable: representation of a variable in these problems.
will return the search tree for DFS with constraint checking. When you have finished your implementation, running python moose_csp.py fc or python moose_csp.py fcps should return the correct search trees under forward checking and forward checking with singleton propagation.
Similarly
Running:
python map_coloring_csp.py [dfs|fc|fcps]
Should return the expected search trees for the B,Y,R, state coloring problem from the 2nd Quiz in 2006.
There are also other fun solved CSP problems in the directory that you can test and play around with. You can submit your own unique solution to an interesting CSP problem to get extra credit!
As extra credit, try to follow the code in moose_csp.py or map_coloring_csp.py, and implement a problem() function that returns a CSP instance for a problem of your own choosing.
You may do one of the problems from past quizzes: the 2009 Time Traveler scheduling problem, the 2010 Jigsaw puzzle question or the phoneme-syllabification problem (from tutorial). Alternately, you may implement something that you find useful or interesting, ideas include: scheduling classes, seating guests for a wedding or dinner party (to maximize harmony), solving crypt-arithmetic puzzles, the 8-queens problem, or crossword puzzles.
You may also try to extend csp.py. For instance, you can add ability to find an optimal solution rather than just a constraint-satisfying solution (i.e. replace DFS with one of the optimal searches we've learned). Or you can add support for multi-variable constraints, and make the code solve the Max-flow problem from the 2006 final.
When you've succeeded in implementing such a problem or extension, send your working code to the TAs. Your reward: either a 1-to-3-day extension (depending on difficulty) on one of the previous or future labs, possibly erasing any late penalties. Or if your lab grade is already perfect, praise and recognition from the 6.034 staff.
Now for something completely different. Learning!
During Obama's visit to MIT, you got a chance to impress him with your analytical thinking. Now, he has hired you to do some political modeling for him. He seems to surround himself with smart people that way.
He takes a moment out of his busy day to explain what you need to do. "I need a better way to tell which of my plans are going to be supported by Congress," he explains. "Do you think we can get a model of Democrats and Republicans in Congress, and which votes separate them the most?"
"Yes, we can!" You answer.
You acquire the data on how everyone in the previous Senate and House of Representatives voted on every issue. (These data are available in machine-readable form via voteview.com. We've included it in the lab directory, in the files beginning with H110 and S110.)
data_reader.py contains functions for reading data in this format.
read_congress_data("FILENAME.ord") reads a specially-formatted file that gives information about each Congressperson and the votes they cast. It returns a list of dictionaries, one for each member of Congress, including the following items:
To make sense of the votes, you will also need information about what they were voting on. This is provided by read_vote_data("FILENAME.csv"), which returns a list of votes in the same order that they appear in the Congresspeople's entries. Each vote is represented a dictionary of information, which you can convert into a readable string by running vote_info(vote).
The lab file reads in the provided data, storing them in the variables senate_people, senate_votes, house_people, and house_votes.
You decide to start by making a nearest-neighbors classifier that can tell Democrats apart from Republicans in the Senate.
We've provided a nearest_neighbors function that classifies data based on training data and a distance function. In particular, this is a third-order function:
So far you've classified Democrats and Republicans, but you haven't created a model of which votes distinguish them. You want to make a classifier that explains the distinctions it makes, so you decide to use an ID-tree classifier.
idtree_maker(votes, disorder_metric) is a third-order function similar to nearest_neighbors. You initialize it by giving it a list of vote information (such as senate_votes or house_votes) and a function for calculating the disorder of two classes. It returns a classifier factory that will produce instances of the CongressIDTree class, defined in classify.py, to distinguish legislators based on their votes.
The possible decision boundaries used by CongressIDTree are, for each vote:
(These are different because it is possible for a legislator to abstain or be absent.)
You can also use CongressIDTree directly to make an ID tree over the entire data set.
If you print a CongressIDTree, then you get a text representation of the tree. Each level of the ID tree shows the minimum disorder it found, the criterion that gives this minimum disorder, and (marked with a +) the decision it makes for legislators who match the criterion, and (marked with a -) the decision for legislators who don't. The decisions are either a party name or another ID tree. An example is shown in the section below.
You start by making an ID tree for the entire Senate. This doesn't leave you anything to test it on, but it will show you the votes that distinguish Republicans from Democrats the most quickly overall. You run this (which you can uncomment in your lab file):
print CongressIDTree(senate_people, senate_votes, homogeneous_disorder)
The ID tree you get here is:
Disorder: - Yes on S.Con.Res. 21: Kyl Amdt. No. 583; To reform the death tax by setting the exemption at $5 million per estate, indexed for inflation, and the top death tax rate at no more than 35% beginning in 2010; to avoid subjecting an estimated 119,200 families, family businesses, and family farms to the death tax each and every year; to promote continued economic growth and job creation; and to make the enhanced teacher deduction permanent.:
Yes on H.R. 1585: Feingold Amdt. No. 2924; To safely redeploy United States troops from Iraq.:
Some things that you can observe from these results are:
You should be able to reduce the depth and complexity of the tree, by changing the disorder metric from the one that looks for the largest homogeneous group to the information-theoretical metric described in lecture.
You can find this formula on page 429 of the textbook.
Example:
information_disorder(["Democrat", "Democrat", "Democrat"], ["Republican", "Republican"]) => 0. information_disorder(["Democrat", "Republican"], ["Republican", "Democrat"]) => 1.
Please just take it that you're to find the n required to classify 90 house members. It will still be instructive to find the smallest required value for the senate, but please leave the variable names and tests as they are.