Beispiel #1
0
def random_swap(graph, num_buses, size_bus, constraints, result):
    #result is a 2d array containing student assignment:
    prev_score = score_output(graph, num_buses, size_bus, constraints, result)
    x1 = random.randint(0, len(result) - 1)
    x2 = random.randint(0, len(result) - 1)

    #prevent two buses are the same
    while x1 == x2:
        x2 = random.randint(0, len(result) - 1)

    bus1 = result[x1]
    bus2 = result[x2]

    #get index of random student
    y1 = random.randint(0, len(bus1) - 1)
    y2 = random.randint(0, len(bus2) - 1)

    #choose random student from buses and swap them
    student1 = bus1[y1]
    student2 = bus2[y2]

    #swap them
    temp = result[x1][y1]
    result[x1][y1] = result[x2][y2]
    result[x2][y2] = temp

    if score_output(graph, num_buses, size_bus, constraints,
                    result) <= prev_score:
        result[x2][y2] = result[x1][y1]
        result[x1][y1] = temp
Beispiel #2
0
def random_move(graph, num_buses, size_bus, constraints, result):
    #result is a 2d array containing student assignment:

    #prevent no place to move.
    if size_bus * num_buses == len(graph.nodes):
        return

    prev_score = score_output(graph, num_buses, size_bus, constraints, result)

    s = random.randint(0, len(result) - 1)
    t = random.randint(0, len(result) - 1)

    while len(result[s]) == 1 or all_full(s, result, size_bus):
        s = random.randint(0, len(result) - 1)

    #prevent two buses are the same
    while t == s or t >= size_bus:
        t = random.randint(0, len(result) - 1)

    source_bus = result[s]
    target_bus = result[t]

    #choose random student from t
    student_index = random.randint(0, len(source_bus) - 1)
    student = source_bus[student_index]

    result[t].append(student)
    del result[s][student_index]

    #check for scoure improvement
    if score_output(graph, num_buses, size_bus, constraints,
                    result) <= prev_score:
        del result[t][-1]
        result[s].append(student)
Beispiel #3
0
def random_two_swap(graph, num_buses, size_bus, constraints, result):
    #result is a 2d array containing student assignment:
    prev_score = score_output(graph, num_buses, size_bus, constraints, result)
    x1 = random.randint(0, len(result) - 1)
    x2 = random.randint(0, len(result) - 1)

    #prevent there's only one student in the bus
    while len(result[x1]) == 1:
        x1 = random.randint(0, len(result) - 1)

    #if all other buses except x1 only has one student, return
    if all_one(x1, result):
        return

    #prevent two buses are the same
    while x1 == x2 or len(result[x2]) == 1:
        x2 = random.randint(0, len(result) - 1)

    bus1 = result[x1]
    bus2 = result[x2]

    #get index of random student
    y1 = random.randint(0, len(bus1) - 1)
    z1 = random.randint(0, len(bus1) - 1)

    while y1 == z1:
        z1 = random.randint(0, len(bus1) - 1)
    y2 = random.randint(0, len(bus2) - 1)
    z2 = random.randint(0, len(bus2) - 1)

    while y2 == z2:
        z2 = random.randint(0, len(bus2) - 1)

    #choose random student from buses and swap them
    student1 = bus1[y1]
    student2 = bus2[y2]

    another_student1 = bus1[z1]
    another_student2 = bus2[z2]

    #swap them
    temp1 = result[x1][y1]
    result[x1][y1] = result[x2][y2]
    result[x2][y2] = temp1

    temp2 = result[x1][z1]
    result[x1][z1] = result[x2][z2]
    result[x2][z2] = temp2

    if score_output(graph, num_buses, size_bus, constraints,
                    result) <= prev_score:
        result[x2][y2] = result[x1][y1]
        result[x1][y1] = temp1

        result[x2][z2] = result[x1][z1]
        result[x1][z1] = temp2
Beispiel #4
0
def threadwork(data):

    pack, _, category_path, output_category_path, input_name, result = data
    print('doing', category_path, input_name)
    filename = output_category_path + "/" + input_name + ".out"

    graph, num_buses, size_bus, constraints = pack

    optimal_result = result

    oriscore = score_output(graph, num_buses, size_bus, constraints, result)[0]
    max_score = oriscore

    direction = 5
    trynum = 16000
    # begin = time.time()
    if len(result) == 1:
        return filename, formatlst(result), 0
    else:
        for j in range(direction):
            result_copy = copy.deepcopy(result)

            for i in range(trynum):
                action = random.randint(0, 2)
                if action == 0:
                    random_two_swap(graph, num_buses, size_bus, constraints,
                                    result_copy)
                elif action == 1:
                    random_move(graph, num_buses, size_bus, constraints,
                                result_copy)
                else:
                    random_swap(graph, num_buses, size_bus, constraints,
                                result_copy)

                current_score = score_output(graph, num_buses, size_bus,
                                             constraints, result_copy)
                if (current_score[0] > max_score):
                    max_score = current_score[0]
                    optimal_result = result_copy
                    print(input_name, 'improved by', max_score - oriscore,
                          'at',
                          int((j * trynum + i) * 100 / (direction * trynum)),
                          '%')

                if (j * trynum + i) % 1000 == 0:
                    print(input_name,
                          int((j * trynum + i) * 100 / (direction * trynum)),
                          '%')
                    # if time.time()- begin> 30:
                    #     print(input_name, 'time_out')
                    #     return filename, optimal_result, max_score-oriscore
    output_file = open(filename, "w")
    output_file.write(formatlst(optimal_result))
    output_file.close()
    print('done', input_name)
    return filename, formatlst(optimal_result), max_score - oriscore
Beispiel #5
0
def swap(graph, num_buses, size_bus, constraints, result):
    prev_score = score_output(graph, num_buses, size_bus, constraints, result)
    for i in range(len(result) - 1):
        for j in range(i + 1, len(result)):
            for x in range(len(result[i])):
                for y in range(len(result[j])):
                    temp = result[i][x]
                    result[i][x] = result[j][y]
                    result[j][y] = temp
                    if score_output(graph, num_buses, size_bus, constraints,
                                    result) > prev_score:
                        swap(result)
                    if i == len(result) - 2 and x == len(
                            result[i]) - 1 and y == len(result[i + 1]) - 1:
                        return
                    result[j][y] = result[i][x]
                    result[i][x] = temp
Beispiel #6
0
def solvewrapper(G, k, c, constraints, rw, graphcpy):
    up = (rw + 1) * 10
    low = max(0, (rw - 1) * 10)
    bestscore = -1.0
    for i in range(low, up, 2):
        G = copy.deepcopy(graphcpy)
        w = i / 100
        plan = solve(G, k, c, constraints, w)
        del G
        score = score_output(graphcpy, k, c, constraints, plan)[0]
        if score > bestscore:
            bestscore = score
            bestplan = plan
            bestw = w
    return bestplan, bestw
Beispiel #7
0
def threadwork(data):
    pack, w, category_path, output_category_path, input_name = data
    # w/=10
    print('doing', category_path, input_name)
    graph, num_buses, size_bus, constraints = pack
    graphcpy = copy.deepcopy(graph)
    #             draw_graph(graph, graph_layout='spring')
    #             print(num_buses, size_bus)

    # raw, bestw = solvewrapper(graph, num_buses, size_bus, constraints, w, graphcpy)
    raw = solve(graph, num_buses, size_bus, constraints, w)
    solution = formatlst(raw)
    filename = output_category_path + "/" + input_name + ".out"
    score = score_output(graphcpy, num_buses, size_bus, constraints, raw)[0]

    return filename, solution, score