Пример #1
0
def get_input_data(input_dir):
    file_names = (
        'alternatives.xml',
        'criteria.xml',
        'interactions.xml',
        'method_parameters.xml',
        'performance_table.xml',
        'weights.xml',
    )
    trees = get_trees(input_dir, file_names)

    alternatives = px.getAlternativesID(trees['alternatives'])
    criteria = px.getCriteriaID(trees['criteria'])
    pref_directions = px.getCriteriaPreferenceDirections(
        trees['criteria'], criteria)
    thresholds = px.getConstantThresholds(trees['criteria'], criteria)
    weights = px.getCriterionValue(trees['weights'], criteria)
    performances = px.getPerformanceTable(trees['performance_table'], 1, 1)
    interactions = get_criteria_interactions(trees['interactions'], criteria)

    check_net_balance(interactions, weights)
    z_function = px.getParameterByName(trees['method_parameters'],
                                       'z_function')

    ret = {
        'alternatives': alternatives,
        'criteria': criteria,
        'interactions': interactions,
        'performances': performances,
        'pref_directions': pref_directions,
        'thresholds': thresholds,
        'weights': weights,
        'z_function': z_function,
    }
    return ret
def get_input_data(input_dir):
    file_names = (
        'alternatives.xml',
        'criteria.xml',
        'interactions.xml',
        'method_parameters.xml',
        'performance_table.xml',
        'weights.xml',
    )
    trees = get_trees(input_dir, file_names)

    alternatives = px.getAlternativesID(trees['alternatives'])
    criteria = px.getCriteriaID(trees['criteria'])
    pref_directions = px.getCriteriaPreferenceDirections(trees['criteria'], criteria)
    thresholds = px.getConstantThresholds(trees['criteria'], criteria)
    weights = px.getCriterionValue(trees['weights'], criteria)
    performances = px.getPerformanceTable(trees['performance_table'], 1, 1)
    interactions = get_criteria_interactions(trees['interactions'], criteria)

    check_net_balance(interactions, weights)
    z_function = px.getParameterByName(trees['method_parameters'], 'z_function')

    ret = {
        'alternatives': alternatives,
        'criteria': criteria,
        'interactions': interactions,
        'performances': performances,
        'pref_directions': pref_directions,
        'thresholds': thresholds,
        'weights': weights,
        'z_function': z_function,
    }
    return ret
Пример #3
0
 def get_pref_directions(*args, **kwargs):
     criteria = px.getCriteriaID(trees['criteria'])
     pref_directions = px.getCriteriaPreferenceDirections(
         trees['criteria'],
         criteria,
     )
     return pref_directions  # dict
def get_input_data(input_dir):
    file_names = (
        'alternatives.xml',
        'categoriesProfiles.xml',
        'criteria.xml',
        'performanceTable.xml',
        'profilesPerformanceTable.xml',
        'weights.xml',
    )
    trees = get_trees(input_dir, file_names)

    alternatives = px.getAlternativesID(trees['alternatives'])
    criteria = px.getCriteriaID(trees['criteria'])
    pref_directions = px.getCriteriaPreferenceDirections(trees['criteria'], criteria)
    thresholds = px.getConstantThresholds(trees['criteria'], criteria)
    weights = px.getCriterionValue(trees['weights'], criteria)
    performances = px.getPerformanceTable(trees['performanceTable'], None, None)
    categories_profiles = get_categories_profiles_central(trees['categoriesProfiles'])
    profiles_performance_table = px.getPerformanceTable(trees['profilesPerformanceTable'],
                                                        None, None)

    ret = {
        'alternatives': alternatives,
        'categories_profiles': categories_profiles,
        'criteria': criteria,
        'performances': performances,
        'pref_directions': pref_directions,
        'profiles_performance_table': profiles_performance_table,
        'thresholds': thresholds,
        'weights': weights,
    }
    return ret
Пример #5
0
def parse_xmcda_files(in_weights, in_hierarchy, in_concorlevel, in_criteria, in_alternatives, in_performances):
    xml_crit = px.parseValidate(in_criteria)
    xml_alt = px.parseValidate(in_alternatives)
    xml_pt = px.parseValidate(in_performances)
    xml_weight = px.parseValidate(in_weights)
    xml_hierarchy = px.parseValidate(in_hierarchy)
    xml_concordance = px.parseValidate(in_concorlevel)
    if xml_crit == None:
        raise ValueError, ["Invalid criteria file"]
    if xml_alt == None:
        raise ValueError, ["Invalid alternative file"]
    if xml_pt == None:
        raise ValueError, ["Invalid performance table file"]
    if xml_weight == None:
        raise ValueError, ["Invalid weight file"]
    if xml_hierarchy == None:
        raise ValueError, ["Invalid assignment file"]
    if xml_concordance == None:
        raise ValueError, ["Invalid concordance file"]

    try:
        alternatives_ids = px.getAlternativesID(xml_alt)
        criteria_ids = px.getCriteriaID(xml_crit)
        performance_table = px.getPerformanceTable(xml_pt, alternatives_ids, criteria_ids)
        criteriaWeight = px.getCriterionValue(xml_weight, criteria_ids, 'Importance')
        preferenceDirections = px.getCriteriaPreferenceDirections(xml_crit, criteria_ids)
        hierarchyArray = get_hierarchy_array(xml_hierarchy)
        criteria_thresholds = px.getConstantThresholds(xml_crit, criteria_ids)
        concordanceCutLev = get_criterion_concordance_cutting_level_value(xml_concordance, 'Concordance')
    except:
        raise ValueError, ["Failed to parse one or more file"]
        return

    return alternatives_ids, criteria_ids, performance_table, criteriaWeight, preferenceDirections, hierarchyArray, criteria_thresholds, concordanceCutLev
def get_input_data(input_dir):
    file_names = (
        'alternatives.xml',
        'categoriesProfiles.xml',
        'criteria.xml',
        'performanceTable.xml',
        'profilesPerformanceTable.xml',
    )
    trees = get_trees(input_dir, file_names)

    alternatives = px.getAlternativesID(trees['alternatives'])
    criteria = px.getCriteriaID(trees['criteria'])
    pref_directions = px.getCriteriaPreferenceDirections(trees['criteria'], criteria)
    thresholds = px.getConstantThresholds(trees['criteria'], criteria)
    performances = px.getPerformanceTable(trees['performanceTable'], None, None)
    categories_profiles = get_categories_profiles_central(trees['categoriesProfiles'])
    profiles_performance_table = px.getPerformanceTable(
        trees['profilesPerformanceTable'], None, None
    )

    ret = {
        'alternatives': alternatives,
        'categories_profiles': categories_profiles,
        'criteria': criteria,
        'performances': performances,
        'pref_directions': pref_directions,
        'profiles_performance_table': profiles_performance_table,
        'thresholds': thresholds,
    }
    return ret
def get_input_data(input_dir):
    file_names = (
        'alternatives.xml',
        'performanceTable.xml',
        'categoriesProfiles.xml',
        'criteria.xml',
        'profilesPerformanceTable.xml',
    )
    trees = get_trees(input_dir, file_names)

    alternatives = px.getAlternativesID(trees['alternatives'])
    criteria = px.getCriteriaID(trees['criteria'])
    pref_directions = px.getCriteriaPreferenceDirections(trees['criteria'], criteria)
    thresholds = px.getConstantThresholds(trees['criteria'], criteria)
    performances = px.getPerformanceTable(trees['performanceTable'], None, None)
    profiles_performance_table = px.getPerformanceTable(trees['profilesPerformanceTable'], None, None)
    cp_tree = trees['categoriesProfiles']
    # we need only categories profiles' names
    categories_profiles = [p for p in cp_tree.xpath('//categoriesProfiles//alternativeID/text()')]

    ret = {
        'alternatives': alternatives,
        'categories_profiles': categories_profiles,
        'criteria': criteria,
        'performances': performances,
        'pref_directions': pref_directions,
        'profiles_performance_table': profiles_performance_table,
        'thresholds': thresholds,
    }
    return ret
