def add_best_legal_rule_match(self): best_score = -1 best_effect_map = None best_sr = None best_tr = None for rule_pair, maps in self.__cand_legal_matches.items(): i, score = find_max(maps, lambda x: x[1]) if score > best_score: best_score = score best_effect_map = maps[i][0] best_sr = rule_pair[0] best_tr = rule_pair[1] if best_sr is None: return (None, None, 0, 0) #pdb.set_trace() for se, te in best_effect_map.items(): self.__add_predicate_match(se, te) self.__update_all_rule_matches() bmap, score = self.__rule_match_score(best_sr, best_tr) #assert score > 0 invalidated = self.add_rule_match(best_sr, best_tr, bmap) return (best_sr, best_tr, invalidated, score)
#module is nothin but the creating some code and put in one other file and then # call a function from that created file to current file # here i have creatred some function of converters in conveter.py and imort them to my module file # so it like header file we create in c language and include it in our surc code #here the key words are used are import import converter converter.kg_to_lbs() from converter import lbs_to_kg lbs_to_kg() s = [10, 55, 30, 70, 44] from find_max import find_max find_max(s) import file file.school() file.college()
def test_find_max_signed_numbers(): assert find_max.find_max([-20, -30, -20, -200, 10, 200]) == 200
def test_find_max_negative_numbers(): assert find_max.find_max([-20, -30, -20, -200]) == -20
def find_mode(whole_list): Table = all_list(whole_list) max_value = find_max(list(Table.values())) for k,v in Table.items(): if v == max_value: return k
def __legal_rule_match_score(self, sr, tr): "Special calculations for legal moves" best, score = find_max(self.__cand_legal_matches[(sr,tr)], lambda x: x[1]) return (self.__cand_legal_matches[(sr,tr)][best][0], score)
def do_mapping(src_int_rep, tgt_int_rep, src_pred_bins={}, tgt_pred_bins={}): src_preds = get_predicates(src_int_rep.get_all_rules(), src_int_rep.get_roles()) tgt_preds = get_predicates(tgt_int_rep.get_all_rules(), tgt_int_rep.get_roles()) # 0 is the default bin that can map to anything else. All predicates that aren't # assigned bins get 0 src_preds = [(p, src_pred_bins.get(p.get_name(), 0)) for p in src_preds] tgt_preds = [(p, tgt_pred_bins.get(p.get_name(), 0)) for p in tgt_preds] # a list whose elements record which commitments were made at each # step, which commitments are suppressed, number of other rule matches # invalidated, and the score history = [] # [(partial map, rule match, suppressed, #invalidated, score)] mapping = PartialMap.create(src_int_rep, src_preds, tgt_int_rep, tgt_preds) history.append(CommitPoint(mapping)) best_map = None legals_mapped = False for x in range(1): bottomed_out = False while not bottomed_out: next_mapping = mapping.copy() if not legals_mapped: sr, tr, invalidated, score = next_mapping.add_best_legal_rule_match() if sr is None: legals_mapped = True continue else: sr, tr, bmap, score = next_mapping.get_best_rule_match() if sr is None: # there's no matches at this point, probably because they're all # suppressed history[-1].can_unroll = False bottomed_out = True continue invalidated = next_mapping.add_rule_match(sr, tr, bmap) history[-1].rule_match = (sr, tr) history[-1].invalidated = invalidated history[-1].score = score if next_mapping.complete(): bottomed_out = True num_matched = len(mapping.get_pred_matches()) # print "Matched %d out of %d source and %d target predicates" % \ # (num_matched, len(src_preds), len(tgt_preds)) if best_map == None: best_map = next_mapping # print 'best map score is now ', best_map.score() else: if best_map.score() < next_mapping.score(): best_map = next_mapping # print 'best map score is now ', best_map.score() else: mapping = next_mapping history.append(CommitPoint(mapping)) # okay, now we have to decide where to unroll to # unroll to most invalidated # unroll_i, invalidated = find_max(history, lambda x: x.invalidated) # print "unrolling to %d, with %d invalidations" % (unroll_i, invalidated) # unroll to most degrees of freedom unroll_i, dof = find_max(history, lambda x: x.map.src_rdof(x.rule_match[0])) # print "unrolling to %d, with %d dof" % (unroll_i, dof) history = history[: unroll_i + 1] mapping = history[unroll_i].map history[unroll_i].suppress_last() return best_map
def mode_in_freq_table(Table): max_value = find_max(list(Table.values())) for k, v in Table.items(): if v == max_value: return k
def test_find_max(self): self.assertEqual(find_max([1, 4, 45, 6, -50, 10, 2]), 45)
def do_mapping(src_int_rep, tgt_int_rep, src_pred_bins={}, tgt_pred_bins={}): src_preds = get_predicates(src_int_rep.get_all_rules(), src_int_rep.get_roles()) tgt_preds = get_predicates(tgt_int_rep.get_all_rules(), tgt_int_rep.get_roles()) # 0 is the default bin that can map to anything else. All predicates that aren't # assigned bins get 0 src_preds = [(p, src_pred_bins.get(p.get_name(), 0)) for p in src_preds] tgt_preds = [(p, tgt_pred_bins.get(p.get_name(), 0)) for p in tgt_preds] # a list whose elements record which commitments were made at each # step, which commitments are suppressed, number of other rule matches # invalidated, and the score history = [ ] # [(partial map, rule match, suppressed, #invalidated, score)] mapping = PartialMap.create(src_int_rep, src_preds, tgt_int_rep, tgt_preds) history.append(CommitPoint(mapping)) best_map = None legals_mapped = False for x in range(1): bottomed_out = False while not bottomed_out: next_mapping = mapping.copy() if not legals_mapped: sr, tr, invalidated, score = next_mapping.add_best_legal_rule_match( ) if sr is None: legals_mapped = True continue else: sr, tr, bmap, score = next_mapping.get_best_rule_match() if sr is None: # there's no matches at this point, probably because they're all # suppressed history[-1].can_unroll = False bottomed_out = True continue invalidated = next_mapping.add_rule_match(sr, tr, bmap) history[-1].rule_match = (sr, tr) history[-1].invalidated = invalidated history[-1].score = score if next_mapping.complete(): bottomed_out = True num_matched = len(mapping.get_pred_matches()) #print "Matched %d out of %d source and %d target predicates" % \ # (num_matched, len(src_preds), len(tgt_preds)) if best_map == None: best_map = next_mapping #print 'best map score is now ', best_map.score() else: if best_map.score() < next_mapping.score(): best_map = next_mapping #print 'best map score is now ', best_map.score() else: mapping = next_mapping history.append(CommitPoint(mapping)) # okay, now we have to decide where to unroll to # unroll to most invalidated #unroll_i, invalidated = find_max(history, lambda x: x.invalidated) #print "unrolling to %d, with %d invalidations" % (unroll_i, invalidated) # unroll to most degrees of freedom unroll_i, dof = find_max(history, lambda x: x.map.src_rdof(x.rule_match[0])) #print "unrolling to %d, with %d dof" % (unroll_i, dof) history = history[:unroll_i + 1] mapping = history[unroll_i].map history[unroll_i].suppress_last() return best_map