org.cpsolver.coursett.constraint

## Class FlexibleConstraint

- java.lang.Object
- - org.cpsolver.ifs.model.Constraint<V,T>
  - - org.cpsolver.ifs.assignment.context.ConstraintWithContext<Lecture,Placement,FlexibleConstraint.FlexibleConstraintContext>
    - - org.cpsolver.coursett.constraint.FlexibleConstraint

- All Implemented Interfaces:
    Comparable<Constraint<Lecture,Placement>>, CanHoldContext, HasAssignmentContext<Lecture,Placement,FlexibleConstraint.FlexibleConstraintContext>
  Direct Known Subclasses:
    BreakFlexibleConstraint, MaxBlockFlexibleConstraint, MaxBreaksFlexibleConstraint, MaxConsecutiveDaysFlexibleConstraint, MaxDaysFlexibleConstraint, MaxHalfDaysFlexibleConstraint, MaxHolesFlexibleConstraint, MaxWeeksFlexibleConstraint

      public abstract class FlexibleConstraint
      extends ConstraintWithContext<Lecture,Placement,FlexibleConstraint.FlexibleConstraintContext>

  Flexible constraint.
  This constraint expresses relations between several classes. Provides similar functions as Group constraint. Unlike group constraint, Flexible constraint is able to parse some of its parameters from its reference field
  Version:
    CourseTT 1.3 (University Course Timetabling)
    Copyright (C) 2013 Matej Lukac
    This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
    This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
    You should have received a copy of the GNU Lesser General Public License along with this library; if not see http://www.gnu.org/licenses/.
  Author:
    Matej Lukac

