Esempio n. 1
0
def main(name_file, UB):
    MAX_ITER = 500000
    MAX_TIME_SEC = 7200
    costs, incidence_matrix = my_io.read_file_format_or_library(name_file)
    columns = transform_incidence2column(incidence_matrix)
    #passo0
    dual_lagrangean = [1]*len(incidence_matrix)
    start = colect_time.cpu_time()
    set_covering = SetCoveringLagrangeanProblem(costs=costs, incidence_matrix=incidence_matrix,columns=columns, dual_lagrangean=dual_lagrangean)
    set_covering.create_cplex_problem()
    set_covering.c.solve()
    m1 = 0.001
    x = set_covering.c.solution.get_values(set_covering.names_variables)
    v = subtract_two_vectors([1]*len(incidence_matrix), multiply_matrix_by_vector(incidence_matrix, x))
    z = x[:]
    dual_lagrangean_aux = dual_lagrangean[:]
    w = v[:]
    p = dual_lagrangean[:]
    epsilon = 0
    t = 1
    k = 1
    s = 0.1
    objective = 0
    while True:
        #passo1
        dual_lagrangean = add_two_vectors(dual_lagrangean_aux, number_multiply_vector(s, w))
        ro = s * product_dot(w, w) + abs(product_dot(w, subtract_two_vectors(dual_lagrangean_aux,p))) + epsilon
        if stop(w,dual_lagrangean_aux,p) or UB-objective < 1 or colect_time.cpu_time() - start > MAX_TIME_SEC or t > MAX_ITER:
            end = colect_time.cpu_time()
            break
        #passo2:
        objective, x = resolve_sub_problem_lagrangean(costs=costs, incidence_matrix=incidence_matrix,columns=columns, dual_lagrangean=dual_lagrangean)
        v = subtract_two_vectors([1]*len(incidence_matrix), multiply_matrix_by_vector(incidence_matrix, x))
        
        #passo3
        objective_aux, x_aux = resolve_sub_problem_lagrangean(costs=costs, incidence_matrix=incidence_matrix,columns=columns, dual_lagrangean=dual_lagrangean_aux)
        if objective >= objective_aux + m1 * ro:
            dual_lagrangean_aux = dual_lagrangean[:]
            k = k + 1
        #passo4
        s = 0.5*((UB-objective)/product_dot(w,w))
        alpha = resolve_problem_aux(v, w, s, product_dot(v, subtract_two_vectors(dual_lagrangean_aux,dual_lagrangean)), product_dot(w, subtract_two_vectors(dual_lagrangean_aux, p))+epsilon)
        z = add_two_vectors(number_multiply_vector(alpha, x),number_multiply_vector(1-alpha, z))
        w = add_two_vectors(number_multiply_vector(alpha, v),number_multiply_vector(1-alpha, w))
        p = add_two_vectors(number_multiply_vector(alpha, dual_lagrangean),number_multiply_vector(1-alpha, p))
        aux = 1-alpha * product_dot(subtract_two_vectors(v, w), subtract_two_vectors(p, dual_lagrangean))
        epsilon = alpha * aux + (1 - alpha) * epsilon
        t = t + 1
    print name_file, objective, t, end
Esempio n. 2
0
def execute(name_file):
    costs, incidence_matrix = my_io.read_file_format_or_library(name_file)
    columns = transform_incidence2column(incidence_matrix)
    set_covering = SetCoveringProblem(costs=costs, incidence_matrix=incidence_matrix, columns=columns)
    start = colect_time.cpu_time()
    set_covering.create_cplex_problem()
    set_covering.c.solve()
    solution = set_covering.c.solution
    alphas = (1, [0] * set_covering.num_variables)
    constraint_add = []
    relax_linear = solution.get_objective_value()
    # while (not verify_list_parameter_is_integer(solution.get_values()) and alphas != (0, [0]*set_covering.num_variables) and colect_time.cpu_time() - start <= 7200):
    #    set_covering.create_cg_separate()
    #    alphas = set_covering.resolve_cg_separate(solution.get_values())
    #    if alphas != (0, [0]*set_covering.num_variables):
    #        constraint_add.append(alphas)
    #        set_covering.add_constraint_in_problem(alphas)
    #        set_covering.c.solve()
    end = colect_time.cpu_time()
    print name_file, relax_linear, solution.get_objective_value(), len(constraint_add), end - start
def volume(file_name,UB):
    MAX_ITER = 500000
    MAX_TIME_SEC = 7200
    nodes,edges_source = read_instance(file_name)
    start = colect_time.cpu_time()
    bMatchingAndTree = BMatchingAndTree(nodes,edges_source)
    bMatchingAndTree.create_model()
    t = 0
    pi_t = create_pi(bMatchingAndTree)
    best_pi = create_pi(bMatchingAndTree)
    best_lower_bound = 0
    while UB - best_lower_bound > 1 and t < MAX_ITER and colect_time.cpu_time() - start < MAX_TIME_SEC:
        bMatchingAndTree.add_function_objective(pi_t)
        bMatchingAndTree.c.solve()
        solution_value = bMatchingAndTree.c.solution.get_objective_value()
        pi_t = update_pi(best_pi, bMatchingAndTree, UB, solution_value, u=0.01)
        if solution_value > best_lower_bound:
            best_lower_bound = solution_value
            best_pi = pi_t
        else:
            break
        t += 1
    print file_name, best_lower_bound, t, colect_time.cpu_time()