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/.
  Author:
    Tomáš Müller

- - ### Nested Class Summary

    Nested Classes
    Modifier and Type | Class and Description
    --- | ---
    `class` | `ExamDistributionConstraint.Context`
    `static class` | `ExamDistributionConstraint.DistributionType`
    `static interface` | `ExamDistributionConstraint.PairCheck`
    `static interface` | `ExamDistributionConstraint.PeriodCheck`
    `static interface` | `ExamDistributionConstraint.RoomCheck`

  - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `static int` | `sDistSameRoom` Deprecated.

    - ### 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, ExamDistributionConstraint.DistributionType type, boolean hard, int weight)` Constructor
    `ExamDistributionConstraint(long id, int type, boolean hard, int weight)` Deprecated. use `ExamDistributionConstraint(long, DistributionType, boolean, int)`
    `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 Deprecated Methods
    Modifier and Type | Method and Description
    --- | ---
    `boolean` | `check(ExamPlacement first, ExamPlacement second)` Deprecated. use `ExamDistributionConstraint.DistributionType.isSatisfied(ExamPlacement, ExamPlacement)`
    `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
    `int` | `countViolations(Assignment<Exam,ExamPlacement> assignment)` Return number of all violated pairs for a soft constraint
    `int` | `countViolations(Assignment<Exam,ExamPlacement> assignment, ExamPlacement p)` Return number of violated pairs for a soft constraint and the given placement
    `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
    `ExamDistributionConstraint.DistributionType` | `getDistributionType()`
    `int` | `getType()` Constraint type
    `String` | `getTypeString()` Constraint type name
    `int` | `getWeight()` If not hard, penalty for violation
    `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

          @Deprecated
          public static int sDistSameRoom

      Deprecated.

  - ### Constructor Detail

    - #### ExamDistributionConstraint

          public ExamDistributionConstraint(long id,
                                            ExamDistributionConstraint.DistributionType 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

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

      Deprecated. use `ExamDistributionConstraint(long, DistributionType, boolean, int)`
      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

    - #### getDistributionType

          public ExamDistributionConstraint.DistributionType getDistributionType()

    - #### getTypeString

          public String getTypeString()

      Constraint type name
      Returns:
        constraint type name (one of `ExamDistributionConstraint.DistributionType.getReference()`)

    - #### 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

          @Deprecated
          public boolean check(ExamPlacement first,
                                           ExamPlacement second)

      Deprecated. use `ExamDistributionConstraint.DistributionType.isSatisfied(ExamPlacement, ExamPlacement)`
      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

    - #### countViolations

          public int countViolations(Assignment<Exam,ExamPlacement> assignment,
                                     ExamPlacement p)

      Return number of violated pairs for a soft constraint and the given placement
      Parameters:
        `assignment` - current assignment
        `p` - exam assignment to be made
      Returns:
        number of examination pairs violated

    - #### countViolations

          public int countViolations(Assignment<Exam,ExamPlacement> assignment)

      Return number of all violated pairs for a soft constraint
      Parameters:
        `assignment` - current assignment
      Returns:
        number of examination pairs violated

    - #### 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