org.cpsolver.exam.model

## Class ExamDistributionConstraint

- java.lang.Object
- - org.cpsolver.ifs.model.Constraint<V,T>
  - - org.cpsolver.ifs.assignment.context.ConstraintWithContext<Exam,ExamPlacement,ExamDistributionConstraint.Context>
    - - org.cpsolver.exam.model.ExamDistributionConstraint

- All Implemented Interfaces:
    Comparable<Constraint<Exam,ExamPlacement>>, CanHoldContext, HasAssignmentContext<Exam,ExamPlacement,ExamDistributionConstraint.Context>

      public class ExamDistributionConstraint
      extends ConstraintWithContext<Exam,ExamPlacement,ExamDistributionConstraint.Context>

  Distribution binary constraint.
  The following binary distribution constraints are implemented
  - Same room
  - Different room
  - Same period
  - Different period
  - Precedence
  - Same day
  Version:
    ExamTT 1.3 (Examination Timetabling)
    Copyright (C) 2008 - 2014 Tomáš Müller
    muller@unitime.org
    http://muller.unitime.org
    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/.

- - ### Nested Class Summary

    Nested Classes
    Modifier and Type | Class and Description
    --- | ---
    `class` | `ExamDistributionConstraint.Context`

  - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `static int` | `sDistDifferentDay` Different day constraint type
    `static int` | `sDistDifferentPeriod` Different period constraint type
    `static int` | `sDistDifferentRoom` Different room constraint type
    `static int` | `sDistPrecedence` Precedence constraint type
    `static int` | `sDistPrecedenceRev` Precedence constraint type (reverse order)
    `static int` | `sDistSameDay` Same day constraint type
    `static int` | `sDistSamePeriod` Same period constraint type
    `static int` | `sDistSameRoom` Same room constraint type
    `static String[]` | `sDistType` Distribution type name

    - ### 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
    `ExamDistributionConstraint(long id, int type, boolean hard, int weight)` Constructor
    `ExamDistributionConstraint(long id, String type, boolean hard, int weight)` Constructor
    `ExamDistributionConstraint(long id, String type, String pref)` Constructor

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `computeConflicts(Assignment<Exam,ExamPlacement> assignment, ExamPlacement givenPlacement, Set<ExamPlacement> conflicts)` Compute conflicts -- there is a conflict if the other variable is assigned and `check(ExamPlacement, ExamPlacement)` is false
    `ExamDistributionConstraint.Context` | `createAssignmentContext(Assignment<Exam,ExamPlacement> assignment)` Create a new assignment context for the given assignment.
    `boolean` | `equals(Object o)` Compare with other constraint for equality
    `int` | `getType()` Constraint type
    `String` | `getTypeString()` Constraint type name
    `int` | `getWeight()` If not hard, penalty for violation
    `boolean` | `check(ExamPlacement first, ExamPlacement second)` Check assignments of the given exams
    `boolean` | `inConflict(Assignment<Exam,ExamPlacement> assignment, ExamPlacement givenPlacement)` Check for conflict -- there is a conflict if the other variable is assigned and `check(ExamPlacement, ExamPlacement)` is false
    `boolean` | `isConsistent(ExamPlacement first, ExamPlacement second)` Consistency check -- `check(ExamPlacement, ExamPlacement)` is called
    `boolean` | `isHard()` True if hard (must be satisfied), false for soft (should be satisfied)
    `boolean` | `isPeriodRelated()` True if the constraint is related to periods
    `boolean` | `isRoomRelated()` True if the constraint is related to rooms
    `boolean` | `isSatisfied(Assignment<Exam,ExamPlacement> assignment)` Return true if this is hard constraint or this is a soft constraint without any violation
    `boolean` | `isSatisfied(Assignment<Exam,ExamPlacement> assignment, ExamPlacement p)` Return true if this is hard constraint or this is a soft constraint without any violation
    `String` | `toString()` String representation -- constraint type name (exam 1, exam 2)

    - ### 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, computeConflictsNoForwardCheck, constraintListeners, countAssignedVariables, countVariables, getDescription, getId, getModel, getName, hashCode, removeConstraintListener, removeVariable, variables`

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

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

