- Timestamp:
- 05/14/11 16:45:46 (13 years ago)
- 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 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 { … … 85 85 public ValueParameter<ItemList<IOperator>> OperatorsParameter { 86 86 get { return (ValueParameter<ItemList<IOperator>>)Parameters["Operators"]; } 87 } 88 public OptionalValueParameter<EvaluationCache> CacheParameter { 89 get { return (OptionalValueParameter<EvaluationCache>)Parameters["Cache"]; } 87 90 } 88 91 #endregion … … 141 144 UserDefinedSolutionCreator solutionCreator = new UserDefinedSolutionCreator(); 142 145 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() })); 144 147 Parameters.Add(new ValueParameter<IExternalEvaluationProblemEvaluator>("Evaluator", "The evaluator that collects the values to exchange.", evaluator)); 145 148 Parameters.Add(new ValueParameter<ISolutionCreator>("SolutionCreator", "An operator to create the solution components.", solutionCreator)); … … 148 151 Parameters.Add(new OptionalValueParameter<IScope>("BestKnownSolution", "The best known solution for this external evaluation problem.")); 149 152 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.")); 150 154 151 155 InitializeOperators(); 152 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 153 172 } 154 173 … … 223 242 } 224 243 private void ParameterizeEvaluator() { 225 Evaluator.Client Parameter.ActualName = ClientParameter.Name;244 Evaluator.ClientsParameter.ActualName = ClientsParameter.Name; 226 245 } 227 246 private void ParameterizeOperators() { -
branches/histogram/HeuristicLab.Problems.ExternalEvaluation/3.3/ExternalEvaluator.cs
r5445 r6195 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; … … 32 35 [StorableClass] 33 36 public class ExternalEvaluator : ValuesCollector, IExternalEvaluationProblemEvaluator { 37 38 #region Parameters 34 39 public ILookupParameter<DoubleValue> QualityParameter { 35 40 get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; } 36 41 } 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"]; } 39 44 } 40 41 45 public IValueParameter<SolutionMessageBuilder> MessageBuilderParameter { 42 46 get { return (IValueParameter<SolutionMessageBuilder>)Parameters["MessageBuilder"]; } 43 47 } 48 #endregion 44 49 45 private SolutionMessageBuilder MessageBuilder { 50 #region Parameter Values 51 protected SolutionMessageBuilder MessageBuilder { 46 52 get { return MessageBuilderParameter.Value; } 47 53 } 54 protected CheckedItemCollection<IEvaluationServiceClient> Clients { 55 get { return ClientsParameter.ActualValue; } 56 } 57 #endregion 48 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 49 66 [StorableConstructor] 50 67 protected ExternalEvaluator(bool deserializing) : base(deserializing) { } 51 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 } 52 75 public override IDeepCloneable Clone(Cloner cloner) { 53 76 return new ExternalEvaluator(this, cloner); 54 77 } 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 60 92 } 93 #endregion 61 94 62 95 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 80 99 if (QualityParameter.ActualValue == null) 81 100 QualityParameter.ActualValue = new DoubleValue(answer.Quality); … … 84 103 return base.Apply(); 85 104 } 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 86 149 } 87 150 } -
branches/histogram/HeuristicLab.Problems.ExternalEvaluation/3.3/HeuristicLab.Problems.ExternalEvaluation-3.3.csproj
r5163 r6195 98 98 </Reference> 99 99 <Reference Include="System.Drawing" /> 100 <Reference Include="System.Windows.Forms" /> 100 101 <Reference Include="System.Xml.Linq"> 101 102 <RequiredTargetFramework>3.5</RequiredTargetFramework> … … 115 116 <Compile Include="Converters\IntegerConverter.cs" /> 116 117 <Compile Include="Converters\StringConverter.cs" /> 118 <Compile Include="CachedExternalEvaluator.cs" /> 119 <Compile Include="EvaluationCache.cs" /> 117 120 <Compile Include="Drivers\EvaluationServiceClient.cs" /> 118 121 <Compile Include="Drivers\EvaluationChannel.cs" /> … … 150 153 <Name>HeuristicLab.Common-3.3</Name> 151 154 </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> 152 159 <ProjectReference Include="..\..\HeuristicLab.Core\3.3\HeuristicLab.Core-3.3.csproj"> 153 160 <Project>{C36BD924-A541-4A00-AFA8-41701378DDC5}</Project> … … 165 172 <Project>{6908BDCE-D925-43F3-94AC-A531E6DF2591}</Project> 166 173 <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> 167 182 </ProjectReference> 168 183 <ProjectReference Include="..\..\HeuristicLab.Operators\3.3\HeuristicLab.Operators-3.3.csproj"> -
branches/histogram/HeuristicLab.Problems.ExternalEvaluation/3.3/HeuristicLabProblemsExternalEvaluationPlugin.cs.frame
r5446 r6195 23 23 24 24 namespace HeuristicLab.Problems.ExternalEvaluation { 25 [Plugin("HeuristicLab.Problems.ExternalEvaluation", "3.3. 3.$WCREV$")]25 [Plugin("HeuristicLab.Problems.ExternalEvaluation", "3.3.4.$WCREV$")] 26 26 [PluginFile("HeuristicLab.Problems.ExternalEvaluation-3.3.dll", PluginFileType.Assembly)] 27 27 [PluginDependency("HeuristicLab.Analysis", "3.3")] -
branches/histogram/HeuristicLab.Problems.ExternalEvaluation/3.3/Interfaces/IExternalEvaluationProblemEvaluator.cs
r5445 r6195 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 } -
branches/histogram/HeuristicLab.Problems.ExternalEvaluation/3.3/Properties/AssemblyInfo.frame
r5446 r6195 34 34 // [assembly: AssemblyVersion("1.0.*")] 35 35 [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.