コード例 #1
0
def solve_randomly(prob: problem.Problem,
                   seed: int,
                   distro: [] = None,
                   max_cost=None):
    random.seed(seed)

    if distro is None:
        prob.apply_distro(gen_distro(prob))
    else:
        prob.apply_distro(distro)

    cost_so_far = 0
    while not prob.is_solved():
        todo = prob.sources_todo()
        delivs = prob.delivs_avail()
        random.shuffle(todo)

        if len(todo) < len(delivs):
            i = 0
            for t in todo:
                cost_so_far += delivs[i].make_move(
                    t, prob.get_place(t.label.lower()))
                i += 1
        else:
            i = 0
            for deliv in delivs:
                cost_so_far += deliv.make_move(
                    todo[i], prob.get_place(todo[i].label.lower()))
                i += 1

        if max_cost is not None and cost_so_far >= max_cost:
            break  # stop execution if this seed is infeasible.

    return cost_so_far