示例#1
0
 def get_credibility(*args, **kwargs):
     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 InputDataError(
             "'cut_weakest' option requires credibility as an additional "
             "input (apart from outranking)."
         )
     credibility = _get_alternatives_comparisons(
         tree,
         alternatives,
         categories_profiles=categories_profiles,
     )
     return credibility  # NoneType, Vividict
示例#2
0
 def get_credibility(*args, **kwargs):
     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 InputDataError(
             "'cut_weakest' option requires credibility as an additional "
             "input (apart from outranking)."
         )
     credibility = _get_alternatives_comparisons(
         tree,
         alternatives,
         categories_profiles=categories_profiles,
     )
     return credibility  # NoneType, Vividict
示例#3
0
 def get_discordance(*args, **kwargs):
     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
     discordance = _get_alternatives_comparisons(
         trees['discordance'],
         alternatives,
         categories_profiles=categories_profiles,
         use_partials=use_partials,
     )
     return discordance  # Vividict
def get_input_data(input_dir):
    file_names = (
        'alternatives.xml',
        'method_parameters.xml',
        'outranking.xml',
    )
    trees = get_trees(input_dir, file_names)

    alternatives = px.getAlternativesID(trees['alternatives'])
    alternatives.sort()
    outranking = get_intersection_distillation(trees['outranking'],
                                               alternatives)
    if outranking == None:
        outranking = px.getAlternativesComparisons(trees['outranking'],
                                                   alternatives)
    eliminate_cycles_method = px.getParameterByName(trees['method_parameters'],
                                                    'eliminate_cycles_method')
    if eliminate_cycles_method not in ['aggregate', 'cut_weakest']:
        raise RuntimeError("Invalid/missing method for cycle elimination.")
    cut_threshold = px.getParameterByName(trees['method_parameters'],
                                          'cut_threshold')
    check_cut_threshold(cut_threshold)

    ret = {
        'alternatives': alternatives,
        'cut_threshold': cut_threshold,
        'eliminate_cycles_method': eliminate_cycles_method,
        'outranking': outranking,
    }
    return ret
示例#5
0
 def get_discordance(*args, **kwargs):
     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
     discordance = _get_alternatives_comparisons(
         trees['discordance'],
         alternatives,
         categories_profiles=categories_profiles,
         use_partials=use_partials,
     )
     return discordance  # Vividict
def get_input_data(input_dir):
    file_names = (
        'alternatives.xml',
        'method_parameters.xml',
        'outranking.xml',
    )
    trees = get_trees(input_dir, file_names)

    alternatives = px.getAlternativesID(trees['alternatives'])
    alternatives.sort()
    outranking = get_intersection_distillation(trees['outranking'], alternatives)
    if outranking == None:
        outranking = px.getAlternativesComparisons(trees['outranking'], alternatives)
    eliminate_cycles_method = px.getParameterByName(trees['method_parameters'],
                                                    'eliminate_cycles_method')
    if eliminate_cycles_method not in ['aggregate', 'cut_weakest']:
        raise RuntimeError("Invalid/missing method for cycle elimination.")
    cut_threshold = px.getParameterByName(trees['method_parameters'], 'cut_threshold')
    check_cut_threshold(cut_threshold)

    ret = {
        'alternatives': alternatives,
        'cut_threshold': cut_threshold,
        'eliminate_cycles_method': eliminate_cycles_method,
        'outranking': outranking,
    }
    return ret
def get_input_data(input_dir):
    file_names = (
        'alternatives.xml',
        'concordance.xml',
        'method_parameters.xml',
        'discordance_binary.xml',
    )
    trees = get_trees(input_dir, file_names)

    alternatives = px.getAlternativesID(trees['alternatives'])
    # we can also get alternatives from 'concordance.xml', therefore 'alternatives.xml'
    # can be optional - like here:
    # alternatives = list(set([i.text for i in trees['concordance'].findall(".//alternativeID")]))
    concordance = px.getAlternativesComparisons(trees['concordance'],
                                                alternatives)
    discordance_binary = px.getAlternativesComparisons(
        trees['discordance_binary'], alternatives)
    cut_threshold = px.getParameterByName(trees['method_parameters'],
                                          'cut_threshold')
    check_cut_threshold(cut_threshold)

    ret = {
        'alternatives': alternatives,
        'concordance': concordance,
        'cut_threshold': cut_threshold,
        'discordance_binary': discordance_binary,
    }
    return ret
示例#8
0
def makeDistilation(
    in_selected, in_alternatives, in_credibility, out_descending, out_ascending, out_medianPreorder, out_final
):
    xml_alternatives = px.parseValidate(in_alternatives)
    xml_selected = px.parseValidate(in_selected)
    xml_alternatives = px.parseValidate(in_alternatives)
    xml_credibility = px.parseValidate(in_credibility)
    if xml_selected == None:
        raise ValueError, "Invalid selected file"
    if xml_alternatives == None:
        raise ValueError, "Invalid alternative file"
    if xml_credibility == None:
        raise ValueError, "Invalid credibility file"

    onCriterion = px.getParameterByName(xml_selected, "selectedCriterion")
    alternativesID = px.getAlternativesID(xml_alternatives)
    alternativesComparisions = getAlternativesComparisonsAtCriteria(xml_credibility, alternativesID)

    if not alternativesComparisions.has_key(onCriterion):
        raise ValueError, "Invalid selected criterion"

    distillation = Distillation(alternativesID, alternativesComparisions[onCriterion])

    output_distillation(out_ascending, distillation.downwards(), "downward distillation", onCriterion)
    output_distillation(out_descending, distillation.upwards(), "upward distillation", onCriterion)
    output_distillation(out_medianPreorder, distillation.medianPreorder(), "median Preorder", onCriterion)

    writeAlternativeComparision(out_final, distillation.intersectionUpDowns, "outranks", onCriterion)
示例#9
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',
        'concordance.xml',
        'method_parameters.xml',
        'discordance_binary.xml',
    )
    trees = get_trees(input_dir, file_names)

    alternatives = px.getAlternativesID(trees['alternatives'])
    # we can also get alternatives from 'concordance.xml', therefore 'alternatives.xml'
    # can be optional - like here:
    # alternatives = list(set([i.text for i in trees['concordance'].findall(".//alternativeID")]))
    concordance = px.getAlternativesComparisons(trees['concordance'], alternatives)
    discordance_binary = px.getAlternativesComparisons(trees['discordance_binary'], alternatives)
    cut_threshold = px.getParameterByName(trees['method_parameters'], 'cut_threshold')
    check_cut_threshold(cut_threshold)

    ret = {
        'alternatives': alternatives,
        'concordance': concordance,
        'cut_threshold': cut_threshold,
        'discordance_binary': discordance_binary,
    }
    return ret
