Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
05/14/11 16:45:46 (13 years ago)
Author:
abeham
Message:

#1465

  • updated branch with latest version of trunk
Location:
branches/histogram
Files:
7 edited
2 copied

Legend:

Unmodified
Added
Removed
  • branches/histogram

  • branches/histogram/HeuristicLab.Problems.ExternalEvaluation/3.3/ExternalEvaluationProblem.cs

    r5809 r6195  
    5353
    5454    #region Parameters
    55     public IValueParameter<IEvaluationServiceClient> ClientParameter {
    56       get { return (IValueParameter<IEvaluationServiceClient>)Parameters["Client"]; }
     55    public IValueParameter<CheckedItemCollection<IEvaluationServiceClient>> ClientsParameter {
     56      get { return (IValueParameter<CheckedItemCollection<IEvaluationServiceClient>>)Parameters["Clients"]; }
    5757    }
    5858    public IValueParameter<IExternalEvaluationProblemEvaluator> EvaluatorParameter {
     
    8585    public ValueParameter<ItemList<IOperator>> OperatorsParameter {
    8686      get { return (ValueParameter<ItemList<IOperator>>)Parameters["Operators"]; }
     87    }
     88    public OptionalValueParameter<EvaluationCache> CacheParameter {
     89      get { return (OptionalValueParameter<EvaluationCache>)Parameters["Cache"]; }
    8790    }
    8891    #endregion
     
    141144      UserDefinedSolutionCreator solutionCreator = new UserDefinedSolutionCreator();
    142145
    143       Parameters.Add(new ValueParameter<IEvaluationServiceClient>("Client", "The client that is used to communicate with the external application.", new EvaluationServiceClient()));
     146      Parameters.Add(new ValueParameter<CheckedItemCollection<IEvaluationServiceClient>>("Clients", "The clients that are used to communicate with the external application.", new CheckedItemCollection<IEvaluationServiceClient>() { new EvaluationServiceClient() }));
    144147      Parameters.Add(new ValueParameter<IExternalEvaluationProblemEvaluator>("Evaluator", "The evaluator that collects the values to exchange.", evaluator));
    145148      Parameters.Add(new ValueParameter<ISolutionCreator>("SolutionCreator", "An operator to create the solution components.", solutionCreator));
     
    148151      Parameters.Add(new OptionalValueParameter<IScope>("BestKnownSolution", "The best known solution for this external evaluation problem."));
    149152      Parameters.Add(new ValueParameter<ItemList<IOperator>>("Operators", "The operators that are passed to the algorithm.", new ItemList<IOperator>()));
     153      Parameters.Add(new OptionalValueParameter<EvaluationCache>("Cache", "Cache of previously evaluated solutions."));
    150154
    151155      InitializeOperators();
    152156      AttachEventHandlers();
     157    }
     158    [StorableHook(HookType.AfterDeserialization)]
     159    private void AfterDeserialization() {
     160      // BackwardsCompatibility3.3
     161      #region Backwards compatible code, remove with 3.4
     162      if (!Parameters.ContainsKey("Clients")) {
     163        Parameters.Add(new ValueParameter<CheckedItemCollection<IEvaluationServiceClient>>("Clients", "The clients that are used to communicate with the external application.", new CheckedItemCollection<IEvaluationServiceClient>() { new EvaluationServiceClient() }));
     164        if (Parameters.ContainsKey("Client")) {
     165          var client = ((IValueParameter<IEvaluationServiceClient>)Parameters["Client"]).Value;
     166          if (client != null)
     167            ClientsParameter.Value = new CheckedItemCollection<IEvaluationServiceClient>() { client };
     168          Parameters.Remove("Client");
     169        }
     170      }
     171      #endregion
    153172    }
    154173
     
    223242    }
    224243    private void ParameterizeEvaluator() {
    225       Evaluator.ClientParameter.ActualName = ClientParameter.Name;
     244      Evaluator.ClientsParameter.ActualName = ClientsParameter.Name;
    226245    }
    227246    private void ParameterizeOperators() {
  • branches/histogram/HeuristicLab.Problems.ExternalEvaluation/3.3/ExternalEvaluator.cs

    r5445 r6195  
    2121
    2222using System;
     23using System.Collections.Generic;
     24using System.Linq;
     25using System.Threading;
    2326using HeuristicLab.Common;
    2427using HeuristicLab.Core;
     
    3235  [StorableClass]
    3336  public class ExternalEvaluator : ValuesCollector, IExternalEvaluationProblemEvaluator {
     37
     38    #region Parameters
    3439    public ILookupParameter<DoubleValue> QualityParameter {
    3540      get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
    3641    }
    37     public IValueLookupParameter<IEvaluationServiceClient> ClientParameter {
    38       get { return (IValueLookupParameter<IEvaluationServiceClient>)Parameters["Client"]; }
     42    public IValueLookupParameter<CheckedItemCollection<IEvaluationServiceClient>> ClientsParameter {
     43      get { return (ValueLookupParameter<CheckedItemCollection<IEvaluationServiceClient>>)Parameters["Clients"]; }
    3944    }
    40 
    4145    public IValueParameter<SolutionMessageBuilder> MessageBuilderParameter {
    4246      get { return (IValueParameter<SolutionMessageBuilder>)Parameters["MessageBuilder"]; }
    4347    }
     48    #endregion
    4449
    45     private SolutionMessageBuilder MessageBuilder {
     50    #region Parameter Values
     51    protected SolutionMessageBuilder MessageBuilder {
    4652      get { return MessageBuilderParameter.Value; }
    4753    }
     54    protected CheckedItemCollection<IEvaluationServiceClient> Clients {
     55      get { return ClientsParameter.ActualValue; }
     56    }
     57    #endregion
    4858
     59    #region Fields
     60    protected HashSet<IEvaluationServiceClient> activeClients = new HashSet<IEvaluationServiceClient>();
     61    protected object clientLock = new object();
     62    protected AutoResetEvent clientAvailable = new AutoResetEvent(false);
     63    #endregion
     64
     65    #region Construction & Cloning
    4966    [StorableConstructor]
    5067    protected ExternalEvaluator(bool deserializing) : base(deserializing) { }
    5168    protected ExternalEvaluator(ExternalEvaluator original, Cloner cloner) : base(original, cloner) { }
     69    public ExternalEvaluator()
     70      : base() {
     71      Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The quality of the current solution."));
     72      Parameters.Add(new ValueLookupParameter<CheckedItemCollection<IEvaluationServiceClient>>("Clients", "Collection of clients which communicate the the external process. These clients my be contacted in parallel."));
     73      Parameters.Add(new ValueParameter<SolutionMessageBuilder>("MessageBuilder", "The message builder that converts from HeuristicLab objects to SolutionMessage representation.", new SolutionMessageBuilder()));
     74    }
    5275    public override IDeepCloneable Clone(Cloner cloner) {
    5376      return new ExternalEvaluator(this, cloner);
    5477    }
    55     public ExternalEvaluator()
    56       : base() {
    57       Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The quality of the current solution."));
    58       Parameters.Add(new ValueLookupParameter<IEvaluationServiceClient>("Client", "The client that communicates with the external process."));
    59       Parameters.Add(new ValueParameter<SolutionMessageBuilder>("MessageBuilder", "The message builder that converts from HeuristicLab objects to SolutionMessage representation.", new SolutionMessageBuilder()));
     78    [StorableHook(HookType.AfterDeserialization)]
     79    private void AfterDeserialization() {
     80      // BackwardsCompatibility3.3
     81      #region Backwards compatible code, remove with 3.4
     82      if (!Parameters.ContainsKey("Clients")) {
     83        Parameters.Add(new ValueLookupParameter<CheckedItemCollection<IEvaluationServiceClient>>("Clients", "Collection of clients which communicate the the external process. These clients my be contacted in parallel."));
     84        if (Parameters.ContainsKey("Client")) {
     85          var client = ((IValueLookupParameter<IEvaluationServiceClient>)Parameters["Client"]).Value;
     86          if (client != null)
     87            ClientsParameter.Value = new CheckedItemCollection<IEvaluationServiceClient>() { client };
     88          Parameters.Remove("Client");
     89        }
     90      }
     91      #endregion
    6092    }
     93    #endregion
    6194
    6295    public override IOperation Apply() {
    63       IEvaluationServiceClient client = ClientParameter.ActualValue;
    64       SolutionMessage.Builder protobufBuilder = SolutionMessage.CreateBuilder();
    65       protobufBuilder.SolutionId = 0;
    66       foreach (IParameter param in CollectedValues) {
    67         IItem value = param.ActualValue;
    68         if (value != null) {
    69           ILookupParameter lookupParam = param as ILookupParameter;
    70           string name = lookupParam != null ? lookupParam.TranslatedName : param.Name;
    71           try {
    72             MessageBuilder.AddToMessage(value, name, protobufBuilder);
    73           }
    74           catch (ArgumentException ex) {
    75             throw new InvalidOperationException("ERROR in " + Name + ": Parameter " + name + " cannot be added to the message.", ex);
    76           }
    77         }
    78       }
    79       QualityMessage answer = client.Evaluate(protobufBuilder.Build());
     96
     97      QualityMessage answer = EvaluateOnNextAvailableClient(BuildSolutionMessage());
     98
    8099      if (QualityParameter.ActualValue == null)
    81100        QualityParameter.ActualValue = new DoubleValue(answer.Quality);
     
    84103      return base.Apply();
    85104    }
     105
     106    protected QualityMessage EvaluateOnNextAvailableClient(SolutionMessage message) {
     107      IEvaluationServiceClient client = null;
     108      lock (clientLock) {
     109        client = Clients.CheckedItems.FirstOrDefault(c => !activeClients.Contains(c));
     110        while (client == null && Clients.Count > 0) {
     111          Monitor.Exit(clientLock);
     112          clientAvailable.WaitOne();
     113          Monitor.Enter(clientLock);
     114          client = Clients.CheckedItems.FirstOrDefault(c => !activeClients.Contains(c));
     115        }
     116        if (client != null)
     117          activeClients.Add(client);
     118      }
     119      try {
     120        return client.Evaluate(message);
     121      } finally {
     122        lock (clientLock) {
     123          activeClients.Remove(client);
     124          clientAvailable.Set();
     125        }
     126      }
     127    }
     128
     129    protected SolutionMessage BuildSolutionMessage() {
     130      lock (clientLock) {
     131        SolutionMessage.Builder protobufBuilder = SolutionMessage.CreateBuilder();
     132        protobufBuilder.SolutionId = 0;
     133        foreach (IParameter param in CollectedValues) {
     134          IItem value = param.ActualValue;
     135          if (value != null) {
     136            ILookupParameter lookupParam = param as ILookupParameter;
     137            string name = lookupParam != null ? lookupParam.TranslatedName : param.Name;
     138            try {
     139              MessageBuilder.AddToMessage(value, name, protobufBuilder);
     140            } catch (ArgumentException ex) {
     141              throw new InvalidOperationException(string.Format("ERROR while building solution message: Parameter {0} cannot be added to the message", name), ex);
     142            }
     143          }
     144        }
     145        return protobufBuilder.Build();
     146      }
     147    }
     148
    86149  }
    87150}
  • branches/histogram/HeuristicLab.Problems.ExternalEvaluation/3.3/HeuristicLab.Problems.ExternalEvaluation-3.3.csproj

    r5163 r6195  
    9898    </Reference>
    9999    <Reference Include="System.Drawing" />
     100    <Reference Include="System.Windows.Forms" />
    100101    <Reference Include="System.Xml.Linq">
    101102      <RequiredTargetFramework>3.5</RequiredTargetFramework>
     
    115116    <Compile Include="Converters\IntegerConverter.cs" />
    116117    <Compile Include="Converters\StringConverter.cs" />
     118    <Compile Include="CachedExternalEvaluator.cs" />
     119    <Compile Include="EvaluationCache.cs" />
    117120    <Compile Include="Drivers\EvaluationServiceClient.cs" />
    118121    <Compile Include="Drivers\EvaluationChannel.cs" />
     
    150153      <Name>HeuristicLab.Common-3.3</Name>
    151154    </ProjectReference>
     155    <ProjectReference Include="..\..\HeuristicLab.Core.Views\3.3\HeuristicLab.Core.Views-3.3.csproj">
     156      <Project>{E226881D-315F-423D-B419-A766FE0D8685}</Project>
     157      <Name>HeuristicLab.Core.Views-3.3</Name>
     158    </ProjectReference>
    152159    <ProjectReference Include="..\..\HeuristicLab.Core\3.3\HeuristicLab.Core-3.3.csproj">
    153160      <Project>{C36BD924-A541-4A00-AFA8-41701378DDC5}</Project>
     
    165172      <Project>{6908BDCE-D925-43F3-94AC-A531E6DF2591}</Project>
    166173      <Name>ProtocolBuffers</Name>
     174    </ProjectReference>
     175    <ProjectReference Include="..\..\HeuristicLab.MainForm.WindowsForms\3.3\HeuristicLab.MainForm.WindowsForms-3.3.csproj">
     176      <Project>{AB687BBE-1BFE-476B-906D-44237135431D}</Project>
     177      <Name>HeuristicLab.MainForm.WindowsForms-3.3</Name>
     178    </ProjectReference>
     179    <ProjectReference Include="..\..\HeuristicLab.MainForm\3.3\HeuristicLab.MainForm-3.3.csproj">
     180      <Project>{3BD61258-31DA-4B09-89C0-4F71FEF5F05A}</Project>
     181      <Name>HeuristicLab.MainForm-3.3</Name>
    167182    </ProjectReference>
    168183    <ProjectReference Include="..\..\HeuristicLab.Operators\3.3\HeuristicLab.Operators-3.3.csproj">
  • branches/histogram/HeuristicLab.Problems.ExternalEvaluation/3.3/HeuristicLabProblemsExternalEvaluationPlugin.cs.frame

    r5446 r6195  
    2323
    2424namespace HeuristicLab.Problems.ExternalEvaluation {
    25   [Plugin("HeuristicLab.Problems.ExternalEvaluation", "3.3.3.$WCREV$")]
     25  [Plugin("HeuristicLab.Problems.ExternalEvaluation", "3.3.4.$WCREV$")]
    2626  [PluginFile("HeuristicLab.Problems.ExternalEvaluation-3.3.dll", PluginFileType.Assembly)]
    2727  [PluginDependency("HeuristicLab.Analysis", "3.3")]
  • branches/histogram/HeuristicLab.Problems.ExternalEvaluation/3.3/Interfaces/IExternalEvaluationProblemEvaluator.cs

    r5445 r6195  
    2525namespace HeuristicLab.Problems.ExternalEvaluation {
    2626  public interface IExternalEvaluationProblemEvaluator : ISingleObjectiveEvaluator {
    27     IValueLookupParameter<IEvaluationServiceClient> ClientParameter { get; }
     27    IValueLookupParameter<CheckedItemCollection<IEvaluationServiceClient>> ClientsParameter { get; }
    2828  }
    2929}
  • branches/histogram/HeuristicLab.Problems.ExternalEvaluation/3.3/Properties/AssemblyInfo.frame

    r5446 r6195  
    3434// [assembly: AssemblyVersion("1.0.*")]
    3535[assembly: AssemblyVersion("3.3.0.0")]
    36 [assembly: AssemblyFileVersion("3.3.3.$WCREV$")]
     36[assembly: AssemblyFileVersion("3.3.4.$WCREV$")]
Note: See TracChangeset for help on using the changeset viewer.