Пример #8
0
 def get_pref_directions(*args, **kwargs):
     criteria = px.getCriteriaID(trees['criteria'])
     pref_directions = px.getCriteriaPreferenceDirections(
         trees['criteria'],
         criteria,
     )
     return pref_directions  # dict
Пример #9
0
def get_input_data(input_dir):
    file_names = (
        'alternatives.xml',
        'performanceTable.xml',
        'categoriesProfiles.xml',
        'criteria.xml',
        'profilesPerformanceTable.xml',
    )
    trees = get_trees(input_dir, file_names)

    alternatives = px.getAlternativesID(trees['alternatives'])
    criteria = px.getCriteriaID(trees['criteria'])
    pref_directions = px.getCriteriaPreferenceDirections(
        trees['criteria'], criteria)
    thresholds = px.getConstantThresholds(trees['criteria'], criteria)
    performances = px.getPerformanceTable(trees['performanceTable'], None,
                                          None)
    profiles_performance_table = px.getPerformanceTable(
        trees['profilesPerformanceTable'], None, None)
    cp_tree = trees['categoriesProfiles']
    # we need only categories profiles' names
    categories_profiles = [
        p for p in cp_tree.xpath('//categoriesProfiles//alternativeID/text()')
    ]

    ret = {
        'alternatives': alternatives,
        'categories_profiles': categories_profiles,
        'criteria': criteria,
        'performances': performances,
        'pref_directions': pref_directions,
        'profiles_performance_table': profiles_performance_table,
        'thresholds': thresholds,
    }
    return ret
def get_input_data(input_dir):
    file_names = (
        'alternatives.xml',
        'performanceTable.xml',
        'categoriesProfiles.xml',
        'criteria.xml',
        'profilesPerformanceTable.xml',
        'weights.xml',
    )
    trees = get_trees(input_dir, file_names)

    alternatives = px.getAlternativesID(trees['alternatives'])
    criteria = px.getCriteriaID(trees['criteria'])
    pref_directions = px.getCriteriaPreferenceDirections(
        trees['criteria'], criteria)
    thresholds = px.getConstantThresholds(trees['criteria'], criteria)
    weights = px.getCriterionValue(trees['weights'], criteria)
    performances = px.getPerformanceTable(trees['performanceTable'], 1, 1)

    # we can't assume that categories will be always available as a separate
    # input file, therefore it's better to extract them from categoriesProfiles
    cp_tree = trees['categoriesProfiles']
    categories = list(
        set(cp_tree.xpath('//categoriesProfiles//limits//categoryID/text()')))
    # since we just need names of categories profiles, it's better to get them like below
    # - otherwise, to get 'full' categories profiles, we should use this:
    # categories_profiles = px.getCategoriesProfiles(trees['categoriesProfiles'], categories)
    categories_profiles = [
        p for p in cp_tree.xpath('//categoriesProfiles//alternativeID/text()')
    ]
    # last two args to getPerformanceTable are not used at all anyway...
    profiles_performance_table = px.getPerformanceTable(
        trees['profilesPerformanceTable'], None, None)

    ret = {
        'alternatives': alternatives,
        'categories_profiles': categories_profiles,
        'criteria': criteria,
        'performances': performances,
        'pref_directions': pref_directions,
        'profiles_performance_table': profiles_performance_table,
        'thresholds': thresholds,
        'weights': weights,
    }
    return ret
def get_input_data(input_dir):
    file_names = (
        'alternatives.xml',
        'performanceTable.xml',
        'categoriesProfiles.xml',
        'criteria.xml',
        'profilesPerformanceTable.xml',
        'weights.xml',
    )
    trees = get_trees(input_dir, file_names)

    alternatives = px.getAlternativesID(trees['alternatives'])
    criteria = px.getCriteriaID(trees['criteria'])
    pref_directions = px.getCriteriaPreferenceDirections(trees['criteria'], criteria)
    thresholds = px.getConstantThresholds(trees['criteria'], criteria)
    weights = px.getCriterionValue(trees['weights'], criteria)
    performances = px.getPerformanceTable(trees['performanceTable'], 1, 1)

    # we can't assume that categories will be always available as a separate
    # input file, therefore it's better to extract them from categoriesProfiles
    cp_tree = trees['categoriesProfiles']
    categories = list(set(cp_tree.xpath('//categoriesProfiles//limits//categoryID/text()')))
    # since we just need names of categories profiles, it's better to get them like below
    # - otherwise, to get 'full' categories profiles, we should use this:
    # categories_profiles = px.getCategoriesProfiles(trees['categoriesProfiles'], categories)
    categories_profiles = [p for p in cp_tree.xpath('//categoriesProfiles//alternativeID/text()')]
    # last two args to getPerformanceTable are not used at all anyway...
    profiles_performance_table = px.getPerformanceTable(trees['profilesPerformanceTable'], None, None)

    ret = {
        'alternatives': alternatives,
        'categories_profiles': categories_profiles,
        'criteria': criteria,
        'performances': performances,
        'pref_directions': pref_directions,
        'profiles_performance_table': profiles_performance_table,
        'thresholds': thresholds,
        'weights': weights,
    }
    return ret
def get_input_data(input_dir):
    file_names = (
        'alternatives.xml',
        'criteria.xml',
        'performanceTable.xml',
    )
    trees = get_trees(input_dir, file_names)

    criteria = px.getCriteriaID(trees['criteria'])
    pref_directions = px.getCriteriaPreferenceDirections(
        trees['criteria'], criteria)
    thresholds = px.getConstantThresholds(trees['criteria'], criteria)
    performances = px.getPerformanceTable(trees['performanceTable'], None,
                                          None)

    ret = {
        'criteria': criteria,
        'performances': performances,
        'pref_directions': pref_directions,
        'thresholds': thresholds,
    }
    return ret
Пример #13
0
def parse_xmcda_files(in_dir):
    xml_crit = PyXMCDA.parseValidate(in_dir+"/criteria.xml")
    xml_alt = PyXMCDA.parseValidate(in_dir+"/alternatives.xml")
    xml_pt = PyXMCDA.parseValidate(in_dir+"/perfs_table.xml")
    xml_assign = PyXMCDA.parseValidate(in_dir+"/assign.xml")
    xml_cat = PyXMCDA.parseValidate(in_dir+"/categories.xml")

    if xml_crit == None:
        error_list.append("Invalid criteria file")
        return
    if xml_alt == None:
        error_list.append("Invalid alternative file")
        return
    if xml_pt == None:
        error_list.append("Invalid performance table file")
        return
    if xml_assign == None:
        error_list.append("Invalid assignment file")
        return
    if xml_cat == None:
        error_list.append("Invalid categories file")
        return

    try:
        alt_id = PyXMCDA.getAlternativesID(xml_alt)
        crit_id = PyXMCDA.getCriteriaID(xml_crit)
        pt = PyXMCDA.getPerformanceTable(xml_pt, alt_id, crit_id)
        cat_id = PyXMCDA.getCategoriesID(xml_cat)
        cat_rank = PyXMCDA.getCategoriesRank(xml_cat, cat_id)
        assign = PyXMCDA.getAlternativesAffectations(xml_assign)
        pref_dir = PyXMCDA.getCriteriaPreferenceDirections(xml_crit, crit_id)
    except:
        error_list.append("Failed to parse one or more file")
        return

    return (alt_id, crit_id, pt, cat_id, cat_rank, assign, pref_dir)
