Constraints

What really makes scheduling interesting are constraints. This is where the magic starts to happen. The good thing is that you can implement almost any possible constraint in Schedule4Net. Suppose, for example, that we require in our schedules that no job with an even id must start at the same time as a job with an odd id. We can write following constraint to achieve just that:
class EvenOddConstraint : AbstractPairConstraint<ScheduledItem>
{
    protected override ConstraintDecision CheckConstraint(ScheduledItem item1, ScheduledItem item2)
    {
        Job job1 = item1.ItemToSchedule as Job;
        Job job2 = item2.ItemToSchedule as Job;
        return new ConstraintDecision(true, (job1.JobId % 2) == (job2.JobId % 2), 100);
    }
}

The framework already comes with some predefined constraints, ready-to-use:
  • NoOverlappingConstraint: prevents items on the same lane to overlap each other
  • DependenciesConstraint: ensures that item-dependencies are met by the scheduler
  • StartNowConstraint: tells the scheduler that items should start rather sooner than later

Types of constraints

There exist two different types of constraints (actually four, but more on that later):
  1. SingleItemConstraint: This constraint is provided with a single scheduled item and has to decide if this items violates the constraint or not. This constraint is used if a given scheduled item can be evaluated without looking at any other item in the schedule. For example, this might be an expiration constraint ("must not start later than").
  2. ItemPairConstraint: This constraint is provided with two items from the schedule and has to decide if they violate the constraint or not. If you're not sure where to start with this constraint, just extend the AbstractPairConstraint class and you are good to go. For example, such a constraint could check if the execution times of two items overlap or if some dependency logic is fulfilled ("schedule item A after item B").
Note: Some scenarios require the possibility to create constraint with more than just one or two inputs. For example, if I want to be able to always schedule three out of four possible items at the same time then a constraint would have to be able to look at all the items in a schedule (not just one or two at a time). So, if you require such a logic in a constraint you either have to rethink your problem or you are unlikely to find this framework very useful.

Last edited Feb 21, 2013 at 12:14 PM by galetzka, version 1

Comments

No comments yet.