示例#1
0
# {'book_scores':book_scores, days:'days', 'libraries':libraries}
def solver(inputs):
    books_so_far = set()
    book_scores = inputs["book_scores"]
    days_left = inputs["days"]
    libraries = inputs["libraries"]
    n_books = inputs['n_books']
    result = []

    while len(libraries) > 0:
        libraries = sorted(libraries,
                           key=lambda x: x.score3(books_so_far),
                           reverse=True)
        result.append(libraries[0])
        for book in libraries[0].books.keys():
            books_so_far.add(book)
            if len(books_so_far) == n_books: break

        if len(books_so_far) == n_books: break

        libraries.pop(0)
        days_left -= result[-1].signup_days
        if len(result) % 100 == 0:
            print(" of libraries selected ", len(result))

    return result


if __name__ == "__main__":
    utils.solve_files('data', solver)
            cur = v.score_if_assigned(ride)
            if (not vehicle) or (cur[0] > best[0]
                                 or cur[0] == best[0] and cur[1] < best[1]):
                best, vehicle = cur, v
        vehicle.assign(ride)
    sol = {v.id: v.get_idx() for v in vehicles}
    return sol


def simulatedAnnealingSolver(inputs, T=100, n_iter=10000, temp_update=.9):
    if inputs["rides"] < 100:
        n_iter = 300
        temp_update = .9
    elif inputs["rides"] < 1000:
        n_iter = 1000
        temp_update = .95
    else:
        n_iter = 5000
        temp_update = .99
    model = SimulatedAnnealing(inputs,
                               T=T,
                               n_iter=n_iter,
                               temp_update=temp_update)
    model.fit()
    print("score ", model.cur_score)
    return model.solution


if __name__ == "__main__":
    utils.solve_files('data', "result_SA", simulatedAnnealingSolver)