Whenever you try to write a code fit to enable a library communication with the server, you must keep in mind what follows:
- The library must know the server’s IP address, the listening port, and the name of the objective function it has to apply. The MATLAB code, written according to the guidelines given in the previous section, must use the specific mechanisms offered by the library for its configuration.
- The developed code must be placed in the class responsible for the evaluation of the solutions. If it is a class of which only one instance is created during the execution, the connection with the server may be established in the constructor of the class. If on the contrary we are dealing with a class of which a large number of instances are created, you must control the creation of only one connection with the server. Before you perform an evaluation, you must check if the connection with the server has already been established. Otherwise, you must establish it. This way the connection is only established in the first evaluation.
- To evaluate the solutions you must follow the protocols explained below in Section 2. You will choose one or another according the representation of the solutions. If you are using a binary encoding, you must compress the solutions before sending them to the server. The methods to compress and decompress solutions are available both in its Java and C/C++ versions.
- There are several ways of closing the connection with the server: for a C++ library and if there is only one instance of the class responsible for the evaluations, you can use the destructor of the class to close the connection. If more than one instance is created, you can not use the destructor because you do not know which instance is going to be the last one. You could keep an account of the existing number of objects and close the connection when the last one is destroyed. Another solution is not to close the connection, and let the server detect the departure of the client and close the connection. If the library is implemented in Java, the best solution is to leave the connection closing up to the server, because there are not destructors.
- To establish and close the connections with the server to apply operators, if your library allows it, you can apply what has been said above. The difference between this class and the one in charge of the evaluation lies on the protocol followed in the communication with the server. Nowadays there are two protocols available as it has been shown in Section 2. Your library may need a new protocol. Even if you implement a new protocol, your library must first send a byte with value 2 to indicate that it wants to apply an operator. Then it must send the name of the MATLAB function implementing this operator, preceded by an integer with its length. The next step is to send a byte identifying the library (different from 1, jEA and 2, MALLBA). From that moment you implement your own protocol. In the next section we are going to explain what has to be changed in the server to implement this new protocol. If the library is implemented in C/C++ you must take into account the representation of data. To convert the data to the order of bytes in the network you may use the ApleeSeeds library. The Java libraries do not need to carry out this data conversion because Java uses the network byte order, regardless of the platform.
- When using the process-level evaluation we have followed two strategies, conditioned by the language the library is implemented in. In the Java libraries jEA and ssGA, the library itself accedes to MATLAB in order to evaluate solutions and apply operators, using native methods. You can check MATLABProblemLocal and MATLABOperatorLocal for jEA and MATLABProblemLocal for ssGA, to make them serve as a reference. In the C/C++ libraries, like MALLBA, we have used a Java class acting as a server (LocalServer from the server package). The only difference that the implementation of the library shows in this case is the use of named pipes instead of sockets.
Everything explained about the implementation carried out for the already integrated libraries may guide you. Nevertheless you may implement your classes as you wish, but always keeping in mind that the behaviour of the library as regards the configuration parameters must be the one shown in Section 5 of the user’s guide.