Пример #14
0
def main(argv=None):
	if argv is None:
		argv = sys.argv
	
	parser = OptionParser()
	
	parser.add_option("-i", "--in", dest="in_dir")
	parser.add_option("-o", "--out", dest="out_dir")
	
	(options, args) = parser.parse_args(argv[1:])
	
	in_dir = options.in_dir
	out_dir = options.out_dir
	
	critAverage = {}
	critNormalSD = {}
	critTriangSD = {}
	
	# Creating a list for error messages
	errorList = []
	
	# If some mandatory input files are missing
	if not os.path.isfile (in_dir+"/alternatives.xml") or not os.path.isfile (in_dir+"/criteria.xml") :
		errorList.append("Some input files are missing")
	
	else :
		
		# We parse all the mandatory input files
		xmltree_alternatives = PyXMCDA.parseValidate(in_dir+"/alternatives.xml")
		xmltree_criteria = PyXMCDA.parseValidate(in_dir+"/criteria.xml")
		
		# We check if all madatory input files are valid
		if xmltree_alternatives == None :
			errorList.append("The alternatives file can't be validated.")
		if xmltree_criteria == None :
			errorList.append("The criteria file can't be validated.")
	
		if not errorList :
		
			alternativesId = PyXMCDA.getAlternativesID(xmltree_alternatives)
			criteriaId = PyXMCDA.getCriteriaID(xmltree_criteria)
			
			if not alternativesId :
				errorList.append("No alternatives found. Is your alternatives file correct ?")
			if not criteriaId :
				errorList.append("No criteria found. Is your criteria file correct ?")
	
	if not errorList :
	
		# We check if parameters for criteria distribution profile have been provided
		if os.path.isfile (in_dir+"/criteriaProfiles.xml") :
			xmltree_CritProfile = PyXMCDA.parseValidate(in_dir+"/criteriaProfiles.xml")
			if xmltree_CritProfile == None :
				errorList.append ("criteriaProfiles file can't be validated.")
			else :
				critAverage = PyXMCDA.getNamedParametersByName (xmltree_CritProfile, "distributionAverage")
				critNormalSD = PyXMCDA.getNamedParametersByName (xmltree_CritProfile, "normalDistributionStandardDeviation")
				critTriangSD = PyXMCDA.getNamedParametersByName (xmltree_CritProfile, "triangularDistributionStandardDeviation")
				# ...
				
	if not errorList :
	
		# We check if a seed is provided for the random generation
		if os.path.isfile (in_dir+"/seed.xml") :
			xmltree_seed = PyXMCDA.parseValidate(in_dir+"/seed.xml")
			if xmltree_seed == None :
				errorList.append ("seed file can't be validated.")
			else :
				seed = PyXMCDA.getParameterByName (xmltree_seed, "seed")
				if not isinstance(seed,int) :
					errorList.append ("seed value should be a strictly positive integer")
				else :
					if seed <= 0 :
						errorList.append ("seed should be a strictly positive integer")
					else:
						# We initialize the random generator
						random.seed(seed)
						
	if not errorList :
	
		for i in range(20):
			print random.random()
	
		# We recover criteria scale information
		criteriaTypes = PyXMCDA.getCriteriaScalesTypes (xmltree_criteria, criteriaId)
		criteriaDir = PyXMCDA.getCriteriaPreferenceDirections (xmltree_criteria, criteriaId)
		criteriaUB = PyXMCDA.getCriteriaUpperBounds (xmltree_criteria, criteriaId)
		criteriaLB = PyXMCDA.getCriteriaLowerBounds (xmltree_criteria, criteriaId)
		criteriaRL = PyXMCDA.getCriteriaRankedLabel (xmltree_criteria, criteriaId)
		
		# We add some default lower and upper bounds
		for crit in criteriaId :
			if not criteriaLB.has_key (crit) or criteriaLB[crit] == None :
				if criteriaTypes[crit] == "quantitative" :
					criteriaLB[crit] = 0.0
				else :
					criteriaLB[crit] = 1
			if not criteriaUB.has_key (crit) or criteriaUB[crit] == None :
				if criteriaTypes[crit] == "quantitative" :
					criteriaUB[crit] = 100.0
				else :
					if criteriaRL.has_key(crit) and criteriaRL[crit] != None :
						criteriaUB[crit] = len(criteriaRL[crit])
					else :
						criteriaUB[crit] = 10
				
		# We construct the performance Tableau
		Tab = {}
		for crit in criteriaId :
			Tab[crit] = {}
			
			if critNormalSD.has_key(crit) :
				sd = critNormalSD[crit]
				if critAverage.has_key(crit) :
					average = critAverage[crit]
				else :
					average = (criteriaLB[crit] + criteriaUB[crit])/2.0
				for alt in alternativesId :			
					temp = criteriaLB[crit] -1
					while temp < criteriaLB[crit] or temp > criteriaUB[crit] :
						temp = random.gauss (average,sd)
					Tab[crit][alt] = temp
					if criteriaTypes[crit] == "qualitative" :
						Tab[crit][alt] = int(Tab[crit][alt])
					
			elif critTriangSD.has_key (crit) :
				for alt in alternativesId :
					# TO BE CHANGED !!!
					Tab[crit][alt] = random.randint (criteriaLB[crit], criteriaUB[crit])
			
			else :
				for alt in alternativesId :
					if criteriaTypes[crit] == "quantitative" :
						Tab[crit][alt] = float(random.randint (criteriaLB[crit]*100, criteriaUB[crit]*100))/100.0
					else :
						Tab[crit][alt] = random.randint (criteriaLB[crit], criteriaUB[crit])
		
		# We construct the performanceTable.xml file
		filePerfTable = open(out_dir+"/performanceTable.xml", 'w')
		PyXMCDA.writeHeader (filePerfTable)
		
		filePerfTable.write ("<performanceTable>\n")
		
		for alt in alternativesId :
			filePerfTable.write ("\t<alternativePerformances>\n\t\t<alternativeID>" + alt + "</alternativeID>\n")
			
			for crit in criteriaId :
				if criteriaTypes[crit] == "quantitative" :
					filePerfTable.write ("\t\t<performance>\n\t\t\t<criterionID>" + crit + "</criterionID>\n\t\t\t<value><real>"+ str(Tab[crit][alt]) + "</real></value>\n\t\t</performance>\n")
				else :
					filePerfTable.write ("\t\t<performance>\n\t\t\t<criterionID>" + crit + "</criterionID>\n\t\t\t<value><integer>"+ str(Tab[crit][alt]) + "</integer></value>\n\t\t</performance>\n")
			
			filePerfTable.write ("\t</alternativePerformances>\n")
		
		filePerfTable.write ("</performanceTable>\n")
		
		PyXMCDA.writeFooter(filePerfTable)
		filePerfTable.close()
	
	# Creating log and error file, messages.xml
	fileMessages = open(out_dir+"/messages.xml", 'w')
	PyXMCDA.writeHeader (fileMessages)
	
	if not errorList :
	
		PyXMCDA.writeLogMessages (fileMessages, ["Execution ok"])
	else :
		PyXMCDA.writeErrorMessages (fileMessages, errorList)
		
	PyXMCDA.writeFooter(fileMessages)
	fileMessages.close()
