@@ -15,10 +15,28 @@ def forward_checking(state, verbose=False):
15
15
if not basic :
16
16
return False
17
17
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
19
39
20
- raise NotImplementedError
21
-
22
40
# Now Implement forward checking + (constraint) propagation through
23
41
# singleton domains.
24
42
def forward_checking_prop_singleton (state , verbose = False ):
@@ -28,7 +46,45 @@ def forward_checking_prop_singleton(state, verbose=False):
28
46
return False
29
47
30
48
# 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
+
32
88
33
89
## The code here are for the tester
34
90
## Do not change.
@@ -70,7 +126,11 @@ def csp_solver_tree(problem, checker):
70
126
71
127
def euclidean_distance (list1 , list2 ):
72
128
# 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 )
74
134
75
135
#Once you have implemented euclidean_distance, you can check the results:
76
136
#evaluate(nearest_neighbors(euclidean_distance, 1), senate_group1, senate_group2)
@@ -79,7 +139,7 @@ def euclidean_distance(list1, list2):
79
139
## deals better with independents. Make a classifier that makes at most 3
80
140
## errors on the Senate.
81
141
82
- my_classifier = nearest_neighbors (hamming_distance , 1 )
142
+ my_classifier = nearest_neighbors (euclidean_distance , 3 )
83
143
#evaluate(my_classifier, senate_group1, senate_group2, verbose=1)
84
144
85
145
### Part 2: ID Trees
@@ -89,8 +149,35 @@ def euclidean_distance(list1, list2):
89
149
## which should lead to simpler trees.
90
150
91
151
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
93
158
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
+
94
181
#print CongressIDTree(senate_people, senate_votes, information_disorder)
95
182
#evaluate(idtree_maker(senate_votes, homogeneous_disorder), senate_group1, senate_group2)
96
183
@@ -119,22 +206,22 @@ def limited_house_classifier(house_people, house_votes, n, verbose = False):
119
206
120
207
## Find a value of n that classifies at least 430 representatives correctly.
121
208
## Hint: It's not 10.
122
- N_1 = 10
209
+ N_1 = 46
123
210
rep_classified = limited_house_classifier (house_people , house_votes , N_1 )
124
211
125
212
## Find a value of n that classifies at least 90 senators correctly.
126
- N_2 = 10
213
+ N_2 = 70
127
214
senator_classified = limited_house_classifier (senate_people , senate_votes , N_2 )
128
215
129
216
## 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
131
218
old_senator_classified = limited_house_classifier (last_senate_people , last_senate_votes , N_3 )
132
219
133
220
134
221
## 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 "
138
225
139
226
140
227
## This function is used by the tester, please don't modify it!
0 commit comments