JGAP

org.jgap.distr.grid.gp
Class JGAPClientGP

java.lang.Object
  extended by java.lang.Thread
      extended by org.jgap.distr.grid.gp.JGAPClientGP
All Implemented Interfaces:
java.lang.Runnable

public class JGAPClientGP
extends java.lang.Thread

A client defines work for the grid and sends it to the JGAPServer. Use this class as base class for your grid client implementations.

Since:
3.2

Nested Class Summary
 
Nested classes/interfaces inherited from class java.lang.Thread
java.lang.Thread.State, java.lang.Thread.UncaughtExceptionHandler
 
Field Summary
static java.lang.String APP_VERSION
           
static java.lang.String CLIENT_DATABASE
           
static java.lang.String CONTEXT_ANY
           
static java.lang.String CONTEXT_ID_ANY
           
static java.lang.String CONTEXT_ID_EMPTY
           
static java.lang.String CONTEXT_WORK_REQUEST
           
static java.lang.String CONTEXT_WORK_RESULT
           
static java.lang.Object[][] FIELDSTOSKIP
           
protected  GridNodeClientConfig m_gridconfig
           
protected  JGAPRequestGP m_workReq
           
static java.lang.String MODULE_ANY
           
static java.lang.String MODULE_CS
           
static java.lang.String MODULE_SC
           
static java.lang.String MODULE_SW
           
static java.lang.String MODULE_WS
           
static java.lang.String RESULTS_DATABASE
           
static int TIMEOUT_SECONDS
           
static int WAITTIME_SECONDS
           
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
JGAPClientGP(GridNodeClientConfig a_gridconfig, java.lang.String a_clientClassName, boolean a_WANMode, boolean a_receiveOnly, boolean a_list, boolean a_no_comm, boolean a_no_evolution, boolean a_endless, int a_max_fetch_results)
           
JGAPClientGP(IGridClientMediator a_gcmed, GridNodeClientConfig a_gridconfig, java.lang.String a_clientClassName, boolean a_WANMode, boolean a_receiveOnly, boolean a_list, boolean a_no_comm, boolean a_no_evolution, boolean a_endless, int a_max_fetch_results)
           
 
Method Summary
protected  void afterEvolve(IGridClientMediator a_gcmed)
          Called in run() after one evolution step is executed.
protected  boolean afterSendWorkRequests(JGAPRequestGP[] a_workRequests)
          Called in run() after sending work requests successfully.
protected  void afterStopped(java.lang.Throwable a_t)
          Called after stopping the client in run().
protected  void beforeEvolve(IGridClientMediator a_gcmed)
          Called in run() before one evolution step is executed.
protected  boolean beforeGenerateWorkRequests()
          Called in run() before generating work requests for sending.
protected  boolean beforeSendWorkRequests(JGAPRequestGP[] a_workRequests)
          Called in run() before sending work requests.
protected  void checkForUpdates(java.lang.String a_URL, java.lang.String a_libDir, java.lang.String a_workDir)
           
protected  void errorOnSendWorkRequests(java.lang.Throwable uex, JGAPRequestGP[] a_workRequests)
           
protected  void evolve(IGridClientMediator a_gcmed, boolean a_receiveOnly)
          If necessary: override to implement your evolution cycle individually.
 GPConfiguration getConfiguration()
           
 java.lang.String[] getFilenames(java.lang.String a_dir)
           
 IGridClientMediator getGridClientMediator()
           
protected  IGridConfigurationGP getGridConfigurationGP()
           
protected  java.lang.String getKeyFromObject(java.lang.Object a_obj)
          Override in sub classes.
 int getMaxFetchResults()
           
protected  java.lang.String getResultFilename(JGAPResultGP a_result)
           
protected  java.lang.String getRunID()
           
 java.lang.String getWorkDirectory()
           
 boolean isNoCommunication()
           
protected  void listRequests()
          Override in sub classes: list available requests
protected  void listResults()
          Override in sub classes: list available results
static void main(java.lang.String[] args)
          Starts a client (first parameter: name of specific setup class).
protected static Options makeOptions()
           
protected  void onBeginOfRunning()
          Called at start of run().
protected  void onDeleteError(java.lang.Exception a_ex)
          Called in case of any unhandled error when trying to delete a request or result.
protected  void onError(java.lang.Exception a_ex)
          Called in run() in case of any unhandled error.
