Free cookie consent management tool by TermsFeed Policy Generator

# Changes between Version 23 and Version 24 of Documentation/Howto/OptimizeExternalApplications

Ignore:
Timestamp:
07/18/12 03:57:31 (10 years ago)
Comment:

updated documentation

### Legend:

Unmodified
 v23 === Technology & Background === Among the many possibilities and technologies that have emerged to provide a base for performing "distributed computing" in a wide sense, one of the first technologies was Remote Procedure Call (RPC). The idea is very simple: Instead of calling a local procedure to do some kind of calculation, a procedure is called that is not defined within the same executable or one of its dependencies. The client thus is in some ways hands the parameters of the method to another program or server, waits for the computation and then reads back the return value. In such a broad sense this is how most web applications nowadays work and indeed it is not until the invention of Representational State Transfer (REST) that there was a revolutionary change from this early RPC paradigm. Recently [http://www.google.com Google], one of the biggest players in the world wide web released parts of their core RPC technologies to the general public. The framework that they call [http://code.google.com/p/protobuf/ Protocol Buffers] combines a domain specific language (DSL) for describing messages with an RPC framework that is used to pass these messages among different servers. The RPC framework itself is not released, but the DSL for describing messages and translating them to objects of several supported programming languages was released under the BSD license. Google directly supports C++, Java, and Python, but many developers have programmed ports of protocol buffers to different languages such as C#, Visual Basic, Objective C, Perl, Haskell, and [http://code.google.com/p/protobuf/wiki/ThirdPartyAddOns many more]. The [http://code.google.com/apis/protocolbuffers/docs/overview.html documentation] on protocol buffers is extremely helpful in understanding the framework. A similar framework called Thrift has evolved that backs the operation of [http://www.facebook.com Facebook] and was also released to the public. These technologies provide convenient ways to define messages, manipulate them, and serialize them to small sizes at high speeds. In HeuristicLab using remote procedure calls seems to fit very well with what we are trying to achieve in "exporting" the problem definition. The problem, to an optimizer, is basically the evaluation function and the solution representation is their common knowledge. A client is provided in HeuristicLab as well as a framework that enables developers to write a service which exposes the evaluation function to HeuristicLab. Using this service foreign language applications and problems can effectively communicate with HeuristicLab and have their parameters optimized by HeuristicLab's powerful optimization library. Please note that while we're talking about RPC, the provided frameworks are not compatible with the RPC standard, but are rather simplified to ease application. Think of RPC as a paradigm rather than a standard. Communication also requires a given media over which to exchange the information. So far HeuristicLab offers two choices for the underlying media: 1. The external program can be started as a process from HeuristicLab and the communication occurs via the process' stdin and stdout. This requires that the external program can be executed under Windows. If the developer controls the standard input and output streams and does not need to write or read other data through them, this might be a simple solution. 2. The external program is started independently from HeuristicLab and opens a TCP/IP port for communicating over a network. This is independent of the platform and a universal solution that should work in most cases. Regardless of the chosen media, the solution receiving and quality sending processes are abstracted from the developer through our service framework. For writing services, generally two types of services are supported by our framework: Push Service and Poll Service. It depends on the application which one of these is more suitable. If the developer is in charge of the control flow the push service seems plausible, if however the application flow cannot be fully controlled by the developer the poll service is the better suited option. === Push Service === As the name suggests when implementing a push service the solutions are pushed into the evaluation method. The developer has to provide a class that is able to perform the evaluation task. This class specifies a method that takes a SolutionMessage and returns a double value indicating the quality of the given solution. The method is called in a new thread whenever a new solution is received by the framework. === Poll Service === In this type the received solutions have to be polled. The service receives solutions in its own thread and puts them into a queue, waiting for the developer to process them. It provides two public methods, one that returns the next solution from the queue and blocks until a solution becomes available and another that sends the quality back to HeuristicLab. Recently [http://www.google.com Google], released parts of their core RPC technologies to the general public. The framework that they call [http://code.google.com/p/protobuf/ Protocol Buffers] combines a domain specific language (DSL) for describing messages within an RPC framework that is used to pass these messages among different servers. The RPC framework itself is not released, but the DSL for describing messages and translating them to objects of several supported programming languages was released under the BSD license. Google directly supports C++, Java, and Python, but many developers have programmed ports of protocol buffers to different languages such as C#, Visual Basic, Objective C, Perl, Haskell, and [http://code.google.com/p/protobuf/wiki/ThirdPartyAddOns many more]. The [http://code.google.com/apis/protocolbuffers/docs/overview.html documentation] on protocol buffers is extremely helpful in understanding the framework. Protocol buffers provide convenient ways to define messages, manipulate them, and serialize them to small sizes at high speeds. In HeuristicLab using remote procedure calls seems to fit very well with what we are trying to achieve in "exporting" the problem definition. The problem, to an optimizer, is basically the evaluation function and the solution representation is their common knowledge. A client is provided in HeuristicLab as well as a framework that enables developers to write a service which exposes the evaluation function to HeuristicLab. Using this service foreign language applications and problems can effectively communicate with HeuristicLab and have their parameters optimized by HeuristicLab's powerful optimization library. Communication also requires a given media over which to exchange the information and the preferred choice in HeuristicLab is to communicate via the TCP/IP network: The external program is started independently from HeuristicLab and opens a TCP/IP port for communicating over a network. Regardless of the chosen media, the solution receiving and quality sending processes are abstracted from the developer through our service framework. For writing services, generally two types of services are supported by our framework: Push Service and Poll Service. Generally, the poll service is a simple to use solution that should fit most needs. The implementation of these service frameworks are attached to the end of this page and are available in Java and in C# in binary as well as source code: * Download the [[attachment:HL3ExternalEvaluation.jar|Java JAR library]] (binary) * Download the [[attachment:HL3ExternalEvaluationCSharp.zip|C# library]] (binary) * Download the [[attachment:HeuristicLab.Problems.ExternalEvaluation (src).zip|Source code]] - useful for understanding how to use the library === Application Scenarios === {{{#!java public class RandomSocketPollEvaluator { public static void main(String[] args) { ServerSocketListenerFactory factory = new ServerSocketListenerFactory(8842); PollService service = new PollService(factory, 1); service.start(); public static void main(String[] args) { ServerSocketListenerFactory factory = new ServerSocketListenerFactory(8842); PollService service = new PollService(factory, 1); service.start(); Random random = new Random(); while (true) { SolutionMessage msg = service.getSolution(); double quality = 0; Random random = new Random(); while (true) { SolutionMessage msg = service.getSolution(); try { service.sendQuality(msg, random.nextDouble()); } catch (IOException e) { e.printStackTrace(); } // replace the line below to quality the quality quality = random.nextDouble(); // for the simple purpose here it's just a random number try { service.sendQuality(msg, quality); } catch (IOException e) { e.printStackTrace(); } } }}} This code shows a concrete example of how to use the !PollService and the ease of integrating this into an external java application. Using a !PushService is slightly different as can be seen in the following example. As mentioned it depends on the application which of the two possibilities is more suited. {{{#!java public class RandomSocketPushEvaluator { private PushService service; public static void main(String[] args) { RandomSocketPushEvaluator main = new RandomSocketPushEvaluator(); main.run(); System.out.println("Service is running, terminate by pressing or ."); System.console().readLine(); // note: you may not always have a console main.terminate(); } private void run() { ServerSocketListenerFactory factory = new ServerSocketListenerFactory(8842); service = new PushService(factory, 1, new RandomEvaluator()); service.start(); } private void terminate() { service.stop(); } private class RandomEvaluator implements IEvaluationService { Random random; public RandomEvaluator() { random = new Random(); } @Override public double evaluate(SolutionMessage msg) { return random.nextDouble(); } } } }}} This code shows a concrete example of how to use the !PollService and the ease of integrating this into an external java application. As of July 2012 you can also extend the quality message with own fields and transmit them back to HeuristicLab. If you provide a simple plugin with your own evaluator you may further process this information inside HeuristicLab, e.g. through an Analyzer that includes these in the results. The source code contains a simple plugin with an extended protocol buffer message that would add a repetition field to return the number of repetitions that have been performed. This is just for demo purposes, you can use add any number of extensions that you like. === Preparing an ExternalEvaluationProblem ===