示例#11
0
def makeDistilation(in_selected, in_alternatives, in_credibility,
                    out_descending, out_ascending, out_medianPreorder,
                    out_final):
    xml_alternatives = px.parseValidate(in_alternatives)
    xml_selected = px.parseValidate(in_selected)
    xml_alternatives = px.parseValidate(in_alternatives)
    xml_credibility = px.parseValidate(in_credibility)
    if xml_selected == None:
        raise ValueError, "Invalid selected file"
    if xml_alternatives == None:
        raise ValueError, "Invalid alternative file"
    if xml_credibility == None:
        raise ValueError, "Invalid credibility file"

    onCriterion = px.getParameterByName(xml_selected, 'selectedCriterion')
    alternativesID = px.getAlternativesID(xml_alternatives)
    alternativesComparisions = getAlternativesComparisonsAtCriteria(
        xml_credibility, alternativesID)

    if not alternativesComparisions.has_key(onCriterion):
        raise ValueError, 'Invalid selected criterion'

    distillation = Distillation(alternativesID,
                                alternativesComparisions[onCriterion])

    output_distillation(out_ascending, distillation.downwards(),
                        'downward distillation', onCriterion)
    output_distillation(out_descending, distillation.upwards(),
                        'upward distillation', onCriterion)
    output_distillation(out_medianPreorder, distillation.medianPreorder(),
                        'median Preorder', onCriterion)

    writeAlternativeComparision(out_final, distillation.intersectionUpDowns,
                                'outranks', onCriterion)
def get_input_data(input_dir):
    file_names = (
        'alternatives.xml',
        'categories.xml',
        'categoriesProfiles.xml',
        'credibility.xml',
        'method_parameters.xml',
    )
    trees = get_trees(input_dir, file_names)

    alternatives = px.getAlternativesID(trees['alternatives'])
    categories = px.getCategoriesID(trees['categories'])
    categories_rank = px.getCategoriesRank(trees['categories'], categories)
    categories_profiles = get_categories_profiles_central(trees['categoriesProfiles'])
    credibility = getAlternativesComparisons(trees['credibility'], alternatives,
                                             categories_profiles)
    cut_threshold = px.getParameterByName(trees['method_parameters'], 'cut_threshold')
    check_cut_threshold(cut_threshold)

    ret = {
        'alternatives': alternatives,
        'categories_rank': categories_rank,
        'categories_profiles': categories_profiles,
        'credibility': credibility,
        'cut_threshold': cut_threshold,
    }
    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
def get_input_data(input_dir):
    file_names = (
        'alternatives.xml',
        'categoriesProfiles.xml',
        'credibility.xml',
        'method_parameters.xml',
    )
    trees = get_trees(input_dir, file_names)

    alternatives = px.getAlternativesID(trees['alternatives'])
    cp_tree = trees['categoriesProfiles']
    categories = list(
        set(cp_tree.xpath('//categoriesProfiles//limits//categoryID/text()')))
    categories_profiles = px.getCategoriesProfiles(trees['categoriesProfiles'],
                                                   categories)
    profiles_names = [
        p for p in cp_tree.xpath('//categoriesProfiles//alternativeID/text()')
    ]
    credibility = getAlternativesComparisons(trees['credibility'],
                                             alternatives, profiles_names)
    cut_threshold = px.getParameterByName(trees['method_parameters'],
                                          'cut_threshold')
    check_cut_threshold(cut_threshold)

    ret = {
        'alternatives': alternatives,
        'categories_profiles': categories_profiles,
        'credibility': credibility,
        'cut_threshold': cut_threshold,
    }
    return ret
示例#15
0
 def get_cut_threshold(*args, **kwargs):
     cut_threshold = px.getParameterByName(
         trees['method_parameters'],
         'cut_threshold',
     )
     if cut_threshold is None or not (0 <= float(cut_threshold) <= 1):
         raise InputDataError("'cut_threshold' should be in range [0, 1] "
                              "(most commonly used values are 0.6 or 0.7).")
     return cut_threshold  # float
示例#16
0
 def get_profiles_categories(*args, **kwargs):
     #profilesCategories = px.getProfilesCategories(trees['categories_profiles'], None)
     comparison_with = kwargs.get('comparison_with')
     if comparison_with is None:
         comparison_with = px.getParameterByName(
             trees['method_parameters'],
             'comparison_with',
         )
     if comparison_with in ('boundary_profiles', 'central_profiles'):
         profilesCategories = _get_profiles_categories(trees['categories_profiles'], comparison_with, trees['categories'])
     return profilesCategories
示例#17
0
 def get_cut_threshold(*args, **kwargs):
     cut_threshold = px.getParameterByName(
         trees['method_parameters'],
         'cut_threshold',
     )
     if cut_threshold is None or not (0 <= float(cut_threshold) <= 1):
         raise InputDataError(
             "'cut_threshold' should be in range [0, 1] "
             "(most commonly used values are 0.6 or 0.7)."
         )
     return cut_threshold  # float
示例#18
0
 def get_categories_profiles(*args, **kwargs):
     comparison_with = kwargs.get('comparison_with')
     if comparison_with is None:
         comparison_with = px.getParameterByName(
             trees['method_parameters'],
             'comparison_with',
         )
     categories_profiles = _get_categories_profiles(
         trees.get('categories_profiles'),
         comparison_with,
     )
     return categories_profiles  # NoneType, dict, list
示例#19
0
 def get_categories_profiles(*args, **kwargs):
     comparison_with = kwargs.get('comparison_with')
     if comparison_with is None:
         comparison_with = px.getParameterByName(
             trees['method_parameters'],
             'comparison_with',
         )
     categories_profiles = _get_categories_profiles(
         trees.get('categories_profiles'),
         comparison_with,
     )
     return categories_profiles  # NoneType, dict, list
示例#20
0
def makeSelection(in_selected, in_outranking, out_outranking):
    xml_outranking = px.parseValidate(in_outranking)
    xml_selected = px.parseValidate(in_selected)
    if xml_selected == None:
        raise ValueError, "Invalid selected file"
    if xml_outranking == None:
        raise ValueError, "Invalid xml_outranking file"

    onCriterion = px.getParameterByName(xml_selected, 'selectedCriterion')
    alternativesComparisions = getAlternativesComparisonsAtCriteria(xml_outranking)

    if not alternativesComparisions.has_key(onCriterion):
        raise ValueError, 'Invalid selected criterion'
    writeAlternativeComparisionOnCriterion(out_outranking, alternativesComparisions, onCriterion, 'outranks')
示例#21
0
 def get_profiles_performance_table(*args, **kwargs):
     comparison_with = px.getParameterByName(
         trees['method_parameters'],
         'comparison_with',
     )
     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 InputDataError(msg)
         profiles_performance_table = px.getPerformanceTable(
             tree, None, None)
     else:
         profiles_performance_table = None
     return profiles_performance_table  # NoneType, dict
示例#22
0
 def get_profiles_performance_table(*args, **kwargs):
     comparison_with = px.getParameterByName(
         trees['method_parameters'],
         'comparison_with',
     )
     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 InputDataError(msg)
         profiles_performance_table = px.getPerformanceTable(tree, None, None)
     else:
         profiles_performance_table = None
     return profiles_performance_table  # NoneType, dict