protected  void onErrorReceiveWorkResults(JGAPRequestGP[] a_workRequests, java.lang.Exception a_ex)
          Called in run() on error when receiving work results.
protected  void presetPopulation(JGAPRequestGP a_workRequest)
          Presets initial population to be included for input to workers.
protected  void receiveWorkResults(JGAPRequestGP[] workList)
           
protected  void removeEntries(java.util.Map a_cachedKeys, java.util.Map a_foundKeys)
           
protected  boolean resultReceived(GPPopulation a_pop)
          New results has been received.
protected  boolean resultReceived(IGPProgram a_fittest)
          A new result has been received.
 void run()
          Threaded: Splits work, sends it to workers and receives computed solutions.
protected  void saveResult(java.lang.String a_dir, java.lang.String a_filename, IGPProgram a_obj)
          Saves a result to disk.
protected  JGAPRequestGP[] sendWorkRequests(int a_evolutionIndex, IClientEvolveStrategyGP evolver, IRequestSplitStrategyGP splitter, IClientFeedbackGP feedback)
           
protected  void sendWorkRequests(JGAPRequestGP[] a_workList)
           
 void setWorkDirectory(java.lang.String a_workDir)
           
 void setWorkRequest(JGAPRequestGP a_request)
           
protected  void showCurrentResults()
           
 void start()
           
 void writeToFile(java.lang.Object a_obj, java.lang.String a_dir, java.lang.String a_filename)
          Writes an object to a local file.
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

APP_VERSION

public static final java.lang.String APP_VERSION
See Also:
Constant Field Values

MODULE_CS

public static final java.lang.String MODULE_CS
See Also:
Constant Field Values

CLIENT_DATABASE

public static final java.lang.String CLIENT_DATABASE
See Also:
Constant Field Values

RESULTS_DATABASE

public static final java.lang.String RESULTS_DATABASE
See Also:
Constant Field Values

MODULE_SC

public static final java.lang.String MODULE_SC
See Also:
Constant Field Values

MODULE_SW

public static final java.lang.String MODULE_SW
See Also:
Constant Field Values

MODULE_WS

public static final java.lang.String MODULE_WS
See Also:
Constant Field Values

MODULE_ANY

public static final java.lang.String MODULE_ANY
See Also:
Constant Field Values

CONTEXT_WORK_REQUEST

public static final java.lang.String CONTEXT_WORK_REQUEST
See Also:
Constant Field Values

CONTEXT_WORK_RESULT

public static final java.lang.String CONTEXT_WORK_RESULT
See Also:
Constant Field Values

CONTEXT_ANY

public static final java.lang.String CONTEXT_ANY
See Also:
Constant Field Values

CONTEXT_ID_EMPTY

public static final java.lang.String CONTEXT_ID_EMPTY
See Also:
Constant Field Values

CONTEXT_ID_ANY

public static final java.lang.String CONTEXT_ID_ANY
See Also:
Constant Field Values

TIMEOUT_SECONDS

public static final int TIMEOUT_SECONDS
See Also:
Constant Field Values

WAITTIME_SECONDS

public static final int WAITTIME_SECONDS
See Also:
Constant Field Values

FIELDSTOSKIP

public static final java.lang.Object[][] FIELDSTOSKIP

m_gridconfig

protected GridNodeClientConfig m_gridconfig

m_workReq

protected JGAPRequestGP m_workReq
Constructor Detail

JGAPClientGP

public JGAPClientGP(GridNodeClientConfig a_gridconfig,
                    java.lang.String a_clientClassName,
                    boolean a_WANMode,
                    boolean a_receiveOnly,
                    boolean a_list,
                    boolean a_no_comm,
                    boolean a_no_evolution,
                    boolean a_endless,
                    int a_max_fetch_results)
             throws java.lang.Exception
Throws:
java.lang.Exception

JGAPClientGP

public JGAPClientGP(IGridClientMediator a_gcmed,
                    GridNodeClientConfig a_gridconfig,
                    java.lang.String a_clientClassName,
                    boolean a_WANMode,
                    boolean a_receiveOnly,
                    boolean a_list,
                    boolean a_no_comm,
                    boolean a_no_evolution,
                    boolean a_endless,
                    int a_max_fetch_results)
             throws java.lang.Exception
