Changeset 11892 for branches/ProgrammableProblem/HeuristicLab.Problems.ExternalEvaluation/3.4/ExternalEvaluationProblem.cs
- Timestamp:
- 02/04/15 18:14:48 (10 years ago)
- Location:
- branches/ProgrammableProblem/HeuristicLab.Problems.ExternalEvaluation
- Files:
-
- 1 added
- 1 edited
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
branches/ProgrammableProblem/HeuristicLab.Problems.ExternalEvaluation/3.4/ExternalEvaluationProblem.cs
r11616 r11892 24 24 using System.Drawing; 25 25 using System.Linq; 26 using HeuristicLab.Analysis;27 using HeuristicLab.Collections;26 using System.Threading; 27 using Google.ProtocolBuffers; 28 28 using HeuristicLab.Common; 29 29 using HeuristicLab.Core; 30 30 using HeuristicLab.Data; 31 using HeuristicLab.Optimization;32 using HeuristicLab.Optimization.Operators;33 31 using HeuristicLab.Parameters; 34 32 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 33 using HeuristicLab.Problems.Programmable; 35 34 36 35 namespace HeuristicLab.Problems.ExternalEvaluation { … … 38 37 [Creatable("Problems")] 39 38 [StorableClass] 40 public sealed class ExternalEvaluationProblem : ParameterizedNamedItem, ISingleObjectiveHeuristicOptimizationProblem, IStorableContent { 41 public string Filename { get; set; } 39 public sealed class ExternalEvaluationProblem : SingleObjectiveBasicProblem<IEncoding> { 40 private HashSet<IEvaluationServiceClient> activeClients = new HashSet<IEvaluationServiceClient>(); 41 private object clientLock = new object(); 42 42 43 43 public static new Image StaticItemImage { … … 45 45 } 46 46 47 public new ParameterCollection Parameters{48 get { return base.Parameters; }47 public OptionalValueParameter<EvaluationCache> CacheParameter { 48 get { return (OptionalValueParameter<EvaluationCache>)Parameters["Cache"]; } 49 49 } 50 IKeyedItemCollection<string, IParameter> IParameterizedItem.Parameters {51 get { return Parameters; }52 }53 54 #region Parameters55 50 public IValueParameter<CheckedItemCollection<IEvaluationServiceClient>> ClientsParameter { 56 51 get { return (IValueParameter<CheckedItemCollection<IEvaluationServiceClient>>)Parameters["Clients"]; } 57 52 } 58 public IValueParameter< IExternalEvaluationProblemEvaluator> EvaluatorParameter {59 get { return (IValueParameter< IExternalEvaluationProblemEvaluator>)Parameters["Evaluator"]; }53 public IValueParameter<SolutionMessageBuilder> MessageBuilderParameter { 54 get { return (IValueParameter<SolutionMessageBuilder>)Parameters["MessageBuilder"]; } 60 55 } 61 public ValueParameter<BoolValue> MaximizationParameter { 62 get { return (ValueParameter<BoolValue>)Parameters["Maximization"]; } 56 57 public EvaluationCache Cache { 58 get { return CacheParameter.Value; } 63 59 } 64 IParameter ISingleObjectiveHeuristicOptimizationProblem.MaximizationParameter{65 get { return MaximizationParameter; }60 public CheckedItemCollection<IEvaluationServiceClient> Clients { 61 get { return ClientsParameter.Value; } 66 62 } 67 public ValueParameter<ISolutionCreator> SolutionCreatorParameter {68 get { return (ValueParameter<ISolutionCreator>)Parameters["SolutionCreator"]; }63 public SolutionMessageBuilder MessageBuilder { 64 get { return MessageBuilderParameter.Value; } 69 65 } 70 IParameter IHeuristicOptimizationProblem.SolutionCreatorParameter {71 get { return SolutionCreatorParameter; }72 }73 IParameter IHeuristicOptimizationProblem.EvaluatorParameter {74 get { return EvaluatorParameter; }75 }76 public OptionalValueParameter<DoubleValue> BestKnownQualityParameter {77 get { return (OptionalValueParameter<DoubleValue>)Parameters["BestKnownQuality"]; }78 }79 IParameter ISingleObjectiveHeuristicOptimizationProblem.BestKnownQualityParameter {80 get { return BestKnownQualityParameter; }81 }82 public OptionalValueParameter<IScope> BestKnownSolutionParameter {83 get { return (OptionalValueParameter<IScope>)Parameters["BestKnownSolution"]; }84 }85 public ValueParameter<ItemList<IItem>> OperatorsParameter {86 get { return (ValueParameter<ItemList<IItem>>)Parameters["Operators"]; }87 }88 public OptionalValueParameter<EvaluationCache> CacheParameter {89 get { return (OptionalValueParameter<EvaluationCache>)Parameters["Cache"]; }90 }91 #endregion92 93 #region Properties94 public BoolValue Maximization {95 get { return MaximizationParameter.Value; }96 set { MaximizationParameter.Value = value; }97 }98 public ISolutionCreator SolutionCreator {99 get { return SolutionCreatorParameter.Value; }100 set { SolutionCreatorParameter.Value = value; }101 }102 ISolutionCreator IHeuristicOptimizationProblem.SolutionCreator {103 get { return SolutionCreatorParameter.Value; }104 }105 public IExternalEvaluationProblemEvaluator Evaluator {106 get { return EvaluatorParameter.Value; }107 set { EvaluatorParameter.Value = value; }108 }109 ISingleObjectiveEvaluator ISingleObjectiveHeuristicOptimizationProblem.Evaluator {110 get { return EvaluatorParameter.Value; }111 }112 IEvaluator IHeuristicOptimizationProblem.Evaluator {113 get { return EvaluatorParameter.Value; }114 }115 public DoubleValue BestKnownQuality {116 get { return BestKnownQualityParameter.Value; }117 set { BestKnownQualityParameter.Value = value; }118 }119 public IEnumerable<IItem> Operators {120 get { return OperatorsParameter.Value; }121 }122 private BestScopeSolutionAnalyzer BestScopeSolutionAnalyzer {123 get { return OperatorsParameter.Value.OfType<BestScopeSolutionAnalyzer>().FirstOrDefault(); }124 }125 #endregion126 66 127 67 [StorableConstructor] 128 68 private ExternalEvaluationProblem(bool deserializing) : base(deserializing) { } 129 private ExternalEvaluationProblem(ExternalEvaluationProblem original, Cloner cloner) 130 : base(original, cloner) { 131 RegisterEventHandlers(); 132 } 69 private ExternalEvaluationProblem(ExternalEvaluationProblem original, Cloner cloner) : base(original, cloner) { } 133 70 public override IDeepCloneable Clone(Cloner cloner) { 134 71 return new ExternalEvaluationProblem(this, cloner); … … 136 73 public ExternalEvaluationProblem() 137 74 : base() { 138 ExternalEvaluator evaluator = new ExternalEvaluator(); 139 UserDefinedSolutionCreator solutionCreator = new UserDefinedSolutionCreator(); 140 75 Parameters.Add(new OptionalValueParameter<EvaluationCache>("Cache", "Cache of previously evaluated solutions.")); 141 76 Parameters.Add(new ValueParameter<CheckedItemCollection<IEvaluationServiceClient>>("Clients", "The clients that are used to communicate with the external application.", new CheckedItemCollection<IEvaluationServiceClient>() { new EvaluationServiceClient() })); 142 Parameters.Add(new ValueParameter<IExternalEvaluationProblemEvaluator>("Evaluator", "The evaluator that collects the values to exchange.", evaluator)); 143 Parameters.Add(new ValueParameter<ISolutionCreator>("SolutionCreator", "An operator to create the solution components.", solutionCreator)); 144 Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to false as most test functions are minimization problems.", new BoolValue(false))); 145 Parameters.Add(new OptionalValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this problem.")); 146 Parameters.Add(new OptionalValueParameter<IScope>("BestKnownSolution", "The best known solution for this external evaluation problem.")); 147 Parameters.Add(new ValueParameter<ItemList<IItem>>("Operators", "The operators and items that the problem provides to the algorithms.", new ItemList<IItem>())); 148 Parameters.Add(new OptionalValueParameter<EvaluationCache>("Cache", "Cache of previously evaluated solutions.")); 149 150 InitializeOperators(); 151 RegisterEventHandlers(); 152 } 153 [StorableHook(HookType.AfterDeserialization)] 154 private void AfterDeserialization() { 155 // BackwardsCompatibility3.3 156 #region Backwards compatible code, remove with 3.4 157 if (!Parameters.ContainsKey("Clients")) { 158 Parameters.Add(new ValueParameter<CheckedItemCollection<IEvaluationServiceClient>>("Clients", "The clients that are used to communicate with the external application.", new CheckedItemCollection<IEvaluationServiceClient>() { new EvaluationServiceClient() })); 159 if (Parameters.ContainsKey("Client")) { 160 var client = ((IValueParameter<IEvaluationServiceClient>)Parameters["Client"]).Value; 161 if (client != null) 162 ClientsParameter.Value = new CheckedItemCollection<IEvaluationServiceClient>() { client }; 163 Parameters.Remove("Client"); 164 } 165 } 166 167 if (Parameters.ContainsKey("Operators") && Parameters["Operators"] is ValueParameter<ItemList<IOperator>>) { 168 ItemList<IOperator> tmp = ((ValueParameter<ItemList<IOperator>>)Parameters["Operators"]).Value; 169 Parameters.Remove("Operators"); 170 Parameters.Add(new ValueParameter<ItemList<IItem>>("Operators", "The operators and items that the problem provides to the algorithms.", new ItemList<IItem>(tmp), false)); 171 } 172 #endregion 173 RegisterEventHandlers(); 77 Parameters.Add(new ValueParameter<SolutionMessageBuilder>("MessageBuilder", "The message builder that converts from HeuristicLab objects to SolutionMessage representation.", new SolutionMessageBuilder())); 174 78 } 175 79 176 #region Events 177 public event EventHandler SolutionCreatorChanged; 178 private void OnSolutionCreatorChanged() { 179 EventHandler handler = SolutionCreatorChanged; 180 if (handler != null) handler(this, EventArgs.Empty); 181 } 182 public event EventHandler EvaluatorChanged; 183 private void OnEvaluatorChanged() { 184 EventHandler handler = EvaluatorChanged; 185 if (handler != null) handler(this, EventArgs.Empty); 186 } 187 public event EventHandler OperatorsChanged; 188 private void OnOperatorsChanged() { 189 EventHandler handler = OperatorsChanged; 190 if (handler != null) handler(this, EventArgs.Empty); 191 } 192 public event EventHandler Reset; 193 private void OnReset() { 194 EventHandler handler = Reset; 195 if (handler != null) handler(this, EventArgs.Empty); 80 public override bool Maximization { 81 get { return Parameters.ContainsKey("Maximization") && ((IValueParameter<BoolValue>)Parameters["Maximization"]).Value.Value; } 196 82 } 197 83 198 private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) { 199 OnSolutionCreatorChanged(); 84 public override double Evaluate(Individual individual, IRandom random) { 85 return Cache == null ? EvaluateOnNextAvailableClient(BuildSolutionMessage(individual)).Quality 86 : Cache.GetValue(BuildSolutionMessage(individual), m => EvaluateOnNextAvailableClient(m).Quality); 200 87 } 201 private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {202 Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);203 ParameterizeOperators();204 OnEvaluatorChanged();205 }206 private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {207 ParameterizeOperators();208 }209 private void OperatorsParameter_ValueChanged(object sender, EventArgs e) {210 OnOperatorsChanged();211 }212 private void OperatorsParameter_Value_ItemsAdded(object sender, EventArgs e) {213 OnOperatorsChanged();214 }215 private void OperatorsParameter_Value_ItemsRemoved(object sender, EventArgs e) {216 OnOperatorsChanged();217 }218 private void OperatorsParameter_Value_CollectionReset(object sender, EventArgs e) {219 OnOperatorsChanged();220 }221 #endregion222 88 223 #region Helper 224 private void RegisterEventHandlers() { 225 SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged); 226 EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged); 227 Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged); 228 OperatorsParameter.ValueChanged += new EventHandler(OperatorsParameter_ValueChanged); 229 OperatorsParameter.Value.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<IItem>>(OperatorsParameter_Value_ItemsAdded); 230 OperatorsParameter.Value.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<IItem>>(OperatorsParameter_Value_ItemsRemoved); 231 OperatorsParameter.Value.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<IItem>>(OperatorsParameter_Value_CollectionReset); 232 } 233 private void InitializeOperators() { 234 ItemList<IItem> operators = OperatorsParameter.Value; 235 operators.Add(new BestScopeSolutionAnalyzer()); 236 ParameterizeAnalyzers(); 237 } 238 private void ParameterizeAnalyzers() { 239 BestScopeSolutionAnalyzer.ResultsParameter.ActualName = "Results"; 240 BestScopeSolutionAnalyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName; 241 BestScopeSolutionAnalyzer.BestKnownQualityParameter.ActualName = BestKnownQualityParameter.Name; 242 BestScopeSolutionAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name; 243 } 244 private void ParameterizeEvaluator() { 245 Evaluator.ClientsParameter.ActualName = ClientsParameter.Name; 246 } 247 private void ParameterizeOperators() { 248 // This is a best effort approach to wiring 249 string qualityName = Evaluator.QualityParameter.ActualName; 250 foreach (IOperator op in OperatorsParameter.Value) { 251 foreach (ILookupParameter<DoubleValue> param in op.Parameters.OfType<ILookupParameter<DoubleValue>>()) { 252 if (param.Name.Equals("Quality")) param.ActualName = qualityName; 89 private QualityMessage EvaluateOnNextAvailableClient(SolutionMessage message) { 90 IEvaluationServiceClient client = null; 91 lock (clientLock) { 92 client = Clients.CheckedItems.FirstOrDefault(c => !activeClients.Contains(c)); 93 while (client == null && Clients.CheckedItems.Any()) { 94 Monitor.Wait(clientLock); 95 client = Clients.CheckedItems.FirstOrDefault(c => !activeClients.Contains(c)); 253 96 } 254 foreach (IScopeTreeLookupParameter<DoubleValue> param in op.Parameters.OfType<IScopeTreeLookupParameter<DoubleValue>>()) { 255 if (param.Name.Equals("Quality")) param.ActualName = qualityName; 97 if (client != null) 98 activeClients.Add(client); 99 } 100 try { 101 return client.Evaluate(message, GetQualityMessageExtensions()); 102 } finally { 103 lock (clientLock) { 104 activeClients.Remove(client); 105 Monitor.PulseAll(clientLock); 256 106 } 257 107 } 258 108 } 259 #endregion 109 110 private ExtensionRegistry GetQualityMessageExtensions() { 111 return ExtensionRegistry.CreateInstance(); 112 } 113 114 private SolutionMessage BuildSolutionMessage(Individual individual) { 115 lock (clientLock) { 116 SolutionMessage.Builder protobufBuilder = SolutionMessage.CreateBuilder(); 117 protobufBuilder.SolutionId = 0; 118 var scope = new Scope(); 119 individual.CopyToScope(scope); 120 foreach (var variable in scope.Variables) { 121 try { 122 MessageBuilder.AddToMessage(variable.Value, variable.Name, protobufBuilder); 123 } catch (ArgumentException ex) { 124 throw new InvalidOperationException(string.Format("ERROR while building solution message: Parameter {0} cannot be added to the message", name), ex); 125 } 126 } 127 return protobufBuilder.Build(); 128 } 129 } 260 130 } 261 131 }
Note: See TracChangeset
for help on using the changeset viewer.