5.5 Methodologies used in Optimisation Service
5.6.1 Techniques for creating Stateless, Reverse Communication Opti-
The code for the Amoeba optimisation algorithm was written in the C language of which the function prototype is shown in listing 5.1 and the full listing of the source code is found in Appendix B.
A function pointer to the objective function must be supplied to the amoeba function which is then statically linked after compilation into a executable (main entry point not shown). In order to do this, source code or a library containing an implementation of the objective function must be available.
If we look at the source code for the Amoeba C source code we see that it synchronously invokes the objective function (funk) locally (within the helper function amotry) by passing in input parameters (psum) and waits for its results. Consequently, execution of the objection function and optimisation algorithm are tightly-coupled to each other. However we wish to able to perform the execution of the objection function on different computer to the execution of the optimisation algorithm. A number of solutions where explored in order to do this.
The simplest solution which requires no recoding of the optimisation function is too remove the objection function implementation within funk and replace it with a remote procedure call (RPC) to another machine that has an executable containing the actual implementation of the objective function, shown in listing5.2. The usage of RPC is just an example, it could be replaced in the same manner with a call to a method of a Web Service.
The main issue with this solution is that the optimisation algorithm must remain in memory on the computer it is running whilst it synchronously waits for the objective
1 f l o a t f u n k (f l o a t [ ] p ) 2 {
3 /∗ e x a m p l e o f how we m i g h t c a l l t h e o b j e c t f u n c t i o n on a m a c h i n e c a l l e d w h i s k e y ∗/
4 return r e m o t e f u n k ( ” r p c:\\w h i s k e y.s o t o n.a c.uk\myfunk ” , p ) 5 }
Listing 5.2: Example pseudocode for objective function replacing implementation
with remote procedure call to enable execution of code on another machine.
function to complete. If either the remote machine or the local machine running the optimisation crashes all previous work is lost. In addition, synchronous waiting ties up valuable resources of the local machine whilst it idles. What is required is that the optimisation algorithm save its state somehow during these idle times then terminate itself so it use no resources and resume from where it left off once an objective function execution completes. As discussed in section 5.5.1, reverse communication gives us an approach that enables us to achieve this. Implementation of the optimisation algorithm to support reverse communication approach involved coding the algorithm as a state machine.
Java-based source code found in Appendix C contains our implementation of the down- hill simplex algorithm. Its implementation is encapsulated within a stateless Session EJB which exposes its interface locally to the Servlet layer of the J2EE hosting environ- ment. There are two methods of interest: bootstrap method, which must be called first, initialises the optimisation; and the next method, which must be called successively by the client through the interface exposed by Servlet, which performs the optimisation itself.
Each client call to the next method represents the completion of the execution of the objective function by the client. The input parameters to the next method are its state, shown in listing 5.3 containing the context of the optimisation prior execution and the results of the optimisation function. The next function returns when it either reaches a point in the algorithm where it requires that the client compute the result of a new matrix of input parameters on the objective function, it has found the optimum result or it exceeds the maximum number of objective function iterations. The next method returns to the client via the servlet interface (e.g. HTTP response) a representation (e.g. XML) of the optimisation state data which the client can check the value of the entry point into the state machine to see which condition has happened. The client extracts from the state data the input parameters for the objective function (x[]), executes the objective function, updates the result (res) of the state data, and call next method again (e.g. with a http request) at the same time passing the updated state data to the service.
1 package o p t i m i s a t i o n; 2 3 import j a v a.i o.S e r i a l i z a b l e; 4 5 p u b l i c c l a s s AmoebaState 6 implements S e r i a l i z a b l e 7 { 8 /∗i n i t i a l i n p u t p a r a m e t e r s s u p p l i e d b y c l i e n t and b o o t s t r a p∗/ 9 p u b l i c i n t ndim; 10 p u b l i c f l o a t p[ ] [ ], y[ ], f t o l; 11 12 /∗c o n t e x t u a l s t a t e o f o p t i m i s a t i o n∗/
13 p u b l i c f l o a t r t o l , summ , swap , y s a v e , y t r y , psum[ ] ;
14 p u b l i c i n t i , i h i , i l o , i n h i , j , mpts; 15 16 /∗e n t r y p o i n t i n t o t h e s t a t e m a c h i n e∗/ 17 p u b l i c i n t s t a t e; 18 19 /∗r e s u l t o f t h e o b j e c t i v e f u n c t i o n∗/ 20 p u b l i c f l o a t r e s; 21 22 /∗i n p u t p a r a m e t e r s t o t h e o b j e c t i v e f u n c t i o n∗/ 23 p u b l i c f l o a t x[ ] ; 24 25 /∗number o f o b j e c t i v e f u n c t i o n i t e r a t i o n s∗/ 26 p u b l i c i n t n f u n k; 27 28 p u b l i c Amotry amotry; /∗ a d d i t i o n a l s t a t e o f a m o t r y h e l p e r f u n c t i o n ∗/ 29 30 p u b l i c AmoebaState ( ) { 31 super( ); 32 /∗p u b l i c p a r a m e t e r l e s s c o n s t r u c t o r r e q u i r e d b y J a v a s e r i a l i s e r∗/ 33 } 34 35 p u b l i c O b j e c t c l o n e ( ) throws C l o n e N o t S u p p o r t e d E x c e p t i o n 36 { 37 /∗h e l p e r f u n c t i o n t o make a c o p y o f t h e s t a t e∗/ 38 /∗i m p l e m e n t a t i o n n o t s h o w n f o r c o n c i s e n e s s∗/ 39 } 40 }
Listing 5.3: Implementation of the state object for the Session EJB downhill simplex algorithm.