net.sf.cpsolver.ifs.model

## Class Model<V extends Variable<V,T>,T extends Value<V,T>>

- java.lang.Object
- - net.sf.cpsolver.ifs.model.Model<V,T>

- Direct Known Subclasses:
    ConstantModel, CSPModel, ExamModel, JobShopModel, RPPModel, StructuredCSPModel, StudentSctBBTest, StudentSectioningModel, TimetableModel

      public class Model<V extends Variable<V,T>,T extends Value<V,T>>
      extends Object

  Generic model (definition of a problem).
  It consists of variables and constraints. It has also capability of memorizing the current and the best ever found assignment.
  Example usage:
  `MyModel model = new MyModel();
  Variable a = new MyVariable("A");
  model.addVariable(a);
  Variable b = new MyVariable("B");
  model.addVariable(b);
  Variable c = new MyVariable("C");
  model.addVariable(c);
  Constraint constr = MyConstraint("all-different");
  model.addConstraint(constr);
  constr.addVariable(a);
  constr.addVariable(b);
  constr.addVariable(c);
  solver.setInitialSolution(model);`
  Version:
    IFS 1.2 (Iterative Forward Search)
    Copyright (C) 2006 - 2010 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/.
  See Also:
    `Variable`, `Constraint`, `Solution`, `Solver`

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `protected Collection<V>` | `iAssignedVariables`
    `protected Collection<V>` | `iPerturbVariables`
    `protected Collection<V>` | `iUnassignedVariables`
    `protected static DecimalFormat` | `sDoubleFormat`
    `protected static DecimalFormat` | `sPercentageFormat`
    `protected static DecimalFormat` | `sTimeFormat`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `Model()` Constructor

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `addConstraint(Constraint<V,T> constraint)` Adds a constraint to the model
    `void` | `addCriterion(Criterion<V,T> criterion)` Register a new criterion
    `void` | `addGlobalConstraint(GlobalConstraint<V,T> constraint)` Adds a global constraint to the model
    `void` | `addModelListener(ModelListener<V,T> listener)` Adds a model listener
    `void` | `addVariable(V variable)` Adds a variable to the model
    `void` | `afterAssigned(long iteration, T value)` Called after a value is assigned to its variable
    `void` | `afterUnassigned(long iteration, T value)` Called after a value is unassigned from its variable
    `Collection<V>` | `assignedVariables()` The list of assigned variables in the model
    `void` | `beforeAssigned(long iteration, T value)` Called before a value is assigned to its variable
    `void` | `beforeUnassigned(long iteration, T value)` Called before a value is unassigned from its variable
    `Collection<V>` | `bestUnassignedVariables()` The list of unassigned variables in the best ever found solution
    `void` | `clearBest()` Clear the best ever found assignment
    `Map<Constraint<V,T>,Set<T>>` | `conflictConstraints(T value)` The list of constraints which are in a conflict with the given value if it is assigned to its variable.
    `Set<T>` | `conflictValues(T value)` Returns the set of conflicting variables with this value, if it is assigned to its variable
    `List<Constraint<V,T>>` | `constraints()` The list of constraints in the model
    `int` | `countConstraints()` The number of constraints in the model
    `int` | `countGlobalConstraints()` The number of global constraints in the model
    `int` | `countVariables()` The number of variables in the model
    `int` | `getBestPerturbations()` Returns the number of perturbation variables in the best ever found solution
    `int` | `getBestUnassignedVariables()` Returns the number of unassigned variables in the best ever found solution
    `Collection<Criterion<V,T>>` | `getCriteria()` List all registered criteria
    `Criterion<V,T>` | `getCriterion(Class<? extends Criterion<V,T>> criterion)` Return a registered criterion of the given type.
    `Map<String,String>` | `getExtendedInfo()` Extended information about current solution.
    `Map<String,String>` | `getInfo()` Returns information about the current solution.
    `Map<String,String>` | `getInfo(Collection<V> variables)` Returns information about the current solution.
    `protected List<InfoProvider<V>>` | `getInfoProviders()` Registered info providers (see `InfoProvider`)
    `List<ModelListener<V,T>>` | `getModelListeners()` The list of model listeners
    `protected String` | `getPerc(double value, double min, double max)`
    `protected String` | `getPercRev(double value, double min, double max)`
    `double` | `getTotalValue()` Value of the current solution.
    `double` | `getTotalValue(Collection<V> variables)` Value of the current solution.
    `List<GlobalConstraint<V,T>>` | `globalConstraints()` The list of global constraints in the model
    `boolean` | `inConflict(T value)` Return true if the given value is in conflict with a hard constraint
    `boolean` | `init(Solver<V,T> solver)` Model initialization
    `protected void` | `invalidateVariablesWithInitialValueCache()` Invalidates cache containing all variables that possess an initial value
    `ModelListener<V,T>` | `modelListenerOfType(Class<ModelListener<V,T>> type)` The list of model listeners that are of the given class
    `int` | `nrAssignedVariables()` Number of assigned variables
    `int` | `nrUnassignedVariables()` Number of unassigned variables
    `Collection<V>` | `perturbVariables()` The list of perturbation variables in the model, i.e., the variables which has an initial value but which are not assigned with this value.
    `List<V>` | `perturbVariables(Collection<V> variables)` The list of perturbation variables in the model, i.e., the variables which has an initial value but which are not assigned with this value.
    `void` | `removeConstraint(Constraint<V,T> constraint)` Removes a constraint from the model
    `void` | `removeCriterion(Class<? extends Criterion<V,T>> criterion)` Unregister an existing criterion
    `void` | `removeCriterion(Criterion<V,T> criterion)` Unregister an existing criterion
    `void` | `removeGlobalConstraint(GlobalConstraint<V,T> constraint)` Removes a global constraint from the model
    `void` | `removeModelListener(ModelListener<V,T> listener)` Removes a model listener
    `void` | `removeVariable(V variable)` Removes a variable from the model
    `void` | `restoreBest()`
    `protected void` | `restoreBest(Comparator<V> assignmentOrder)` Restore the best ever found assignment into the current assignment
    `void` | `saveBest()` Save the current assignment as the best ever found assignment
    `String` | `toString()`
    `List<Constraint<V,T>>` | `unassignedHardConstraints()` The list of hard constraints which contain at least one variable that is not assigned.
    `Collection<V>` | `unassignedVariables()` The list of unassigned variables in the model
    `List<V>` | `variables()` The list of variables in the model
    `Collection<V>` | `variablesWithInitialValue()` The list of variables without initial value
    `void` | `weaken(T value)` Weaken all weakening constraint so that the given value can be assigned without them creating a conflict using `WeakeningConstraint.weaken(Value)`.

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

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

