net.sf.cpsolver.ifs.model
Class Variable<V extends Variable<V,T>,T extends Value<V,T>>

java.lang.Object
  extended by net.sf.cpsolver.ifs.model.Variable<V,T>
All Implemented Interfaces:
Comparable<V>
Direct Known Subclasses:
Activity, CSPVariable, Exam, Lecture, Operation, Rectangle, Request

public class Variable<V extends Variable<V,T>,T extends Value<V,T>>
extends Object
implements Comparable<V>

Generic variable.

Besides a domain of values, a variable also contains information about assigned value, the value assigned in the best ever found solution and also the initial value (minimal perturbations problem). It also knows what constraints are associated with this variable and has a unique id.

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:
Value, Model, Solver

Field Summary
protected  long iId
           
protected  T iValue
          Assigned value
 
Constructor Summary
Variable()
          Constructor
Variable(T initialValue)
          Constructor
 
Method Summary
 void addContstraint(Constraint<V,T> constraint)
          Adds a constraint.
 void addVariableListener(VariableListener<T> listener)
          Adds variable listener
 void assign(long iteration, T value)
          Assign value to this variable.
 int compareTo(V variable)
           
 List<Constraint<V,T>> constraints()
          Return the list of constraints associated with this variable
 Map<V,List<Constraint<V,T>>> constraintVariables()
          Returns a table of all variables linked with this variable by a constraint.
 long countAssignments()
          Return how many times was this variable assigned in the past.
 boolean equals(Object o)
           
 T getAssignment()
          Returns current assignment
 T getBestAssignment()
          Returns the value from the best ever found soultion.
 long getBestAssignmentIteration()
          Returns the iteration when the best value was assigned
 String getDescription()
          Variable's description -- for printing purposes
 Object getExtra()
          Extra information to which can be used by an extension (see Extension).
 long getId()
          Unique id
 T getInitialAssignment()
          Returns initial assignment
 Model<V,T> getModel()
          Model, the variable belong to
 String getName()
          Variable's name -- for printing purposes
 List<VariableListener<T>> getVariableListeners()
          Return variable listeners
 List<Constraint<V,T>> hardConstraints()
          Return the list of hard constraints associated with this variable
 boolean hasAssignment()
          Returns true if the variable is assigned
 int hashCode()
           
 boolean hasInitialAssignment()
          Returns true if the variable has an initial assignment
 boolean hasValues()
          True, if the variable's domain is not empty
 long lastAssignmentIteration()
          Returns the iteration when the variable was assigned for the last time (-1 if never)
 long lastUnassignmentIteration()
          Returns the iteration when the variable was unassigned for the last time (-1 if never)
 void removeContstraint(Constraint<V,T> constraint)
          Removes a constraint.
 void removeInitialValue()
          Permanently remove the initial value from the variable's domain -- for testing MPP
 void removeValue(long iteration, T value)
          Permanently remove a value from variables domain.
 void removeVariableListener(VariableListener<T> listener)
          Removes variable listener
 void setBestAssignment(T value)
          Sets variable's value of the best ever found solution.
 void setExtra(Object object)
          Extra information to which can be used by an extension (see Extension).
 void setInitialAssignment(T initialValue)
          Sets initial assignment
 void setModel(Model<V,T> model)
          Set the model to which the variable belongs to
protected  void setValues(List<T> values)
          Sets the domain
 List<Constraint<V,T>> softConstraints()
          Return the list of soft constraints associated with this variable
 String toString()
           
 void unassign(long iteration)
          Unassign value from this variable.
 List<T> values()
          Domain
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

iId

protected long iId

iValue

protected T extends Value<V,T> iValue
Assigned value

Constructor Detail

Variable

public Variable()
Constructor


Variable

public Variable(T initialValue)
Constructor

Parameters:
initialValue - initial value (minimal-perturbation problem)
Method Detail

getModel

public Model<V,T> getModel()
Model, the variable belong to


setModel

public void setModel(Model<V,T> model)
Set the model to which the variable belongs to


values

public List<T> values()
Domain


setValues

protected void setValues(List<T> values)
Sets the domain


hasValues

public boolean hasValues()
True, if the variable's domain is not empty


getAssignment

public T getAssignment()
Returns current assignment


hasAssignment

public boolean hasAssignment()
Returns true if the variable is assigned


getInitialAssignment

public T getInitialAssignment()
Returns initial assignment


setInitialAssignment

public void setInitialAssignment(T initialValue)
Sets initial assignment


hasInitialAssignment

public boolean hasInitialAssignment()
Returns true if the variable has an initial assignment


assign

public void assign(long iteration,
                   T value)
Assign value to this variable. If the variable has already assigned another value, it is unassigned first. Also, all conflicting values are unassigned before the given value is assigned to this variable.

Parameters:
iteration - current iteration
value - the value to be assigned

unassign

public void unassign(long iteration)
Unassign value from this variable.

Parameters:
iteration - current iteration

countAssignments

public long countAssignments()
Return how many times was this variable assigned in the past.


addContstraint

public void addContstraint(Constraint<V,T> constraint)
Adds a constraint. Called automatically when the constraint is added to the model, i.e., Model.addConstraint(Constraint) is called.

Parameters:
constraint - added constraint

removeContstraint

public void removeContstraint(Constraint<V,T> constraint)
Removes a constraint. Called automatically when the constraint is removed from the model, i.e., Model.removeConstraint(Constraint) is called.

Parameters:
constraint - added constraint

constraints

public List<Constraint<V,T>> constraints()
Return the list of constraints associated with this variable


hardConstraints

public List<Constraint<V,T>> hardConstraints()
Return the list of hard constraints associated with this variable


softConstraints

public List<Constraint<V,T>> softConstraints()
Return the list of soft constraints associated with this variable


toString

public String toString()
Overrides:
toString in class Object

getId

public long getId()
Unique id


hashCode

public int hashCode()
Overrides:
hashCode in class Object

getName

public String getName()
Variable's name -- for printing purposes


getDescription

public String getDescription()
Variable's description -- for printing purposes


setBestAssignment

public void setBestAssignment(T value)
Sets variable's value of the best ever found solution. Called when Model.saveBest() is called.


getBestAssignment

public T getBestAssignment()
Returns the value from the best ever found soultion.


getBestAssignmentIteration

public long getBestAssignmentIteration()
Returns the iteration when the best value was assigned


lastAssignmentIteration

public long lastAssignmentIteration()
Returns the iteration when the variable was assigned for the last time (-1 if never)


lastUnassignmentIteration

public long lastUnassignmentIteration()
Returns the iteration when the variable was unassigned for the last time (-1 if never)


compareTo

public int compareTo(V variable)
Specified by:
compareTo in interface Comparable<V extends Variable<V,T>>

equals

public boolean equals(Object o)
Overrides:
equals in class Object

addVariableListener

public void addVariableListener(VariableListener<T> listener)
Adds variable listener


removeVariableListener

public void removeVariableListener(VariableListener<T> listener)
Removes variable listener


getVariableListeners

public List<VariableListener<T>> getVariableListeners()
Return variable listeners


setExtra

public void setExtra(Object object)
Extra information to which can be used by an extension (see Extension).


getExtra

public Object getExtra()
Extra information to which can be used by an extension (see Extension).


removeValue

public void removeValue(long iteration,
                        T value)
Permanently remove a value from variables domain.


constraintVariables

public Map<V,List<Constraint<V,T>>> constraintVariables()
Returns a table of all variables linked with this variable by a constraint.

Returns:
table (variable, constraint)

removeInitialValue

public void removeInitialValue()
Permanently remove the initial value from the variable's domain -- for testing MPP