- - ### Nested Class Summary

    Nested Classes
    Modifier and Type | Class and Description
    --- | ---
    `class` | `FlexibleConstraint.Block` A block is a list of placements sorted by startSlot, which are BTB.
    `class` | `FlexibleConstraint.FlexibleConstraintContext`
    `static class` | `FlexibleConstraint.FlexibleConstraintType` Flexible constraint types
    `protected static class` | `FlexibleConstraint.PlacementTimeComparator` Placement comparator: earlier placement first, shorter placement first if both start at the same time.

  - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `protected FlexibleConstraint.FlexibleConstraintType` | `iConstraintType`
    `protected Integer` | `iDayOfWeekOffset`
    `protected Boolean` | `iPreciseDateComputation`
    `protected int` | `iPreference`
    `protected String` | `iReference`
    `protected List<BitSet>` | `iWeeks`

    - ### Fields inherited from class org.cpsolver.ifs.model.Constraint

      `iConstraintListeners, iId`

    - ### Fields inherited from interface org.cpsolver.ifs.assignment.context.CanHoldContext

      `sMaxSize`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `FlexibleConstraint(Long id, String owner, String preference, String reference)` Constructor

  - ### Method Summary

    All Methods Instance Methods Abstract Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `FlexibleConstraint.FlexibleConstraintContext` | `createAssignmentContext(Assignment<Lecture,Placement> assignment)` Create a new assignment context for the given assignment.
    `double` | `getCurrentPreference(Assignment<Lecture,Placement> assignment, Set<Placement> conflicts, HashMap<Lecture,Placement> assignments)` Return the current preference of the flexible constraint, considering conflicts and new assignments.
    `int` | `getDayOfWeekOffset()`
    `String` | `getName()` Constraint's name -- for printing purposes
    `abstract double` | `getNrViolations(Assignment<Lecture,Placement> assignment, Set<Placement> conflicts, HashMap<Lecture,Placement> assignments)` Return current number of violations.
    `String` | `getOwner()`
    `String` | `getPrologPreference()` Prolog reference: "R" for required, "P" for prohibited", "-2",.."2" for preference
    `String` | `getReference()`
    `protected Set<Placement>` | `getRelevantPlacements(Assignment<Lecture,Placement> assignment, int dayCode, Set<Placement> conflicts, Placement value, HashMap<Lecture,Placement> assignments, BitSet week)` Returns placements of variables assigned to this constraint with assignment which satisfy following conditions: They must be taught in the day included in dayCode.
    `FlexibleConstraint.FlexibleConstraintType` | `getType()`
    `List<BitSet>` | `getWeeks()` Return weeks of the term.
    `boolean` | `isConsistent(Placement value1, Placement value2)` Returns true if the given assignments are consistent respecting this constraint.
    `boolean` | `isHard()` Returns true if the constraint is hard.
    `boolean` | `isPreciseDateComputation()`
    `protected List<FlexibleConstraint.Block>` | `mergeToBlocks(List<Placement> sorted, int maxBreakBetweenBTB)` Creates a list of blocks from a placements sorted by startSlot
    `protected boolean` | `shareWeeksAndDay(TimeLocation t, BitSet week, int dayCode)` Used to determine the daycode and week of a timelocation
    `String` | `toString()`

    - ### Methods inherited from class org.cpsolver.ifs.assignment.context.ConstraintWithContext

      `assigned, getAssignmentContextReference, getContext, getContext, setAssignmentContextReference, setModel, unassigned`

    - ### Methods inherited from class org.cpsolver.ifs.model.Constraint

      `addConstraintListener, addVariable, assignedVariables, compareTo, computeConflicts, computeConflictsNoForwardCheck, constraintListeners, countAssignedVariables, countVariables, equals, getDescription, getId, getModel, hashCode, inConflict, removeConstraintListener, removeVariable, variables`

    - ### Methods inherited from class java.lang.Object

      `clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

- - ### Field Detail

    - #### iPreference

          protected int iPreference

    - #### iConstraintType

          protected FlexibleConstraint.FlexibleConstraintType iConstraintType

    - #### iReference

          protected String iReference

    - #### iWeeks

          protected List<BitSet> iWeeks

    - #### iDayOfWeekOffset

          protected Integer iDayOfWeekOffset

    - #### iPreciseDateComputation

          protected Boolean iPreciseDateComputation

  - ### Constructor Detail

    - #### FlexibleConstraint

          public FlexibleConstraint(Long id,
                                    String owner,
                                    String preference,
                                    String reference)

      Constructor
      Parameters:
        `id` - unique id
        `owner` - identifier of distribution preference the constraint was created for
        `preference` - time preference ("R" for required, "P" for prohibited, "-2", "-1", "1", "2" for soft preference)
        `reference` - parameters of the constraint in String form

  - ### Method Detail

    - #### getNrViolations

          public abstract double getNrViolations(Assignment<Lecture,Placement> assignment,
                                                 Set<Placement> conflicts,
                                                 HashMap<Lecture,Placement> assignments)

      Return current number of violations.
      Parameters:
        `assignment` - current assignment
        `conflicts` - conflicting placements to be unassigned
        `assignments` - assigned placements
      Returns:
        the number of violations of the constraint during days and all weeks of the semester

    - #### getWeeks

          public List<BitSet> getWeeks()

      Return weeks of the term.
      Returns:
        a list of bitsets (one for each week of the term) representing datePatterns or null if semester is whole semester is considered

    - #### getDayOfWeekOffset

          public int getDayOfWeekOffset()

    - #### isPreciseDateComputation

          public boolean isPreciseDateComputation()

    - #### isConsistent

          public boolean isConsistent(Placement value1,
                                      Placement value2)

      Description copied from class: `Constraint`
      Returns true if the given assignments are consistent respecting this constraint. This method is used by MAC (see `MacPropagation`).
      Overrides:
        `isConsistent` in class `Constraint<Lecture,Placement>`
      Parameters:
        `value1` - a value
        `value2` - a value
      Returns:
        true if the constraint is ok with the assignment

    - #### getRelevantPlacements

          protected Set<Placement> getRelevantPlacements(Assignment<Lecture,Placement> assignment,
                                                         int dayCode,
                                                         Set<Placement> conflicts,
                                                         Placement value,
                                                         HashMap<Lecture,Placement> assignments,
                                                         BitSet week)

      Returns placements of variables assigned to this constraint with assignment which satisfy following conditions: They must be taught in the day included in dayCode. They cannot be included in conflicts Their date pattern intersects with week
      Parameters:
        `assignment` - current assignment
        `dayCode` - representation of days in week combination
        `conflicts` - placements to be unassigned
        `value` - placement to be assigned
        `assignments` - placements of variables
        `week` - bitset representing a date pattern
      Returns:
        placements of variables assigned to this constraint with assignment which satisfy conditions above

    - #### shareWeeksAndDay

          protected boolean shareWeeksAndDay(TimeLocation t,
                                             BitSet week,
                                             int dayCode)

      Used to determine the daycode and week of a timelocation
      Parameters:
        `t` - timelocation
        `week` - date pattern compared to timelocation
        `dayCode` - days compared to timelocation
      Returns:
        true if TimeLocation matches the date pattern and days

    - #### mergeToBlocks

          protected List<FlexibleConstraint.Block> mergeToBlocks(List<Placement> sorted,
                                                                 int maxBreakBetweenBTB)

      Creates a list of blocks from a placements sorted by startSlot
      Parameters:
        `sorted` - list of placements sorted by startSlot
        `maxBreakBetweenBTB` - maximum number of free slots between BTB placements
      Returns:
        groups of BTB placements as a list of blocks

    - #### isHard

          public boolean isHard()

      Description copied from class: `Constraint`
      Returns true if the constraint is hard. Only hard constraints are allowed to unassign a variable when there is a conflict with a value that is being assigned
      Overrides:
        `isHard` in class `Constraint<Lecture,Placement>`
      Returns:
        true if the constraint is hard

    - #### getName

          public String getName()

      Description copied from class: `Constraint`
      Constraint's name -- for printing purposes
      Overrides:
        `getName` in class `Constraint<Lecture,Placement>`
      Returns:
        constraint name

    - #### getType

          public FlexibleConstraint.FlexibleConstraintType getType()

    - #### getReference

          public String getReference()

    - #### getOwner

          public String getOwner()

    - #### getPrologPreference

          public String getPrologPreference()

      Prolog reference: "R" for required, "P" for prohibited", "-2",.."2" for preference
      Returns:
        prolog preference

    - #### getCurrentPreference

          public double getCurrentPreference(Assignment<Lecture,Placement> assignment,
                                             Set<Placement> conflicts,
                                             HashMap<Lecture,Placement> assignments)

      Return the current preference of the flexible constraint, considering conflicts and new assignments. Used to compute value for flexible constraint criterion.
      Parameters:
        `assignment` - current assignment
        `conflicts` - conflicting assignment
        `assignments` - proposed assignments
      Returns:
        the current preference of the flexible constraint

    - #### toString

          public String toString()

      Overrides:
        `toString` in class `Object`

    - #### createAssignmentContext

          public FlexibleConstraint.FlexibleConstraintContext createAssignmentContext(Assignment<Lecture,Placement> assignment)

      Description copied from interface: `HasAssignmentContext`
      Create a new assignment context for the given assignment. If there are already some variables assigned in the value, the method should make sure that the context is appropriately initialized.
      Parameters:
        `assignment` - an assignment for which there needs to be an assignment context
      Returns:
        a new instance of the assignment context, filled according to the given assignment