def check_concordance(xml_concordance, hierarchyArray, outfile):
    ret = ""
    concordances = px.getCriterionValue(xml_concordance,
                                        [(getOriginalName(v))
                                         for v, k in hierarchyArray.items()],
                                        'Concordance')
    if concordances.__len__() > 0:
        concordances = calculate_new_concordance(concordances, hierarchyArray)
        output_criteriaValues(outfile, concordances, 'Concordance')
    else:
        check = px.getParameterByName(xml_concordance, 'percentage',
                                      'concordanceLevel')
        if check != None:
            content = """
    <methodParameters name="concordanceLevel">
        <parameter name="percentage">
            <value><real>%f</real></value>
        </parameter>
    </methodParameters>""" % check
            write_xmcda_content(outfile, content)
def check_concordance(xml_concordance, hierarchyArray, outfile):
    ret = ""
    concordances = px.getCriterionValue(
        xml_concordance, [(getOriginalName(v)) for v, k in hierarchyArray.items()], "Concordance"
    )
    if concordances.__len__() > 0:
        concordances = calculate_new_concordance(concordances, hierarchyArray)
        output_criteriaValues(outfile, concordances, "Concordance")
    else:
        check = px.getParameterByName(xml_concordance, "percentage", "concordanceLevel")
        if check != None:
            content = (
                """
    <methodParameters name="concordanceLevel">
        <parameter name="percentage">
            <value><real>%f</real></value>
        </parameter>
    </methodParameters>"""
                % check
            )
            write_xmcda_content(outfile, content)
示例#25
0
def get_criterion_concordance_cutting_level_value (xmltree, mcdaConcept=None) :

    if mcdaConcept == None :
        strSearch = "criteriaValues"
    else :
        strSearch = "criteriaValues[@mcdaConcept=\'" + mcdaConcept + "\']"
    try:
        criteriaValues = xmltree.xpath(strSearch)[0]
    except:
        criteriaValues = None
        #return {}

    if criteriaValues is None:
        return px.getParameterByName(xmltree, 'percentage', 'concordanceLevel')

    values = {}

    for criterionValue in criteriaValues.findall("./criterionValue"):
        crit = criterionValue.find ("criterionID").text
        values[crit] = px.getValue (criterionValue)

    return values
示例#26
0
 def get_cv_crossed(*args, **kwargs):
     # 'cv_crossed' stands for 'counter-veto 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
     cv_crossed = _get_alternatives_comparisons(
         trees['counter_veto_crossed'],
         alternatives,
         categories_profiles=categories_profiles,
         use_partials=True,
         mcda_concept='counterVetoCrossed',
     )
     return cv_crossed  # Vividict
示例#27
0
 def get_concordance(*args, **kwargs):
     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,
         )
         concordance = _get_alternatives_comparisons(
             trees['concordance'],
             alternatives,
             categories_profiles,
         )
     else:
         concordance = px.getAlternativesComparisons(
             trees['concordance'],
             alternatives,
         )
     return concordance  # Vividict, dict
示例#28
0
 def get_concordance(*args, **kwargs):
     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,
         )
         concordance = _get_alternatives_comparisons(
             trees['concordance'],
             alternatives,
             categories_profiles,
         )
     else:
         concordance = px.getAlternativesComparisons(
             trees['concordance'],
             alternatives,
         )
     return concordance  # Vividict, dict
示例#29
0
 def get_cv_crossed(*args, **kwargs):
     # 'cv_crossed' stands for 'counter-veto 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
     cv_crossed = _get_alternatives_comparisons(
         trees['counter_veto_crossed'],
         alternatives,
         categories_profiles=categories_profiles,
         use_partials=True,
         mcda_concept='counterVetoCrossed',
     )
     return cv_crossed  # Vividict
