def test002_auck_all_errors(self): random.seed(2) crits = generate_criteria(5) model = generate_random_mrsort_model(len(crits), 2) alts = generate_alternatives(1000) pt = generate_random_performance_table(alts, crits) aa = model.get_assignments(pt) aa_err = add_errors_in_assignments(aa, model.categories, 1) auck = model.auck(aa_err, pt, 1) self.assertEqual(auck, 0)
def one_test(self, seed, na, nc, ncat, pcerrors): model = generate_random_mrsort_model(nc, ncat, seed) a = generate_alternatives(na) pt = generate_random_performance_table(a, model.criteria) aa = model.pessimist(pt) aa_err = aa.copy() add_errors_in_assignments(aa_err, model.categories, pcerrors / 100) model2 = model.copy() model2.bpt = None model2.cv = None model2.lbda = None mip = MipMRSort(model2, pt, aa_err) obj = mip.solve() aa2 = model2.pessimist(pt) ca = compute_ca(aa, aa2) ca2 = compute_ca(aa_err, aa2) self.assertEqual(ca2, obj / len(a)) self.assertLessEqual(pcerrors / 100, ca2)
a = generate_alternatives(15000) pt = generate_random_performance_table(a, model.criteria) errors = 0.0 delta = 0.0001 nlearn = 1.00 # Assign the alternative with the model aa = model.pessimist(pt) a_learn = random.sample(a, int(nlearn*len(a))) aa_learn = AlternativesAssignments([ aa[alt.id] for alt in a_learn ]) pt_learn = PerformanceTable([ pt[alt.id] for alt in a_learn ]) aa_err = aa_learn.copy() aa_erroned = add_errors_in_assignments(aa_err, model.categories, errors) print('Original model') print('==============') print("Number of alternatives: %d" % len(a)) print("Number of learning alternatives: %d" % len(aa_learn)) print("Errors in alternatives assignments: %g%%" % (errors*100)) cids = model.criteria.keys() model.bpt.display(criterion_ids = cids) model.cv.display(criterion_ids = cids) print("lambda\t%.7s" % model.lbda) print("delta: %g" % delta) #print(aa) vpt = model.vpt model2 = model.copy()
c = generate_criteria(7, random_direction = True) cv = generate_random_criteria_values(c, seed = 1) cv.normalize_sum_to_unity() cat = generate_categories(3) cfs = generate_random_criteria_functions(c, nseg_min = 3, nseg_max = 3) catv = generate_random_categories_values(cat) u = AVFSort(c, cv, cfs, catv) # Generate random alternative and compute assignments a = generate_alternatives(1000) pt = generate_random_performance_table(a, c) aa = u.get_assignments(pt) aa_err = aa.copy() aa_erroned = add_errors_in_assignments(aa_err, cat.keys(), 0.0) print('==============') print('Original model') print('==============') print("Number of alternatives: %d" % len(a)) print('Criteria weights:') cv.display() print('Criteria functions:') cfs.display() print('Categories values:') catv.display() print("Errors in alternatives assignments: %g %%" \ % (len(aa_erroned) / len(a) * 100)) # Learn the parameters from assignment examples
a = generate_alternatives(15000) pt = generate_random_performance_table(a, model.criteria) errors = 0.0 delta = 0.0001 nlearn = 1.00 # Assign the alternative with the model aa = model.pessimist(pt) a_learn = random.sample(a, int(nlearn * len(a))) aa_learn = AlternativesAssignments([aa[alt.id] for alt in a_learn]) pt_learn = PerformanceTable([pt[alt.id] for alt in a_learn]) aa_err = aa_learn.copy() aa_erroned = add_errors_in_assignments(aa_err, model.categories, errors) print('Original model') print('==============') print("Number of alternatives: %d" % len(a)) print("Number of learning alternatives: %d" % len(aa_learn)) print("Errors in alternatives assignments: %g%%" % (errors * 100)) cids = model.criteria.keys() model.bpt.display(criterion_ids=cids) model.cv.display(criterion_ids=cids) print("lambda\t%.7s" % model.lbda) print("delta: %g" % delta) #print(aa) model2 = model.copy() t1 = time.time()
def test_lp_avfsort(seed, na, nc, ncat, ns, na_gen, pcerrors): # Generate a random 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) 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) gi_worst = AlternativePerformances('worst', {c.id: 0 for c in model.criteria}) gi_best = AlternativePerformances('best', {c.id: 1 for c in model.criteria}) css = CriteriaValues([]) for c in model.criteria: cs = CriterionValue(c.id, ns) css.append(cs) # Run linear program t1 = time.time() lp = LpAVFSort(model.criteria, css, model.categories_profiles.to_categories(), gi_worst, gi_best) t2 = time.time() obj, cv_l, cfs_l, catv_l = lp.solve(aa_err, pt) t3 = time.time() model2 = AVFSort(model.criteria, cv_l, cfs_l, catv_l) # Compute new assignment and classification accuracy aa2 = model2.get_assignments(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.get_assignments(pt_gen) ca_gen = compute_ca(aa, aa2) # Save all infos in test_result class t = test_result("%s-%d-%d-%d-%d-%d-%g" % (seed, na, nc, ncat, ns, na_gen, pcerrors)) # Input params t['seed'] = seed t['na'] = na t['nc'] = nc t['ncat'] = ncat t['ns'] = ns 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_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_meta_electre_tri_profiles(seed, na, nc, ncat, na_gen, pcerrors, max_loops): # Generate an ELECTRE TRI model and assignment examples model = generate_random_mrsort_model(nc, ncat, 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) # Initiate model with random profiles model2.bpt = generate_random_profiles(model.profiles, model.criteria) # Add errors in assignment examples aa_err = aa.copy() aa_erroned = add_errors_in_assignments(aa_err, model.categories, pcerrors / 100) # Sort the performance table pt_sorted = SortedPerformanceTable(pt) t1 = time.time() # Run the algorithm meta = algo(model2, pt_sorted, aa_err) ca2_iter = [1] * (max_loops + 1) aa2 = model2.pessimist(pt) ca2 = compute_ca(aa_err, aa2) ca2_best = ca2 best_bpt = model2.bpt.copy() ca2_iter[0] = ca2 nloops = 0 for k in range(max_loops): if ca2_best == 1: break meta.optimize() nloops += 1 aa2 = meta.aa ca2 = compute_ca(aa_err, aa2) ca2_iter[k + 1] = ca2 if ca2 > ca2_best: ca2_best = ca2 best_bpt = model2.bpt.copy() t_total = time.time() - t1 # Determine the number of erroned alternatives badly assigned model2.bpt = best_bpt aa2 = model2.pessimist(pt) ok = ok_errors = ok2_errors = 0 for alt in a: if aa_err(alt.id) == aa2(alt.id) and alt.id in aa_erroned: ok2_errors += 1 if aa(alt.id) == aa2(alt.id): if alt.id in aa_erroned: ok_errors += 1 ok += 1 total = len(a) ca_best = ok / total ca_best_errors = ok_errors / total ca2_best_errors = ok2_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) # Save all infos in test_result class t = test_result("%s-%d-%d-%d-%d-%g-%d" % (seed, na, nc, ncat, na_gen, pcerrors, max_loops)) # 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 # Ouput params t['ca_best'] = ca_best t['ca_best_errors'] = ca_best_errors t['ca2_best'] = ca2_best t['ca2_best_errors'] = ca2_best_errors t['ca_gen'] = ca_gen t['nloops'] = nloops t['t_total'] = t_total t['ca2_iter'] = ca2_iter return t
def test_meta_electre_tri_global(seed, na, nc, ncat, ns, na_gen, pcerrors, max_oloops, nmodels, max_loops): # Generate a random UTADIS model model = generate_random_avfsort_model(nc, ncat, ns, ns, seed) cats = model.cat_values.get_ordered_categories() # Generate a set of alternatives a = generate_alternatives(na) pt = generate_random_performance_table(a, model.criteria) aa = model.get_assignments(pt) # Add errors in assignment examples aa_err = aa.copy() aa_erroned = add_errors_in_assignments(aa_err, cats, pcerrors / 100) # Sort the performance table pt_sorted = SortedPerformanceTable(pt) t1 = time.time() # Perform at max oloops on the set of metas meta = MetaMRSortPop3(nmodels, model.criteria, model.cat_values.to_categories(), pt_sorted, aa_err) ca2_iter = [meta.metas[0].ca] + [1] * (max_loops) nloops = 0 for i in range(0, max_loops): model2, ca2 = meta.optimize(max_oloops) ca2_iter[i + 1] = ca2 nloops += 1 if ca2 == 1: break 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.get_assignments(pt_gen) aa_gen2 = model2.pessimist(pt_gen) ca_gen = compute_ca(aa_gen, 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)) # Input params t['seed'] = seed t['na'] = na t['nc'] = nc t['ncat'] = ncat t['ns'] = ns t['na_gen'] = na_gen t['pcerrors'] = pcerrors t['max_loops'] = max_loops t['nmodels'] = nmodels t['max_oloops'] = max_oloops # Ouput params t['ca_best'] = ca_best t['ca_errors'] = ca_errors t['ca2_best'] = ca2 t['ca2_errors'] = ca2_errors t['ca_gen'] = ca_gen t['nloops'] = nloops t['t_total'] = t_total t['ca2_iter'] = ca2_iter return t
def test_meta_electre_tri_global(seed, na, nc, ncat, ns, na_gen, pcerrors, max_oloops, nmodels, max_loops): # Generate a random UTADIS model model = generate_random_avfsort_model(nc, ncat, ns, ns, seed) cats = model.cat_values.get_ordered_categories() # Generate a set of alternatives a = generate_alternatives(na) pt = generate_random_performance_table(a, model.criteria) aa = model.get_assignments(pt) # Add errors in assignment examples aa_err = aa.copy() aa_erroned = add_errors_in_assignments(aa_err, cats, pcerrors / 100) # Sort the performance table pt_sorted = SortedPerformanceTable(pt) t1 = time.time() # Perform at max oloops on the set of metas meta = MetaMRSortPop3(nmodels, model.criteria, model.cat_values.to_categories(), pt_sorted, aa_err) ca2_iter = [meta.metas[0].ca] + [1] * (max_loops) nloops = 0 for i in range(0, max_loops): model2, ca2 = meta.optimize(max_oloops) ca2_iter[i + 1] = ca2 nloops += 1 if ca2 == 1: break 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.get_assignments(pt_gen) aa_gen2 = model2.pessimist(pt_gen) ca_gen = compute_ca(aa_gen, 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)) # Input params t['seed'] = seed t['na'] = na t['nc'] = nc t['ncat'] = ncat t['ns'] = ns t['na_gen'] = na_gen t['pcerrors'] = pcerrors t['max_loops'] = max_loops t['nmodels'] = nmodels t['max_oloops'] = max_oloops # Ouput params t['ca_best'] = ca_best t['ca_errors'] = ca_errors t['ca2_best'] = ca2 t['ca2_errors'] = ca2_errors t['ca_gen'] = ca_gen t['nloops'] = nloops t['t_total'] = t_total t['ca2_iter'] = ca2_iter return t