def capacities_are_monotone(criteria, capacities): n = len(criteria) for i in range(2, n + 1): for combi in [c for c in combinations(criteria.keys(), i)]: cid = CriteriaSet(combi) for combi2 in [c for c in combinations(combi, i - 1)]: cidj = combi2[0] if (i == 2) else CriteriaSet(combi2) if capacities[cidj].value > capacities[cid].value: return False return True
def test001(self): c = generate_criteria(3) cat = generate_categories(3) cps = generate_categories_profiles(cat) bp1 = AlternativePerformances('b1', { 'c1': 0.75, 'c2': 0.75, 'c3': 0.75 }) bp2 = AlternativePerformances('b2', { 'c1': 0.25, 'c2': 0.25, 'c3': 0.25 }) bpt = PerformanceTable([bp1, bp2]) cv1 = CriterionValue('c1', 0.2) cv2 = CriterionValue('c2', 0.2) cv3 = CriterionValue('c3', 0.2) cv12 = CriterionValue(CriteriaSet(['c1', 'c2']), -0.1) cv23 = CriterionValue(CriteriaSet(['c2', 'c3']), 0.2) cv13 = CriterionValue(CriteriaSet(['c1', 'c3']), 0.3) cvs = CriteriaValues([cv1, cv2, cv3, cv12, cv23, cv13]) lbda = 0.6 model = MRSort(c, cvs, bpt, lbda, cps) ap1 = AlternativePerformances('a1', {'c1': 0.3, 'c2': 0.3, 'c3': 0.3}) ap2 = AlternativePerformances('a2', {'c1': 0.8, 'c2': 0.8, 'c3': 0.8}) ap3 = AlternativePerformances('a3', {'c1': 0.3, 'c2': 0.3, 'c3': 0.1}) ap4 = AlternativePerformances('a4', {'c1': 0.3, 'c2': 0.1, 'c3': 0.3}) ap5 = AlternativePerformances('a5', {'c1': 0.1, 'c2': 0.3, 'c3': 0.3}) ap6 = AlternativePerformances('a6', {'c1': 0.8, 'c2': 0.8, 'c3': 0.1}) ap7 = AlternativePerformances('a7', {'c1': 0.8, 'c2': 0.1, 'c3': 0.8}) ap8 = AlternativePerformances('a8', {'c1': 0.1, 'c2': 0.8, 'c3': 0.8}) pt = PerformanceTable([ap1, ap2, ap3, ap4, ap5, ap6, ap7, ap8]) aa = model.get_assignments(pt) self.assertEqual(aa['a1'].category_id, "cat2") self.assertEqual(aa['a2'].category_id, "cat1") self.assertEqual(aa['a3'].category_id, "cat3") self.assertEqual(aa['a4'].category_id, "cat2") self.assertEqual(aa['a5'].category_id, "cat2") self.assertEqual(aa['a6'].category_id, "cat3") self.assertEqual(aa['a7'].category_id, "cat1") self.assertEqual(aa['a8'].category_id, "cat1")
def generate_random_capacities(criteria, seed = None, k = 3): if seed is not None: random.seed(seed) n = len(criteria) r = [round(random.random(), k) for i in range(2 ** n - 2)] + [1.0] r.sort() j = 0 cvs = CriteriaValues() for i in range(1, n + 1): combis = [c for c in combinations(criteria.keys(), i)] random.shuffle(combis) for combi in combis: if i == 1: cid = combi[0] else: cid = CriteriaSet(combi) cv = CriterionValue(cid, r[j]) cvs.append(cv) j += 1 return cvs
def solve_cplex(self): self.lp.solve() status = self.lp.solution.get_status() if status != self.lp.solution.status.MIP_optimal: raise RuntimeError("Solver status: %s" % status) obj = self.lp.solution.get_objective_value() cvs = CriteriaValues() m = ['m_%d' % i for i in range(len(self.mindices))] mindices_map = dict(zip(self.mindices, m)) for m, vname in mindices_map.items(): cv = CriterionValue() if len(m) > 1: cv.id = CriteriaSet(m) else: cv.id = next(iter(m)) cv.value = self.lp.solution.get_values(vname) cvs.append(cv) self.model.cv = cvs self.model.lbda = self.lp.solution.get_values("lambda") return obj
def capacities_to_mobius(criteria, capacities): cvs = CriteriaValues() n = len(criteria) for i in range(1, n + 1): for combi in [c for c in combinations(criteria.keys(), i)]: cid = combi[0] if (i == 1) else CriteriaSet(combi) m = capacities[cid].value for j in range(1, i): for combi2 in [c for c in combinations(combi, j)]: cidj = combi2[0] if (j == 1) else CriteriaSet(combi2) m -= cvs[cidj].value cv = CriterionValue(cid, m) cvs.append(cv) return cvs
def mobius_to_capacities(criteria, mobius): cvs = CriteriaValues() n = len(criteria) for i in range(1, n + 1): for combi in [c for c in combinations(criteria.keys(), i)]: cid = combi[0] if (i == 1) else CriteriaSet(combi) v = 0 for j in range(1, i + 1): for combi2 in [c for c in combinations(combi, j)]: cidj = combi2[0] if (j == 1) else CriteriaSet(combi2) if cidj in mobius: v += mobius[cidj].value cv = CriterionValue(cid, v) cvs.append(cv) return cvs
def test002(self): c = generate_criteria(3) cat = generate_categories(3) cps = generate_categories_profiles(cat) bp1 = AlternativePerformances('b1', { 'c1': 0.75, 'c2': 0.75, 'c3': 0.75 }) bp2 = AlternativePerformances('b2', { 'c1': 0.25, 'c2': 0.25, 'c3': 0.25 }) bpt = PerformanceTable([bp1, bp2]) cv1 = CriterionValue('c1', 0.2) cv2 = CriterionValue('c2', 0.2) cv3 = CriterionValue('c3', 0.2) cv12 = CriterionValue(CriteriaSet(['c1', 'c2']), -0.1) cv23 = CriterionValue(CriteriaSet(['c2', 'c3']), 0.2) cv13 = CriterionValue(CriteriaSet(['c1', 'c3']), 0.3) cvs = CriteriaValues([cv1, cv2, cv3, cv12, cv23, cv13]) lbda = 0.6 model = MRSort(c, cvs, bpt, lbda, cps) a = generate_alternatives(10000) pt = generate_random_performance_table(a, model.criteria) aa = model.get_assignments(pt) model2 = MRSort(c, None, bpt, None, cps) lp = LpMRSortMobius(model2, pt, aa) obj = lp.solve() aa2 = model2.get_assignments(pt) self.assertEqual(obj, 0) self.assertEqual(aa, aa2)
def solve_cplex(self): self.lp.solve() status = self.lp.solution.get_status() if status != self.lp.solution.status.MIP_optimal: raise RuntimeError("Solver status: %s" % status) obj = self.lp.solution.get_objective_value() cvs = CriteriaValues() for c in self.criteria: cv = CriterionValue() cv.id = c.id cv.value = self.lp.solution.get_values('m_' + c.id) cvs.append(cv) for c in self.m2indices: cv = CriterionValue() cv.id = CriteriaSet(c) cv.value = self.lp.solution.get_values("m2p_%s" % c) cv.value -= self.lp.solution.get_values("m2n_%s" % c) cvs.append(cv) self.model.cv = cvs self.model.lbda = self.lp.solution.get_values("lambda") pt = PerformanceTable() for p in self.__profiles: ap = AlternativePerformances(p) for c in self.criteria: perf = self.lp.solution.get_values("g_%s_%s" % (p, c.id)) ap.performances[c.id] = round(perf, 5) pt.append(ap) self.model.bpt = pt self.model.bpt.update_direction(self.model.criteria) return obj
from __future__ import division import os, sys sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)) + "/../") from collections import OrderedDict from pymcda.types import CriteriaSet from pymcda.types import PerformanceTable, AlternativePerformances from pymcda.types import AlternativeAssignment, AlternativesAssignments from pymcda.utils import powerset fmin1 = [ CriteriaSet(['c1', 'c2']), CriteriaSet(['c1', 'c3']), CriteriaSet(['c2', 'c4']), CriteriaSet(['c3', 'c4']) ] gmax1 = [CriteriaSet(['c2', 'c3']), CriteriaSet(['c1', 'c4'])] fmin2 = [ CriteriaSet(['c1', 'c2']), CriteriaSet(['c1', 'c3']), CriteriaSet(['c2', 'c4']) ] gmax2 = [ CriteriaSet(['c2', 'c3']), CriteriaSet(['c1', 'c4']), CriteriaSet(['c3', 'c4']) ] fmin3 = [ CriteriaSet(['c1', 'c2']), CriteriaSet(['c1', 'c3']),
from pymcda.types import CriteriaValues, CriterionValue from pymcda.types import CriteriaSet from pymcda.utils import print_pt_and_assignments from pymcda.utils import compute_ca from pymcda.utils import compute_number_of_winning_coalitions from pymcda.pt_sorted import SortedPerformanceTable from pymcda.ui.graphic import display_electre_tri_models # Generate a random ELECTRE TRI BM model model = generate_random_mrsort_model(5, 3, 123) cv1 = CriterionValue('c1', 0.2) cv2 = CriterionValue('c2', 0.2) cv3 = CriterionValue('c3', 0.2) cv4 = CriterionValue('c4', 0.2) cv5 = CriterionValue('c5', 0.2) cv12 = CriterionValue(CriteriaSet(['c1', 'c2']), -0.1) cv13 = CriterionValue(CriteriaSet(['c1', 'c3']), 0.1) cv14 = CriterionValue(CriteriaSet(['c1', 'c4']), -0.1) cv15 = CriterionValue(CriteriaSet(['c1', 'c5']), 0.1) cv23 = CriterionValue(CriteriaSet(['c2', 'c3']), 0.1) cv24 = CriterionValue(CriteriaSet(['c2', 'c4']), -0.1) cv25 = CriterionValue(CriteriaSet(['c2', 'c5']), 0.1) cv34 = CriterionValue(CriteriaSet(['c3', 'c4']), 0.1) cv35 = CriterionValue(CriteriaSet(['c3', 'c5']), -0.1) cv45 = CriterionValue(CriteriaSet(['c4', 'c5']), -0.1) cvs = CriteriaValues([ cv1, cv2, cv3, cv4, cv5, cv12, cv13, cv14, cv15, cv23, cv24, cv25, cv34, cv35, cv45 ]) model.cv = cvs model.lbda = 0.6
XMCDA_URL = 'http://www.decision-deck.org/2009/XMCDA-2.1.0' if __name__ == "__main__": f = bz2.BZ2File(sys.argv[1]) tree = ElementTree.parse(f) root = tree.getroot() xmcda_criteria = root.find(".//criteria") c = Criteria().from_xmcda(xmcda_criteria) print(c) cids = c.keys() f.close() c_pset = CriteriaSets(set(CriteriaSet(i) for i in powerset(c.keys()))) xmcda_csets = root.findall(".//criteriaSets") i = 0 xmcda2 = ElementTree.Element("{%s}XMCDA" % XMCDA_URL) xmcda2.append(xmcda_criteria) xmcda3 = ElementTree.Element("{%s}XMCDA" % XMCDA_URL) xmcda3.append(xmcda_criteria) for xmcda in xmcda_csets: fmins = CriteriaSets().from_xmcda(xmcda) gmaxs = compute_gmax(c_pset, fmins) lbdas = cplex_lbda_minmax(cids, fmins, gmaxs) if lbdas is None: i += 1 xmcda2.append(xmcda)