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()
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()