Пример #15
0
def get_input_data(input_dir, filenames, params, **kwargs):
    trees = _get_trees(input_dir, filenames)
    d = _create_data_object(params)
    for p in params:
        if p == 'alternatives':
            d.alternatives = px.getAlternativesID(trees['alternatives'])

        elif p == 'categories_profiles':
            comparison_with = kwargs.get('comparison_with')
            if comparison_with is None:
                comparison_with = px.getParameterByName(
                    trees['method_parameters'], 'comparison_with')
            d.categories_profiles = _get_categories_profiles(
                trees.get('categories_profiles'), comparison_with)

        elif p == 'categories_rank':
            categories = px.getCategoriesID(trees['categories'])
            d.categories_rank = px.getCategoriesRank(trees['categories'],
                                                     categories)

        elif p == 'comparison_with':
            d.comparison_with = px.getParameterByName(
                trees['method_parameters'], 'comparison_with')

        elif p == 'concordance':
            alternatives = px.getAlternativesID(trees['alternatives'])
            comparison_with = px.getParameterByName(trees['method_parameters'],
                                                    'comparison_with')
            if comparison_with in ('boundary_profiles', 'central_profiles'):
                categories_profiles = _get_categories_profiles(
                    trees['categories_profiles'], comparison_with)
                d.concordance = _get_alternatives_comparisons(
                    trees['concordance'], alternatives, categories_profiles)
            else:
                d.concordance = px.getAlternativesComparisons(
                    trees['concordance'], alternatives)

        elif p == 'credibility':
            alternatives = px.getAlternativesID(trees['alternatives'])
            comparison_with = kwargs.get('comparison_with')
            if not comparison_with:
                comparison_with = px.getParameterByName(
                    trees['method_parameters'], 'comparison_with')
            if comparison_with in ('boundary_profiles', 'central_profiles'):
                categories_profiles = _get_categories_profiles(
                    trees['categories_profiles'], comparison_with)
            else:
                categories_profiles = None
            eliminate_cycles_method = px.getParameterByName(
                trees.get('method_parameters'), 'eliminate_cycles_method')
            tree = trees.get('credibility')
            if eliminate_cycles_method == 'cut_weakest' and tree is None:
                raise RuntimeError(
                    "'cut_weakest' option requires credibility as "
                    "an additional input (apart from outranking).")
            d.credibility = _get_alternatives_comparisons(
                tree, alternatives, categories_profiles=categories_profiles)

        elif p == 'criteria':
            d.criteria = px.getCriteriaID(trees['criteria'])

        elif p == 'cut_threshold':
            cut_threshold = px.getParameterByName(trees['method_parameters'],
                                                  'cut_threshold')
            if cut_threshold is None or not (0 <= float(cut_threshold) <= 1):
                raise RuntimeError(
                    "'cut_threshold' should be in range [0, 1] "
                    "(most commonly used values are 0.6 or 0.7).")
            d.cut_threshold = cut_threshold

        # 'cv_crossed' == 'counter-veto crossed'
        elif p == 'cv_crossed':
            alternatives = px.getAlternativesID(trees['alternatives'])
            comparison_with = px.getParameterByName(trees['method_parameters'],
                                                    'comparison_with')
            if comparison_with in ('boundary_profiles', 'central_profiles'):
                categories_profiles = _get_categories_profiles(
                    trees['categories_profiles'], comparison_with)
            else:
                categories_profiles = None
            d.cv_crossed = _get_alternatives_comparisons(
                trees['counter_veto_crossed'],
                alternatives,
                categories_profiles=categories_profiles,
                use_partials=True,
                mcda_concept='counterVetoCrossed')

        elif p == 'discordance':
            alternatives = px.getAlternativesID(trees['alternatives'])
            comparison_with = px.getParameterByName(trees['method_parameters'],
                                                    'comparison_with')
            if kwargs.get('use_partials') is not None:
                use_partials = kwargs.get('use_partials')
            else:
                parameter = px.getParameterByName(trees['method_parameters'],
                                                  'use_partials')
                use_partials = True if parameter == 'true' else False
            if comparison_with in ('boundary_profiles', 'central_profiles'):
                categories_profiles = _get_categories_profiles(
                    trees['categories_profiles'], comparison_with)
            else:
                categories_profiles = None
            d.discordance = _get_alternatives_comparisons(
                trees['discordance'],
                alternatives,
                categories_profiles=categories_profiles,
                use_partials=use_partials)

        elif p == 'eliminate_cycles_method':
            d.eliminate_cycles_method = px.getParameterByName(
                trees['method_parameters'], 'eliminate_cycles_method')

        elif p == 'interactions':
            criteria = px.getCriteriaID(trees['criteria'])
            d.interactions = _get_criteria_interactions(
                trees['interactions'], criteria)

        elif p == 'outranking':
            d.outranking = _get_outranking_crisp(trees['outranking'])

        elif p == 'performances':
            d.performances = px.getPerformanceTable(trees['performance_table'],
                                                    None, None)

        elif p == 'pref_directions':
            criteria = px.getCriteriaID(trees['criteria'])
            d.pref_directions = px.getCriteriaPreferenceDirections(
                trees['criteria'], criteria)

        elif p == 'profiles_performance_table':
            if comparison_with in ('boundary_profiles', 'central_profiles'):
                tree = trees.get('profiles_performance_table')
                if tree is None:
                    msg = (
                        "Missing profiles performance table (did you forget "
                        "to provide 'profiles_performance_table.xml' file?).")
                    raise RuntimeError(msg)
                d.profiles_performance_table = px.getPerformanceTable(
                    tree, None, None)
            else:
                d.profiles_performance_table = None

        elif p == 'reinforcement_factors':
            criteria = px.getCriteriaID(trees['criteria'])
            factors = {}
            for c in criteria:
                rf = px.getCriterionValue(trees['reinforcement_factors'], c,
                                          'reinforcement_factors')
                if len(rf) == 0:
                    continue
                if rf.get(c) <= 1:
                    msg = ("Reinforcement factor for criterion '{}' should be "
                           "higher than 1.0 (ideally between 1.2 and 1.5).")
                    raise RuntimeError(msg)
                factors.update(rf)
            d.reinforcement_factors = factors

        elif p == 'thresholds':
            criteria = px.getCriteriaID(trees['criteria'])
            d.thresholds = _get_thresholds(trees['criteria'])

        elif p == 'weights':
            criteria = px.getCriteriaID(trees['criteria'])
            d.weights = px.getCriterionValue(trees['weights'], criteria)

        elif p == 'z_function':
            d.z_function = px.getParameterByName(trees['method_parameters'],
                                                 'z_function')

        elif p == 'with_denominator':
            parameter = px.getParameterByName(trees['method_parameters'],
                                              'with_denominator')
            d.with_denominator = True if parameter == 'true' else False

        elif p == 'only_max_discordance':
            parameter = px.getParameterByName(trees['method_parameters'],
                                              'only_max_discordance')
            d.only_max_discordance = True if parameter == 'true' else False

        elif p == 'use_partials':
            parameter = px.getParameterByName(trees['method_parameters'],
                                              'use_partials')
            d.use_partials = True if parameter == 'true' else False

        elif p == 'use_pre_veto':
            parameter = px.getParameterByName(trees['method_parameters'],
                                              'use_pre_veto')
            d.use_pre_veto = True if parameter == 'true' else False

        else:
            raise RuntimeError("Unknown parameter '{}' specified.".format(p))

    for param in params:
        data = getattr(d, param)
        if type(data) in (type(list), type(dict)) and len(data) == 0:
            raise RuntimeError(
                "No content for '{}' parameter provided.".format(param))
    return d