Throws:
java.lang.Exception
Method Detail

getRunID

protected java.lang.String getRunID()
Returns:
the most possibly unique ID of a single program execution
Since:
3.3.3

setWorkRequest

public void setWorkRequest(JGAPRequestGP a_request)

onBeginOfRunning

protected void onBeginOfRunning()
                         throws java.lang.Exception
Called at start of run(). Override in sub classes if needed.

Throws:
java.lang.Exception
Since:
3.3.3

beforeSendWorkRequests

protected boolean beforeSendWorkRequests(JGAPRequestGP[] a_workRequests)
                                  throws java.lang.Exception
Called in run() before sending work requests. Override in sub classes if needed.

Parameters:
a_workRequests - work requests pending to be sent
Returns:
true: do send work requests, false: don't send any work request
Throws:
java.lang.Exception
Since:
3.3.3

beforeGenerateWorkRequests

protected boolean beforeGenerateWorkRequests()
                                      throws java.lang.Exception
Called in run() before generating work requests for sending. Override in sub classes if needed.

Returns:
true: do generate work requests, false: don't generate any work request
Throws:
java.lang.Exception
Since:
3.3.3

afterSendWorkRequests

protected boolean afterSendWorkRequests(JGAPRequestGP[] a_workRequests)
                                 throws java.lang.Exception
Called in run() after sending work requests successfully. Override in sub classes if needed.

Parameters:
a_workRequests - the sent requests
Returns:
true: process further, false: stop processing the rest
Throws:
java.lang.Exception
Since:
3.3.3

errorOnSendWorkRequests

protected void errorOnSendWorkRequests(java.lang.Throwable uex,
                                       JGAPRequestGP[] a_workRequests)
                                throws java.lang.Exception
Throws:
java.lang.Exception

beforeEvolve

protected void beforeEvolve(IGridClientMediator a_gcmed)
                     throws java.lang.Exception
Called in run() before one evolution step is executed. Override in sub classes if needed.

Parameters:
a_gcmed - the GridClient mediator
Throws:
java.lang.Exception
Since:
3.3.3

afterEvolve

protected void afterEvolve(IGridClientMediator a_gcmed)
                    throws java.lang.Exception
Called in run() after one evolution step is executed. Override in sub classes if needed.

Parameters:
a_gcmed - the GridClient mediator
Throws:
java.lang.Exception
Since:
3.3.3

afterStopped

protected void afterStopped(java.lang.Throwable a_t)
                     throws java.lang.Exception
Called after stopping the client in run(). Override in sub classes if needed.

Parameters:
a_t - null if no error occured on stopping, otherwise exception object
Throws:
java.lang.Exception
Since:
3.3.3

onError

protected void onError(java.lang.Exception a_ex)
                throws java.lang.Exception
Called in run() in case of any unhandled error. Override in sub classes if needed.

Parameters:
a_ex - exception object expressing the error
Throws:
java.lang.Exception
Since:
3.3.3

onDeleteError

protected void onDeleteError(java.lang.Exception a_ex)
                      throws java.lang.Exception
Called in case of any unhandled error when trying to delete a request or result. Override in sub classes if needed.

Parameters:
a_ex - exception object expressing the error
Throws:
java.lang.Exception
Since:
3.3.4

onErrorReceiveWorkResults

protected void onErrorReceiveWorkResults(JGAPRequestGP[] a_workRequests,
                                         java.lang.Exception a_ex)
                                  throws java.lang.Exception
Called in run() on error when receiving work results. Override in sub classes if needed.

Parameters:
a_workRequests - for which to receive results
a_ex - Exception occured
Throws:
java.lang.Exception - rethrow an unhandled exception!
Since:
3.3.4

run

public void run()
Threaded: Splits work, sends it to workers and receives computed solutions.

Specified by:
run in interface java.lang.Runnable
Overrides:
run in class java.lang.Thread
Since:
3.01

sendWorkRequests

protected JGAPRequestGP[] sendWorkRequests(int a_evolutionIndex,
                                           IClientEvolveStrategyGP evolver,
                                           IRequestSplitStrategyGP splitter,
                                           IClientFeedbackGP feedback)
                                    throws java.lang.Exception
Throws:
java.lang.Exception

sendWorkRequests

protected void sendWorkRequests(JGAPRequestGP[] a_workList)
                         throws java.lang.Exception