- - ### Field Detail

    - #### sTimeFormat

          protected static DecimalFormat sTimeFormat

    - #### sDoubleFormat

          protected static DecimalFormat sDoubleFormat

    - #### sPercentageFormat

          protected static DecimalFormat sPercentageFormat

    - #### iUnassignedVariables

          protected Collection<V extends Variable<V,T>> iUnassignedVariables

    - #### iAssignedVariables

          protected Collection<V extends Variable<V,T>> iAssignedVariables

    - #### iPerturbVariables

          protected Collection<V extends Variable<V,T>> iPerturbVariables

  - ### Constructor Detail

    - #### Model

          public Model()

      Constructor

  - ### Method Detail

    - #### variables

          public List<V> variables()

      The list of variables in the model

    - #### countVariables

          public int countVariables()

      The number of variables in the model

    - #### addVariable

          public void addVariable(V variable)

      Adds a variable to the model

    - #### removeVariable

          public void removeVariable(V variable)

      Removes a variable from the model

    - #### constraints

          public List<Constraint<V,T>> constraints()

      The list of constraints in the model

    - #### countConstraints

          public int countConstraints()

      The number of constraints in the model

    - #### addConstraint

          public void addConstraint(Constraint<V,T> constraint)

      Adds a constraint to the model

    - #### removeConstraint

          public void removeConstraint(Constraint<V,T> constraint)

      Removes a constraint from the model

    - #### globalConstraints

          public List<GlobalConstraint<V,T>> globalConstraints()

      The list of global constraints in the model

    - #### countGlobalConstraints

          public int countGlobalConstraints()

      The number of global constraints in the model

    - #### addGlobalConstraint

          public void addGlobalConstraint(GlobalConstraint<V,T> constraint)

      Adds a global constraint to the model

    - #### removeGlobalConstraint

          public void removeGlobalConstraint(GlobalConstraint<V,T> constraint)

      Removes a global constraint from the model

    - #### unassignedVariables

          public Collection<V> unassignedVariables()

      The list of unassigned variables in the model

    - #### nrUnassignedVariables

          public int nrUnassignedVariables()

      Number of unassigned variables

    - #### assignedVariables

          public Collection<V> assignedVariables()

      The list of assigned variables in the model

    - #### nrAssignedVariables

          public int nrAssignedVariables()

      Number of assigned variables

    - #### perturbVariables

          public Collection<V> perturbVariables()

      The list of perturbation variables in the model, i.e., the variables which has an initial value but which are not assigned with this value.

    - #### perturbVariables

          public List<V> perturbVariables(Collection<V> variables)

      The list of perturbation variables in the model, i.e., the variables which has an initial value but which are not assigned with this value. Only variables from the given set are considered.

    - #### conflictValues

          public Set<T> conflictValues(T value)

      Returns the set of conflicting variables with this value, if it is assigned to its variable

    - #### inConflict

          public boolean inConflict(T value)

      Return true if the given value is in conflict with a hard constraint

    - #### variablesWithInitialValue

          public Collection<V> variablesWithInitialValue()

      The list of variables without initial value

    - #### invalidateVariablesWithInitialValueCache

          protected void invalidateVariablesWithInitialValueCache()

      Invalidates cache containing all variables that possess an initial value

    - #### beforeAssigned

          public void beforeAssigned(long iteration,
                                     T value)

      Called before a value is assigned to its variable

    - #### beforeUnassigned

          public void beforeUnassigned(long iteration,
                                       T value)

      Called before a value is unassigned from its variable

    - #### afterAssigned

          public void afterAssigned(long iteration,
                                    T value)

      Called after a value is assigned to its variable

    - #### afterUnassigned

          public void afterUnassigned(long iteration,
                                      T value)

      Called after a value is unassigned from its variable

    - #### toString

          public String toString()

      Overrides:
        `toString` in class `Object`

    - #### getPerc

          protected String getPerc(double value,
                                   double min,
                                   double max)

    - #### getPercRev

          protected String getPercRev(double value,
                                      double min,
                                      double max)

    - #### getInfo

          public Map<String,String> getInfo()

      Returns information about the current solution. Information from all model listeners and constraints is also included.

    - #### getExtendedInfo

          public Map<String,String> getExtendedInfo()

      Extended information about current solution. Similar to `getInfo()`, but some more information (that is more expensive to compute) might be added.

    - #### getInfo

          public Map<String,String> getInfo(Collection<V> variables)

      Returns information about the current solution. Information from all model listeners and constraints is also included. Only variables from the given set are considered.

    - #### getBestUnassignedVariables

          public int getBestUnassignedVariables()

      Returns the number of unassigned variables in the best ever found solution

    - #### getBestPerturbations

          public int getBestPerturbations()

      Returns the number of perturbation variables in the best ever found solution

    - #### saveBest

          public void saveBest()

      Save the current assignment as the best ever found assignment

    - #### clearBest

          public void clearBest()

      Clear the best ever found assignment

    - #### restoreBest

          protected void restoreBest(Comparator<V> assignmentOrder)

      Restore the best ever found assignment into the current assignment

    - #### restoreBest

          public void restoreBest()

    - #### bestUnassignedVariables

          public Collection<V> bestUnassignedVariables()

      The list of unassigned variables in the best ever found solution

    - #### getTotalValue

          public double getTotalValue()

      Value of the current solution. It is the sum of all assigned values, i.e., `Value.toDouble()`.

    - #### getTotalValue

          public double getTotalValue(Collection<V> variables)

      Value of the current solution. It is the sum of all assigned values, i.e., `Value.toDouble()`. Only variables from the given set are considered.

    - #### addModelListener

          public void addModelListener(ModelListener<V,T> listener)

      Adds a model listener

    - #### removeModelListener

          public void removeModelListener(ModelListener<V,T> listener)

      Removes a model listener

    - #### init

          public boolean init(Solver<V,T> solver)

      Model initialization

    - #### getModelListeners

          public List<ModelListener<V,T>> getModelListeners()

      The list of model listeners

    - #### modelListenerOfType

          public ModelListener<V,T> modelListenerOfType(Class<ModelListener<V,T>> type)

      The list of model listeners that are of the given class

    - #### conflictConstraints

          public Map<Constraint<V,T>,Set<T>> conflictConstraints(T value)

      The list of constraints which are in a conflict with the given value if it is assigned to its variable. This means the constraints, which adds a value into the set of conflicting values in `Constraint.computeConflicts(Value, Set)`.

    - #### unassignedHardConstraints

          public List<Constraint<V,T>> unassignedHardConstraints()

      The list of hard constraints which contain at least one variable that is not assigned.

    - #### getInfoProviders

          protected List<InfoProvider<V>> getInfoProviders()

      Registered info providers (see `InfoProvider`)

    - #### addCriterion

          public void addCriterion(Criterion<V,T> criterion)

      Register a new criterion

    - #### removeCriterion

          public void removeCriterion(Criterion<V,T> criterion)

      Unregister an existing criterion

    - #### removeCriterion

          public void removeCriterion(Class<? extends Criterion<V,T>> criterion)

      Unregister an existing criterion

    - #### getCriterion

          public Criterion<V,T> getCriterion(Class<? extends Criterion<V,T>> criterion)

      Return a registered criterion of the given type.

    - #### getCriteria

          public Collection<Criterion<V,T>> getCriteria()

      List all registered criteria

    - #### weaken

          public void weaken(T value)

      Weaken all weakening constraint so that the given value can be assigned without them creating a conflict using `WeakeningConstraint.weaken(Value)`. This method is handy for instance when an existing solution is being loaded into the solver.