Index: branches/ProgrammableProblem/HeuristicLab.Problems.ExternalEvaluation/3.4/ExternalEvaluationProblem.cs
===================================================================
--- trunk/sources/HeuristicLab.Problems.ExternalEvaluation/3.3/ExternalEvaluationProblem.cs	(revision 11616)
+++ branches/ProgrammableProblem/HeuristicLab.Problems.ExternalEvaluation/3.4/ExternalEvaluationProblem.cs	(revision 11892)
@@ -24,13 +24,12 @@
 using System.Drawing;
 using System.Linq;
-using HeuristicLab.Analysis;
-using HeuristicLab.Collections;
+using System.Threading;
+using Google.ProtocolBuffers;
 using HeuristicLab.Common;
 using HeuristicLab.Core;
 using HeuristicLab.Data;
-using HeuristicLab.Optimization;
-using HeuristicLab.Optimization.Operators;
 using HeuristicLab.Parameters;
 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
+using HeuristicLab.Problems.Programmable;
 
 namespace HeuristicLab.Problems.ExternalEvaluation {
@@ -38,6 +37,7 @@
   [Creatable("Problems")]
   [StorableClass]
-  public sealed class ExternalEvaluationProblem : ParameterizedNamedItem, ISingleObjectiveHeuristicOptimizationProblem, IStorableContent {
-    public string Filename { get; set; }
+  public sealed class ExternalEvaluationProblem : SingleObjectiveBasicProblem<IEncoding> {
+    private HashSet<IEvaluationServiceClient> activeClients = new HashSet<IEvaluationServiceClient>();
+    private object clientLock = new object();
 
     public static new Image StaticItemImage {
@@ -45,90 +45,27 @@
     }
 
-    public new ParameterCollection Parameters {
-      get { return base.Parameters; }
+    public OptionalValueParameter<EvaluationCache> CacheParameter {
+      get { return (OptionalValueParameter<EvaluationCache>)Parameters["Cache"]; }
     }
-    IKeyedItemCollection<string, IParameter> IParameterizedItem.Parameters {
-      get { return Parameters; }
-    }
-
-    #region Parameters
     public IValueParameter<CheckedItemCollection<IEvaluationServiceClient>> ClientsParameter {
       get { return (IValueParameter<CheckedItemCollection<IEvaluationServiceClient>>)Parameters["Clients"]; }
     }
-    public IValueParameter<IExternalEvaluationProblemEvaluator> EvaluatorParameter {
-      get { return (IValueParameter<IExternalEvaluationProblemEvaluator>)Parameters["Evaluator"]; }
+    public IValueParameter<SolutionMessageBuilder> MessageBuilderParameter {
+      get { return (IValueParameter<SolutionMessageBuilder>)Parameters["MessageBuilder"]; }
     }
-    public ValueParameter<BoolValue> MaximizationParameter {
-      get { return (ValueParameter<BoolValue>)Parameters["Maximization"]; }
+
+    public EvaluationCache Cache {
+      get { return CacheParameter.Value; }
     }
-    IParameter ISingleObjectiveHeuristicOptimizationProblem.MaximizationParameter {
-      get { return MaximizationParameter; }
+    public CheckedItemCollection<IEvaluationServiceClient> Clients {
+      get { return ClientsParameter.Value; }
     }
-    public ValueParameter<ISolutionCreator> SolutionCreatorParameter {
-      get { return (ValueParameter<ISolutionCreator>)Parameters["SolutionCreator"]; }
+    public SolutionMessageBuilder MessageBuilder {
+      get { return MessageBuilderParameter.Value; }
     }
-    IParameter IHeuristicOptimizationProblem.SolutionCreatorParameter {
-      get { return SolutionCreatorParameter; }
-    }
-    IParameter IHeuristicOptimizationProblem.EvaluatorParameter {
-      get { return EvaluatorParameter; }
-    }
-    public OptionalValueParameter<DoubleValue> BestKnownQualityParameter {
-      get { return (OptionalValueParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
-    }
-    IParameter ISingleObjectiveHeuristicOptimizationProblem.BestKnownQualityParameter {
-      get { return BestKnownQualityParameter; }
-    }
-    public OptionalValueParameter<IScope> BestKnownSolutionParameter {
-      get { return (OptionalValueParameter<IScope>)Parameters["BestKnownSolution"]; }
-    }
-    public ValueParameter<ItemList<IItem>> OperatorsParameter {
-      get { return (ValueParameter<ItemList<IItem>>)Parameters["Operators"]; }
-    }
-    public OptionalValueParameter<EvaluationCache> CacheParameter {
-      get { return (OptionalValueParameter<EvaluationCache>)Parameters["Cache"]; }
-    }
-    #endregion
-
-    #region Properties
-    public BoolValue Maximization {
-      get { return MaximizationParameter.Value; }
-      set { MaximizationParameter.Value = value; }
-    }
-    public ISolutionCreator SolutionCreator {
-      get { return SolutionCreatorParameter.Value; }
-      set { SolutionCreatorParameter.Value = value; }
-    }
-    ISolutionCreator IHeuristicOptimizationProblem.SolutionCreator {
-      get { return SolutionCreatorParameter.Value; }
-    }
-    public IExternalEvaluationProblemEvaluator Evaluator {
-      get { return EvaluatorParameter.Value; }
-      set { EvaluatorParameter.Value = value; }
-    }
-    ISingleObjectiveEvaluator ISingleObjectiveHeuristicOptimizationProblem.Evaluator {
-      get { return EvaluatorParameter.Value; }
-    }
-    IEvaluator IHeuristicOptimizationProblem.Evaluator {
-      get { return EvaluatorParameter.Value; }
-    }
-    public DoubleValue BestKnownQuality {
-      get { return BestKnownQualityParameter.Value; }
-      set { BestKnownQualityParameter.Value = value; }
-    }
-    public IEnumerable<IItem> Operators {
-      get { return OperatorsParameter.Value; }
-    }
-    private BestScopeSolutionAnalyzer BestScopeSolutionAnalyzer {
-      get { return OperatorsParameter.Value.OfType<BestScopeSolutionAnalyzer>().FirstOrDefault(); }
-    }
-    #endregion
 
     [StorableConstructor]
     private ExternalEvaluationProblem(bool deserializing) : base(deserializing) { }
-    private ExternalEvaluationProblem(ExternalEvaluationProblem original, Cloner cloner)
-      : base(original, cloner) {
-      RegisterEventHandlers();
-    }
+    private ExternalEvaluationProblem(ExternalEvaluationProblem original, Cloner cloner) : base(original, cloner) { }
     public override IDeepCloneable Clone(Cloner cloner) {
       return new ExternalEvaluationProblem(this, cloner);
@@ -136,126 +73,59 @@
     public ExternalEvaluationProblem()
       : base() {
-      ExternalEvaluator evaluator = new ExternalEvaluator();
-      UserDefinedSolutionCreator solutionCreator = new UserDefinedSolutionCreator();
-
+      Parameters.Add(new OptionalValueParameter<EvaluationCache>("Cache", "Cache of previously evaluated solutions."));
       Parameters.Add(new ValueParameter<CheckedItemCollection<IEvaluationServiceClient>>("Clients", "The clients that are used to communicate with the external application.", new CheckedItemCollection<IEvaluationServiceClient>() { new EvaluationServiceClient() }));
-      Parameters.Add(new ValueParameter<IExternalEvaluationProblemEvaluator>("Evaluator", "The evaluator that collects the values to exchange.", evaluator));
-      Parameters.Add(new ValueParameter<ISolutionCreator>("SolutionCreator", "An operator to create the solution components.", solutionCreator));
-      Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to false as most test functions are minimization problems.", new BoolValue(false)));
-      Parameters.Add(new OptionalValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this problem."));
-      Parameters.Add(new OptionalValueParameter<IScope>("BestKnownSolution", "The best known solution for this external evaluation problem."));
-      Parameters.Add(new ValueParameter<ItemList<IItem>>("Operators", "The operators and items that the problem provides to the algorithms.", new ItemList<IItem>()));
-      Parameters.Add(new OptionalValueParameter<EvaluationCache>("Cache", "Cache of previously evaluated solutions."));
-
-      InitializeOperators();
-      RegisterEventHandlers();
-    }
-    [StorableHook(HookType.AfterDeserialization)]
-    private void AfterDeserialization() {
-      // BackwardsCompatibility3.3
-      #region Backwards compatible code, remove with 3.4
-      if (!Parameters.ContainsKey("Clients")) {
-        Parameters.Add(new ValueParameter<CheckedItemCollection<IEvaluationServiceClient>>("Clients", "The clients that are used to communicate with the external application.", new CheckedItemCollection<IEvaluationServiceClient>() { new EvaluationServiceClient() }));
-        if (Parameters.ContainsKey("Client")) {
-          var client = ((IValueParameter<IEvaluationServiceClient>)Parameters["Client"]).Value;
-          if (client != null)
-            ClientsParameter.Value = new CheckedItemCollection<IEvaluationServiceClient>() { client };
-          Parameters.Remove("Client");
-        }
-      }
-
-      if (Parameters.ContainsKey("Operators") && Parameters["Operators"] is ValueParameter<ItemList<IOperator>>) {
-        ItemList<IOperator> tmp = ((ValueParameter<ItemList<IOperator>>)Parameters["Operators"]).Value;
-        Parameters.Remove("Operators");
-        Parameters.Add(new ValueParameter<ItemList<IItem>>("Operators", "The operators and items that the problem provides to the algorithms.", new ItemList<IItem>(tmp), false));
-      }
-      #endregion
-      RegisterEventHandlers();
+      Parameters.Add(new ValueParameter<SolutionMessageBuilder>("MessageBuilder", "The message builder that converts from HeuristicLab objects to SolutionMessage representation.", new SolutionMessageBuilder()));
     }
 
-    #region Events
-    public event EventHandler SolutionCreatorChanged;
-    private void OnSolutionCreatorChanged() {
-      EventHandler handler = SolutionCreatorChanged;
-      if (handler != null) handler(this, EventArgs.Empty);
-    }
-    public event EventHandler EvaluatorChanged;
-    private void OnEvaluatorChanged() {
-      EventHandler handler = EvaluatorChanged;
-      if (handler != null) handler(this, EventArgs.Empty);
-    }
-    public event EventHandler OperatorsChanged;
-    private void OnOperatorsChanged() {
-      EventHandler handler = OperatorsChanged;
-      if (handler != null) handler(this, EventArgs.Empty);
-    }
-    public event EventHandler Reset;
-    private void OnReset() {
-      EventHandler handler = Reset;
-      if (handler != null) handler(this, EventArgs.Empty);
+    public override bool Maximization {
+      get { return Parameters.ContainsKey("Maximization") && ((IValueParameter<BoolValue>)Parameters["Maximization"]).Value.Value; }
     }
 
-    private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
-      OnSolutionCreatorChanged();
+    public override double Evaluate(Individual individual, IRandom random) {
+      return Cache == null ? EvaluateOnNextAvailableClient(BuildSolutionMessage(individual)).Quality
+        : Cache.GetValue(BuildSolutionMessage(individual), m => EvaluateOnNextAvailableClient(m).Quality);
     }
-    private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
-      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
-      ParameterizeOperators();
-      OnEvaluatorChanged();
-    }
-    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
-      ParameterizeOperators();
-    }
-    private void OperatorsParameter_ValueChanged(object sender, EventArgs e) {
-      OnOperatorsChanged();
-    }
-    private void OperatorsParameter_Value_ItemsAdded(object sender, EventArgs e) {
-      OnOperatorsChanged();
-    }
-    private void OperatorsParameter_Value_ItemsRemoved(object sender, EventArgs e) {
-      OnOperatorsChanged();
-    }
-    private void OperatorsParameter_Value_CollectionReset(object sender, EventArgs e) {
-      OnOperatorsChanged();
-    }
-    #endregion
 
-    #region Helper
-    private void RegisterEventHandlers() {
-      SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
-      EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
-      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
-      OperatorsParameter.ValueChanged += new EventHandler(OperatorsParameter_ValueChanged);
-      OperatorsParameter.Value.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<IItem>>(OperatorsParameter_Value_ItemsAdded);
-      OperatorsParameter.Value.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<IItem>>(OperatorsParameter_Value_ItemsRemoved);
-      OperatorsParameter.Value.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<IItem>>(OperatorsParameter_Value_CollectionReset);
-    }
-    private void InitializeOperators() {
-      ItemList<IItem> operators = OperatorsParameter.Value;
-      operators.Add(new BestScopeSolutionAnalyzer());
-      ParameterizeAnalyzers();
-    }
-    private void ParameterizeAnalyzers() {
-      BestScopeSolutionAnalyzer.ResultsParameter.ActualName = "Results";
-      BestScopeSolutionAnalyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
-      BestScopeSolutionAnalyzer.BestKnownQualityParameter.ActualName = BestKnownQualityParameter.Name;
-      BestScopeSolutionAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
-    }
-    private void ParameterizeEvaluator() {
-      Evaluator.ClientsParameter.ActualName = ClientsParameter.Name;
-    }
-    private void ParameterizeOperators() {
-      // This is a best effort approach to wiring
-      string qualityName = Evaluator.QualityParameter.ActualName;
-      foreach (IOperator op in OperatorsParameter.Value) {
-        foreach (ILookupParameter<DoubleValue> param in op.Parameters.OfType<ILookupParameter<DoubleValue>>()) {
-          if (param.Name.Equals("Quality")) param.ActualName = qualityName;
+    private QualityMessage EvaluateOnNextAvailableClient(SolutionMessage message) {
+      IEvaluationServiceClient client = null;
+      lock (clientLock) {
+        client = Clients.CheckedItems.FirstOrDefault(c => !activeClients.Contains(c));
+        while (client == null && Clients.CheckedItems.Any()) {
+          Monitor.Wait(clientLock);
+          client = Clients.CheckedItems.FirstOrDefault(c => !activeClients.Contains(c));
         }
-        foreach (IScopeTreeLookupParameter<DoubleValue> param in op.Parameters.OfType<IScopeTreeLookupParameter<DoubleValue>>()) {
-          if (param.Name.Equals("Quality")) param.ActualName = qualityName;
+        if (client != null)
+          activeClients.Add(client);
+      }
+      try {
+        return client.Evaluate(message, GetQualityMessageExtensions());
+      } finally {
+        lock (clientLock) {
+          activeClients.Remove(client);
+          Monitor.PulseAll(clientLock);
         }
       }
     }
-    #endregion
+
+    private ExtensionRegistry GetQualityMessageExtensions() {
+      return ExtensionRegistry.CreateInstance();
+    }
+
+    private SolutionMessage BuildSolutionMessage(Individual individual) {
+      lock (clientLock) {
+        SolutionMessage.Builder protobufBuilder = SolutionMessage.CreateBuilder();
+        protobufBuilder.SolutionId = 0;
+        var scope = new Scope();
+        individual.CopyToScope(scope);
+        foreach (var variable in scope.Variables) {
+          try {
+            MessageBuilder.AddToMessage(variable.Value, variable.Name, protobufBuilder);
+          } catch (ArgumentException ex) {
+            throw new InvalidOperationException(string.Format("ERROR while building solution message: Parameter {0} cannot be added to the message", name), ex);
+          }
+        }
+        return protobufBuilder.Build();
+      }
+    }
   }
 }
