def test_lp_learning_weights(seed, na, nc, ncat, na_gen, pcerrors): # Generate an ELECTRE TRI model and assignment examples if random_model_type == 'default': model = generate_random_mrsort_model(nc, ncat, seed) elif random_model_type == 'choquet': model = generate_random_mrsort_choquet_model(nc, ncat, 2, seed) model2 = model.copy() # Generate a first set of alternatives a = generate_alternatives(na) pt = generate_random_performance_table(a, model.criteria) aa = model.pessimist(pt) # Add errors in assignment examples aa_err = aa.copy() aa_erroned = add_errors_in_assignments(aa_err, model.categories, pcerrors / 100) # Run linear program t1 = time.time() if random_model_type == 'default': lp_weights = LpMRSortWeights(model2, pt, aa_err, 0.0001) else: lp_weights = LpMRSortMobius(model2, pt, aa_err, 0.0001) t2 = time.time() obj = lp_weights.solve() t3 = time.time() # Compute new assignment and classification accuracy aa2 = model2.pessimist(pt) ok = ok_errors = ok2 = ok2_errors = 0 for alt in a: if aa_err(alt.id) == aa2(alt.id): ok2 += 1 if alt.id in aa_erroned: ok2_errors += 1 if aa(alt.id) == aa2(alt.id): ok += 1 if alt.id in aa_erroned: ok_errors += 1 total = len(a) ca2 = ok2 / total ca2_errors = ok2_errors / total ca = ok / total ca_errors = ok_errors / total # Perform the generalization a_gen = generate_alternatives(na_gen) pt_gen = generate_random_performance_table(a_gen, model.criteria) aa = model.pessimist(pt_gen) aa2 = model2.pessimist(pt_gen) ca_gen = compute_ca(aa, aa2) # Save all infos in test_result class t = test_result("%s-%d-%d-%d-%d-%g" % (seed, na, nc, ncat, na_gen, pcerrors)) model.id = 'initial' model2.id = 'learned' pt.id, pt_gen.id = 'learning_set', 'test_set' aa.id = 'aa' aa_err.id = 'aa_err' save_to_xmcda("%s/%s.bz2" % (directory, t.test_name), model, model2, pt, pt_gen, aa, aa_err) # Input params t['seed'] = seed t['na'] = na t['nc'] = nc t['ncat'] = ncat t['na_gen'] = na_gen t['pcerrors'] = pcerrors # Output params t['obj'] = obj t['ca'] = ca t['ca_errors'] = ca_errors t['ca2'] = ca2 t['ca2_errors'] = ca2_errors t['ca_gen'] = ca_gen t['t_total'] = t3 - t1 t['t_const'] = t2 - t1 t['t_solve'] = t3 - t2 return t
def test_mip_mrsort_vc(seed, na, nc, ncat, na_gen, veto_param, pcerrors): # Generate a random ELECTRE TRI BM model if vetot == 'binary': model = generate_random_mrsort_model_with_binary_veto(nc, ncat, seed, veto_func = veto_func, veto_param = veto_param) elif vetot == 'coalition': model = generate_random_mrsort_model_with_coalition_veto(nc, ncat, seed, veto_weights = indep_veto_weights, veto_func = veto_func, veto_param = veto_param) # Generate a set of alternatives a = generate_alternatives(na) pt = generate_random_performance_table(a, model.criteria) aa = model.pessimist(pt) nv_m1_learning = sum([model.count_veto_pessimist(ap) for ap in pt]) # Add errors in assignment examples aa_err = aa.copy() aa_erroned = add_errors_in_assignments_proba(aa_err, model.categories, pcerrors / 100) na_err = len(aa_erroned) # Run the MIP t1 = time.time() model2 = MRSort(model.criteria, None, None, None, model.categories_profiles, None, None, None) if algo == MipMRSortVC and vetot == 'binary': w = {c.id: 1 / len(model.criteria) for c in model.criteria} w1 = w.keys()[0] w[w1] += 1 - sum(w.values()) model2.veto_weights = CriteriaValues([CriterionValue(c.id, w[c.id]) for c in model.criteria]) model2.veto_lbda = min(w.values()) if algo == MipMRSortVC: mip = MipMRSortVC(model2, pt, aa, indep_veto_weights) else: mip = MipMRSort(model2, pt, aa) mip.solve() t_total = time.time() - t1 # Determine the number of erroned alternatives badly assigned aa2 = model2.pessimist(pt) nv_m2_learning = sum([model2.count_veto_pessimist(ap) for ap in pt]) cmatrix_learning = compute_confusion_matrix(aa, aa2, model.categories) ok_errors = ok2_errors = ok = 0 for alt in a: if aa(alt.id) == aa2(alt.id): if alt.id in aa_erroned: ok_errors += 1 ok += 1 if aa_err(alt.id) == aa2(alt.id) and alt.id in aa_erroned: ok2_errors += 1 total = len(a) ca2_errors = ok2_errors / total ca_best = ok / total ca_errors = ok_errors / total # Generate alternatives for the generalization a_gen = generate_alternatives(na_gen) pt_gen = generate_random_performance_table(a_gen, model.criteria) aa_gen = model.pessimist(pt_gen) aa_gen2 = model2.pessimist(pt_gen) nv_m1_gen = sum([model.count_veto_pessimist(ap) for ap in pt_gen]) nv_m2_gen = sum([model2.count_veto_pessimist(ap) for ap in pt_gen]) if len(aa_gen) > 0: cmatrix_gen = compute_confusion_matrix(aa_gen, aa_gen2, model.categories) ca_gen = compute_ca(aa_gen, aa_gen2) aa_gen_err = aa_gen.copy() aa_gen_erroned = add_errors_in_assignments_proba(aa_gen_err, model.categories, pcerrors / 100) aa_gen2 = model2.pessimist(pt_gen) ca_gen_err = compute_ca(aa_gen_err, aa_gen2) # Save all infos in test_result class t = test_result("%s-%d-%d-%d-%d-%s-%d" % (seed, na, nc, ncat, na_gen, veto_param, pcerrors)) model.id = 'initial' model2.id = 'learned' a.id, pt.id = 'learning_set', 'learning_set' aa.id, aa2.id = 'learning_set_m1', 'learning_set_m2' a_gen.id, pt_gen.id = 'test_set', 'test_set' aa_gen.id, aa_gen2.id = 'test_set_m1', 'test_set_m2' save_to_xmcda("%s/%s.bz2" % (directory, t.test_name), model, model2, a, a_gen, pt, pt_gen, aa, aa2, aa_gen, aa_gen2) # Input params t['seed'] = seed t['na'] = na t['nc'] = nc t['ncat'] = ncat t['na_gen'] = na_gen t['veto_param'] = veto_param t['pcerrors'] = pcerrors # Ouput params t['na_err'] = na_err t['nv_m1_learning'] = nv_m1_learning t['nv_m2_learning'] = nv_m2_learning t['nv_m1_gen'] = nv_m1_gen t['nv_m2_gen'] = nv_m2_gen t['ca_best'] = ca_best t['ca_errors'] = ca_errors t['ca_gen'] = ca_gen t['ca_gen_err'] = ca_gen_err t['t_total'] = t_total for k, v in cmatrix_learning.items(): t['learn_%s_%s' % (k[0], k[1])] = v for k, v in cmatrix_gen.items(): t['test_%s_%s' % (k[0], k[1])] = v return t
def test_meta_electre_tri_global(seed, na, nc, ncat, na_gen, pcerrors): # Generate a random ELECTRE TRI BM model if random_model_type == 'default': model = generate_random_mrsort_model(nc, ncat, seed) elif random_model_type == 'choquet': model = generate_random_mrsort_choquet_model(nc, ncat, 2, seed) # Generate a set of alternatives a = generate_alternatives(na) pt = generate_random_performance_table(a, model.criteria) aa = model.pessimist(pt) # Add errors in assignment examples aa_err = aa.copy() aa_erroned = add_errors_in_assignments_proba(aa_err, model.categories, pcerrors / 100) na_err = len(aa_erroned) # Run the MIP t1 = time.time() model2 = MRSort(model.criteria, None, None, None, model.categories_profiles, None, None, None) mip = MipMRSort(model2, pt, aa_err) obj = mip.solve() ca2_best = obj / na aa2 = model2.get_assignments(pt) t_total = time.time() - t1 # Determine the number of erroned alternatives badly assigned aa2 = model2.pessimist(pt) ok_errors = ok2_errors = ok = 0 for alt in a: if aa(alt.id) == aa2(alt.id): if alt.id in aa_erroned: ok_errors += 1 ok += 1 if aa_err(alt.id) == aa2(alt.id) and alt.id in aa_erroned: ok2_errors += 1 total = len(a) ca2_errors = ok2_errors / total ca_best = ok / total ca_errors = ok_errors / total # Generate alternatives for the generalization a_gen = generate_alternatives(na_gen) pt_gen = generate_random_performance_table(a_gen, model.criteria) aa_gen = model.pessimist(pt_gen) aa_gen2 = model2.pessimist(pt_gen) ca_gen = compute_ca(aa_gen, aa_gen2) aa_gen_err = aa_gen.copy() aa_gen_erroned = add_errors_in_assignments_proba(aa_gen_err, model.categories, pcerrors / 100) aa_gen2 = model2.pessimist(pt_gen) ca_gen_err = compute_ca(aa_gen_err, aa_gen2) # Save all infos in test_result class t = test_result("%s-%d-%d-%d-%d-%g" % (seed, na, nc, ncat, na_gen, pcerrors)) model.id = 'initial' model2.id = 'learned' pt.id, pt_gen.id = 'learning_set', 'test_set' aa.id = 'aa' aa_err.id = 'aa_err' save_to_xmcda("%s/%s.bz2" % (directory, t.test_name), model, model2, pt, pt_gen, aa, aa_err) # Input params t['seed'] = seed t['na'] = na t['nc'] = nc t['ncat'] = ncat t['na_gen'] = na_gen t['pcerrors'] = pcerrors # Ouput params t['na_err'] = na_err t['ca_best'] = ca_best t['ca_errors'] = ca_errors t['ca2_best'] = ca2_best t['ca2_errors'] = ca2_errors t['ca_gen'] = ca_gen t['ca_gen_err'] = ca_gen_err t['t_total'] = t_total return t
def run_test(seed, data, pclearning): random.seed(seed) # Separate learning data and test data pt_learning, pt_test = data.pt.split(2, [pclearning, 100 - pclearning]) aa_learning = data.aa.get_subset(pt_learning.keys()) aa_test = data.aa.get_subset(pt_test.keys()) # Initialize ELECTRE-TRI BM model cat_profiles = generate_categories_profiles(data.cats) worst = data.pt.get_worst(data.c) best = data.pt.get_best(data.c) b = generate_alternatives(len(data.cats) - 1, 'b') bpt = None cvs = None lbda = None model = MRSort(data.c, cvs, bpt, lbda, cat_profiles) # Run the linear program t1 = time.time() mip = mip_mrsort(model, pt_learning, aa_learning) obj = mip.solve() t_total = time.time() - t1 # CA learning set aa_learning2 = model.pessimist(pt_learning) ca_learning = compute_ca(aa_learning, aa_learning2) auc_learning = model.auc(aa_learning, pt_learning) diff_learning = compute_confusion_matrix(aa_learning, aa_learning2, model.categories) # Compute CA of test setting if len(aa_test) > 0: aa_test2 = model.pessimist(pt_test) ca_test = compute_ca(aa_test, aa_test2) auc_test = model.auc(aa_test, pt_test) diff_test = compute_confusion_matrix(aa_test, aa_test2, model.categories) else: ca_test = 0 auc_test = 0 ncat = len(data.cats) diff_test = OrderedDict([((a, b), 0) for a in model.categories \ for b in model.categories]) # Compute CA of whole set aa2 = model.pessimist(data.pt) ca = compute_ca(data.aa, aa2) auc = model.auc(data.aa, data.pt) diff_all = compute_confusion_matrix(data.aa, aa2, model.categories) t = test_result("%s-%d-%d" % (data.name, seed, pclearning)) model.id = 'learned' aa_learning.id, aa_test.id = 'learning_set', 'test_set' pt_learning.id, pt_test.id = 'learning_set', 'test_set' save_to_xmcda("%s/%s.bz2" % (directory, t.test_name), model, aa_learning, aa_test, pt_learning, pt_test) t['seed'] = seed t['na'] = len(data.a) t['nc'] = len(data.c) t['ncat'] = len(data.cats) t['pclearning'] = pclearning t['na_learning'] = len(aa_learning) t['na_test'] = len(aa_test) t['obj'] = obj t['ca_learning'] = ca_learning t['ca_test'] = ca_test t['ca_all'] = ca t['auc_learning'] = auc_learning t['auc_test'] = auc_test t['auc_all'] = auc for k, v in diff_learning.items(): t['learn_%s_%s' % (k[0], k[1])] = v for k, v in diff_test.items(): t['test_%s_%s' % (k[0], k[1])] = v for k, v in diff_all.items(): t['all_%s_%s' % (k[0], k[1])] = v t['t_total'] = t_total return t
def run_test(seed, data, pclearning, nloop, nmodels, nmeta): random.seed(seed) global aaa global allm global fct_ca global LOO # Separate learning data and test data if LOO: pt_learning, pt_test = data.pt.split_LOO(seed) else: pt_learning, pt_test = data.pt.split(2, [pclearning, 100 - pclearning]) aa_learning = data.aa.get_subset(pt_learning.keys()) aa_test = data.aa.get_subset(pt_test.keys()) #import pdb; pdb.set_trace() # Initialize a random model cat_profiles = generate_categories_profiles(data.cats) worst = data.pt.get_worst(data.c) best = data.pt.get_best(data.c) b = generate_alternatives(len(data.cats) - 1, 'b') bpt = None cvs = None lbda = None model = MRSort(data.c, cvs, bpt, lbda, cat_profiles) # if LOO: # print(data.c, cvs, bpt, lbda, cat_profiles) # print(model.categories_profiles.to_categories()) # print(model.categories) # import pdb; pdb.set_trace() # Run the metaheuristic t1 = time.time() pt_sorted = SortedPerformanceTable(pt_learning) # Algorithm meta = meta_mrsort(nmodels, model.criteria, model.categories_profiles.to_categories(), pt_sorted, aa_learning, seed = seed * 100) # if LOO: # print(nmodels, model.criteria, # model.categories_profiles.to_categories(), # pt_sorted, aa_learning) #import pdb; pdb.set_trace() #lp_weights = lp_weights, #heur_profiles = heur_profiles, #lp_veto_weights = lp_veto_weights, #heur_veto_profiles = heur_veto_profiles, for i in range(0, nloop): model, ca_learning, all_models = meta.optimize(nmeta, fct_ca) #import pdb; pdb.set_trace() if ca_learning == 1: break t_total = time.time() - t1 aa_learning2 = model.pessimist(pt_learning) ca_learning = compute_ca(aa_learning, aa_learning2) ca_learning_good = compute_ca_good(aa_learning, aa_learning2) #import pdb; pdb.set_trace() auc_learning = model.auc(aa_learning, pt_learning) diff_learning = compute_confusion_matrix(aa_learning, aa_learning2, model.categories) # Compute CA of test setting if len(aa_test) > 0: aa_test2 = model.pessimist(pt_test) ca_test = compute_ca(aa_test, aa_test2) ca_test_good = compute_ca_good(aa_test, aa_test2) auc_test = model.auc(aa_test, pt_test) diff_test = compute_confusion_matrix(aa_test, aa_test2, model.categories) #import pdb; pdb.set_trace() else: ca_test = 0 auc_test = 0 ncat = len(data.cats) diff_test = OrderedDict([((a, b), 0) for a in model.categories \ for b in model.categories]) # Compute CA of whole set aa2 = model.pessimist(data.pt) ca = compute_ca(data.aa, aa2) ca_good = compute_ca_good(data.aa, aa2) auc = model.auc(data.aa, data.pt) diff_all = compute_confusion_matrix(data.aa, aa2, model.categories) t = test_result("%s-%d-%d-%d-%d-%d" % (data.name, seed, nloop, nmodels, nmeta, pclearning)) model.id = 'learned' aa_learning.id, aa_test.id = 'learning_set', 'test_set' pt_learning.id, pt_test.id = 'learning_set', 'test_set' save_to_xmcda("%s/%s.bz2" % (directory, t.test_name), model, aa_learning, aa_test, pt_learning, pt_test) t['seed'] = seed t['na'] = len(data.a) t['nc'] = len(data.c) t['ncat'] = len(data.cats) t['pclearning'] = pclearning t['nloop'] = nloop t['nmodels'] = nmodels t['nmeta'] = nmeta t['na_learning'] = len(aa_learning) t['na_test'] = len(aa_test) t['ca_learning'] = ca_learning t['ca_test'] = ca_test t['ca_all'] = ca t['ca_learning_good'] = ca_learning_good t['ca_test_good'] = ca_test_good t['ca_all_good'] = ca_good t['auc_learning'] = auc_learning t['auc_test'] = auc_test t['auc_all'] = auc # import pdb; pdb.set_trace() aaa[seed]=dict() aaa[seed]['id'] = seed aaa[seed]['learning_asgmt_id'] = [i.id for i in aa_learning] aaa[seed]['learning_asgmt'] = [i.category_id for i in aa_learning] aaa[seed]['learning_asgmt2'] = [i.category_id for i in aa_learning2] aaa[seed]['test_asgmt_id'] = [i.id for i in aa_test] aaa[seed]['test_asgmt'] = [i.category_id for i in aa_test] aaa[seed]['test_asgmt2'] = [i.category_id for i in aa_test2] aaa[seed]['criteria'] = [i for i,j in model.criteria.items()] aaa[seed]['criteria_weights'] = [str(i.value) for i in model.cv.values()] aaa[seed]['profiles_values'] = [str(model.bpt['b1'].performances[i]) for i,j in model.criteria.items()] aaa[seed]['lambda'] = model.lbda #[model.bpt['b1'].performances[i] for i,j in model.criteria.items()] allm[seed]=dict() allm[seed]['id'] = seed current_model = 0 allm[seed]['mresults'] = dict() for all_model in list(all_models)[1:]: current_model += 1 # skipping the 1rst model already treated allm[seed]['mresults'][current_model] = ["",""] aa_learning2_allm = all_model.model.pessimist(pt_learning) ca_learning_allm = compute_ca(aa_learning, aa_learning2_allm) ca_learning_good_allm = compute_ca_good(aa_learning, aa_learning2_allm) auc_learning_allm = all_model.model.auc(aa_learning, pt_learning) # diff_learning_allm = compute_confusion_matrix(aa_learning, aa_learning2_allm, # all_model.model.categories) # Compute CA of test setting if len(aa_test) > 0: aa_test2_allm = all_model.model.pessimist(pt_test) ca_test_allm = compute_ca(aa_test, aa_test2_allm) ca_test_good_allm = compute_ca_good(aa_test, aa_test2_allm) auc_test_allm = all_model.model.auc(aa_test, pt_test) # diff_test_allm = compute_confusion_matrix(aa_test, aa_test2_allm, # all_model.categories) else: ca_test_allm = 0 auc_test_allm = 0 ncat_allm = len(data.cats) # diff_test_allm = OrderedDict([((a, b), 0) for a in all_model.categories \ # for b in all_model.model.categories]) # Compute CA of whole set aa2_allm = all_model.model.pessimist(data.pt) ca_allm = compute_ca(data.aa, aa2_allm) ca_good_allm = compute_ca_good(data.aa, aa2_allm) auc_allm = all_model.model.auc(data.aa, data.pt) #diff_all_allm = compute_confusion_matrix(data.aa, aa2_allm, all_model.model.categories) allm[seed]['mresults'][current_model][0] = 'na_learning,na_test,ca_learning,ca_test,ca_all,ca_learning_good,ca_test_good,ca_all_good,auc_learning,auc_test,auc_all' allm[seed]['mresults'][current_model][1] = str(len(aa_learning)) + "," + str(len(aa_test)) + "," + str(ca_learning_allm) + "," + str(ca_test_allm) + "," + str(ca_allm) + "," + str(ca_learning_good_allm) + "," + str(ca_test_good_allm) + "," + str(ca_good_allm) + "," + str(auc_learning_allm) + "," + str(auc_test_allm) + "," + str(auc_allm) #allm[seed]['mresults'][current_model][1] = #all_model.model.bpt['b1'].performances #all_model.model.cv.values() #import pdb; pdb.set_trace() # allm[seed][current_model]['na_learning'] = len(aa_learning) # allm[seed][current_model]['na_test'] = len(na_test) # allm[seed][current_model]['ca_learning'] = ca_learning_allm # allm[seed][current_model]['ca_test'] = ca_test_allm # allm[seed][current_model]['ca_all'] = ca_allm # allm[seed][current_model]['ca_learning_good'] = ca_learning_good_allm # allm[seed][current_model]['ca_test_good'] = ca_test_good_allm # allm[seed][current_model]['ca_all_good'] = ca_good_allm # allm[seed][current_model]['auc_learning'] = auc_learning_allm # allm[seed][current_model]['auc_test'] = auc_test_allm # allm[seed][current_model]['auc_all'] = auc_allm for k, v in diff_learning.items(): t['learn_%s_%s' % (k[0], k[1])] = v for k, v in diff_test.items(): t['test_%s_%s' % (k[0], k[1])] = v for k, v in diff_all.items(): t['all_%s_%s' % (k[0], k[1])] = v t['t_total'] = t_total return t
def test_meta_electre_tri_global(seed, na, nc, ncat, na_gen, pcerrors, max_oloops, nmodels, max_loops): # Generate a random ELECTRE TRI BM model if random_model_type == 'mrsort': model = generate_random_mrsort_model(nc, ncat, seed) elif random_model_type == 'ncs': model = generate_random_mrsort_choquet_model(nc, ncat, 2, seed) elif random_model_type == 'mrsortcv': model = generate_random_mrsort_model_with_coalition_veto2( nc, ncat, seed) # Generate a set of alternatives a = generate_alternatives(na) pt = generate_random_performance_table(a, model.criteria) aa = model.pessimist(pt) # Add errors in assignment examples aa_err = aa.copy() categories = model.categories_profiles.to_categories() aa_erroned = add_errors_in_assignments_proba(aa_err, model.categories, pcerrors / 100) na_err = len(aa_erroned) # Sort the performance table pt_sorted = SortedPerformanceTable(pt) meta = algo(nmodels, model.criteria, categories, pt_sorted, aa) metas_sorted = meta.sort_models() ca2_iter = [metas_sorted[0].ca] + [1] * (max_loops) t1 = time.time() for i in range(0, max_loops): model2, ca2_best = meta.optimize(max_oloops) ca2_iter[i + 1] = ca2_best if ca2_best == 1: break nloops = i + 1 t_total = time.time() - t1 aa2 = model2.pessimist(pt) ok_errors = ok2_errors = ok = 0 for alt in a: if aa(alt.id) == aa2(alt.id): if alt.id in aa_erroned: ok_errors += 1 ok += 1 if aa_err(alt.id) == aa2(alt.id) and alt.id in aa_erroned: ok2_errors += 1 total = len(a) ca2_errors = ok2_errors / total ca_best = ok / total ca_errors = ok_errors / total # Generate alternatives for the generalization a_gen = generate_alternatives(na_gen) pt_gen = generate_random_performance_table(a_gen, model.criteria) aa_gen = model.pessimist(pt_gen) aa_gen2 = model2.pessimist(pt_gen) ca_gen = compute_ca(aa_gen, aa_gen2) aa_gen_err = aa_gen.copy() aa_gen_erroned = add_errors_in_assignments_proba(aa_gen_err, model.categories, pcerrors / 100) aa_gen2 = model2.pessimist(pt_gen) ca_gen_err = compute_ca(aa_gen_err, aa_gen2) # Save all infos in test_result class t = test_result( "%s-%d-%d-%d-%d-%g-%d-%d-%d" % (seed, na, nc, ncat, na_gen, pcerrors, max_loops, nmodels, max_oloops)) model.id = 'initial' model2.id = 'learned' pt.id, pt_gen.id = 'learning_set', 'test_set' save_to_xmcda("%s/%s.bz2" % (directory, t.test_name), model, model2, pt, pt_gen) # Input params t['seed'] = seed t['na'] = na t['nc'] = nc t['ncat'] = ncat t['na_gen'] = na_gen t['pcerrors'] = pcerrors t['max_loops'] = max_loops t['nmodels'] = nmodels t['max_oloops'] = max_oloops # Ouput params t['na_err'] = na_err t['ca_best'] = ca_best t['ca_errors'] = ca_errors t['ca2_best'] = ca2_best t['ca2_errors'] = ca2_errors t['ca_gen'] = ca_gen t['ca_gen_err'] = ca_gen_err t['nloops'] = nloops t['t_total'] = t_total t['ca2_iter'] = ca2_iter return t
def run_test(seed, data, pclearning, nloop, nmodels, nmeta): random.seed(seed) # Separate learning data and test data pt_learning, pt_test = data.pt.split(2, [pclearning, 100 - pclearning]) aa_learning = data.aa.get_subset(pt_learning.keys()) aa_test = data.aa.get_subset(pt_test.keys()) # Initialize a random model cat_profiles = generate_categories_profiles(data.cats) worst = data.pt.get_worst(data.c) best = data.pt.get_best(data.c) b = generate_alternatives(len(data.cats) - 1, 'b') bpt = None cvs = None lbda = None model = MRSort(data.c, cvs, bpt, lbda, cat_profiles) # Run the metaheuristic t1 = time.time() pt_sorted = SortedPerformanceTable(pt_learning) # Algorithm meta = MetaMRSortPop3(nmodels, model.criteria, model.categories_profiles.to_categories(), pt_sorted, aa_learning, heur_init_profiles, lp_weights, heur_profiles) for i in range(0, nloop): model, ca_learning = meta.optimize(nmeta) t_total = time.time() - t1 aa_learning2 = compute_assignments_majority(meta.models, pt_learning) ca_learning = compute_ca(aa_learning, aa_learning2) auc_learning = compute_auc_majority(meta.models, pt_learning) diff_learning = compute_confusion_matrix(aa_learning, aa_learning2, model.categories) # Compute CA of test setting if len(aa_test) > 0: aa_test2 = compute_assignments_majority(meta.models, pt_test) ca_test = compute_ca(aa_test, aa_test2) auc_test = compute_auc_majority(meta.models, pt_test) diff_test = compute_confusion_matrix(aa_test, aa_test2, model.categories) else: ca_test = 0 auc_test = 0 ncat = len(data.cats) diff_test = OrderedDict([((a, b), 0) for a in model.categories \ for b in model.categories]) # Compute CA of whole set aa2 = compute_assignments_majority(meta.models, data.pt) ca = compute_ca(data.aa, aa2) auc = compute_auc_majority(meta.models, data.pt) diff_all = compute_confusion_matrix(data.aa, aa2, model.categories) t = test_result("%s-%d-%d-%d-%d-%d" % (data.name, seed, nloop, nmodels, nmeta, pclearning)) model.id = 'learned' aa_learning.id, aa_test.id = 'learning_set', 'test_set' pt_learning.id, pt_test.id = 'learning_set', 'test_set' save_to_xmcda("%s/%s.bz2" % (directory, t.test_name), aa_learning, aa_test, pt_learning, pt_test, *meta.models) t['seed'] = seed t['na'] = len(data.a) t['nc'] = len(data.c) t['ncat'] = len(data.cats) t['pclearning'] = pclearning t['nloop'] = nloop t['nmodels'] = nmodels t['nmeta'] = nmeta t['na_learning'] = len(aa_learning) t['na_test'] = len(aa_test) t['ca_learning'] = ca_learning t['ca_test'] = ca_test t['ca_all'] = ca t['auc_learning'] = auc_learning t['auc_test'] = auc_test t['auc_all'] = auc for k, v in diff_learning.items(): t['learn_%s_%s' % (k[0], k[1])] = v for k, v in diff_test.items(): t['test_%s_%s' % (k[0], k[1])] = v for k, v in diff_all.items(): t['all_%s_%s' % (k[0], k[1])] = v t['t_total'] = t_total return t
def run_test(seed, data, pclearning, nseg): random.seed(seed) # Separate learning data and test data pt_learning, pt_test = data.pt.split(2, [pclearning, 100 - pclearning]) aa_learning = data.aa.get_subset(pt_learning.keys()) aa_test = data.aa.get_subset(pt_test.keys()) worst = data.pt.get_worst(data.c) best = data.pt.get_best(data.c) # Run the linear program t1 = time.time() css = CriteriaValues([]) for c in data.c: cs = CriterionValue(c.id, nseg) css.append(cs) lp = LpAVFSort(data.c, css, data.cats, worst, best) obj, cvs, cfs, catv = lp.solve(aa_learning, pt_learning) t_total = time.time() - t1 model = AVFSort(data.c, cvs, cfs, catv) ordered_categories = model.categories # CA learning set aa_learning2 = model.get_assignments(pt_learning) ca_learning = compute_ca(aa_learning, aa_learning2) auc_learning = model.auc(aa_learning, pt_learning) diff_learning = compute_confusion_matrix(aa_learning, aa_learning2, ordered_categories) # Compute CA of test setting if len(aa_test) > 0: aa_test2 = model.get_assignments(pt_test) ca_test = compute_ca(aa_test, aa_test2) auc_test = model.auc(aa_test, pt_test) diff_test = compute_confusion_matrix(aa_test,aa_test2, ordered_categories) else: ca_test = 0 auc_test = 0 ncat = len(data.cats) diff_test = OrderedDict([((a, b), 0) for a in ordered_categories \ for b in ordered_categories]) # Compute CA of whole set aa2 = model.get_assignments(data.pt) ca = compute_ca(data.aa, aa2) auc = model.auc(data.aa, data.pt) diff_all = compute_confusion_matrix(data.aa, aa2, ordered_categories) t = test_result("%s-%d-%d-%d" % (data.name, seed, nseg, pclearning)) model.id = 'learned' aa_learning.id, aa_test.id = 'learning_set', 'test_set' pt_learning.id, pt_test.id = 'learning_set', 'test_set' save_to_xmcda("%s/%s.bz2" % (directory, t.test_name), model, aa_learning, aa_test, pt_learning, pt_test) t['seed'] = seed t['na'] = len(data.a) t['nc'] = len(data.c) t['ncat'] = len(data.cats) t['ns'] = nseg t['pclearning'] = pclearning t['na_learning'] = len(aa_learning) t['na_test'] = len(aa_test) t['obj'] = obj t['ca_learning'] = ca_learning t['ca_test'] = ca_test t['ca_all'] = ca t['auc_learning'] = auc_learning t['auc_test'] = auc_test t['auc_all'] = auc for k, v in diff_learning.items(): t['learn_%s_%s' % (k[0], k[1])] = v for k, v in diff_test.items(): t['test_%s_%s' % (k[0], k[1])] = v for k, v in diff_all.items(): t['all_%s_%s' % (k[0], k[1])] = v t['t_total'] = t_total return t
def test_mip_mrsort_vc(seed, na, nc, ncat, na_gen, veto_param, pcerrors): # Generate a random ELECTRE TRI BM model if vetot == 'binary': model = generate_random_mrsort_model_with_binary_veto( nc, ncat, seed, veto_func=veto_func, veto_param=veto_param) elif vetot == 'coalition': model = generate_random_mrsort_model_with_coalition_veto( nc, ncat, seed, veto_weights=indep_veto_weights, veto_func=veto_func, veto_param=veto_param) # Generate a set of alternatives a = generate_alternatives(na) pt = generate_random_performance_table(a, model.criteria) aa = model.pessimist(pt) nv_m1_learning = sum([model.count_veto_pessimist(ap) for ap in pt]) # Add errors in assignment examples aa_err = aa.copy() aa_erroned = add_errors_in_assignments_proba(aa_err, model.categories, pcerrors / 100) na_err = len(aa_erroned) # Run the MIP t1 = time.time() model2 = MRSort(model.criteria, None, None, None, model.categories_profiles, None, None, None) if algo == MipMRSortVC and vetot == 'binary': w = {c.id: 1 / len(model.criteria) for c in model.criteria} w1 = w.keys()[0] w[w1] += 1 - sum(w.values()) model2.veto_weights = CriteriaValues( [CriterionValue(c.id, w[c.id]) for c in model.criteria]) model2.veto_lbda = min(w.values()) if algo == MipMRSortVC: mip = MipMRSortVC(model2, pt, aa, indep_veto_weights) else: mip = MipMRSort(model2, pt, aa) mip.solve() t_total = time.time() - t1 # Determine the number of erroned alternatives badly assigned aa2 = model2.pessimist(pt) nv_m2_learning = sum([model2.count_veto_pessimist(ap) for ap in pt]) cmatrix_learning = compute_confusion_matrix(aa, aa2, model.categories) ok_errors = ok2_errors = ok = 0 for alt in a: if aa(alt.id) == aa2(alt.id): if alt.id in aa_erroned: ok_errors += 1 ok += 1 if aa_err(alt.id) == aa2(alt.id) and alt.id in aa_erroned: ok2_errors += 1 total = len(a) ca2_errors = ok2_errors / total ca_best = ok / total ca_errors = ok_errors / total # Generate alternatives for the generalization a_gen = generate_alternatives(na_gen) pt_gen = generate_random_performance_table(a_gen, model.criteria) aa_gen = model.pessimist(pt_gen) aa_gen2 = model2.pessimist(pt_gen) nv_m1_gen = sum([model.count_veto_pessimist(ap) for ap in pt_gen]) nv_m2_gen = sum([model2.count_veto_pessimist(ap) for ap in pt_gen]) if len(aa_gen) > 0: cmatrix_gen = compute_confusion_matrix(aa_gen, aa_gen2, model.categories) ca_gen = compute_ca(aa_gen, aa_gen2) aa_gen_err = aa_gen.copy() aa_gen_erroned = add_errors_in_assignments_proba(aa_gen_err, model.categories, pcerrors / 100) aa_gen2 = model2.pessimist(pt_gen) ca_gen_err = compute_ca(aa_gen_err, aa_gen2) # Save all infos in test_result class t = test_result("%s-%d-%d-%d-%d-%s-%d" % (seed, na, nc, ncat, na_gen, veto_param, pcerrors)) model.id = 'initial' model2.id = 'learned' a.id, pt.id = 'learning_set', 'learning_set' aa.id, aa2.id = 'learning_set_m1', 'learning_set_m2' a_gen.id, pt_gen.id = 'test_set', 'test_set' aa_gen.id, aa_gen2.id = 'test_set_m1', 'test_set_m2' save_to_xmcda("%s/%s.bz2" % (directory, t.test_name), model, model2, a, a_gen, pt, pt_gen, aa, aa2, aa_gen, aa_gen2) # Input params t['seed'] = seed t['na'] = na t['nc'] = nc t['ncat'] = ncat t['na_gen'] = na_gen t['veto_param'] = veto_param t['pcerrors'] = pcerrors # Ouput params t['na_err'] = na_err t['nv_m1_learning'] = nv_m1_learning t['nv_m2_learning'] = nv_m2_learning t['nv_m1_gen'] = nv_m1_gen t['nv_m2_gen'] = nv_m2_gen t['ca_best'] = ca_best t['ca_errors'] = ca_errors t['ca_gen'] = ca_gen t['ca_gen_err'] = ca_gen_err t['t_total'] = t_total for k, v in cmatrix_learning.items(): t['learn_%s_%s' % (k[0], k[1])] = v for k, v in cmatrix_gen.items(): t['test_%s_%s' % (k[0], k[1])] = v return t
print("LpAVFSortCompat") lp = LpAVFSortCompat(data.c, css, data.cats, worst, best) obj, cvs, cfs, catv = lp.solve(data.aa, data.pt) model = AVFSort(data.c, cvs, cfs, catv) else: print("Invalid algorithm!") sys.exit(1) t_total = time.time() - t1 model.id = 'learned' data.pt.id = 'learning_set' data.aa.id = 'learning_set' dt = datetime.datetime.now().strftime("%Y%m%d-%H%M%S") save_to_xmcda("%s/%s-all-%s-%s.bz2" % (DATADIR, algo, data.name, dt), data.aa, data.pt, model) aa2 = model.get_assignments(data.pt) ca = compute_ca(data.aa, aa2) auc = model.auc(data.aa, data.pt) anok = [] for a in data.a: if data.aa[a.id].category_id != aa2[a.id].category_id: anok.append(a) if len(anok) > 0: print("Alternatives wrongly assigned:") print_pt_and_assignments(anok.keys(), data.c.keys(), [data.aa, aa2], data.pt)
# # Remove alternatives that cannot be corrected with a veto rule # aa_learning_m2p = discard_undersorted_alternatives(m.categories, # aa_learning, # aa_learning_m2) # aa_learning_m2p = discard_alternatives_in_category(aa_learning_m2p, # m.categories[0]) # Run the metaheuristic meta = MetaMRSortVCPop3(10, m, SortedPerformanceTable(pt_learning), aa_learning) nloops = 10 nmeta = 20 for i in range(nloops): m2, ca = meta.optimize(nmeta) ca_learning_m2 = compute_ca(aa_learning, aa_learning_m2) aa_learning_m3 = m2.pessimist(pt_learning) ca_learning_m3 = compute_ca(aa_learning, aa_learning_m3) if ca_learning_m2 >= ca_learning_m3: model = m else: model = m2 model.id = 'learned' aa_learning.id, aa_test.id = 'learning_set', 'test_set' pt_learning.id, pt_test.id = 'learning_set', 'test_set' save_to_xmcda("%s/%s.bz2" % (directory, fname), model, aa_learning, aa_test, pt_learning, pt_test)
def run_test(seed, data, pclearning, nloop, nmodels, nmeta): random.seed(seed) # Separate learning data and test data pt_learning, pt_test = data.pt.split(2, [pclearning, 100 - pclearning]) aa_learning = data.aa.get_subset(pt_learning.keys()) aa_test = data.aa.get_subset(pt_test.keys()) # Initialize a random model cat_profiles = generate_categories_profiles(data.cats) worst = data.pt.get_worst(data.c) best = data.pt.get_best(data.c) b = generate_alternatives(len(data.cats) - 1, 'b') bpt = None cvs = None lbda = None model = MRSort(data.c, cvs, bpt, lbda, cat_profiles) # Run the metaheuristic t1 = time.time() pt_sorted = SortedPerformanceTable(pt_learning) # Algorithm meta = meta_mrsort(nmodels, model.criteria, model.categories_profiles.to_categories(), pt_sorted, aa_learning, seed=seed * 100) #lp_weights = lp_weights, #heur_profiles = heur_profiles, #lp_veto_weights = lp_veto_weights, #heur_veto_profiles = heur_veto_profiles, for i in range(0, nloop): model, ca_learning = meta.optimize(nmeta) if ca_learning == 1: break t_total = time.time() - t1 aa_learning2 = model.pessimist(pt_learning) ca_learning = compute_ca(aa_learning, aa_learning2) auc_learning = model.auc(aa_learning, pt_learning) diff_learning = compute_confusion_matrix(aa_learning, aa_learning2, model.categories) # Compute CA of test setting if len(aa_test) > 0: aa_test2 = model.pessimist(pt_test) ca_test = compute_ca(aa_test, aa_test2) auc_test = model.auc(aa_test, pt_test) diff_test = compute_confusion_matrix(aa_test, aa_test2, model.categories) else: ca_test = 0 auc_test = 0 ncat = len(data.cats) diff_test = OrderedDict([((a, b), 0) for a in model.categories \ for b in model.categories]) # Compute CA of whole set aa2 = model.pessimist(data.pt) ca = compute_ca(data.aa, aa2) auc = model.auc(data.aa, data.pt) diff_all = compute_confusion_matrix(data.aa, aa2, model.categories) t = test_result("%s-%d-%d-%d-%d-%d" % (data.name, seed, nloop, nmodels, nmeta, pclearning)) model.id = 'learned' aa_learning.id, aa_test.id = 'learning_set', 'test_set' pt_learning.id, pt_test.id = 'learning_set', 'test_set' save_to_xmcda("%s/%s.bz2" % (directory, t.test_name), model, aa_learning, aa_test, pt_learning, pt_test) t['seed'] = seed t['na'] = len(data.a) t['nc'] = len(data.c) t['ncat'] = len(data.cats) t['pclearning'] = pclearning t['nloop'] = nloop t['nmodels'] = nmodels t['nmeta'] = nmeta t['na_learning'] = len(aa_learning) t['na_test'] = len(aa_test) t['ca_learning'] = ca_learning t['ca_test'] = ca_test t['ca_all'] = ca t['auc_learning'] = auc_learning t['auc_test'] = auc_test t['auc_all'] = auc for k, v in diff_learning.items(): t['learn_%s_%s' % (k[0], k[1])] = v for k, v in diff_test.items(): t['test_%s_%s' % (k[0], k[1])] = v for k, v in diff_all.items(): t['all_%s_%s' % (k[0], k[1])] = v t['t_total'] = t_total return t
def test_meta_electre_tri_global(seed, na, nc, ncat, na_gen, pcerrors, max_oloops, nmodels, max_loops): # Generate a random ELECTRE TRI BM model if random_model_type == 'mrsort': model = generate_random_mrsort_model(nc, ncat, seed) elif random_model_type == 'ncs': model = generate_random_mrsort_choquet_model(nc, ncat, 2, seed) elif random_model_type == 'mrsortcv': model = generate_random_mrsort_model_with_coalition_veto2(nc, ncat, seed) # Generate a set of alternatives a = generate_alternatives(na) pt = generate_random_performance_table(a, model.criteria) aa = model.pessimist(pt) # Add errors in assignment examples aa_err = aa.copy() categories = model.categories_profiles.to_categories() aa_erroned = add_errors_in_assignments_proba(aa_err, model.categories, pcerrors / 100) na_err = len(aa_erroned) # Sort the performance table pt_sorted = SortedPerformanceTable(pt) meta = algo(nmodels, model.criteria, categories, pt_sorted, aa) metas_sorted = meta.sort_models() ca2_iter = [metas_sorted[0].ca] + [1] * (max_loops) t1 = time.time() for i in range(0, max_loops): model2, ca2_best = meta.optimize(max_oloops) ca2_iter[i + 1] = ca2_best if ca2_best == 1: break nloops = i + 1 t_total = time.time() - t1 aa2 = model2.pessimist(pt) ok_errors = ok2_errors = ok = 0 for alt in a: if aa(alt.id) == aa2(alt.id): if alt.id in aa_erroned: ok_errors += 1 ok += 1 if aa_err(alt.id) == aa2(alt.id) and alt.id in aa_erroned: ok2_errors += 1 total = len(a) ca2_errors = ok2_errors / total ca_best = ok / total ca_errors = ok_errors / total # Generate alternatives for the generalization a_gen = generate_alternatives(na_gen) pt_gen = generate_random_performance_table(a_gen, model.criteria) aa_gen = model.pessimist(pt_gen) aa_gen2 = model2.pessimist(pt_gen) ca_gen = compute_ca(aa_gen, aa_gen2) aa_gen_err = aa_gen.copy() aa_gen_erroned = add_errors_in_assignments_proba(aa_gen_err, model.categories, pcerrors / 100) aa_gen2 = model2.pessimist(pt_gen) ca_gen_err = compute_ca(aa_gen_err, aa_gen2) # Save all infos in test_result class t = test_result("%s-%d-%d-%d-%d-%g-%d-%d-%d" % (seed, na, nc, ncat, na_gen, pcerrors, max_loops, nmodels, max_oloops)) model.id = 'initial' model2.id = 'learned' pt.id, pt_gen.id = 'learning_set', 'test_set' save_to_xmcda("%s/%s.bz2" % (directory, t.test_name), model, model2, pt, pt_gen) # Input params t['seed'] = seed t['na'] = na t['nc'] = nc t['ncat'] = ncat t['na_gen'] = na_gen t['pcerrors'] = pcerrors t['max_loops'] = max_loops t['nmodels'] = nmodels t['max_oloops'] = max_oloops # Ouput params t['na_err'] = na_err t['ca_best'] = ca_best t['ca_errors'] = ca_errors t['ca2_best'] = ca2_best t['ca2_errors'] = ca2_errors t['ca_gen'] = ca_gen t['ca_gen_err'] = ca_gen_err t['nloops'] = nloops t['t_total'] = t_total t['ca2_iter'] = ca2_iter return t