示例#30
0
文件: MccP.py 项目: paterijk/MccP
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 not os.path.isfile(in_dir + "/alternatives.xml"):
        errorList.append("alternatives.xml is missing")
    if not os.path.isfile(in_dir + "/relation.xml"):
        errorList.append("relation.xml is missing")
    if not os.path.isfile(in_dir + "/parameters.xml"):
        errorList.append("parameters.xml is missing")

    if not errorList:
        # We parse all the mandatory input files
        xmltree_alternatives = PyXMCDA.parseValidate(in_dir +
                                                     "/alternatives.xml")
        xmltree_relation = PyXMCDA.parseValidate(in_dir + "/relation.xml")
        xmltree_parameters = PyXMCDA.parseValidate(in_dir + "/parameters.xml")

        # We check if all madatory input files are valid
        if xmltree_alternatives == None:
            errorList.append("alternatives.xml can't be validated.")
        if xmltree_relation == None:
            errorList.append("relation.xml can't be validated.")
        if xmltree_parameters == None:
            errorList.append("parameters.xml can't be validated.")

        if not errorList:

            alternativesId = PyXMCDA.getAlternativesID(xmltree_alternatives)
            alternativesRel = PyXMCDA.getAlternativesComparisons(
                xmltree_relation, alternativesId)
            relationMin = PyXMCDA.getParameterByName(xmltree_parameters, "min")
            relationMax = PyXMCDA.getParameterByName(xmltree_parameters, "max")
            relationCut = PyXMCDA.getParameterByName(xmltree_parameters, "cut")
            relationBipolar = PyXMCDA.getParameterByName(
                xmltree_parameters, "bipolar")

            if not alternativesId:
                errorList.append("No active alternatives found.")
            if not alternativesRel:
                errorList.append("Problems between relation and alternatives.")
            if relationMin is None:
                errorList.append("No \'min\' method parameter found")
            if relationMax is None:
                errorList.append("No \'max\' method parameter found")
            if relationCut is None:
                errorList.append("No \'cut\' method parameter found")
            if relationBipolar is None:
                errorList.append("No \'bipolar\' method parameter found")

        if not errorList:

            alg = algMccP(alternativesId, alternativesRel, relationMin,
                          relationMax, relationCut, relationBipolar)
            results = alg.Run()

            fo = open(out_dir + "/output.xml", 'w')
            PyXMCDA.writeHeader(fo)
            fo.write('<alternativesAffectations>\n')
            for o in results:
                fo.write('\t<alternativeAffectation>\n\t\t<alternativeID>' +
                         o + '</alternativeID>\n\t\t\t<categoryID>' +
                         results[o] +
                         '</categoryID>\n\t</alternativeAffectation>\n')
            fo.write('</alternativesAffectations>')
            PyXMCDA.writeFooter(fo)
            fo.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()
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()
示例#32
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 lists for error and log messages
	errorList = []
	logList = []
	
	# If some mandatory input files are missing
	if not os.path.isfile (in_dir+"/alternatives.xml") or not os.path.isfile (in_dir+"/categories.xml") or not os.path.isfile (in_dir+"/categoriesProfiles.xml") or not os.path.isfile (in_dir+"/stabilityRelation.xml"):
		errorList.append("Some input files are missing")
	
	else :
		
		if os.path.isfile (in_dir+"/sortingMode.xml") :
			xmltree_mode = PyXMCDA.parseValidate(in_dir+"/sortingMode.xml")
			if xmltree_mode == None :
				errorList.append ("sortingMode file cannot be validated.")
			else :
				mode = PyXMCDA.getParameterByName (xmltree_mode, "sortingMode")
				if not (mode == "pessimistic" or mode == "optimistic"):
					errorList.append ("Value of parameter sortingMode should be 'pessimistic' or 'optimistic'.")			
			
		xmltree_alternatives = PyXMCDA.parseValidate(in_dir+"/alternatives.xml")
		xmltree_categories = PyXMCDA.parseValidate(in_dir+"/categories.xml")
		xmltree_profiles = PyXMCDA.parseValidate(in_dir+"/categoriesProfiles.xml")
		xmltree_altStability = PyXMCDA.parseValidate(in_dir+"/stabilityRelation.xml")
		
		if xmltree_alternatives == None :
			errorList.append("The alternatives file cannot be validated.")
		if xmltree_categories == None :
			errorList.append("The categories file cannot be validated.")
		if xmltree_profiles == None :
			errorList.append("The categoriesProfiles file cannot be validated.")
		if xmltree_altStability == None :
			errorList.append("The alternatives comparisons file cannot be validated.")
				
		if not errorList :
		
			alternativesId = PyXMCDA.getAlternativesID(xmltree_alternatives, "ACTIVEREAL")
			allalt = PyXMCDA.getAlternativesID(xmltree_alternatives, "ACTIVE")
			categoriesId = PyXMCDA.getCategoriesID(xmltree_categories)
			categoriesRank = PyXMCDA.getCategoriesRank(xmltree_categories, categoriesId)
			altStability = PyXMCDA.getAlternativesComparisons (xmltree_altStability, allalt)
			
			if not alternativesId:
				errorList.append("No alternatives found.")
			if not categoriesId:
				errorList.append("No categories found.")
			if not altStability :
				errorList.append("No alternatives comparisons found.")
					
	if not errorList :
		
		catPro = PyXMCDA.getCategoriesProfiles(xmltree_profiles, categoriesId)
		proCat = PyXMCDA.getProfilesCategories(xmltree_profiles, categoriesId)
		profilesId = proCat.keys()
		
		# On retourne la liste pour trier selon les rangs
		rankCategories = {}
		for i, j in categoriesRank.iteritems():
			rankCategories[j] = i
			
		ranks = rankCategories.keys()[:]
		
		ranks.sort()
		lowestRank = ranks.pop()
		
		# Un tableau pour conserver les affectations
		affectations = {}
		
		if mode == "pessimistic":
			# Electre tri pessimistic rule
			for alt in alternativesId:
				affectations[alt] = []
				for rank in ranks:
					profile = catPro[rankCategories[rank]]["lower"]
					if altStability[alt][profile] >= -1 and altStability[alt][profile] <= 1:
						# Surclassement instable, on ajoute les categories sup et inf
						if affectations[alt].count(proCat[profile]["lower"]) == 0:
							affectations[alt].append(proCat[profile]["lower"])
						if affectations[alt].count(proCat[profile]["upper"]) == 0:
							affectations[alt].append(proCat[profile]["upper"])
					if altStability[alt][profile] > 1:
						# Surclassement stable, on ajoute que sup et on arrete
						if affectations[alt].count(proCat[profile]["upper"]) == 0:
							affectations[alt].append(proCat[profile]["upper"])
							break
				
				if affectations[alt] == []:
					# Tous les surc stables et negatifs, on force la categorie la plus basse
					affectations[alt] = [rankCategories[lowestRank]]

		else:
			errorList.append("Optimistic rule is not taken into account yet")
	
	if not errorList :	
					
		# Creating alternativesAffectations file
		fileAffectations = open(out_dir+"/alternativesAffectations.xml",'w')
		PyXMCDA.writeHeader(fileAffectations)
		
		# We write some information about the generated file
		fileAffectations.write ("\t<projectReference>\n\t\t<title>Stable alternatives affectation</title>\n\t\t<version>"+VERSION+"</version>\n\t\t<author>ws_PyXMCDA suite (TV)</author>\n\t</projectReference>\n\n")
		
		fileAffectations.write("\t<alternativesAffectations>\n")
		
		for alt in alternativesId:
			fileAffectations.write("\t\t<alternativeAffectation>\n\t\t\t<alternativeID>"+alt+"</alternativeID>\n\t\t\t<categoriesSet>\n")
			
			for cat in affectations[alt]:
				fileAffectations.write("\t\t\t\t<element><categoryID>"+cat+"</categoryID></element>\n")

			fileAffectations.write("\t\t\t</categoriesSet>\n\t\t</alternativeAffectation>\n")
		
		fileAffectations.write("\t</alternativesAffectations>\n")
		PyXMCDA.writeFooter(fileAffectations)
		fileAffectations.close()
	
	
	# Creating log and error file, messages.xml
	fileMessages = open(out_dir+"/messages.xml", 'w')
	PyXMCDA.writeHeader (fileMessages)
	
	if not errorList :
		logList.append("Execution ok")
		PyXMCDA.writeLogMessages (fileMessages, logList)
	else :
		PyXMCDA.writeErrorMessages (fileMessages, errorList)
		
	PyXMCDA.writeFooter(fileMessages)
	fileMessages.close()
示例#33
0
文件: MccP.py 项目: paterijk/MccP
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 not os.path.isfile (in_dir+"/alternatives.xml"):
        errorList.append("alternatives.xml is missing")
    if not os.path.isfile (in_dir+"/relation.xml"):
        errorList.append("relation.xml is missing")
    if not os.path.isfile (in_dir+"/parameters.xml"):
        errorList.append("parameters.xml is missing")

    if not errorList:
        # We parse all the mandatory input files
        xmltree_alternatives = PyXMCDA.parseValidate(in_dir+"/alternatives.xml")
        xmltree_relation = PyXMCDA.parseValidate(in_dir+"/relation.xml")
        xmltree_parameters = PyXMCDA.parseValidate(in_dir+"/parameters.xml")
        
        # We check if all madatory input files are valid
        if xmltree_alternatives == None :
            errorList.append("alternatives.xml can't be validated.")
        if xmltree_relation == None :
            errorList.append("relation.xml can't be validated.")
        if xmltree_parameters == None :
            errorList.append("parameters.xml can't be validated.")
            
        if not errorList :

            alternativesId = PyXMCDA.getAlternativesID(xmltree_alternatives)
            alternativesRel = PyXMCDA.getAlternativesComparisons (xmltree_relation, alternativesId)
            relationMin = PyXMCDA.getParameterByName (xmltree_parameters, "min")
            relationMax = PyXMCDA.getParameterByName (xmltree_parameters, "max")
            relationCut = PyXMCDA.getParameterByName (xmltree_parameters, "cut")
            relationBipolar = PyXMCDA.getParameterByName (xmltree_parameters, "bipolar")

            if not alternativesId :
                errorList.append("No active alternatives found.")
            if not alternativesRel :
                errorList.append("Problems between relation and alternatives.")
            if relationMin is None:
                errorList.append("No \'min\' method parameter found")
            if relationMax is None:
                errorList.append("No \'max\' method parameter found")
            if relationCut is None:
                errorList.append("No \'cut\' method parameter found")
            if relationBipolar is None:
                errorList.append("No \'bipolar\' method parameter found")

        if not errorList :
            
            alg = algMccP(alternativesId, alternativesRel, relationMin, relationMax, relationCut, relationBipolar)
            results = alg.Run()
            
            fo = open(out_dir+"/output.xml",'w')
            PyXMCDA.writeHeader(fo)
            fo.write('<alternativesAffectations>\n')
            for o in results:
                fo.write('\t<alternativeAffectation>\n\t\t<alternativeID>'+o+'</alternativeID>\n\t\t\t<categoryID>'+results[o]+'</categoryID>\n\t</alternativeAffectation>\n')
            fo.write('</alternativesAffectations>')
            PyXMCDA.writeFooter(fo)
            fo.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()