- - ### Field Detail

    - #### sDistSameRoom

          public static final int sDistSameRoom

      Same room constraint type
      See Also:
        Constant Field Values

    - #### sDistDifferentRoom

          public static final int sDistDifferentRoom

      Different room constraint type
      See Also:
        Constant Field Values

    - #### sDistSamePeriod

          public static final int sDistSamePeriod

      Same period constraint type
      See Also:
        Constant Field Values

    - #### sDistDifferentPeriod

          public static final int sDistDifferentPeriod

      Different period constraint type
      See Also:
        Constant Field Values

    - #### sDistPrecedence

          public static final int sDistPrecedence

      Precedence constraint type
      See Also:
        Constant Field Values

    - #### sDistPrecedenceRev

          public static final int sDistPrecedenceRev

      Precedence constraint type (reverse order)
      See Also:
        Constant Field Values

    - #### sDistSameDay

          public static final int sDistSameDay

      Same day constraint type
      See Also:
        Constant Field Values

    - #### sDistDifferentDay

          public static final int sDistDifferentDay

      Different day constraint type
      See Also:
        Constant Field Values

    - #### sDistType

          public static final String[] sDistType

      Distribution type name

  - ### Constructor Detail

    - #### ExamDistributionConstraint

          public ExamDistributionConstraint(long id,
                                            int type,
                                            boolean hard,
                                            int weight)

      Constructor
      Parameters:
        `id` - constraint unique id
        `type` - constraint type
        `hard` - true if the constraint is hard (cannot be violated)
        `weight` - if not hard, penalty for violation

    - #### ExamDistributionConstraint

          public ExamDistributionConstraint(long id,
                                            String type,
                                            String pref)

      Constructor
      Parameters:
        `id` - constraint unique id
        `type` - constraint type (EX_SAME_PREF, EX_SAME_ROOM, or EX_PRECEDENCE)
        `pref` - preference (R/P for required/prohibited, or -2, -1, 0, 1, 2 for preference (from preferred to discouraged))

    - #### ExamDistributionConstraint

          public ExamDistributionConstraint(long id,
                                            String type,
                                            boolean hard,
                                            int weight)

      Constructor
      Parameters:
        `id` - constraint unique id
        `type` - constraint type name
        `hard` - true if the constraint is hard
        `weight` - constraint penalty if violated (for soft constraint)

  - ### Method Detail

    - #### isHard

          public boolean isHard()

      True if hard (must be satisfied), false for soft (should be satisfied)
      Overrides:
        `isHard` in class `Constraint<Exam,ExamPlacement>`
      Returns:
        true if the constraint is hard

    - #### getWeight

          public int getWeight()

      If not hard, penalty for violation
      Returns:
        constraint penalty if violated

    - #### getType

          public int getType()

      Constraint type
      Returns:
        constraint type

    - #### getTypeString

          public String getTypeString()

      Constraint type name
      Returns:
        constraint type name (one of `sDistType`)

    - #### toString

          public String toString()

      String representation -- constraint type name (exam 1, exam 2)
      Overrides:
        `toString` in class `Object`

    - #### computeConflicts

          public void computeConflicts(Assignment<Exam,ExamPlacement> assignment,
                                       ExamPlacement givenPlacement,
                                       Set<ExamPlacement> conflicts)

      Compute conflicts -- there is a conflict if the other variable is assigned and `check(ExamPlacement, ExamPlacement)` is false
      Specified by:
        `computeConflicts` in class `Constraint<Exam,ExamPlacement>`
      Parameters:
        `assignment` - current assignment
        `givenPlacement` - value to be assigned to its variable
        `conflicts` - resultant set of conflicting values

    - #### inConflict

          public boolean inConflict(Assignment<Exam,ExamPlacement> assignment,
                                    ExamPlacement givenPlacement)

      Check for conflict -- there is a conflict if the other variable is assigned and `check(ExamPlacement, ExamPlacement)` is false
      Overrides:
        `inConflict` in class `Constraint<Exam,ExamPlacement>`
      Parameters:
        `assignment` - current assignment
        `givenPlacement` - given value
      Returns:
        true if there is a conflict with other assigned variables of the constraint

    - #### isConsistent

          public boolean isConsistent(ExamPlacement first,
                                      ExamPlacement second)

      Consistency check -- `check(ExamPlacement, ExamPlacement)` is called
      Overrides:
        `isConsistent` in class `Constraint<Exam,ExamPlacement>`
      Parameters:
        `first` - a value
        `second` - a value
      Returns:
        true if the constraint is ok with the assignment

    - #### check

          public boolean check(ExamPlacement first,
                               ExamPlacement second)

      Check assignments of the given exams
      Parameters:
        `first` - assignment of the first exam
        `second` - assignment of the second exam
      Returns:
        true, if the constraint is satisfied

    - #### equals

          public boolean equals(Object o)

      Compare with other constraint for equality
      Overrides:
        `equals` in class `Constraint<Exam,ExamPlacement>`

    - #### isSatisfied

          public boolean isSatisfied(Assignment<Exam,ExamPlacement> assignment)

      Return true if this is hard constraint or this is a soft constraint without any violation
      Parameters:
        `assignment` - current assignment
      Returns:
        true if the constraint is satisfied

    - #### isSatisfied

          public boolean isSatisfied(Assignment<Exam,ExamPlacement> assignment,
                                     ExamPlacement p)

      Return true if this is hard constraint or this is a soft constraint without any violation
      Parameters:
        `assignment` - current assignment
        `p` - exam assignment to be made
      Returns:
        true if the constraint is satisfied

    - #### isRoomRelated

          public boolean isRoomRelated()

      True if the constraint is related to rooms
      Returns:
        true if the constraint is related to room placement

    - #### isPeriodRelated

          public boolean isPeriodRelated()

      True if the constraint is related to periods
      Returns:
        true if the constraint is related to period placement

    - #### createAssignmentContext

          public ExamDistributionConstraint.Context createAssignmentContext(Assignment<Exam,ExamPlacement> 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