def main(argv=None):
	if argv is None:
		argv = sys.argv
	
	parser = OptionParser()
	
	parser.add_option("-i", "--in", dest="in_dir")
	parser.add_option("-o", "--out", dest="out_dir")
	
	(options, args) = parser.parse_args(argv[1:])
	
	in_dir = options.in_dir
	out_dir = options.out_dir
	
	# Creating a list for error messages
	errorList = []
	
	# If some mandatory input files are missing
	if not os.path.isfile (in_dir+"/alternatives.xml") or not os.path.isfile (in_dir+"/criteria.xml") or not os.path.isfile (in_dir+"/criteriaWeights.xml") or not os.path.isfile (in_dir+"/performanceTable.xml") :
		errorList.append("Some input files are missing")
	
	else :
		
		# We parse all the mandatory input files
		xmltree_alternatives = PyXMCDA.parseValidate(in_dir+"/alternatives.xml")
		xmltree_criteria = PyXMCDA.parseValidate(in_dir+"/criteria.xml")
		xmltree_weights = PyXMCDA.parseValidate(in_dir+"/criteriaWeights.xml")
		xmltree_perfTable = PyXMCDA.parseValidate(in_dir+"/performanceTable.xml")
		
		# We check if all madatory input files are valid
		if xmltree_alternatives == None :
			errorList.append("The alternatives file can't be validated.")
		if xmltree_criteria == None :
			errorList.append("The criteria file can't be validated.")
		if xmltree_perfTable == None :
			errorList.append("The performance table file can't be validated.")
		if xmltree_weights == None :
			errorList.append("The criteria weights file can't be validated.")
		
		# By default, the valuation domain is fixed to [0,1]	
		minValDomain = 0
		maxValDomain = 1
		
		# If a valuation domain input file has been provided
		if os.path.isfile (in_dir+"/valuationDomain.xml") :
		
			xmltree_valuation = PyXMCDA.parseValidate(in_dir+"/valuationDomain.xml")
			if xmltree_valuation == None :
				errorList.append ("valuationDomain file can't be validated.")
				
			else :
			
				minValDomain = PyXMCDA.getParameterByName (xmltree_valuation, "min", "valuationDomain")
				maxValDomain = PyXMCDA.getParameterByName (xmltree_valuation, "max", "valuationDomain")
				
				# We check the validity of the parameters
				if not isinstance(minValDomain,float) and not isinstance(minValDomain,int) :
					errorList.append ("min value should be an integer or a real")
				if not isinstance(maxValDomain,float) and not isinstance(maxValDomain,int) :
					errorList.append ("max value should be an integer or a real")
					
				if not errorList :
					if minValDomain >= maxValDomain :
						errorList.append ("The max value should be strictly greater than the min value")
	
		if not errorList :
		
			alternativesId = PyXMCDA.getAlternativesID(xmltree_alternatives)
			criteriaId = PyXMCDA.getCriteriaID(xmltree_criteria)
			perfTable = PyXMCDA.getPerformanceTable(xmltree_perfTable, alternativesId, criteriaId)
			thresholds = PyXMCDA.getConstantThresholds (xmltree_criteria, criteriaId)
			weights = PyXMCDA.getCriterionValue (xmltree_weights, criteriaId)
		
			if not alternativesId :
				errorList.append("No alternatives found. Is your alternatives file correct ?")
			if not criteriaId :
				errorList.append("No criteria found. Is your criteria file correct ?")
			if not perfTable :
				errorList.append("No performance table found. Is your performance table file correct ?")
			if not weights :
				errorList.append("No weights found. Is your weights file correct ?")
		
	if not errorList :
	
		# We compute the weight sum (only the weights associated to active criteria)
		sumWeights = 0.0
		
		for crit in criteriaId :
			try :
				sumWeights = sumWeights + weights[crit]
			except :
				errorList.append("There is no defined weight for criterion "+crit+".")
		
	if not errorList :
		
		# We recover the criteria preference directions
		criteriaDir = PyXMCDA.getCriteriaPreferenceDirections (xmltree_criteria, criteriaId)
	
		# We compute the alternative comparisons values
		fileAltValues = open(out_dir+"/alternativesComparisons.xml", 'w')
		PyXMCDA.writeHeader (fileAltValues)
		
		fileAltValues.write ("\t<alternativesComparisons>\n\t\t<pairs>\n")
		
		ElemOut = PyXMCDA.getRubisElementaryOutranking (alternativesId, criteriaId, perfTable, thresholds)
		
		for alt1 in alternativesId :
			for alt2 in alternativesId :
			
				fileAltValues.write("\t\t\t<pair>\n\t\t\t\t<initial><alternativeID>"+alt1+"</alternativeID></initial>\n\t\t\t\t<terminal><alternativeID>"+alt2+"</alternativeID></terminal>\n")
				
				# Verifier s'il manque des valeurs !!! try expect
				#fileAltValues.write ("\t\t\t\t<value><NA>not available</NA></value>\n\t\t\t</pair>\n")
				sum = 0.0
				
				for crit in criteriaId :
					
					sum += ElemOut[alt1][alt2][crit] * weights[crit]
					
				sum = sum/sumWeights
				
				# La valeur est entre 0 et 1, on la met dans le bon intervalle
				sum = (maxValDomain - minValDomain)*sum + minValDomain
				
				fileAltValues.write ("\t\t\t\t<value><real>"+str(sum)+"</real></value>\n\t\t\t</pair>\n")
					
			
		fileAltValues.write ("\t\t</pairs>\n\t</alternativesComparisons>\n")
			
		PyXMCDA.writeFooter(fileAltValues)
		fileAltValues.close()
	
	# Creating log and error file, messages.xml
	fileMessages = open(out_dir+"/messages.xml", 'w')
	PyXMCDA.writeHeader (fileMessages)
	
	if not errorList :
	
		PyXMCDA.writeLogMessages (fileMessages, ["Execution ok"])
	else :
		PyXMCDA.writeErrorMessages (fileMessages, errorList)
		
	PyXMCDA.writeFooter(fileMessages)
	fileMessages.close()
