Пример #1
0
def build(
    verbosity, algorithm, objective, diff, input_dir, solution_dir, build_dir
):
    logging.setup(verbosity)
    if input_dir:
        session.folders['input'] = Path(input_dir)

    if solution_dir:
        session.folders['solution'] = Path(solution_dir)

    if build_dir:
        session.folders['build'] = Path(build_dir)

    resources = defn.resources()
    events, slots = events_and_slots(resources)

    slots_by_index = {
        idx: f'{slot.starts_at} {slot.venue}'
        for idx, slot in enumerate(slots)}
    logger.debug(f'\nSlots List:\n{slots_by_index}')

    kwargs = {}
    if objective == 'consistency' or algorithm == 'simulated_annealing' or diff:
        original_solution = io.import_solution(session.folders['solution'])
        revised_solution = [
            item for item in original_solution
            if item[0] < len(events)]
        original_schedule = solution_to_schedule(
            revised_solution, events, slots)
        diff = True
        kwargs['original_schedule'] = original_schedule

    solution = calc.solution(events, slots, algorithm, objective, **kwargs)

    if diff:
        schedule = solution_to_schedule(solution, events, slots)
        event_diff = event_schedule_difference(original_schedule, schedule)
        logger.debug(f'\nevent_diff:')
        for item in event_diff:
            logger.debug(f'{item.event.name} has moved from {item.old_slot.venue} at {item.old_slot.starts_at} to {item.new_slot.venue} at {item.new_slot.starts_at}')

    if solution is not None:
        allocations = defn.allocations(resources)
        unbounded = defn.unbounded(resources)
        defn.add_allocations(events, slots, solution, allocations, unbounded)
        logger.debug(convert.schedule_to_text(solution, events, slots))
        io.export_solution_and_definition(
            resources, events, slots, allocations, solution,
            session.folders['solution'])
Пример #2
0
def schedule_to_text(solution, events, slots):
    schedule = converter.solution_to_schedule(solution, events, slots)
    schedule.sort(key=lambda item: item.slot.starts_at)
    message = 'Schedule:\n\n'
    for item in schedule:
        message += (f'{item.event.name} at {item.slot.starts_at} in '
                    f'{item.slot.venue}\n')
    return message
Пример #3
0
def export_schedule(solution, events, slots, solution_folder):
    """Write a human readable .csv file of the computed solution"""
    csv_file = Path(solution_folder, 'schedule.csv')
    logger.info(f'Exporting schedule to {csv_file}')

    schedule = converter.solution_to_schedule(solution, events, slots)
    scheduled_items = [{
        'event_index': events.index(item.event),
        'event': f'{item.event.name}',
        'slot_index': slots.index(item.slot),
        'slot': f'{item.slot.starts_at} {item.slot.venue}'
    } for item in schedule]
    with csv_file.open('w', newline='') as f:
        writer = csv.DictWriter(
            f, fieldnames=['event_index', 'event', 'slot_index', 'slot'])
        writer.writeheader()
        for item in scheduled_items:
            writer.writerow(item)
Пример #4
0
def schedule(events, slots, objective_function=None, solver=None, **kwargs):
    """Compute a schedule in schedule form

    Parameters
    ----------
    events : list or tuple
        of :py:class:`resources.Event` instances
    slots : list or tuple
        of :py:class:`resources.Slot` instances
    solver : pulp.solver
        a pulp solver
    objective_function : callable
        from lp_problem.objective_functions
    kwargs : keyword arguments
        arguments for the objective function

    Returns
    -------
    list
        A list of instances of :py:class:`resources.ScheduledItem`
    """
    return conv.solution_to_schedule(
        solution(events, slots, objective_function, solver=solver, **kwargs),
        events, slots)
Пример #5
0
def test_solution_to_schedule(valid_solution, valid_schedule, events, slots):
    schedule = converter.solution_to_schedule(valid_solution, events, slots)
    assert type(schedule) is list
    assert list(schedule) == valid_schedule