CSPGen
Class Variable

java.lang.Object
  extended byCSPGen.Variable

public class Variable
extends java.lang.Object

An instance of this class represents one variable in a CSP. Each instance stores the curren tvalue of the variable, the constraints the variable is involved in, the livedomain, the order (a unique ID number for the variable), and the domain size of the variable. The constructor of this class should be called within the constructor of the Problem class with the value of domain size and order of variables so that user of this generator need not access it.

Since this is a generator of Flawless Random CSPs, and in Random CSPs, all the variables are supposed to have same domain, there is no "domain" field in this class. The domain consists of all the integers on [0,domainsize). For similar reasons each instance is identified by a unique ID number stored in the id field instead of a name.

See Also:
Problem, BinaryConstraint

Field Summary
protected  java.util.ArrayList constraints
          The pool of all binary constraints this variable is involved in.
protected  int currentValue
          The current value of this variable.
 int domainSize
          The domain size of this variable.
 int id
          The unique identifier of this variable.
protected  boolean[] liveDomain
          The data structure to record if values are still in the livedomain.
 Problem problem
           
 
Constructor Summary
protected Variable(Problem p, int d, int id)
          The constructor of a Variable object, it will be called in the constructor of Problem Class
 
Method Summary
protected  void addConstraint(BinaryConstraint bc)
          Called by the BinaryConstraint class constructor to notify the Variable that it is constrained by the given constraint.
 boolean constrainedBy(int v)
          Returns true iff this variable shares a constraint with the given variable.
 boolean constrainedBy(Variable v)
          Returns true iff this variable shares a constraint with the given variable.
 java.util.Enumeration constraints()
          Returns an enumeration on all the constraints involving this Variable.
 BinaryConstraint constraintWith(int v)
          Checks if this variable shares are constraint with the given variable.
 BinaryConstraint constraintWith(Variable v)
          Checks if this variable shares are constraint with the given variable.
 void eliminateValue(int x)
          Removes the given value from into the live domain of this variable.
static boolean equal(Variable v1, Variable v2)
          This method determines if two variables are in fact the same one.
 boolean equals(java.lang.Object o)
          Determines if this instance is equal to the given instance.
 int getValue()
          Returns the current assignment of this variable.
 boolean isConsistant()
          Returns true iff none of the constraints on this variable are violated.
 boolean liveDomain(int x)
          Returns true iff the given value is in this variable's live domain.
 int numConstraints()
          Returns the total number of constraints on this Variable.
 void releaseAllValues()
          Adds all possible values back into the live domain of this variable.
 void releaseValue(int x)
          Adds the given value back into the live domain of this variable.
 void setValue(int v)
          Sets the current assignment of this variable.
 int value()
          Returns the current assignment of this variable.
 void value(int v)
          Sets the current assignment of this variable.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

id

public final int id
The unique identifier of this variable.

All identifiers should be in [0, n) where n is the number of variables.


domainSize

public final int domainSize
The domain size of this variable.


problem

public final Problem problem

currentValue

protected int currentValue
The current value of this variable.
The initial value will be -1, which means that this variable has not been assigned a value.


liveDomain

protected boolean[] liveDomain
The data structure to record if values are still in the livedomain. The size of the array will be equal to the domain size of this variable.

All values in the array will be initialized to true.


constraints

protected java.util.ArrayList constraints
The pool of all binary constraints this variable is involved in.

Constructor Detail

Variable

protected Variable(Problem p,
                   int d,
                   int id)
The constructor of a Variable object, it will be called in the constructor of Problem Class

Parameters:
p - the creating Problem
id - the identifier (order) of this variable
d - the domain size of this variable
Method Detail

constrainedBy

public boolean constrainedBy(Variable v)
Returns true iff this variable shares a constraint with the given variable.

Parameters:
v - the Variable to be checked

constrainedBy

public boolean constrainedBy(int v)
Returns true iff this variable shares a constraint with the given variable.

Parameters:
v - the id of the Variable to be checked

constraintWith

public BinaryConstraint constraintWith(Variable v)
Checks if this variable shares are constraint with the given variable.

Parameters:
v - the Variable to be checked
Returns:
If this variable shares a constraint with the given variable then their BinaryConstraint will be returned.
Otherwise null is returned.

constraintWith

public BinaryConstraint constraintWith(int v)
Checks if this variable shares are constraint with the given variable.

Parameters:
v - the id of the Variable to be checked
Returns:
If this variable shares a constraint with the given variable then their BinaryConstraint will be returned.
Otherwise null is returned.

isConsistant

public boolean isConsistant()
Returns true iff none of the constraints on this variable are violated.


addConstraint

protected void addConstraint(BinaryConstraint bc)
Called by the BinaryConstraint class constructor to notify the Variable that it is constrained by the given constraint.

Parameters:
bc - the BinaryConstraint making the call

numConstraints

public int numConstraints()
Returns the total number of constraints on this Variable.


constraints

public java.util.Enumeration constraints()
Returns an enumeration on all the constraints involving this Variable.


equal

public static boolean equal(Variable v1,
                            Variable v2)
This method determines if two variables are in fact the same one.

Parameters:
v1 - the first variable to be compared
v2 - the second variable to be compared
Returns:
true if these two variables have the same id false otherwise.

value

public int value()
Returns the current assignment of this variable.


getValue

public int getValue()
Returns the current assignment of this variable.


value

public void value(int v)
Sets the current assignment of this variable. Any value less than 0 means unassigned.

If the given value is >= the domain size then the current value will be set to -1.


setValue

public void setValue(int v)
Sets the current assignment of this variable. Any value less than 0 means unassigned.

If the given value is >= the domain size then the current value will be set to -1.

See Also:
value(int v)

liveDomain

public boolean liveDomain(int x)
Returns true iff the given value is in this variable's live domain.

Throws:
java.lang.ArrayIndexOutOfBoundsException - if the given value is not part of this variable's domain

eliminateValue

public void eliminateValue(int x)
Removes the given value from into the live domain of this variable.

Throws:
java.lang.ArrayIndexOutOfBoundsException - if the given value is not part of this variable's domain

releaseValue

public void releaseValue(int x)
Adds the given value back into the live domain of this variable.

Throws:
java.lang.ArrayIndexOutOfBoundsException - if the given value is not part of this variable's domain

releaseAllValues

public void releaseAllValues()
Adds all possible values back into the live domain of this variable.


equals

public boolean equals(java.lang.Object o)
Determines if this instance is equal to the given instance.

Requirements are that the two variables have the same problem (determined using ==) and that they have the same id. Under normal circumstances (v1==v2) should yield the same results as (v1.equals(v2)).

Parameters:
o - the object to which the comparison is made