Exemplo n.º 1
0
def etri_infer_parameters(
    learning_alts, criteria, pt, affectations, nprofiles, model, weights=None, lbda=None, profiles=None
):
    categories = [(i + 1) for i in range(nprofiles + 1)]
    categories_rank = {}
    for i, cat in enumerate(categories):
        categories_rank[cat] = i + 1
    infile = glpk.create_input_file(
        learning_alts, criteria, pt, categories, categories_rank, affectations, weights, lbda, profiles
    )
    print "GLPK file:", infile.name
    (status, output) = glpk.solve(infile.name, model)
    if status:
        sys.exit("gklp returned status %d" % status)
    infile.close()
    return glpk.parse_output(output, learning_alts, criteria)
Exemplo n.º 2
0
def main(argv=None):
    if argv is None:
        argv = sys.argv

    # Parsing of the options
    (in_dir, out_dir) = parse_cmdline(argv)
    if error_list:
        return error_list

    check_input_files(in_dir)
    if error_list:
        create_error_file(out_dir, error_list)
        return error_list

    xmcda_input = parse_xmcda_files(in_dir)
    if xmcda_input == None or error_list:
        create_error_file(out_dir, error_list)
        return error_list

    alt_id = xmcda_input[0]
    crit_id = xmcda_input[1]
    pt = xmcda_input[2]
    cat_id = xmcda_input[3]
    cat_rank = xmcda_input[4]
    assign = xmcda_input[5]
    pref_dir = xmcda_input[6]

    # Kludge: invert the categories rank... Beurk
    invert_cat_rank(cat_rank)

    cat_id_sorted = get_sorted_categories(cat_rank)

    # Name of the profile alternatives
    palts_id = [ "b%d" % (i+1) for i in range(len(cat_id)-1) ]

    log("GLPK INPUT:")
    log('alt  ids   : %s' % alt_id)
    log('crit ids   : %s' % crit_id)
    log('perfs      : %s' % pt)
    log('categories : %s' % cat_id)
    log('affect     : %s' % assign)
    log('pref_dir   : %s' % pref_dir)

    check_input_parameters(alt_id, crit_id, pt, cat_id, assign)
    if error_list:
        create_error_file(out_dir, error_list)
        return error_list

    fixed_params = get_fixed_parameters(in_dir, alt_id, crit_id, pt, cat_id, cat_rank, pref_dir)
    fixed_weights = fixed_params[0]
    fixed_lambda = fixed_params[1]
    fixed_cat_profiles = fixed_params[2]
    fixed_pt_profiles = fixed_params[3]
    log('fixed_weights  : %s' % fixed_weights)
    log('fixed_lambda  : %s' % fixed_lambda)
    log('fixed_cat_profiles  : %s' % fixed_cat_profiles)
    log('fixed_pt_profiles  : %s' % fixed_pt_profiles)

    try:
        (gmin, gmax) = get_min_max(pt)
        if fixed_pt_profiles:
            (gmin2, gmax2) = get_min_max(fixed_pt_profiles)
            for c, val in gmin.iteritems():
                if gmin[c] > gmin2[c]:
                    gmin[c] = gmin2[c]
                if gmax[c] < gmax2[c]:
                    gmax[c] = gmax2[c]
    except:
        error_list.append("Impossible to get min max values in pt")
        create_error_file(out_dir, error_list)
        return error_list

    try:
        normalize(pt, gmin, gmax, pref_dir) 
    except:
        error_list.append("Impossible to convert performance table")
        create_error_file(out_dir, error_list)
        return error_list

    if fixed_pt_profiles:
        try:
            normalize(fixed_pt_profiles, gmin, gmax, pref_dir)
            fixed_profiles = get_sorted_profiles(fixed_cat_profiles, cat_rank, fixed_pt_profiles)
        except:
            error_list.append("Impossible to convert profile performance table")
            create_error_file(out_dir, error_list)
            return error_list
    else:
        fixed_profiles = None

    if fixed_weights:
        glpk_model = "inf_etri_bm_fixed_weights.mod"
    elif fixed_pt_profiles:
        glpk_model = "inf_etri_bm_fixed_profiles.mod"
    else:
        glpk_model = "inf_etri_bm.mod"

    log("GLPK model used: %s" % glpk_model)

    try:
        input_file = glpk.create_input_file(alt_id, crit_id, pt, cat_id, cat_rank, assign, fixed_weights, fixed_lambda, fixed_profiles)
    except:
        error_list.append("Impossible to create glpk input file")

    if error_list:
        create_error_file(out_dir, error_list)
        return error_list

    (status, output) = glpk.solve(glpk_model, input_file.name)
    if status:
        error_list.append("GLPK returned status %d" % status);
        input_file.close()
        create_error_file(out_dir, error_list)
        return error_list

    input_file.close()

    out_params = glpk.parse_output(output, alt_id, crit_id)
    if out_params == None:
        error_list.append("Error parsing output parameters")
        create_error_file(out_dir, error_list)
        return error_list

    weights = out_params[0]
    profiles = out_params[1]
    for profile in profiles:
        denormalize(profile, gmin, gmax, pref_dir)
    lbda = out_params[2]
    compat = out_params[3]
    
    log("GLPK OUTPUT:")
    log("weights  : %s" % weights)
    log("profiles : %s" % profiles)
    log("lambda   : %s" % lbda)
    log("compat   : %s" % compat)
    
    if not weights:
        error_list.append("Impossible to get weights from the solver")
    
    if not profiles:
        error_list.append("Impossible to get profiles from the solver")
    
    if not lbda:
        error_list.append("Impossible to get lambda from the solver")
    
    if not compat:
        error_list.append("Impossible to get compatible alternatives from the solver")

    if error_list:
        create_error_file(out_dir, error_list)
        return error_list
    
    out_weights = xmcda.format_criteria_weights(weights)
    out_catprof = xmcda.format_category_profiles(profiles, palts_id, cat_id_sorted)
    out_refalts = xmcda.format_pt_reference_alternatives(profiles, palts_id, crit_id)
    out_lambda = xmcda.format_lambda(lbda)
    out_compat = xmcda.format_format_compatible_alternatives(compat, alt_id)
    create_output_files(out_dir, out_weights, out_catprof, out_refalts, out_lambda, out_compat)

    logs = [ "Execution ok" ]
    create_log_file(out_dir, logs)