Throws:
java.lang.Exception

receiveWorkResults

protected void receiveWorkResults(JGAPRequestGP[] workList)
                           throws java.lang.Exception
Throws:
java.lang.Exception

getResultFilename

protected java.lang.String getResultFilename(JGAPResultGP a_result)

evolve

protected void evolve(IGridClientMediator a_gcmed,
                      boolean a_receiveOnly)
               throws java.lang.Exception
If necessary: override to implement your evolution cycle individually.

Parameters:
a_gcmed - the GridClient mediator
a_receiveOnly - false: Don't send any work requests, just receive results from former evolutions
Throws:
java.lang.Exception

start

public void start()
Overrides:
start in class java.lang.Thread

getConfiguration

public GPConfiguration getConfiguration()

getGridClientMediator

public IGridClientMediator getGridClientMediator()

getGridConfigurationGP

protected IGridConfigurationGP getGridConfigurationGP()

writeToFile

public void writeToFile(java.lang.Object a_obj,
                        java.lang.String a_dir,
                        java.lang.String a_filename)
                 throws java.lang.Exception
Writes an object to a local file.

Parameters:
a_obj - the object to persist
a_dir - directory to write the file to
a_filename - name of the file to create
Throws:
java.lang.Exception
Since:
3.3.3

setWorkDirectory

public void setWorkDirectory(java.lang.String a_workDir)
                      throws java.io.IOException
Throws:
java.io.IOException

getWorkDirectory

public java.lang.String getWorkDirectory()

checkForUpdates

protected void checkForUpdates(java.lang.String a_URL,
                               java.lang.String a_libDir,
                               java.lang.String a_workDir)
                        throws java.lang.Exception
Throws:
java.lang.Exception

listRequests

protected void listRequests()
Override in sub classes: list available requests


listResults

protected void listResults()
Override in sub classes: list available results

Since:
3.3.3

isNoCommunication

public boolean isNoCommunication()
Returns:
false: normal mode, true: do not communicate with the server
Since:
3.3.3

main

public static void main(java.lang.String[] args)
Starts a client (first parameter: name of specific setup class).

Parameters:
args - String[]
Since:
3.01

makeOptions

protected static Options makeOptions()

removeEntries

protected void removeEntries(java.util.Map a_cachedKeys,
                             java.util.Map a_foundKeys)

getKeyFromObject

protected java.lang.String getKeyFromObject(java.lang.Object a_obj)
                                     throws java.lang.Exception
Override in sub classes.

Parameters:
a_obj - the object to get the key from
Returns:
the key of the object
Throws:
java.lang.Exception

resultReceived

protected boolean resultReceived(GPPopulation a_pop)
                          throws java.lang.Exception
New results has been received. Care that the best of them are stored in case it is a top 3 result.

Parameters:
a_pop - the fittest results received for a work request
Returns:
true: new top result
Throws:
java.lang.Exception
Since:
3.3.3

resultReceived

protected boolean resultReceived(IGPProgram a_fittest)
                          throws java.lang.Exception
A new result has been received. Care that it is stored to top list on disk in case it is a top 3 result. Also store it in other cases and if the result is not too bad to be able to mix it in when generating new work requests.

Parameters:
a_fittest - the fittest result received for a work request
Returns:
true: new top result
Throws:
java.lang.Exception
Since:
3.3.3

saveResult

protected void saveResult(java.lang.String a_dir,
                          java.lang.String a_filename,
                          IGPProgram a_obj)
                   throws java.lang.Exception
Saves a result to disk.

Parameters:
a_dir - the directory to put the result into
a_filename - name of the file to write
a_obj - the result object to write
Throws:
java.lang.Exception
Since:
3.3.4

getFilenames

public java.lang.String[] getFilenames(java.lang.String a_dir)
                                throws java.lang.Exception
Throws:
java.lang.Exception

presetPopulation

protected void presetPopulation(JGAPRequestGP a_workRequest)
                         throws java.lang.Exception
Presets initial population to be included for input to workers.

Parameters:
a_workRequest - the work request that is about to be sent.
Throws:
java.lang.Exception
Since:
3.3.3

showCurrentResults

protected void showCurrentResults()
                           throws java.lang.Exception
Throws:
java.lang.Exception

getMaxFetchResults

public int getMaxFetchResults()

JGAP