Пример #17
0
def get_input_data(input_dir, filenames, params, **kwargs):
    trees = _get_trees(input_dir, filenames)
    d = _create_data_object(params)
    for p in params:
        if p == 'alternatives':
            d.alternatives = px.getAlternativesID(trees['alternatives'])

        elif p == 'categories_profiles':
            comparison_with = kwargs.get('comparison_with')
            if comparison_with is None:
                comparison_with = px.getParameterByName(trees['method_parameters'], 'comparison_with')
            d.categories_profiles = _get_categories_profiles(trees.get('categories_profiles'),
                                                             comparison_with)

        elif p == 'categories_rank':
            categories = px.getCategoriesID(trees['categories'])
            d.categories_rank = px.getCategoriesRank(trees['categories'], categories)

        elif p == 'comparison_with':
            d.comparison_with = px.getParameterByName(trees['method_parameters'], 'comparison_with')

        elif p == 'concordance':
            alternatives = px.getAlternativesID(trees['alternatives'])
            comparison_with = px.getParameterByName(trees['method_parameters'], 'comparison_with')
            if comparison_with in ('boundary_profiles', 'central_profiles'):
                categories_profiles = _get_categories_profiles(trees['categories_profiles'],
                                                               comparison_with)
                d.concordance = _get_alternatives_comparisons(trees['concordance'], alternatives,
                                                              categories_profiles)
            else:
                d.concordance = px.getAlternativesComparisons(trees['concordance'], alternatives)

        elif p == 'credibility':
            alternatives = px.getAlternativesID(trees['alternatives'])
            comparison_with = kwargs.get('comparison_with')
            if not comparison_with:
                comparison_with = px.getParameterByName(trees['method_parameters'], 'comparison_with')
            if comparison_with in ('boundary_profiles', 'central_profiles'):
                categories_profiles = _get_categories_profiles(trees['categories_profiles'],
                                                               comparison_with)
            else:
                categories_profiles = None
            eliminate_cycles_method = px.getParameterByName(trees.get('method_parameters'),
                                                            'eliminate_cycles_method')
            tree = trees.get('credibility')
            if eliminate_cycles_method == 'cut_weakest' and tree is None:
                raise RuntimeError("'cut_weakest' option requires credibility as "
                                   "an additional input (apart from outranking).")
            d.credibility = _get_alternatives_comparisons(tree, alternatives,
                                                          categories_profiles=categories_profiles)

        elif p == 'criteria':
            d.criteria = px.getCriteriaID(trees['criteria'])

        elif p == 'cut_threshold':
            cut_threshold = px.getParameterByName(trees['method_parameters'], 'cut_threshold')
            if cut_threshold is None or not (0 <= float(cut_threshold) <= 1):
                raise RuntimeError(
                    "'cut_threshold' should be in range [0, 1] "
                    "(most commonly used values are 0.6 or 0.7)."
                )
            d.cut_threshold = cut_threshold

        # 'cv_crossed' == 'counter-veto crossed'
        elif p == 'cv_crossed':
            alternatives = px.getAlternativesID(trees['alternatives'])
            comparison_with = px.getParameterByName(trees['method_parameters'], 'comparison_with')
            if comparison_with in ('boundary_profiles', 'central_profiles'):
                categories_profiles = _get_categories_profiles(trees['categories_profiles'],
                                                               comparison_with)
            else:
                categories_profiles = None
            d.cv_crossed = _get_alternatives_comparisons(trees['counter_veto_crossed'],
                                                         alternatives,
                                                         categories_profiles=categories_profiles,
                                                         use_partials=True,
                                                         mcda_concept='counterVetoCrossed')

        elif p == 'discordance':
            alternatives = px.getAlternativesID(trees['alternatives'])
            comparison_with = px.getParameterByName(trees['method_parameters'], 'comparison_with')
            if kwargs.get('use_partials') is not None:
                use_partials = kwargs.get('use_partials')
            else:
                parameter = px.getParameterByName(trees['method_parameters'], 'use_partials')
                use_partials = True if parameter == 'true' else False
            if comparison_with in ('boundary_profiles', 'central_profiles'):
                categories_profiles = _get_categories_profiles(trees['categories_profiles'],
                                                               comparison_with)
            else:
                categories_profiles = None
            d.discordance = _get_alternatives_comparisons(trees['discordance'], alternatives,
                                                          categories_profiles=categories_profiles,
                                                          use_partials=use_partials)

        elif p == 'eliminate_cycles_method':
            d.eliminate_cycles_method = px.getParameterByName(trees['method_parameters'],
                                                              'eliminate_cycles_method')

        elif p == 'interactions':
            criteria = px.getCriteriaID(trees['criteria'])
            d.interactions = _get_criteria_interactions(trees['interactions'], criteria)

        elif p == 'outranking':
            d.outranking = _get_outranking_crisp(trees['outranking'])

        elif p == 'performances':
            d.performances = px.getPerformanceTable(trees['performance_table'], None, None)

        elif p == 'pref_directions':
            criteria = px.getCriteriaID(trees['criteria'])
            d.pref_directions = px.getCriteriaPreferenceDirections(trees['criteria'], criteria)

        elif p == 'profiles_performance_table':
            if comparison_with in ('boundary_profiles', 'central_profiles'):
                tree = trees.get('profiles_performance_table')
                if tree is None:
                    msg = ("Missing profiles performance table (did you forget "
                           "to provide 'profiles_performance_table.xml' file?).")
                    raise RuntimeError(msg)
                d.profiles_performance_table = px.getPerformanceTable(tree, None, None)
            else:
                d.profiles_performance_table = None

        elif p == 'reinforcement_factors':
            criteria = px.getCriteriaID(trees['criteria'])
            factors = {}
            for c in criteria:
                rf = px.getCriterionValue(trees['reinforcement_factors'], c,
                                          'reinforcement_factors')
                if len(rf) == 0:
                    continue
                if rf.get(c) <= 1:
                    msg = ("Reinforcement factor for criterion '{}' should be "
                           "higher than 1.0 (ideally between 1.2 and 1.5).")
                    raise RuntimeError(msg)
                factors.update(rf)
            d.reinforcement_factors = factors

        elif p == 'thresholds':
            criteria = px.getCriteriaID(trees['criteria'])
            d.thresholds = _get_thresholds(trees['criteria'])

        elif p == 'weights':
            criteria = px.getCriteriaID(trees['criteria'])
            d.weights = px.getCriterionValue(trees['weights'], criteria)

        elif p == 'z_function':
            d.z_function = px.getParameterByName(trees['method_parameters'], 'z_function')

        elif p == 'with_denominator':
            parameter = px.getParameterByName(trees['method_parameters'], 'with_denominator')
            d.with_denominator = True if parameter == 'true' else False

        elif p == 'only_max_discordance':
            parameter = px.getParameterByName(trees['method_parameters'], 'only_max_discordance')
            d.only_max_discordance = True if parameter == 'true' else False

        elif p == 'use_partials':
            parameter = px.getParameterByName(trees['method_parameters'], 'use_partials')
            d.use_partials = True if parameter == 'true' else False

        elif p == 'use_pre_veto':
            parameter = px.getParameterByName(trees['method_parameters'], 'use_pre_veto')
            d.use_pre_veto = True if parameter == 'true' else False

        else:
            raise RuntimeError("Unknown parameter '{}' specified.".format(p))

    for param in params:
        data = getattr(d, param)
        if type(data) in (type(list), type(dict)) and len(data) == 0:
            raise RuntimeError("No content for '{}' parameter provided."
                               .format(param))
    return d
Пример #18
0
def main(argv=None):
    if argv is None:
        argv = sys.argv

    parser = OptionParser()

    parser.add_option("-i", "--in", dest="in_dir")
    parser.add_option("-o", "--out", dest="out_dir")

    (options, args) = parser.parse_args(argv[1:])

    in_dir = options.in_dir
    out_dir = options.out_dir

    # Creating a list for error messages
    errorList = []

    # If some mandatory input files are missing
    if (
        not os.path.isfile(in_dir + "/alternatives.xml")
        or not os.path.isfile(in_dir + "/criteria.xml")
        or not os.path.isfile(in_dir + "/criteriaWeights.xml")
        or not os.path.isfile(in_dir + "/performanceTable.xml")
    ):
        errorList.append("Some input files are missing")

    else:

        # We parse all the mandatory input files
        xmltree_alternatives = PyXMCDA.parseValidate(in_dir + "/alternatives.xml")
        xmltree_criteria = PyXMCDA.parseValidate(in_dir + "/criteria.xml")
        xmltree_weights = PyXMCDA.parseValidate(in_dir + "/criteriaWeights.xml")
        xmltree_perfTable = PyXMCDA.parseValidate(in_dir + "/performanceTable.xml")

        # We check if all madatory input files are valid
        if xmltree_alternatives == None:
            errorList.append("The alternatives file can't be validated.")
        if xmltree_criteria == None:
            errorList.append("The criteria file can't be validated.")
        if xmltree_perfTable == None:
            errorList.append("The performance table file can't be validated.")
        if xmltree_weights == None:
            errorList.append("The criteria weights file can't be validated.")

            # By default, the valuation domain is fixed to [0,1]
        minValDomain = 0
        maxValDomain = 1

        # If a valuation domain input file has been provided
        if os.path.isfile(in_dir + "/valuationDomain.xml"):

            xmltree_valuation = PyXMCDA.parseValidate(in_dir + "/valuationDomain.xml")
            if xmltree_valuation == None:
                errorList.append("valuationDomain file can't be validated.")

            else:

                minValDomain = PyXMCDA.getParameterByName(xmltree_valuation, "min", "valuationDomain")
                maxValDomain = PyXMCDA.getParameterByName(xmltree_valuation, "max", "valuationDomain")

                # We check the validity of the parameters
                if not isinstance(minValDomain, float) and not isinstance(minValDomain, int):
                    errorList.append("min value should be an integer or a real")
                if not isinstance(maxValDomain, float) and not isinstance(maxValDomain, int):
                    errorList.append("max value should be an integer or a real")

                if not errorList:
                    if minValDomain >= maxValDomain:
                        errorList.append("The max value should be strictly greater than the min value")

        if not errorList:

            alternativesId = PyXMCDA.getAlternativesID(xmltree_alternatives)
            criteriaId = PyXMCDA.getCriteriaID(xmltree_criteria)
            perfTable = PyXMCDA.getPerformanceTable(xmltree_perfTable, alternativesId, criteriaId)
            thresholds = PyXMCDA.getConstantThresholds(xmltree_criteria, criteriaId)
            weights = PyXMCDA.getCriterionValue(xmltree_weights, criteriaId)

            if not alternativesId:
                errorList.append("No alternatives found. Is your alternatives file correct ?")
            if not criteriaId:
                errorList.append("No criteria found. Is your criteria file correct ?")
            if not perfTable:
                errorList.append("No performance table found. Is your performance table file correct ?")
            if not weights:
                errorList.append("No weights found. Is your weights file correct ?")

    if not errorList:

        # We compute the weight sum (only the weights associated to active criteria)
        sumWeights = 0.0

        for crit in criteriaId:
            try:
                sumWeights = sumWeights + weights[crit]
            except:
                errorList.append("There is no defined weight for criterion " + crit + ".")

    if not errorList:

        # We recover the criteria preference directions
        # criteriaDir = PyXMCDA.getCriteriaPreferenceDirections (xmltree_criteria, criteriaId)
        # Plus necessaire

        # We compute the alternative comparisons values
        fileAltValues = open(out_dir + "/alternativesComparisons.xml", "w")
        PyXMCDA.writeHeader(fileAltValues)

        # We write some information about the generated file
        fileAltValues.write(
            "\t<projectReference>\n\t\t<title>Rubis outranking relation</title>\n\t\t<version>"
            + VERSION
            + "</version>\n\t\t<author>ws_PyXMCDA suite (TV)</author>\n\t</projectReference>\n\n"
        )

        fileAltValues.write("\t<alternativesComparisons>\n\t\t<pairs>\n")

        # ATTENTION Solution rustine
        # On retourne ici le tableau de perf pour les criteres a minimiser
        # Devra etre fait par le getRubisElementaryOutranking
        criteriaDir = PyXMCDA.getCriteriaPreferenceDirections(xmltree_criteria, criteriaId)
        for crit in criteriaId:
            if criteriaDir[crit] == "min":
                for alt in alternativesId:
                    perfTable[alt][crit] = -perfTable[alt][crit]

        ElemOut = PyXMCDA.getRubisElementaryOutranking(alternativesId, criteriaId, perfTable, thresholds)

        tabVetos = PyXMCDA.getVetos(alternativesId, criteriaId, perfTable, thresholds)

        for alt1 in alternativesId:
            for alt2 in alternativesId:

                fileAltValues.write(
                    "\t\t\t<pair>\n\t\t\t\t<initial><alternativeID>"
                    + alt1
                    + "</alternativeID></initial>\n\t\t\t\t<terminal><alternativeID>"
                    + alt2
                    + "</alternativeID></terminal>\n"
                )

                # Verifier s'il manque des valeurs !!! try expect
                # fileAltValues.write ("\t\t\t\t<value><NA>not available</NA></value>\n\t\t\t</pair>\n")
                sum = 0.0
                isVeto = 0
                isWeakVeto = 0

                for crit in criteriaId:

                    sum += ElemOut[alt1][alt2][crit] * weights[crit]

                    # On verifie si un veto est leve
                    if (
                        tabVetos.has_key(alt1)
                        and tabVetos[alt1].has_key(alt2)
                        and tabVetos[alt1][alt2].has_key(crit)
                        and tabVetos[alt1][alt2][crit] == 1
                    ):
                        isVeto = 1

                        # On verifie si un veto faible est leve
                    if (
                        tabVetos.has_key(alt1)
                        and tabVetos[alt1].has_key(alt2)
                        and tabVetos[alt1][alt2].has_key(crit)
                        and tabVetos[alt1][alt2][crit] == 0.5
                    ):
                        isWeakVeto = 1

                sum = sum / sumWeights

                if isVeto == 1:
                    # On utilise le veto classique, qui met la valeur minimale
                    sum = 0.0
                elif isWeakVeto == 1 and sum > 0.5:
                    # Un veto faible est leve
                    sum = 0.5

                    # La valeur est entre 0 et 1, on la met dans le bon intervalle
                sum = (maxValDomain - minValDomain) * sum + minValDomain

                fileAltValues.write("\t\t\t\t<value><real>" + str(sum) + "</real></value>\n\t\t\t</pair>\n")

        fileAltValues.write("\t\t</pairs>\n\t</alternativesComparisons>\n")

        PyXMCDA.writeFooter(fileAltValues)
        fileAltValues.close()

        # Creating log and error file, messages.xml
    fileMessages = open(out_dir + "/messages.xml", "w")
    PyXMCDA.writeHeader(fileMessages)

    if not errorList:

        PyXMCDA.writeLogMessages(fileMessages, ["Execution ok"])
    else:
        PyXMCDA.writeErrorMessages(fileMessages, errorList)

    PyXMCDA.writeFooter(fileMessages)
    fileMessages.close()