示例#34
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
示例#35
0
 def get_param_boolean(param_name, *args, **kwargs):
     parameter = px.getParameterByName(
         trees['method_parameters'],
         param_name,
     )
     return True if parameter == 'true' else False
示例#36
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
	
	# Initialising a list of dictionaries for the preferences
	prefDir = {}
	critType = {}
	lowerBound = {}
	upperBound = {}
	levelNumber = {}
	thresholds = {}
	
	# Creating a list for error messages
	errorList = []
	
	# If some mandatory input files are missing
	if not os.path.isfile (in_dir+"/nbCriteria.xml") and not os.path.isfile (in_dir+"/criteriaNames.xml") :
		errorList.append("No parameter has been provided. You should provide a number of criteria (using nbCriteria.xml file) or a list of criteria names (using criteriaNames.xml file).")
	
	else :
		
		# User provides a list of criteria names
		if os.path.isfile (in_dir+"/criteriaNames.xml") :
		
			# We parse the input file
			xmltree_CritNames = PyXMCDA.parseValidate(in_dir+"/criteriaNames.xml")
			if xmltree_CritNames == None :
				errorList.append ("criteriaNames file can't be validated.")
			
			else :
				# We record the criteria names in critNames
				critNames = PyXMCDA.getParametersByName (xmltree_CritNames, "criteriaNames")
				
				if not critNames :
					errorList.append ("No criterion name has been found in criteriaNames file. Is your file correct ?")
			
		# User provides a number of criteria
		else :
		
			# We parse the input file
			xmltree_nbCrit = PyXMCDA.parseValidate(in_dir+"/nbCriteria.xml")
			if xmltree_nbCrit == None :
				errorList.append ("nbCriteria file can't be validated.")
					
			else :
				
				nbCrit = PyXMCDA.getParameterByName (xmltree_nbCrit, "nbCriteria")
					
				# We check the validity of the parameter
				if not nbCrit :
					errorList.append ("nbCriteria parameter not provided. It should be a strict positive integer.")
				if not errorList and not isinstance(nbCrit,int) :
					errorList.append ("nbCriteria value should be a strict positive integer.")
				if not errorList and nbCrit <= 0 :
					errorList.append ("nbCriteria value should be a scrict positive integer.")
					
				# We check if a prefix parameter has been provided
				if not errorList :
					if os.path.isfile (in_dir+"/criteriaPrefix.xml") :
						xmltree_CritPrefix = PyXMCDA.parseValidate(in_dir+"/criteriaPrefix.xml")
						if xmltree_CritPrefix == None :
							errorList.append ("criteriaPrefix file can't be validated.")
						else :
							critPrefix = PyXMCDA.getParameterByName (xmltree_CritPrefix, "criteriaPrefix")
							
							# We check the validity of the parameter
							if not isinstance(critPrefix,str) :
								errorList.append ("criteriaPrefix parameter should be a label")
					
					else :
						# If no prefix has been provided, the criteria will be called g1, g2, ...
						critPrefix = "g"
						
				if not errorList :
				
					# We create the critNames list
					critNames = []
					for nb in range(nbCrit) :
						critNames.append(critPrefix+str(nb+1))
					
				
	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 :
	
		# We check if a preferenceDirection file has been provided
		if os.path.isfile (in_dir+"/preferenceDirection.xml") :
			xmltree_PrefDir = PyXMCDA.parseValidate(in_dir+"/preferenceDirection.xml")
			if xmltree_PrefDir == None :
				errorList.append ("preferenceDirection file can't be validated.")
			else :
				prefDir = PyXMCDA.getNamedParametersByName (xmltree_PrefDir, "preferenceDirection")
				
				if not prefDir :
					errorList.append("No preference direction found. Is your preferenceDirection file correct ?")
	
	if not errorList :
		
		# We check if a criteriaType file has been provided
		if os.path.isfile (in_dir+"/criteriaType.xml") :
			xmltree_CritType = PyXMCDA.parseValidate(in_dir+"/criteriaType.xml")
			if xmltree_CritType == None :
				errorList.append ("criteriaType file can't be validated.")
			else :
				critType = PyXMCDA.getNamedParametersByName (xmltree_CritType, "criteriaType")
				
				if not critType :
					errorList.append("No criterion type found. Is your criteriaType file correct ?")
	
	if not errorList :
		
		# We check if a lowerBound file has been provided
		if os.path.isfile (in_dir+"/lowerBounds.xml") :
			xmltree_LowerBound = PyXMCDA.parseValidate(in_dir+"/lowerBounds.xml")
			if xmltree_LowerBound == None :
				errorList.append ("lowerBounds file can't be validated.")
			else :
				lowerBound = PyXMCDA.getNamedParametersByName (xmltree_LowerBound, "lowerBounds")
				
				if not lowerBound :
					errorList.append("No lower bound found. Is your lowerBounds file correct ?")
					
	if not errorList :
		
		# We check if an upperBound file has been provided
		if os.path.isfile (in_dir+"/upperBounds.xml") :
			xmltree_UpperBound = PyXMCDA.parseValidate(in_dir+"/upperBounds.xml")
			if xmltree_UpperBound == None :
				errorList.append ("upperBounds file can't be validated.")
			else :
				upperBound = PyXMCDA.getNamedParametersByName (xmltree_UpperBound, "upperBounds")
				
				if not upperBound :
					errorList.append("No upper bound found. Is your upperBounds file correct ?")
	
	if not errorList :
		
		# We check if a numberOfLevels parameter has been provided
		if os.path.isfile (in_dir+"/numberOfLevels.xml") :
			xmltree_levelNumber = PyXMCDA.parseValidate(in_dir+"/numberOfLevels.xml")
			if xmltree_levelNumber == None :
				errorList.append ("numberOfLevels file can't be validated.")
			else :
				levelNumber = PyXMCDA.getNamedParametersByName (xmltree_levelNumber, "numberOfLevels")
				if not levelNumber :
					errorList.append("No number of levels found. Is your numberOfLevels file correct ?")
	
	if not errorList :
		
		# We check if a thresholdsNames file has been provided
		if os.path.isfile (in_dir+"/thresholdsNames.xml") :
			xmltree_Thresholds = PyXMCDA.parseValidate(in_dir+"/thresholdsNames.xml")
			if xmltree_Thresholds == None :
				errorList.append ("thresholdsNames file can't be validated.")
			else :
				thresholds = PyXMCDA.getParametersByName (xmltree_Thresholds, "thresholdsNames")
				
				if not thresholds :
					errorList.append("No threshold name found. Is your thresholdsNames file correct ?")
	
	
	if not errorList :
	
		# We create the criteria.xml file
		fileCrit = open(out_dir+"/criteria.xml", 'w')
		PyXMCDA.writeHeader (fileCrit)
		fileCrit.write ("<criteria>\n")
		
		for crit in critNames :
			fileCrit.write ("\t<criterion id='" + crit + "'>\n\t\t<active>true</active>\n\t\t<scale>\n")
			
			# Opening qualitative or quantitative tag
			if critType.has_key (crit) and critType[crit] == "qualitative" :
				fileCrit.write ("\t\t\t<qualitative>\n")
				varBoundType = "integer"
				if levelNumber.has_key (crit) :
					numberOfLevels = levelNumber[crit]
					valLB = 1
					valUB = numberOfLevels
				else :
					numberOfLevels = 10
					valLB = 1
					valUB = 10
			else :
				# By default, a criterion is quantitative
				fileCrit.write ("\t\t\t<quantitative>\n")
				varBoundType = "real"
				if lowerBound.has_key (crit) :
					valLB = lowerBound[crit]
				else :
					valLB = 0.0
				if upperBound.has_key (crit) :
					valUB = upperBound[crit]
				else :
					valUB = 100.0
			
			# Preference direction information	
			if prefDir.has_key (crit) and prefDir[crit] == "min" :
				fileCrit.write ("\t\t\t\t<preferenceDirection>min</preferenceDirection>\n")
			else :
				# By default, the preference direction is "max"
				fileCrit.write ("\t\t\t\t<preferenceDirection>max</preferenceDirection>\n")
			
			if critType.has_key (crit) and critType[crit] == "qualitative" :
				for nb in range (numberOfLevels) :
					fileCrit.write ("\t\t\t\t<rankedLabel><label>"+str(nb+1)+"</label><rank>"+str(nb+1)+"</rank></rankedLabel>\n")
			
			else :
		
				# Writing minimum tag for the lower bound
				fileCrit.write ("\t\t\t\t<minimum><"+varBoundType+">")
			
				if lowerBound.has_key (crit) :
					valLB = lowerBound[crit]
				
				fileCrit.write (str(valLB)+"</"+varBoundType+"></minimum>\n")
				
				# Writing maximum tag for the upper bound
				fileCrit.write ("\t\t\t\t<maximum><"+varBoundType+">")
					
				fileCrit.write (str(valUB)+"</"+varBoundType+"></maximum>\n")
			
			# Closing quantitative or qualitative tag		
			if critType.has_key (crit) and critType[crit] == "qualitative" :
				fileCrit.write("\t\t\t</qualitative>\n")
			else :		
				fileCrit.write("\t\t\t</quantitative>\n")
			
			# Closing scale tag
			fileCrit.write("\t\t</scale>\n")
			
					
			# Writing thresholds tag
			if thresholds :
				fileCrit.write ("\t\t<thresholds>\n")
			
				pos = 0
				for thre in thresholds :
					
					if critType.has_key (crit) and critType[crit] == "qualitative" :
						# Writing a random integer
						valThre = random.randint(valLB+pos*(valUB-valLB)/len(thresholds),valLB+(pos+1)*(valUB-valLB)/len(thresholds))
					else :
						# Writing a random real
						valThre = float(random.randint(int((valLB+pos*(valUB-valLB)/len(thresholds)))*100,int((valLB+(pos+1)*(valUB-valLB)/len(thresholds)))*100))/100
					
					fileCrit.write ("\t\t\t<threshold id='"+thre+"'>\n\t\t\t\t<constant><"+varBoundType+">"+str(valThre)+"</"+varBoundType+"></constant>\n")
				
					fileCrit.write ("\t\t\t</threshold>\n")
				
					pos += 1
					
				fileCrit.write ("\t\t</thresholds>\n")
			
			# Closing criterion tag
			fileCrit.write("\t</criterion>\n")
	
		fileCrit.write ("</criteria>\n")
				
		PyXMCDA.writeFooter(fileCrit)
		fileCrit.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()
