Skip to content

Commit 30a5d81

Browse files
author
kiarashplusplus
committed
final
1 parent b88072b commit 30a5d81

13 files changed

+112
-13
lines changed

key.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Login information for the 6.034 Automated Tester
2+
3+
USERNAME="kiarash_MIT_EDU"
4+
PASSWORD="ShcD6yxK4cs4a6UUz4he"
5+
XMLRPC_URL="https://6.034.scripts.mit.edu:444/fall12/tester/xmlrpc/"
6+

lab4/classify.pyc

7.08 KB
Binary file not shown.

lab4/csp.pyc

17.2 KB
Binary file not shown.

lab4/data_reader.pyc

4.02 KB
Binary file not shown.

lab4/key.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Login information for the 6.034 Automated Tester
2+
3+
USERNAME="kiarash_MIT_EDU"
4+
PASSWORD="ShcD6yxK4cs4a6UUz4he"
5+
XMLRPC_URL="https://6.034.scripts.mit.edu:444/fall12/tester/xmlrpc/"
6+

lab4/key.pyc

306 Bytes
Binary file not shown.

lab4/lab4.py

Lines changed: 100 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,28 @@ def forward_checking(state, verbose=False):
1515
if not basic:
1616
return False
1717

18-
# Add your forward checking logic here.
18+
var = state.get_current_variable()
19+
20+
if var!=None:
21+
value = var.get_assigned_value()
22+
constraints=state.get_constraints_by_name(var.get_name());
23+
for c in constraints:
24+
if state.get_variable_by_name(c.get_variable_i_name()) is var:
25+
Y = state.get_variable_by_name(c.get_variable_j_name())
26+
for val in Y.get_domain():
27+
if c.check(state, value, val) is False:
28+
Y.reduce_domain(val)
29+
if Y.domain_size() is 0:
30+
return False
31+
elif state.get_variable_by_name(c.get_variable_j_name()) is var:
32+
Y = state.get_variable_by_name(c.get_variable_i_name())
33+
for val in Y.get_domain():
34+
if c.check(state, val, value) is False:
35+
Y.reduce_domain(val)
36+
if Y.domain_size() is 0:
37+
return False
38+
return True
1939

20-
raise NotImplementedError
21-
2240
# Now Implement forward checking + (constraint) propagation through
2341
# singleton domains.
2442
def forward_checking_prop_singleton(state, verbose=False):
@@ -28,7 +46,45 @@ def forward_checking_prop_singleton(state, verbose=False):
2846
return False
2947

3048
# Add your propagate singleton logic here.
31-
raise NotImplementedError
49+
singleton = []
50+
flagged = []
51+
52+
for var in state.get_all_variables():
53+
if var.domain_size() is 1:
54+
singleton.append(var.get_name())
55+
56+
while len(singleton) is not 0:
57+
58+
X = state.get_variable_by_name(singleton[0])
59+
value = X.get_domain()[0]
60+
61+
for c in state.get_constraints_by_name(singleton[0]):
62+
i = state.get_variable_by_name(c.get_variable_i_name())
63+
j = state.get_variable_by_name(c.get_variable_j_name())
64+
if i == X:
65+
for val in j.get_domain():
66+
if c.check(state, value, val) is False:
67+
j.reduce_domain(val)
68+
if j.domain_size() is 0:
69+
return False
70+
elif j is X:
71+
for val in i.get_domain():
72+
if c.check(state, val, value) is False:
73+
i.reduce_domain(val)
74+
if i.domain_size() is 0:
75+
return False
76+
for var in state.get_all_variables():
77+
varName=var.get_name()
78+
if (var.domain_size() is 1) and (varName not in flagged) and (varName not in singleton):
79+
singleton.append(varName)
80+
81+
flagged.append(singleton[0])
82+
83+
singleton.pop(0)
84+
85+
return True
86+
87+
3288

3389
## The code here are for the tester
3490
## Do not change.
@@ -70,7 +126,11 @@ def csp_solver_tree(problem, checker):
70126