Пример #19
0
def get_input_data(input_dir, filenames, params, **kwargs):
    trees = _get_trees(input_dir, filenames)
    d = _create_data_object(params)
    for p in params:
        if p == "alternatives":
            d.alternatives = px.getAlternativesID(trees["alternatives"])

        elif p == "profiles":
            d.profiles = px.getProfilesID(trees["profiles"])

        elif p == "categories_profiles":
            comparison_with = kwargs.get("comparison_with")
            if comparison_with is None:
                comparison_with = px.getParameterByName(trees["method_parameters"], "comparison_with")
            d.categories_profiles = _get_categories_profiles(trees.get("categories_profiles"), comparison_with)

        elif p == "categories_rank":
            categories = px.getCategoriesID(trees["categories"])
            d.categories_rank = px.getCategoriesRank(trees["categories"], categories)

        elif p == "comparison_with":
            d.comparison_with = px.getParameterByName(trees["method_parameters"], "comparison_with")

        elif p == "concordance":

            alternatives = px.getAlternativesID(trees["alternatives"])

            comparison_with = kwargs.get("comparison_with")

            if trees.has_key("methos_parameters"):
                comparison_with = px.getParameterByName(trees["method_parameters"], "comparison_with")

            if kwargs.get("use_partials") is not None:
                use_partials = kwargs.get("use_partials")
            else:
                if trees.has_key("methos_parameters"):
                    parameter = px.getParameterByName(trees["method_parameters"], "use_partials")
                    use_partials = True if parameter == "true" else False

            categories_profiles = None
            profiles = None

            if comparison_with in ("boundary_profiles", "central_profiles"):
                categories_profiles = _get_categories_profiles(trees["categories_profiles"], comparison_with)
            if comparison_with == "profiles":
                profiles = px.getProfilesID(trees["profiles"])

            d.concordance = _get_alternatives_comparisons(
                trees["concordance"],
                alternatives,
                profiles=profiles,
                categories_profiles=categories_profiles,
                use_partials=use_partials,
            )
        elif p == "crisp_concordance":

            alternatives = px.getAlternativesID(trees["alternatives"])

            comparison_with = kwargs.get("comparison_with")

            if trees.has_key("methos_parameters"):
                comparison_with = px.getParameterByName(trees["method_parameters"], "comparison_with")

            if kwargs.get("use_partials") is not None:
                use_partials = kwargs.get("use_partials")
            else:
                if trees.has_key("methos_parameters"):
                    parameter = px.getParameterByName(trees["method_parameters"], "use_partials")
                    use_partials = True if parameter == "true" else False

            categories_profiles = None
            profiles = None

            if comparison_with in ("boundary_profiles", "central_profiles"):
                categories_profiles = _get_categories_profiles(trees["categories_profiles"], comparison_with)
            if comparison_with == "profiles":
                profiles = px.getProfilesID(trees["profiles"])

            d.concordance = _get_alternatives_comparisons(
                trees["concordance"],
                alternatives,
                profiles=profiles,
                categories_profiles=categories_profiles,
                use_partials=use_partials,
                use_value=False,
            )

        elif p == "credibility":
            alternatives = px.getAlternativesID(trees["alternatives"])
            comparison_with = kwargs.get("comparison_with")
            if not comparison_with:
                comparison_with = px.getParameterByName(trees["method_parameters"], "comparison_with")
            if comparison_with in ("boundary_profiles", "central_profiles"):
                categories_profiles = _get_categories_profiles(trees["categories_profiles"], comparison_with)
            else:
                categories_profiles = None
            eliminate_cycles_method = px.getParameterByName(trees.get("method_parameters"), "eliminate_cycles_method")
            tree = trees.get("credibility")
            if eliminate_cycles_method == "cut_weakest" and tree is None:
                raise RuntimeError(
                    "'cut_weakest' option requires credibility as " "an additional input (apart from outranking)."
                )
            d.credibility = _get_alternatives_comparisons(tree, alternatives, categories_profiles=categories_profiles)

        elif p == "criteria":
            if trees.has_key("criteria"):
                d.criteria = px.getCriteriaID(trees["criteria"])

        elif p == "cut_threshold":
            cut_threshold = px.getParameterByName(trees["method_parameters"], "cut_threshold")
            if cut_threshold is None or not (0 <= float(cut_threshold) <= 1):
                raise RuntimeError(
                    "'cut_threshold' should be in range [0, 1] " "(most commonly used values are 0.6 or 0.7)."
                )
            d.cut_threshold = cut_threshold

        # 'cv_crossed' == 'counter-veto crossed'
        elif p == "cv_crossed":
            alternatives = px.getAlternativesID(trees["alternatives"])
            comparison_with = px.getParameterByName(trees["method_parameters"], "comparison_with")
            if comparison_with in ("boundary_profiles", "central_profiles"):
                categories_profiles = _get_categories_profiles(trees["categories_profiles"], comparison_with)
            else:
                categories_profiles = None
            d.cv_crossed = _get_alternatives_comparisons(
                trees["counter_veto_crossed"],
                alternatives,
                categories_profiles=categories_profiles,
                use_partials=True,
                mcda_concept="counterVetoCrossed",
            )

        elif p == "discordance":

            alternatives = px.getAlternativesID(trees["alternatives"])

            comparison_with = kwargs.get("comparison_with")

            if trees.has_key("methos_parameters"):
                comparison_with = px.getParameterByName(trees["method_parameters"], "comparison_with")

            if kwargs.get("use_partials") is not None:
                use_partials = kwargs.get("use_partials")
            else:
                if trees.has_key("methos_parameters"):
                    parameter = px.getParameterByName(trees["method_parameters"], "use_partials")
                    use_partials = True if parameter == "true" else False

            categories_profiles = None
            profiles = None

            if comparison_with in ("boundary_profiles", "central_profiles"):
                categories_profiles = _get_categories_profiles(trees["categories_profiles"], comparison_with)
            if comparison_with == "profiles":
                profiles = px.getProfilesID(trees["profiles"])

            d.discordance = _get_alternatives_comparisons(
                trees["discordance"],
                alternatives,
                profiles=profiles,
                categories_profiles=categories_profiles,
                use_partials=use_partials,
            )

        elif p == "crisp_discordance":

            alternatives = px.getAlternativesID(trees["alternatives"])

            comparison_with = kwargs.get("comparison_with")

            if trees.has_key("methos_parameters"):
                comparison_with = px.getParameterByName(trees["method_parameters"], "comparison_with")

            if kwargs.get("use_partials") is not None:
                use_partials = kwargs.get("use_partials")
            else:
                if trees.has_key("methos_parameters"):
                    parameter = px.getParameterByName(trees["method_parameters"], "use_partials")
                    use_partials = True if parameter == "true" else False

            categories_profiles = None
            profiles = None

            if comparison_with in ("boundary_profiles", "central_profiles"):
                categories_profiles = _get_categories_profiles(trees["categories_profiles"], comparison_with)
            if comparison_with == "profiles":
                profiles = px.getProfilesID(trees["profiles"])

            d.discordance = _get_alternatives_comparisons(
                trees["discordance"],
                alternatives,
                profiles=profiles,
                categories_profiles=categories_profiles,
                use_partials=use_partials,
                use_value=False,
            )

        elif p == "preorder":

            if trees.has_key("preorder"):
                alternatives = px.getAlternativesID(trees["alternatives"])
                d.preorder = px.getAlternativeValue(trees["preorder"], alternatives, None)

        elif p == "downwards":

            alternatives = px.getAlternativesID(trees["alternatives"])
            d.downwards = px.getAlternativeValue(trees["downwards"], alternatives, None)

        elif p == "upwards":

            alternatives = px.getAlternativesID(trees["alternatives"])
            d.upwards = px.getAlternativeValue(trees["upwards"], alternatives, None)

        elif p == "eliminate_cycles_method":
            d.eliminate_cycles_method = px.getParameterByName(trees["method_parameters"], "eliminate_cycles_method")

        elif p == "interactions":
            criteria = px.getCriteriaID(trees["criteria"])
            d.interactions = _get_criteria_interactions(trees["interactions"], criteria)

        elif p == "outranking":
            alternatives = px.getAlternativesID(trees["alternatives"])
            outranking = _get_intersection_distillation(trees["outranking"], alternatives)
            if outranking == None:
                outranking = px.getAlternativesComparisons(trees["outranking"], alternatives)
            if outranking == {}:
                outranking = _get_outranking(trees["outranking"])
            d.outranking = outranking
        elif p == "nonoutranking":
            if trees.has_key("nonoutranking"):
                alternatives = px.getAlternativesID(trees["alternatives"])
                nonoutranking = _get_intersection_distillation(trees["nonoutranking"], alternatives)
                if nonoutranking == None:
                    nonoutranking = px.getAlternativesComparisons(trees["nonoutranking"], alternatives)
                if nonoutranking == {}:
                    nonoutranking = _get_outranking(trees["nonoutranking"])
                d.nonoutranking = nonoutranking
        elif p == "performances":
            d.performances = px.getPerformanceTable(trees["performance_table"], None, None)

        elif p == "pref_directions":
            criteria = px.getCriteriaID(trees["criteria"])
            d.pref_directions = px.getCriteriaPreferenceDirections(trees["criteria"], criteria)

        elif p == "profiles_performance_table":
            if comparison_with in ("boundary_profiles", "central_profiles"):
                tree = trees.get("profiles_performance_table")
                if tree is None:
                    msg = (
                        "Missing profiles performance table (did you forget "
                        "to provide 'profiles_performance_table.xml' file?)."
                    )
                    raise RuntimeError(msg)
                d.profiles_performance_table = px.getPerformanceTable(tree, None, None)
            else:
                d.profiles_performance_table = None

        elif p == "reinforcement_factors":
            criteria = px.getCriteriaID(trees["criteria"])
            factors = {}
            for c in criteria:
                rf = px.getCriterionValue(trees["reinforcement_factors"], c, "reinforcement_factors")
                if len(rf) == 0:
                    continue
                if rf.get(c) <= 1:
                    msg = (
                        "Reinforcement factor for criterion '{}' should be "
                        "higher than 1.0 (ideally between 1.2 and 1.5)."
                    )
                    raise RuntimeError(msg)
                factors.update(rf)
            d.reinforcement_factors = factors

        elif p == "thresholds":
            criteria = px.getCriteriaID(trees["criteria"])
            d.thresholds = _get_thresholds(trees["criteria"])

        elif p == "weights":
            criteria = px.getCriteriaID(trees["criteria"])
            d.weights = px.getCriterionValue(trees["weights"], criteria)

        elif p == "z_function":
            d.z_function = px.getParameterByName(trees["method_parameters"], "z_function")

        elif p == "with_denominator":
            parameter = px.getParameterByName(trees["method_parameters"], "with_denominator")
            d.with_denominator = True if parameter == "true" else False

        elif p == "use_partials":
            parameter = px.getParameterByName(trees["method_parameters"], "use_partials")
            d.use_partials = True if parameter == "true" else False

        elif p == "use_pre_veto":
            parameter = px.getParameterByName(trees["method_parameters"], "use_pre_veto")
            d.use_pre_veto = True if parameter == "true" else False

        elif p == "alpha":
            d.alpha = px.getParameterByName(trees["method_parameters"], "alpha")

        elif p == "beta":
            d.beta = px.getParameterByName(trees["method_parameters"], "beta")

        elif p == "s1":
            d.s1 = px.getParameterByName(trees["method_parameters"], "s1")

        elif p == "s2":
            d.s2 = px.getParameterByName(trees["method_parameters"], "s2")

        elif p == "crisp_outranking":
            d.crisp_outranking = px.getParameterByName(trees["method_parameters"], "crisp_outranking")

        elif p == "direction":
            d.direction = px.getParameterByName(trees["method_parameters"], "direction")

        elif p == "conc_threshold":
            d.conc_threshold = px.getParameterByName(trees["method_parameters"], "conc_threshold")

        elif p == "disc_threshold":
            d.disc_threshold = px.getParameterByName(trees["method_parameters"], "disc_threshold")

        elif p == "comprehensive":
            d.comprehensive = px.getParameterByName(trees["method_parameters"], "comprehensive")

        else:
            raise RuntimeError("Unknown parameter '{}' specified.".format(p))

    return d