def dd3(self, prop, c, r, n): if len(c)<=1: return c l = len(c)/n ci = [c[i-l:min(i, len(c))] for i in range(l, len(c)+l, l)] if len(ci)>n: ci[-2]+=ci[-1] ci = ci[:-1] ti = [self._subtest(prop, e+r) for e in ci] # found in ci for i, t_res in enumerate(ti): if t_res==FAIL: return self.dd3(prop, ci[i], r, 2) ici = [[e for e in c if not e in _c+r] for _c in ci] iti = [self._subtest(prop, e+r) for e in ici] # inference for i in range(len(ti)): if ti[i]==OK and iti[i]==OK: return self.dd3(prop, ci[i], ici[i]+r, 2) + self.dd3(prop, ici[i], ci[i]+r, 2) # preference for i in range(len(ti)): if ti[i]==UNDEF and iti[i]==OK: return self.dd3(prop, ci[i], ici[i]+r, 2) if n<len(c): #cc = cut(c, reduce(cut, [tmp_c for i, tmp_c in enumerate(ici) if iti[i]==FAIL])) #rr = r + reduce(operator.add, [ci[i] for i in range(len(ci)) if ti[i]==OK]) nn = min(len(c), 2*n) return self.dd3(prop, c, r, nn) else: tmp = [ici[i] for i in range(len(ici)) if iti[i]==FAIL] if not tmp: return [] else: return intersect(c, reduce(intersect, [ici[i] for i in range(len(ici)) if iti[i]==FAIL]))
def getAllSupport(list_AttributeValuePairs) : all_supports = list() for i in list_AttributeValuePairs : if not all_supports : all_supports = i.getSupport() else : all_supports = intersect(all_supports, i.getSupport()) return(all_supports)
def collides(self, a=(0,0), b=(0, 0, 0, 0)): angle = math.atan2(self.dx, self.dy) d = util.intersect(angle, a, b) if d <= 0: return self.t if d < self.t: self.t = d return self.t
def calculate_collisions(self, regions): self.collisions = [] for region in regions: for m in self.movements: intersection = intersect((m.start, m.end), region) if intersection != None: time = m.time + distance(m.start, intersection) / m.speed collision = {'time': time, 'region': region} self.collisions.append(collision) break
def intersect(self, region): enter, exit = None, None for m in self.movements: intersection = intersect(m, region) if intersection != None: _enter = m.time_for(intersection[0]) _exit = m.time_for(intersection[1]) if enter == None or _enter < enter: enter = _enter if exit == None or _exit > exit: exit = _exit return (enter, exit)
def add_seas_to(lm): lm.fill_triangle_hash() _seed_bays(lm) removal_queue = set() persistent_lines = set() new_bays = set() _fill_sea_terr_adjacency_lists(lm, symmetric=False) _remove_overlapping_bays(lm) for terr in lm.sea_terrs: if terr not in removal_queue: for terr2 in lm.sea_terrs: if terr2 not in removal_queue and terr2 != terr: if terr.size == terr2.size: terr2.size += 1 if util.intersect(terr.line.a, terr.line.b, terr2.line.a, terr2.line.b): if check_intersections(lm, terr.line.a, terr2.line.b): new_line = Line(terr.line.a, terr2.line.b, terr.line.left, terr2.line.right) new_bay = SeaTerr(new_line) new_bay.adjacencies = terr.adjacencies + terr2.adjacencies new_bay.adjacencies = list(set(new_bay.adjacencies)) new_bay.size = len(new_bay.adjacencies) new_bays.add(new_bay) removal_queue.add(terr) removal_queue.add(terr2) elif terr.size < terr2.size: removal_queue.add(terr) elif terr.size > terr2.size: removal_queue.add(terr2) else: removal_queue.add(terr) else: if terr.line.left == terr2.line.left or terr.line.right == terr2.line.right: if terr.size < terr2.size: removal_queue.add(terr) else: removal_queue.add(terr2) lm.sea_terrs.update(new_bays) lm.sea_terrs = lm.sea_terrs.difference(removal_queue) _bump_out_borders(lm) _fill_sea_terr_adjacency_lists(lm, symmetric=True)
def intersect(self, e): """Return intersection between two edges (aside from end-points).""" if self.head() == e.head() or self.head() == e.tail(): return None if self.tail() == e.head() or self.tail() == e.tail(): return None # compute intersection of two line segments using x,y coords pt = intersect(self.head().x(), self.head().y(), self.tail().x(), self.tail().y(), e.head().x(), e.head().y(), e.tail().x(), e.tail().y()) if pt is None: return None return Point(pt[0], pt[1])
def make_seas(self): if self.verbose: print 'finding bays...' max_seeks = len(self.outside_lines)/3 start_line = self.outside_lines.pop() self.outside_lines.add(start_line) line = start_line.right bay_starts = [] #Find seed lines while line != start_line: if self.angle_between_line_and_next(line) < math.pi*0.4: bay_starts.append(line) line = line.right #Find bays for all seed lines for line in bay_starts: line_left = line line_right = line best_line_left = None best_line_right = None i = 0 last_i = 0 while i < max_seeks: i += 1 line_right = line_right.right test_line = Line(line_left.a, line_right.a) if self.check_intersections(test_line.a, test_line.b) and \ self.check_point(test_line.midpoint): best_line_left, best_line_right = line_left, line_right last_i = i line_left = line_left.left test_line = Line(line_left.a, line_right.a) if self.check_intersections(test_line.a, test_line.b) and \ self.check_point(test_line.midpoint): best_line_left, best_line_right = line_left, line_right last_i = i if best_line_left != None: left = best_line_left right = best_line_right last_i_2 = 0 worked = True while worked: worked = False i = 0 while i < 5: left = left.left test_line = Line(left.a, best_line_right.a) if self.check_intersections(test_line.a, test_line.b) \ and self.check_point(test_line.midpoint): best_line_left = left last_i_2 = last_i + i worked = True i += 1 last_i = last_i_2 i = 0 while i < 5: right = right.right test_line = Line(best_line_left.a, right.a) if self.check_intersections(test_line.a, test_line.b) \ and self.check_point(test_line.midpoint): best_line_right = right last_i_2 = last_i + i worked = True i += 1 new_line = Line( best_line_left.a, best_line_right.a, best_line_left.left, best_line_right ) new_bay = SeaTerr(new_line) self.sea_terrs.add(new_bay) removal_queue = set() persistent_lines = set() for terr in self.sea_terrs: moving_line = terr.line.left.right while moving_line != terr.line.right.left: for terr2 in moving_line.territories: if not terr2 in terr.adjacencies: terr.adjacencies.append(terr2) if not terr in terr2.adjacencies: terr2.adjacencies.append(terr) moving_line = moving_line.right terr.size = len(terr.adjacencies) new_bays = set() for terr in self.sea_terrs: if terr not in removal_queue: moving_line = terr.line.left.right while moving_line != terr.line.right.left: moving_line = moving_line.right for terr2 in self.sea_terrs: if terr2 != terr: if moving_line == terr2.line.left or \ moving_line == terr2.line.right: if terr2.size < terr.size: removal_queue.add(terr2) for terr2 in self.sea_terrs: if terr2 not in removal_queue and terr2 != terr: if terr.size == terr2.size: terr2.size += 1 if util.intersect( terr.line.a, terr.line.b, terr2.line.a, terr2.line.b ): if self.check_intersections( terr.line.a, terr2.line.b ): new_line = Line( terr.line.a, terr2.line.b, terr.line.left, terr2.line.right ) new_bay = SeaTerr(new_line) new_bay.adjacencies = terr.adjacencies + \ terr2.adjacencies new_bay.adjacencies = list(sets.Set( new_bay.adjacencies )) new_bay.size = len(new_bay.adjacencies) new_bays.add(new_bay) removal_queue.add(terr) removal_queue.add(terr2) elif terr.size < terr2.size: removal_queue.add(terr) elif terr.size > terr2.size: removal_queue.add(terr2) else: removal_queue.add(terr) else: if terr.line.left == terr2.line.left or \ terr.line.right == terr2.line.right: if terr.size < terr2.size: removal_queue.add(terr) else: removal_queue.add(terr2) self.sea_terrs.update(new_bays) self.sea_terrs = self.sea_terrs.difference(removal_queue) removal_queue = set() for terr in self.sea_terrs: for terr2 in self.sea_terrs: intersect = False if terr != terr2: s1 = set(terr.adjacencies) s2 = set(terr2.adjacencies) if terr.line.a == terr2.line.a \ or terr.line.b == terr2.line.b \ or (s1 & s2): if terr.size == terr2.size: terr2.size += 1 if terr.size > terr2.size: removal_queue.add(terr2) if terr.size < 3: removal_queue.add(terr) self.sea_terrs = self.sea_terrs.difference(removal_queue) for terr in self.sea_terrs: new_x, new_y = terr.x, terr.y new_x += math.cos(terr.line.normal)*20.0 new_y += math.sin(terr.line.normal)*20.0 new_point = Point(new_x, new_y) nl1 = Line(terr.line.a, new_point, terr.line.left) nl2 = Line(new_point, terr.line.b, nl1, terr.line.right) nl1.right = nl2 terr.lines = [nl1, nl2] line2 = terr.line.left.right while line2 != terr.line.right: line2.color = (1,1,1,1) line2 = line2.right
def check_intersections(self, a, b): if not self.check_collisions: return True for line in self.outside_lines: if util.intersect(a, b, line.a, line.b): return False return True
def test_almost_same_start(self): line1 = [0, 0, 2, 2] line2 = [0.1, 0, 1.9, 2] self.assertTrue(util.intersect(line1, line2))
def test_easy(self): line1 = [0, 0, 2, 2] line2 = [2, 0, 0, 2] self.assertTrue(util.intersect(line1, line2))
def check_intersections(lm, a, b): for line in lm.outside_lines: if util.intersect(a, b, line.a, line.b): return False return True
def getRulesFromData1(decision_table, list_nominal, list_la): # AttributeValuePairs list_attributeValuePairs = getAttributeValueParis(decision_table, list_nominal) #print(list_attributeValuePairs[1].getIdx()) # Rules の初期設定 rules = list() # 各クラスごとにRuleを求める for i in list_la : #print("Deicision Class : " + str(i)) list_concept = list_la[i] #print("list_concept : " + str(sorted(list_concept))) # cocept が空ならStop exitEmptyList(list_concept) # 初期設定( G = B ) list_uncoveredConcept = list_concept[:] ## G が空じゃないならループを続ける while list_uncoveredConcept : # Rule の初期設定 rule = Rule() # T := 空集合 list_T = list() # TGの候補集合を求める list_TG = [avp for avp in list_attributeValuePairs if intersect(list_uncoveredConcept, avp.getSupport())] # ruleを求める #count = 0 while not list_T or not isSuperList(getAllSupport(list_T), list_concept) : bestAttributeValuePair = None list_cover_num = [ len(intersect(list_uncoveredConcept, avp.getSupport())) for avp in list_TG ] #print("list_cover_num:" + str(list_cover_num)) list_TG_max = [ avp for avp in list_TG if len(intersect(list_uncoveredConcept, avp.getSupport())) == max(list_cover_num)] #print("list_TG_max Number:" + str(len(list_TG_max))) if len(list_TG_max) == 1 : bestAttributeValuePair = list_TG_max[0] else : minValue = min([len(avp.getSupport()) for avp in list_TG_max ]) #print("minValue:" + str(minValue)) list_TG_min = [ avp for avp in list_TG_max if len(avp.getSupport()) == minValue] #print("list_TG_min Number:" + str(len(list_TG_min))) bestAttributeValuePair = list_TG_min[0] # T : T U {t} のところ list_T.append(bestAttributeValuePair) #print("list_T : " + str(getAllSupport(list_T))) #print("best Attribute Pair : "+str(bestAttributeValuePair.getSupport())) # G := [t] ∩ G のところ list_uncoveredConcept = intersect(bestAttributeValuePair.getSupport(), list_uncoveredConcept) #print("list_uncoveredConcept : " + str(list_uncoveredConcept)) # TG の更新 T(G) := {t : t ^ G}のところ list_TG = list() list_TG = [avp for avp in list_attributeValuePairs if intersect(list_uncoveredConcept, avp.getSupport())] #print("list_TG : " + str(list_TG)) #print("list_TG : " + str(len(list_TG))) #print("list_T : " + str(list_T)) #print("list_T : " + str(len(list_T))) # T(G) := T(G) - T list_TG = setdiff(list_TG, list_T) #print("list_TG : " + str(len(list_TG))) # list_T から不要なものを取り除く for avp in list_T : list_T_back = list_T[:] list_T_back.remove(avp) if isSuperList(getAllSupport(list_T_back), list_concept) and list_T_back : list_T.remove(avp) # list_T から ruleを作成して、rulesに追加 rule.setIdx(getAllIdx(list_T)) rule.setValue(getAllValue(list_T)) rule.setConsequent(i) rule.setSupport(getAllSupport(list_T)) rules.append(rule) # Gの更新(G := B - [T] のところ) list_uncoveredConcept = list_concept[:] for r in rules : list_uncoveredConcept = setdiff(list_uncoveredConcept, r.getSupport()) # test #del list_uncoveredConcept[0] #print("The Number of uncovered Concept : " + str(len(list_uncoveredConcept))) # 最後のスクリーニング for r in rules: rules_back = rules[:] rules_back.remove(r) if list_concept == getAllSupport(rules_back) : rules.remove(r) # simplicity conditions rules_simple = [simplifyRule(r) for r in rules] # Rule2型にconvert colnames = getColNames(decision_table) rules_convert = [convertRule(r,colnames) for r in rules_simple] # END return(rules_convert)