示例#37
0
 def get_param_boolean(param_name, *args, **kwargs):
     parameter = px.getParameterByName(
         trees['method_parameters'],
         param_name,
     )
     return True if parameter == 'true' else False
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()
示例#39
0
 def get_param_real(param_name, *args, **kwargs):
     param = px.getParameterByName(trees['method_parameters'], param_name)
     return float(param)
示例#40
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 not in_dir:
        errorList.append("option --in is missing")
    if not out_dir:
        errorList.append("option --out is missing")
    
    if not errorList:
        if not os.path.isfile (in_dir+"/alternatives.xml"):
            errorList.append("alternatives.xml is missing")
        if not os.path.isfile (in_dir+"/alternativesComparisons.xml"):
            errorList.append("alternativesComparisons.xml is missing")
        if not os.path.isfile (in_dir+"/methodParameters.xml"):
            errorList.append("methodParameters.xml is missing")

    if not errorList:
        # We parse all the mandatory input files
        xmltree_alternatives = PyXMCDA.parseValidate(in_dir+"/alternatives.xml")
        xmltree_alternativesComparisons = PyXMCDA.parseValidate(in_dir+"/alternativesComparisons.xml")
        xmltree_methodParameters = PyXMCDA.parseValidate(in_dir+"/methodParameters.xml")
        
        # We check if all mandatory input files are valid
        if xmltree_alternatives == None :
            errorList.append("alternatives.xml can't be validated.")
        if xmltree_alternativesComparisons == None :
            errorList.append("alternativesComparisons.xml can't be validated.")
        if xmltree_methodParameters == None :
            errorList.append("methodParameters.xml can't be validated.")
            
        if not errorList :

            alternativesId = PyXMCDA.getAlternativesID(xmltree_alternatives)
            alternativesRel = PyXMCDA.getAlternativesComparisons (xmltree_alternativesComparisons, alternativesId)
            bipolar = PyXMCDA.getParameterByName(xmltree_methodParameters, "bipolar")
            cutlvl = PyXMCDA.getParameterByName(xmltree_methodParameters, "cutlvl")

            if not alternativesId :
                errorList.append("No active alternatives found.")
            if not alternativesRel :
                errorList.append("Problems between relation and alternatives.")
            if not bipolar:
                errorList.append("No bipolar parameter found.")
            if not cutlvl:
                errorList.append("No cutlvl parameter found.")
            missing_eval = False
            for o in alternativesId:
                if not (o in alternativesRel):
                    missing_eval = True
                    break
                else:
                    for p in alternativesId:
                        if not (p in alternativesRel[o]):
                            missing_eval = True
                            break
            if missing_eval:
                errorList.append("Not all alternatives from alternatives.xml contain evaluations in alternativesComparisons.xml. Possible inputs from different sources.")

        if not errorList :
            
            PR = PreferenceRelation(alternativesId, alternativesRel, bipolar, cutlvl)
            R = PR.ExtractR()
            L = ['i','p+','p-','j']
            
            fo = open(out_dir+"/preferenceRelation.xml",'w')
            PyXMCDA.writeHeader(fo)
            fo.write('<alternativesComparisons>\n')
            fo.write('\t<pairs>\n')
            for o in alternativesId:
                for p in alternativesId:
                    fo.write('\t\t<pair>\n')
                    fo.write('\t\t\t<initial>\n')
                    fo.write('\t\t\t\t<alternativeID>'+o+'</alternativeID>\n')
                    fo.write('\t\t\t</initial>\n')
                    fo.write('\t\t\t<terminal>\n')
                    fo.write('\t\t\t\t<alternativeID>'+p+'</alternativeID>\n')
                    fo.write('\t\t\t</terminal>\n')
                    fo.write('\t\t\t<values>\n')
                    for i in range(4):
                        fo.write('\t\t\t\t<value id="'+ L[i] +'">\n')
                        fo.write('\t\t\t\t\t<real>'+str(R[o][p][i])+'</real>\n')
                        fo.write('\t\t\t\t</value>\n')
                    fo.write('\t\t\t</values>\n')
                    fo.write('\t\t</pair>\n')
            fo.write('\t</pairs>\n')
            fo.write('</alternativesComparisons>\n')
            PyXMCDA.writeFooter(fo)
            fo.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()
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 lists for error and log messages
	errorList = []
	logList = []
	
	# 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+"/alternativesComparisons.xml") or not os.path.isfile (in_dir+"/performanceTable.xml") :
		errorList.append("Some input files are missing")
	
	else :
		
		maxWeight = 0
		
		if os.path.isfile (in_dir+"/maxWeight.xml") :
			xmltree_maxWeight = PyXMCDA.parseValidate(in_dir+"/maxWeight.xml")
			if xmltree_maxWeight == None :
				errorList.append ("maxWeight file can't be validated.")
			else :
				maxWeight = PyXMCDA.getParameterByName (xmltree_maxWeight, "maxWeight")
				if not isinstance(maxWeight,int) :
					errorList.append ("maxWeight value should be a strictly positive integer")
				else :
					if maxWeight <= 0 :
						errorList.append ("maxWeightvalue should be a strictly positive integer")
			
			
		xmltree_alternatives = PyXMCDA.parseValidate(in_dir+"/alternatives.xml")
		xmltree_criteria = PyXMCDA.parseValidate(in_dir+"/criteria.xml")
		xmltree_altComparisons = PyXMCDA.parseValidate(in_dir+"/alternativesComparisons.xml")
		xmltree_perfTable = PyXMCDA.parseValidate(in_dir+"/performanceTable.xml")
		
		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_altComparisons == None :
			errorList.append("The alternatives comparisons file can't be validated.")
	
		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)
			altComparisons = PyXMCDA.getAlternativesComparisons (xmltree_altComparisons, alternativesId)
			
			criComparisons = {}
			if os.path.isfile (in_dir+"/criteriaComparisons.xml") :
				xmltree_criComparisons = PyXMCDA.parseValidate(in_dir+"/criteriaComparisons.xml")
				if xmltree_criComparisons == None :
					errorList.append ("criteriaComparisons file can't be validated")
				else :
					criComparisons = PyXMCDA.getCriteriaComparisons (xmltree_criComparisons, criteriaId)
			
			criLB = {}
			if os.path.isfile (in_dir+"/criteriaLowerBounds.xml") :
				xmltree_criLB = PyXMCDA.parseValidate(in_dir+"/criteriaLowerBounds.xml")
				if xmltree_criLB == None :
					errorList.append ("criteriaLowerBounds file can't be validated")
				else :
					criLB = PyXMCDA.getCriterionValue (xmltree_criLB, criteriaId)
					
			criUB = {}
			if os.path.isfile (in_dir+"/criteriaUpperBounds.xml") :
				xmltree_criUB = PyXMCDA.parseValidate(in_dir+"/criteriaUpperBounds.xml")
				if xmltree_criUB == None :
					errorList.append ("criteriaUpperBounds file can't be validated")
				else :
					criUB = PyXMCDA.getCriterionValue (xmltree_criUB, 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 altComparisons :
			#	errorList.append("No alternatives comparisons found. Is your file correct ?")
			if not thresholds :
				errorList.append("Problem when retrieving the thresholds. The thresholds need to be constant.")
		
	if not errorList :
	
		p = subprocess.Popen(['ampl'], shell=False, bufsize=0,stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
		
		# We write in the pipe the first part of the ampl file
		file = open ("amplRoadef2010_model.txt", 'r')
		p.stdin.write(file.read()) 
		p.stdin.write("\n")
		
		lib_ampl_reverse.create_ampl_reverse_data (p.stdin, alternativesId, criteriaId, perfTable, altComparisons, thresholds, maxWeight, criComparisons, criLB, criUB)
		
		file = open ("amplRoadef2010_solve.txt", 'r')
		p.stdin.write(file.read())
		p.stdin.write("\n")
		p.stdin.flush()
		
		# Calling CPlex for solving MILP
		output,stderr = p.communicate()
		status = p.returncode
		
		# We check the correct resolution
		if status == 0  and not stderr :
			
			if PyXMCDA.getStringPart(output, "nolicense") == "" :
			
				if PyXMCDA.getStringPart(output, "infeasible") != "infeasible" :
							
					# We create the criteriaWeights file
					fileWeights = open(out_dir+"/criteriaWeights.xml", 'w')
					PyXMCDA.writeHeader (fileWeights)
					fileWeights.write (PyXMCDA.getStringPart(output, "criteriaValues"))	
					logList.append("Execution ok")
					logList.append(PyXMCDA.getStringPart(output, "slackSum")) 
					logList.append(PyXMCDA.getCleanedStringPart(output, "CPLexInfos"))
					PyXMCDA.writeFooter(fileWeights)
					fileWeights.close()
				
				else :
					errorList.append ("Infeasible problem.")
					errorList.append (PyXMCDA.getStringPart(output, "CPLexInfos"))
								
			else :
				errorList.append ("No license available.")
				errorList.append (PyXMCDA.getStringPart(output, "CPLexInfos"))
			
		else :
			errorList.append ("CPlex is unable to solve the problem.")
			errorList.append ("CPlex returned status : " + str(status))
			errorList.append (stderr)
		
	
	
	# Creating log and error file, messages.xml
	fileMessages = open(out_dir+"/messages.xml", 'w')
	PyXMCDA.writeHeader (fileMessages)
	
	if not errorList :
		PyXMCDA.writeLogMessages (fileMessages, logList)
	else :
		PyXMCDA.writeErrorMessages (fileMessages, errorList)
		
	PyXMCDA.writeFooter(fileMessages)
	fileMessages.close()
示例#42
0
 def get_param_string(param_name, *args, **kwargs):
     param = px.getParameterByName(trees['method_parameters'], param_name)
     return param
示例#43
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
示例#44
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 not in_dir:
        errorList.append("option --in is missing")
    if not out_dir:
        errorList.append("option --out is missing")
    
    if not errorList:
        if not os.path.isfile (in_dir+"/alternatives.xml"):
            errorList.append("alternatives.xml is missing")
        if not os.path.isfile (in_dir+"/preferenceRelation.xml"):
            errorList.append("preferenceRelation.xml is missing")
        if not os.path.isfile (in_dir+"/methodParameters.xml"):
            errorList.append("methodParameters.xml is missing")

    if not errorList:
        # We parse all the mandatory input files
        xmltree_alternatives = PyXMCDA.parseValidate(in_dir+"/alternatives.xml")
        xmltree_preferenceRelation = PyXMCDA.parseValidate(in_dir+"/preferenceRelation.xml")
        xmltree_methodParameters = PyXMCDA.parseValidate(in_dir+"/methodParameters.xml")
        
        # We check if all mandatory input files are valid
        if xmltree_alternatives == None :
            errorList.append("alternatives.xml can't be validated.")
        if xmltree_preferenceRelation == None :
            errorList.append("preferenceRelation.xml can't be validated.")
        if xmltree_methodParameters == None :
            errorList.append("methodParameters.xml can't be validated.")
            
        if not errorList :

            alternativesId = PyXMCDA.getAlternativesID(xmltree_alternatives)
            alternativesRel = PyXMCDA.getAlternativesComparisonsValues(xmltree_preferenceRelation, alternativesId)
            method_type = PyXMCDA.getParameterByName(xmltree_methodParameters, "type")

            if not alternativesId :
                errorList.append("No active alternatives found.")
            if not alternativesRel :
                errorList.append("Problems between relation and alternatives.")
            if not method_type:
                errorList.append("No method type found.")
            missing_eval = False
            for o in alternativesId:
                if not (o in alternativesRel):
                    missing_eval = True
                    break
                else:
                    for p in alternativesId:
                        if not (p in alternativesRel[o]):
                            missing_eval = True
                            break
                        else:
                            if not ('i' in alternativesRel[o][p]):
                                missing_eval = True
                                break
                            if not ('p+' in alternativesRel[o][p]):
                                missing_eval = True
                                break
                            if not ('p-' in alternativesRel[o][p]):
                                missing_eval = True
                                break
                            if not ('j' in alternativesRel[o][p]):
                                missing_eval = True
                                break
            if missing_eval:
                errorList.append("Not all alternatives from alternatives.xml contain evaluations in preferenceRelation.xml, or evaluations are incomplete. Possible inputs from different sources")

            if not errorList :
                alg = Mcc(alternativesId, alternativesRel, method_type)
                K, RK = alg.Run()
                
                fo = open(out_dir+"/alternativesAffectations.xml",'w')
                PyXMCDA.writeHeader(fo)
                fo.write('<alternativesAffectations>\n')
                for i in range(len(K)):
                    for o in K[i]:
                        fo.write('\t<alternativeAffectation>\n\t\t<alternativeID>'+o+'</alternativeID>\n\t\t<categoryID>'+'K'+str(i+1)+'</categoryID>\n\t</alternativeAffectation>\n')
                fo.write('</alternativesAffectations>')
                PyXMCDA.writeFooter(fo)
                fo.close()
                
                fo = open(out_dir+"/clustersRelations.xml",'w')
                PyXMCDA.writeHeader(fo)
                fo.write('<categoriesComparisons>\n')
                fo.write('\t<pairs>\n')
                for i in range(len(K)):
                    for j in range(len(K)):                        
                        fo.write('\t\t<pair>\n')
                        fo.write('\t\t\t<initial>\n')
                        fo.write('\t\t\t\t<categoryID>'+'K'+str(i+1)+'</categoryID>\n')
                        fo.write('\t\t\t</initial>\n')
                        fo.write('\t\t\t<terminal>\n')
                        fo.write('\t\t\t\t<categoryID>'+'K'+str(j+1)+'</categoryID>\n')
                        fo.write('\t\t\t</terminal>\n')
                        fo.write('\t\t\t<value>\n')
                        fo.write('\t\t\t\t<label>'+RK[i][j]+'</label>\n')
                        fo.write('\t\t\t</value>\n')
                        fo.write('\t\t</pair>\n')
                fo.write('\t</pairs>\n')
                fo.write('</categoriesComparisons>')
                PyXMCDA.writeFooter(fo)
                fo.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()
示例#45
0
 def get_param_string(param_name, *args, **kwargs):
     param = px.getParameterByName(trees['method_parameters'], param_name)
     return param
示例#46
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()
示例#48
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 + "/nbAlternatives.xml") and not os.path.isfile(in_dir + "/alternativesNames.xml"):
        errorList.append(
            "No parameter has been provided. You should provide a number of alternatives (using nbAlternatives.xml file) or a list of alternatives names (using alternativesNames.xml file)."
        )

    else:

        # User provide a list of alternatives names
        if os.path.isfile(in_dir + "/alternativesNames.xml"):

            # We parse the input file
            xmltree_AltNames = PyXMCDA.parseValidate(in_dir + "/alternativesNames.xml")
            if xmltree_AltNames == None:
                errorList.append("alternativesNames file can't be validated.")

            else:
                # We record the alternatives names in altNames
                altNames = PyXMCDA.getParametersByName(xmltree_AltNames, "alternativesNames")

                if not altNames:
                    errorList.append(
                        "No alternative name has been found in alternativesNames file. Is your file correct ?"
                    )

                    # user provide a number of alternatives
        else:

            # We parse the input file
            xmltree_nbAlt = PyXMCDA.parseValidate(in_dir + "/nbAlternatives.xml")
            if xmltree_nbAlt == None:
                errorList.append("nbAlternatives file can't be validated.")

            else:

                nbAlt = PyXMCDA.getParameterByName(xmltree_nbAlt, "nbAlternatives")

                # We check the validity of the parameter
                if not nbAlt:
                    errorList.append("nbAlternatives parameter not provided. It should be a strict positive integer.")
                if not errorList and not isinstance(nbAlt, int):
                    errorList.append("nbAlternatives value should be a strict positive integer.")
                if not errorList and nbAlt <= 0:
                    errorList.append("nbAlternatives value should be a scrict positive integer.")

                    # We check if a prefix parameter has been provided
                if not errorList:
                    if os.path.isfile(in_dir + "/alternativesPrefix.xml"):
                        xmltree_AltPrefix = PyXMCDA.parseValidate(in_dir + "/alternativesPrefix.xml")
                        if xmltree_AltPrefix == None:
                            errorList.append("alternativesPrefix file can't be validated.")
                        else:
                            altPrefix = PyXMCDA.getParameterByName(xmltree_AltPrefix, "alternativesPrefix")

                            # We check the validity of the parameter
                            if not isinstance(altPrefix, str):
                                errorList.append("alternativesPrefix parameter should be a label")

                    else:
                        # If no prefix has been provided, the alternatives will be called a1, a2, ...
                        altPrefix = "a"

                if not errorList:

                    # We create the altNames list
                    altNames = []
                    for nb in range(nbAlt):
                        altNames.append(altPrefix + str(nb + 1))

    if not errorList:

        # We create the alternatives.xml file
        fileAlt = open(out_dir + "/alternatives.xml", "w")
        PyXMCDA.writeHeader(fileAlt)
        fileAlt.write("<alternatives>\n")

        for alt in altNames:
            fileAlt.write("\t<alternative id='" + alt + "'>\n\t\t<active>true</active>\n\t</alternative>\n")

        fileAlt.write("</alternatives>\n")

        PyXMCDA.writeFooter(fileAlt)
        fileAlt.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()