- Timestamp:
- 05/12/11 15:50:06 (14 years ago)
- Location:
- trunk/sources/HeuristicLab.Problems.ExternalEvaluation/3.3
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Problems.ExternalEvaluation/3.3/CachedExternalEvaluator.cs
r6183 r6189 44 44 set { QualityParameter.ActualValue = value; } 45 45 } 46 protected IEvaluationServiceClient Client {47 get { return ClientParameter.ActualValue; }48 }49 46 protected EvaluationCache Cache { 50 47 get { return CacheParameter.ActualValue; } … … 71 68 72 69 if (Quality == null) Quality = new DoubleValue(0); 73 Quality.Value = Cache.GetValue(BuildSolutionMessage(), m => Client.Evaluate(m).Quality);70 Quality.Value = Cache.GetValue(BuildSolutionMessage(), m => EvaluateOnNextAvailableClient(m).Quality); 74 71 if (Successor != null) 75 72 return ExecutionContext.CreateOperation(Successor); -
trunk/sources/HeuristicLab.Problems.ExternalEvaluation/3.3/ExternalEvaluationProblem.cs
r6183 r6189 53 53 54 54 #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"]; } 57 57 } 58 58 public IValueParameter<IExternalEvaluationProblemEvaluator> EvaluatorParameter { … … 144 144 UserDefinedSolutionCreator solutionCreator = new UserDefinedSolutionCreator(); 145 145 146 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() })); 147 147 Parameters.Add(new ValueParameter<IExternalEvaluationProblemEvaluator>("Evaluator", "The evaluator that collects the values to exchange.", evaluator)); 148 148 Parameters.Add(new ValueParameter<ISolutionCreator>("SolutionCreator", "An operator to create the solution components.", solutionCreator)); … … 155 155 InitializeOperators(); 156 156 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 157 172 } 158 173 … … 227 242 } 228 243 private void ParameterizeEvaluator() { 229 Evaluator.Client Parameter.ActualName = ClientParameter.Name;244 Evaluator.ClientsParameter.ActualName = ClientsParameter.Name; 230 245 } 231 246 private void ParameterizeOperators() { -
trunk/sources/HeuristicLab.Problems.ExternalEvaluation/3.3/ExternalEvaluator.cs
r6140 r6189 21 21 22 22 using System; 23 using System.Collections.Generic; 24 using System.Linq; 25 using System.Threading; 23 26 using HeuristicLab.Common; 24 27 using HeuristicLab.Core; … … 33 36 public class ExternalEvaluator : ValuesCollector, IExternalEvaluationProblemEvaluator { 34 37 38 #region Parameters 35 39 public ILookupParameter<DoubleValue> QualityParameter { 36 40 get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; } 37 41 } 38 public IValueLookupParameter< IEvaluationServiceClient> ClientParameter {39 get { return ( IValueLookupParameter<IEvaluationServiceClient>)Parameters["Client"]; }42 public IValueLookupParameter<CheckedItemCollection<IEvaluationServiceClient>> ClientsParameter { 43 get { return (ValueLookupParameter<CheckedItemCollection<IEvaluationServiceClient>>)Parameters["Clients"]; } 40 44 } 41 42 45 public IValueParameter<SolutionMessageBuilder> MessageBuilderParameter { 43 46 get { return (IValueParameter<SolutionMessageBuilder>)Parameters["MessageBuilder"]; } 44 47 } 48 #endregion 45 49 50 #region Parameter Values 46 51 protected SolutionMessageBuilder MessageBuilder { 47 52 get { return MessageBuilderParameter.Value; } 48 53 } 54 protected CheckedItemCollection<IEvaluationServiceClient> Clients { 55 get { return ClientsParameter.ActualValue; } 56 } 57 #endregion 49 58 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 50 66 [StorableConstructor] 51 67 protected ExternalEvaluator(bool deserializing) : base(deserializing) { } 52 68 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 } 53 75 public override IDeepCloneable Clone(Cloner cloner) { 54 76 return new ExternalEvaluator(this, cloner); 55 77 } 56 public ExternalEvaluator() 57 : base() { 58 Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The quality of the current solution.")); 59 Parameters.Add(new ValueLookupParameter<IEvaluationServiceClient>("Client", "The client that communicates with the external process.")); 60 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 61 92 } 93 #endregion 62 94 63 95 public override IOperation Apply() { 64 SolutionMessage message = BuildSolutionMessage();65 96 66 IEvaluationServiceClient client = ClientParameter.ActualValue; 67 QualityMessage answer = client.Evaluate(message); 97 QualityMessage answer = EvaluateOnNextAvailableClient(BuildSolutionMessage()); 68 98 69 99 if (QualityParameter.ActualValue == null) … … 74 104 } 75 105 76 protected virtual SolutionMessage BuildSolutionMessage() { 77 SolutionMessage.Builder protobufBuilder = SolutionMessage.CreateBuilder(); 78 protobufBuilder.SolutionId = 0; 79 foreach (IParameter param in CollectedValues) { 80 IItem value = param.ActualValue; 81 if (value != null) { 82 ILookupParameter lookupParam = param as ILookupParameter; 83 string name = lookupParam != null ? lookupParam.TranslatedName : param.Name; 84 try { 85 MessageBuilder.AddToMessage(value, name, protobufBuilder); 86 } catch (ArgumentException ex) { 87 throw new InvalidOperationException("ERROR in " + Name + ": Parameter " + name + " cannot be added to the message.", ex); 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 } 88 143 } 89 144 } 145 return protobufBuilder.Build(); 90 146 } 91 return protobufBuilder.Build();92 147 } 148 93 149 } 94 150 } -
trunk/sources/HeuristicLab.Problems.ExternalEvaluation/3.3/Interfaces/IExternalEvaluationProblemEvaluator.cs
r5445 r6189 25 25 namespace HeuristicLab.Problems.ExternalEvaluation { 26 26 public interface IExternalEvaluationProblemEvaluator : ISingleObjectiveEvaluator { 27 IValueLookupParameter< IEvaluationServiceClient> ClientParameter { get; }27 IValueLookupParameter<CheckedItemCollection<IEvaluationServiceClient>> ClientsParameter { get; } 28 28 } 29 29 }
Note: See TracChangeset
for help on using the changeset viewer.