71127
def euclidean_distance(list1, list2):
72128
# this is not the right solution!
73-
return hamming_distance(list1, list2)
129+
val=0
130+
for i in xrange(len(list1)):
131+
val+=(list1[i]-list2[i])**2
132+
133+
return math.sqrt(val)
74134

75135
#Once you have implemented euclidean_distance, you can check the results:
76136
#evaluate(nearest_neighbors(euclidean_distance, 1), senate_group1, senate_group2)
@@ -79,7 +139,7 @@ def euclidean_distance(list1, list2):
79139
## deals better with independents. Make a classifier that makes at most 3
80140
## errors on the Senate.
81141

82-
my_classifier = nearest_neighbors(hamming_distance, 1)
142+
my_classifier = nearest_neighbors(euclidean_distance, 3)
83143
#evaluate(my_classifier, senate_group1, senate_group2, verbose=1)
84144

85145
### Part 2: ID Trees
@@ -89,8 +149,35 @@ def euclidean_distance(list1, list2):
89149
## which should lead to simpler trees.
90150

91151
def information_disorder(yes, no):
92-
return homogeneous_disorder(yes, no)
152+
yesList = []
153+
154+
for y in yes :
155+
if y not in yesList:
156+
yesList.append(y)
157+
d1 = 0.0
93158

159+
for yClass in yesList:
160+
f = yes.count(yClass)/float(len(yes))
161+
d1 += -1*(f * math.log(f)/float(math.log(2)))
162+
163+
164+
noList = []
165+
166+
for n in no:
167+
if n not in noList:
168+
noList.append(n)
169+
170+
d2 = 0.0
171+
172+
for nClass in noList:
173+
f = no.count(nClass)/float(len(no))
174+
d2 += -1*(f * math.log(f)/float(math.log(2)))
175+
176+
tLen = float(len(no)) + float(len(yes))
177+
178+
return (len(yes)/tLen)*d1 + (len(no)/tLen)*d2
179+
180+
94181
#print CongressIDTree(senate_people, senate_votes, information_disorder)
95182
#evaluate(idtree_maker(senate_votes, homogeneous_disorder), senate_group1, senate_group2)
96183

@@ -119,22 +206,22 @@ def limited_house_classifier(house_people, house_votes, n, verbose = False):
119206

120207
## Find a value of n that classifies at least 430 representatives correctly.
121208
## Hint: It's not 10.
122-
N_1 = 10
209+
N_1 = 46
123210
rep_classified = limited_house_classifier(house_people, house_votes, N_1)
124211

125212
## Find a value of n that classifies at least 90 senators correctly.
126-
N_2 = 10
213+
N_2 = 70
127214
senator_classified = limited_house_classifier(senate_people, senate_votes, N_2)
128215

129216
## Now, find a value of n that classifies at least 95 of last year's senators correctly.
130-
N_3 = 10
217+
N_3 = 25
131218
old_senator_classified = limited_house_classifier(last_senate_people, last_senate_votes, N_3)
132219

133220

134221
## The standard survey questions.
135-
HOW_MANY_HOURS_THIS_PSET_TOOK = ""
136-
WHAT_I_FOUND_INTERESTING = ""
137-
WHAT_I_FOUND_BORING = ""
222+
HOW_MANY_HOURS_THIS_PSET_TOOK = "7"
223+
WHAT_I_FOUND_INTERESTING = "knn concept"
224+
WHAT_I_FOUND_BORING = "APIs are a little tricky"
138225

139226

140227
## This function is used by the tester, please don't modify it!

lab4/lab4.pyc

5.55 KB
Binary file not shown.

lab4/map_coloring_csp.pyc

2.42 KB
Binary file not shown.

lab4/mat_vec_ops.pyc

2.57 KB
Binary file not shown.

lab4/moose_csp.pyc

3.75 KB
Binary file not shown.

lab4/tester.pyc

10.5 KB
Binary file not shown.

lab4/tests.pyc

7.44 KB
Binary file not shown.

0 commit comments

Comments
 (0)