C# Class Cream.Solver

An abstract class for constraint solvers. A solver is constructed with a {@linkplain Network constraint network} which is used by the solver to find solutions. Please note that any network can not be simultaneously shared by two different solvers. Solvers can be used in three typical ways. As a subroutine: {@link #FindFirst()}, {@link #FindBest()}, etc.
Solution solution = solver.FindFirst();
As a handler caller: {@link #FindAll(ISolutionHandler handler)}, etc.
solver.FindAll(new ISolutionHandler() { public synchronized void Solved(Solver solver, Solution solution) { ..... } });
As a coroutine: {@link #Start()}, {@link #WaitNext()}, {@link #Resume()}, {@link #Stop()}, etc.
for (solver.Start(); solver.WaitNext(); solver.Resume()) { Solution solution = solver.getSolution(); ..... } solver.Stop();
Inheritance: IThreadRunnable
Mostra file Open project: kikoanis/CSharpCream Class Usage Examples

Protected Properties

Property Type Description
bestSolution Solution
bestValue int
count long
debug bool
name String
network Cream.Network
option int
solution Solution
startTime long
totalTimeout long

Public Methods

Method Description
ClearBest ( ) : void

Clears the best solution this solver has been found.

FindAll ( ISolutionHandler handler ) : void

Invokes the handler for each solution. This method is equivalent to {@link #FindAll(ISolutionHandler, long) FindFirst(handler, 0)}.

FindAll ( ISolutionHandler handler, long timeout ) : void

Invokes the handler for each solution with the timeout. This method is implemented as follows:

 ClearBest(); Start(handler, timeout); Join(); 

FindBest ( ) : Solution

Finds the best solution. This method is equivalent to {@link #FindBest(long) FindBest(0)}.

FindBest ( long timeout ) : Solution

Finds the best solution with the timeout. This method is implemented as follows:

 ClearBest(); for (Start(timeout); WaitNext(); Resume()) { ; } Stop(); return getBestSolution(); 

FindFirst ( ) : Solution

Finds the first solution. This method is equivalent to {@link #FindFirst(long) FindFirst(0)}.

FindFirst ( long timeout ) : Solution

Finds the first solution with the timeout. This method is implemented as follows:

 ClearBest(); Start(timeout); WaitNext(); Stop(); return getBestSolution(); 

GetCount ( ) : long
GetElapsedTime ( ) : long
GetMonitor ( ) : Monitor

Gets the monitor.

GetOption ( ) : int

Returns the option value.

Join ( ) : void

Waits until the solver ends the execution.

ResetIDCounter ( ) : void

Resets the ID counter to be 0.

Resume ( ) : void

Resumes the execution of the solver.

Run ( ) : void

The body of the solver. This method is called from {@link Solver#Start} methods.

SetMonitor ( Monitor mon ) : void

Sets the mon.

Start ( ) : void

Starts the solver in a new thread, and immediately returns to the caller. The {@link #WaitNext()} and {@link #WaitNext(long timeout)} methods can be used to wait the next solution. When a solution is found, the solver suspends the execution until the {@link #Resume()} method is called. You can Stop the solver anytime by calling the {@link #Stop()} method.

Start ( ISolutionHandler handler ) : void

Starts the solver in a new thread, and immediately returns to the caller. The handler is called for each solution and at the end of the solver execution. You can Stop the solver anytime by calling the {@link #Stop()} method.

Start ( ISolutionHandler handler, long timeout ) : void

Starts the solver in a new thread with the timeout, and immediately returns to the caller. When the timeout milliseconds have been elapsed since the Start of the solver, it stops the execution. The handler is called for each solution and at the end of the solver execution. You can Stop the solver anytime by calling the {@link #Stop()} method.

Start ( long timeout ) : void

Starts the solver in a new thread with the timeout, and immediately returns to the caller. When the timeout milliseconds have been elapsed since the Start of the solver, it stops the execution. The {@link #WaitNext()} and {@link #WaitNext(long timeout)} methods can be used to wait the next solution, or to detect the timeout. When a solution is found, the solver suspends the execution until the {@link #Resume()} method is called. You can Stop the solver anytime by calling the {@link #Stop()} method.

Stop ( ) : void

Stops the execution of the solver.

ToString ( ) : String

Returns the name of this solver.

WaitNext ( ) : bool

Waits for the next solution, or the end of the solver execution. It returns true if the next solution is available, false if the solver ends the execution.

WaitNext ( long timeout ) : bool

Waits for the next solution, or the end of the solver execution with the timeout. It returns true if the next solution is available within the timeout milliseconds, false if the solver ends the execution or the timeout milliseconds have been elapsed since the Start of this method.

Protected Methods

Method Description
Fail ( ) : void
IsBetter ( int value1, int value2 ) : bool
IsOption ( int opt ) : bool
Solver ( Cream.Network network ) : System

Constructs a solver for the given network (for invocation by subclass constructors). This constructor is equivalent to Solver(network, Default, null).

Solver ( Cream.Network network, String name ) : System

Constructs a solver for the given network and name (for invocation by subclass constructors). This constructor is equivalent to Solver(network, Default, name).

Solver ( Cream.Network network, int option ) : System

Constructs a solver for the given network and option (for invocation by subclass constructors). This constructor is equivalent to Solver(network, option, null).

Solver ( Cream.Network network, int option, String name ) : System

Constructs a solver for the given network, option, and name (for invocation by subclass constructors). When option is Default, None is used if the network has no objective variable, or else Minimize is used. Solvers and subclasses have their ID number starting from 0.

Success ( ) : void
UpdateBest ( ) : bool

Method Details

ClearBest() public method

Clears the best solution this solver has been found.
public ClearBest ( ) : void
return void

Fail() protected method

protected Fail ( ) : void
return void

FindAll() public method

Invokes the handler for each solution. This method is equivalent to {@link #FindAll(ISolutionHandler, long) FindFirst(handler, 0)}.
public FindAll ( ISolutionHandler handler ) : void
handler ISolutionHandler solution handler ///
return void

FindAll() public method

Invokes the handler for each solution with the timeout. This method is implemented as follows:
 ClearBest(); Start(handler, timeout); Join(); 
public FindAll ( ISolutionHandler handler, long timeout ) : void
handler ISolutionHandler solution handler ///
timeout long timeout in milliseconds (non-positive value means no timeout) ///
return void

FindBest() public method

Finds the best solution. This method is equivalent to {@link #FindBest(long) FindBest(0)}.
public FindBest ( ) : Solution
return Solution

FindBest() public method

Finds the best solution with the timeout. This method is implemented as follows:
 ClearBest(); for (Start(timeout); WaitNext(); Resume()) { ; } Stop(); return getBestSolution(); 
public FindBest ( long timeout ) : Solution
timeout long timeout in milliseconds (non-positive value means no timeout) ///
return Solution

FindFirst() public method

Finds the first solution. This method is equivalent to {@link #FindFirst(long) FindFirst(0)}.
public FindFirst ( ) : Solution
return Solution

FindFirst() public method

Finds the first solution with the timeout. This method is implemented as follows:
 ClearBest(); Start(timeout); WaitNext(); Stop(); return getBestSolution(); 
public FindFirst ( long timeout ) : Solution
timeout long timeout in milliseconds (non-positive value means no timeout) ///
return Solution

GetCount() public method

public GetCount ( ) : long
return long

GetElapsedTime() public method

public GetElapsedTime ( ) : long
return long

GetMonitor() public method

Gets the monitor.
public GetMonitor ( ) : Monitor
return Monitor

GetOption() public method

Returns the option value.
public GetOption ( ) : int
return int

IsBetter() protected method

protected IsBetter ( int value1, int value2 ) : bool
value1 int
value2 int
return bool

IsOption() protected method

protected IsOption ( int opt ) : bool
opt int
return bool

Join() public method

Waits until the solver ends the execution.
public Join ( ) : void
return void

ResetIDCounter() public static method

Resets the ID counter to be 0.
public static ResetIDCounter ( ) : void
return void

Resume() public method

Resumes the execution of the solver.
public Resume ( ) : void
return void

Run() public abstract method

The body of the solver. This method is called from {@link Solver#Start} methods.
public abstract Run ( ) : void
return void

SetMonitor() public method

Sets the mon.
public SetMonitor ( Monitor mon ) : void
mon Monitor monitor ///
return void

Solver() protected method

Constructs a solver for the given network (for invocation by subclass constructors). This constructor is equivalent to Solver(network, Default, null).
protected Solver ( Cream.Network network ) : System
network Cream.Network the constraint network ///
return System

Solver() protected method

Constructs a solver for the given network and name (for invocation by subclass constructors). This constructor is equivalent to Solver(network, Default, name).
protected Solver ( Cream.Network network, String name ) : System
network Cream.Network the constraint network ///
name String the name of the solver ///
return System

Solver() protected method

Constructs a solver for the given network and option (for invocation by subclass constructors). This constructor is equivalent to Solver(network, option, null).
protected Solver ( Cream.Network network, int option ) : System
network Cream.Network the constraint network ///
option int the option for search strategy ///
return System

Solver() protected method

Constructs a solver for the given network, option, and name (for invocation by subclass constructors). When option is Default, None is used if the network has no objective variable, or else Minimize is used. Solvers and subclasses have their ID number starting from 0.
protected Solver ( Cream.Network network, int option, String name ) : System
network Cream.Network the constraint network ///
option int the option for search strategy, or Default for default search strategy ///
name String the name of the solver, or null for a default name ///
return System

Start() public method

Starts the solver in a new thread, and immediately returns to the caller. The {@link #WaitNext()} and {@link #WaitNext(long timeout)} methods can be used to wait the next solution. When a solution is found, the solver suspends the execution until the {@link #Resume()} method is called. You can Stop the solver anytime by calling the {@link #Stop()} method.
public Start ( ) : void
return void

Start() public method

Starts the solver in a new thread, and immediately returns to the caller. The handler is called for each solution and at the end of the solver execution. You can Stop the solver anytime by calling the {@link #Stop()} method.
public Start ( ISolutionHandler handler ) : void
handler ISolutionHandler solution handler ///
return void

Start() public method

Starts the solver in a new thread with the timeout, and immediately returns to the caller. When the timeout milliseconds have been elapsed since the Start of the solver, it stops the execution. The handler is called for each solution and at the end of the solver execution. You can Stop the solver anytime by calling the {@link #Stop()} method.
public Start ( ISolutionHandler handler, long timeout ) : void
handler ISolutionHandler solution handler ///
timeout long timeout in milliseconds (non-positive value means no timeout) ///
return void

Start() public method

Starts the solver in a new thread with the timeout, and immediately returns to the caller. When the timeout milliseconds have been elapsed since the Start of the solver, it stops the execution. The {@link #WaitNext()} and {@link #WaitNext(long timeout)} methods can be used to wait the next solution, or to detect the timeout. When a solution is found, the solver suspends the execution until the {@link #Resume()} method is called. You can Stop the solver anytime by calling the {@link #Stop()} method.
public Start ( long timeout ) : void
timeout long timeout in milliseconds (non-positive value means no timeout) ///
return void

Stop() public method

Stops the execution of the solver.
public Stop ( ) : void
return void

Success() protected method

protected Success ( ) : void
return void

ToString() public method

Returns the name of this solver.
public ToString ( ) : String
return String

UpdateBest() protected method

protected UpdateBest ( ) : bool
return bool

WaitNext() public method

Waits for the next solution, or the end of the solver execution. It returns true if the next solution is available, false if the solver ends the execution.
public WaitNext ( ) : bool
return bool

WaitNext() public method

Waits for the next solution, or the end of the solver execution with the timeout. It returns true if the next solution is available within the timeout milliseconds, false if the solver ends the execution or the timeout milliseconds have been elapsed since the Start of this method.
public WaitNext ( long timeout ) : bool
timeout long timeout in milliseconds (non-positive value means no timeout) ///
return bool

Property Details

bestSolution protected_oe property

protected Solution,Cream bestSolution
return Solution

bestValue protected_oe property

protected int bestValue
return int

count protected_oe property

protected long count
return long

debug protected_oe property

protected bool debug
return bool

name protected_oe property

protected String name
return String

network protected_oe property

protected Network,Cream network
return Cream.Network

option protected_oe property

protected int option
return int

solution protected_oe property

protected Solution,Cream solution
return Solution

startTime protected_oe property

protected long startTime
return long

totalTimeout protected_oe property

protected long totalTimeout
return long