示例#1
0
def _schedule_all_events(events, slots, X, summation_type=None):

    shape = Shape(len(events), len(slots))
    summation = lpu.summation_functions[summation_type]

    label = 'Event either not scheduled or scheduled multiple times'
    for event in range(shape.events):
        yield Constraint(
            f'{label} - event: {event}',
            summation(X[event, slot] for slot in range(shape.slots)) == 1)
示例#2
0
def _max_one_event_per_slot(events, slots, X, summation_type=None):

    shape = Shape(len(events), len(slots))
    summation = lpu.summation_functions[summation_type]

    label = 'Slot with multiple events scheduled'
    for slot in range(shape.slots):
        yield Constraint(
            f'{label} - slot: {slot}',
            summation(X[(event, slot)] for event in range(shape.events)) <= 1)
示例#3
0
def solution(events, slots, objective_function=None, solver=None, **kwargs):
    """Compute a schedule in solution 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 tuples giving the event and slot index (for the given
        events and slots lists) for all scheduled items.

    Example
    -------
    For a solution where

        * event 0 is scheduled in slot 1
        * event 1 is scheduled in slot 4
        * event 2 is scheduled in slot 5

    the resulting list would be::

        [(0, 1), (1, 4), (2, 5)]
    """
    shape = Shape(len(events), len(slots))
    problem = pulp.LpProblem()
    X = lp.utils.variables(shape)
    beta = pulp.LpVariable("upper_bound")

    for constraint in lp.constraints.all_constraints(events, slots, X, beta,
                                                     'lpsum'):
        problem += constraint.condition

    if objective_function is not None:
        problem += objective_function(events=events,
                                      slots=slots,
                                      X=X,
                                      beta=beta,
                                      **kwargs)

    status = problem.solve(solver=solver)
    if status == 1:
        return [item for item, variable in X.items() if variable.value() > 0]
    else:
        raise ValueError('No valid solution found')
示例#4
0
def shape(events, slots):
    return Shape(len(events), len(slots))