def solve(self): self.fmins, self.gmaxs = \ compute_winning_and_loosing_coalitions(self.cvs, self.lbda) # self.fmins = compute_minimal_winning_coalitions(self.fmins) # self.gmaxs = compute_maximal_loosing_coalitions(self.gmaxs) if self.solver == 'cplex': obj, cvs2, lbda2 = self.solve_cplex() self.fmins2, self.gmaxs2 = \ compute_winning_and_loosing_coalitions(cvs2, lbda2) if self.fmins2 != self.fmins: raise RuntimeError("fmins") if self.gmaxs2 != self.gmaxs: raise RuntimeError("gmaxs") return obj, cvs2, lbda2
def test_heur_mrsort_coalitions(seed, na, nc, ncat, pcexamples, pcerrors): # Generate an ELECTRE TRI model and assignment examples model = generate_random_mrsort_model(nc, ncat, seed) # Generate a first set of alternatives a = generate_alternatives(na) pt = generate_random_performance_table(a, model.criteria) # Compute assignments aa = model.pessimist(pt) # Run the heuristic heur = HeurMRSortCoalitions(model.criteria, model.categories, pt, aa) coal2 = heur.find_coalitions(int(na * pcexamples)) # Compute the original winning coalitions winning, loosing = compute_winning_and_loosing_coalitions( model.cv, model.lbda) # Compare orignal and computed coalitions coal_ni = list((set(winning) ^ set(coal2)) & set(winning)) coal_add = list((set(winning) ^ set(coal2)) & set(coal2)) # Save all infos in test_result class t = test_result("%s-%d-%d-%d-%g-%g" % (seed, na, nc, ncat, pcexamples, pcerrors)) # Input params t['seed'] = seed t['na'] = na t['nc'] = nc t['ncat'] = ncat t['pcexamples'] = pcexamples t['pcerrors'] = pcerrors # Output params t['ncoal'] = len(winning) t['ncoal_ni'] = len(coal_ni) t['ncoal_add'] = len(coal_add) return t
nok_erroned += 1 print("Good assignments : %3g %%" \ % (float(total-nok)/total*100)) print("Bad assignments : %3g %%" \ % (float(nok)/total*100)) if aa_erroned: print("Bad in erroned assignments: %3g %%" \ % (float(nok_erroned)/total*100)) if len(anok) > 0: print("Alternatives wrongly assigned:") print_pt_and_assignments(anok, model.criteria.keys(), [aa, aa_learned], pt) win1, loose1 = compute_winning_and_loosing_coalitions(model.cv, model.lbda) win2, loose2 = compute_winning_and_loosing_coalitions(model2.cv, model2.lbda) coali = list(set(win1) & set(win2)) coal1e = list(set(win1) ^ set(coali)) coal2e = list(set(win2) ^ set(coali)) print("Number of coalitions original: %d" % len(win1)) print("Number of coalitions learned: %d" % len(win2)) print("Number of common coalitions: %d" % len(coali)) print("Coalitions in original and not in learned: %s" % '; '.join(map(str, coal1e))) print("Coalitions in learned and not in original: %s"
if alt.id in aa_erroned: nok_erroned += 1 print("Good assignments : %3g %%" \ % (float(total-nok)/total*100)) print("Bad assignments : %3g %%" \ % (float(nok)/total*100)) if aa_erroned: print("Bad in erroned assignments: %3g %%" \ % (float(nok_erroned)/total*100)) if len(anok) > 0: print("Alternatives wrongly assigned:") print_pt_and_assignments(anok.keys(), model.criteria.keys(), [aa, aa_learned], pt) win1, loose1 = compute_winning_and_loosing_coalitions(model.cv, model.lbda) win2, loose2 = compute_winning_and_loosing_coalitions( model2.cv, model2.lbda) coali = list(set(win1) & set(win2)) coal1e = list(set(win1) ^ set(coali)) coal2e = list(set(win2) ^ set(coali)) print("Number of coalitions original: %d" % len(win1)) print("Number of coalitions learned: %d" % len(win2)) print("Number of common coalitions: %d" % len(coali)) print("Coalitions in original and not in learned: %s" % '; '.join(map(str, coal1e))) print("Coalitions in learned and not in original: %s" % '; '.join(map(str, coal2e)))
if __name__ == "__main__": from pymcda.generate import generate_random_mrsort_model from pymcda.generate import generate_alternatives from pymcda.generate import generate_random_performance_table from pymcda.generate import generate_random_profiles from pymcda.pt_sorted import SortedPerformanceTable from pymcda.utils import compute_ca from pymcda.utils import compute_winning_and_loosing_coalitions from pymcda.utils import display_coalitions from pymcda.learning.lp_mrsort_weights import LpMRSortWeights from pymcda.ui.graphic import display_electre_tri_models model = generate_random_mrsort_model(10, 3, 17) winning, loosing = compute_winning_and_loosing_coalitions( model.cv, model.lbda) print("Number of coalitions: %d" % len(winning)) a = generate_alternatives(1000) pt = generate_random_performance_table(a, model.criteria) sorted_pt = SortedPerformanceTable(pt) aa = model.pessimist(pt) for cat in model.categories_profiles.get_ordered_categories(): pc = len(aa.get_alternatives_in_category(cat)) / len(aa) * 100 print("Percentage of alternatives in %s: %g %%" % (cat, pc)) # Learn the weights with random generated profiles for i in range(10): model2 = model.copy()
print("Adjusted weights and lambda") lp = LpMRSortPostWeights(m.cv, m.lbda) obj, m.cv, m.lbda = lp.solve() print(m.cv) print("lambda: %s\n" % m.lbda) if m.veto is not None: print("Adjusted veto weights and lambda") lp = LpMRSortPostWeights(m.veto_weights, m.veto_lbda) obj, m.veto_weights, m.veto_lbda = lp.solve() print(m.veto_weights) print("lambda: %s\n" % m.veto_lbda) if len(m.criteria) < 15: winning, loosing = compute_winning_and_loosing_coalitions(m.cv, m.lbda) nwinning = len(winning) print("Number of winning concordance relations: %d (/%d)" % (nwinning, 2**len(m.criteria))) mwinning = compute_minimal_winning_coalitions(winning) mloosing = compute_maximal_loosing_coalitions(loosing) print("Minimal winning coalitions:") for win in mwinning: print(win) print("Maximal loosing coalitions:") for loose in mloosing: print(loose) print("\n\n")
return obj, cvs2, lbda2 if __name__ == "__main__": import random from pymcda.generate import generate_random_criteria_weights from pymcda.generate import generate_criteria random.seed(3) c = generate_criteria(3) cvs = generate_random_criteria_weights(c) # lbda = round(random.uniform(0.5, 1), 3) lbda = 0.5 suf, insuf = compute_winning_and_loosing_coalitions(cvs, lbda) print(c) print(cvs) print("lbda: %f" % lbda) lp = LpMRSortPostWeights(cvs, lbda, 100) obj, cvs2, lbda2 = lp.solve() print("objective: %f" % obj) print(cvs2) print("lbda2: %f" % lbda2) suf2, insuf2 = compute_winning_and_loosing_coalitions(cvs2, lbda2) for coa in suf ^ suf2:
self.model.bpt = bpt if __name__ == "__main__": from pymcda.generate import generate_random_mrsort_model from pymcda.generate import generate_alternatives from pymcda.generate import generate_random_performance_table from pymcda.generate import generate_random_profiles from pymcda.pt_sorted import SortedPerformanceTable from pymcda.utils import compute_ca from pymcda.utils import compute_winning_and_loosing_coalitions from pymcda.utils import display_coalitions from pymcda.learning.lp_mrsort_weights import LpMRSortWeights from pymcda.ui.graphic import display_electre_tri_models model = generate_random_mrsort_model(10, 3, 17) winning, loosing = compute_winning_and_loosing_coalitions(model.cv, model.lbda) print("Number of coalitions: %d" % len(winning)) a = generate_alternatives(1000) pt = generate_random_performance_table(a, model.criteria) sorted_pt = SortedPerformanceTable(pt) aa = model.pessimist(pt) for cat in model.categories_profiles.get_ordered_categories(): pc = len(aa.get_alternatives_in_category(cat)) / len(aa) * 100 print("Percentage of alternatives in %s: %g %%" % (cat, pc)) # Learn the weights with random generated profiles for i in range(10): model2 = model.copy()