1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Drawing;
|
---|
25 | using System.Linq;
|
---|
26 | using HeuristicLab.Analysis;
|
---|
27 | using HeuristicLab.Collections;
|
---|
28 | using HeuristicLab.Common;
|
---|
29 | using HeuristicLab.Core;
|
---|
30 | using HeuristicLab.Data;
|
---|
31 | using HeuristicLab.Optimization;
|
---|
32 | using HeuristicLab.Optimization.Operators;
|
---|
33 | using HeuristicLab.Parameters;
|
---|
34 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
35 |
|
---|
36 | namespace HeuristicLab.Problems.ExternalEvaluation {
|
---|
37 | [Item("External Evaluation Problem", "A problem that is evaluated in a different process.")]
|
---|
38 | [StorableClass]
|
---|
39 | public sealed class ExternalEvaluationProblem : ParameterizedNamedItem, ISingleObjectiveHeuristicOptimizationProblem, IStorableContent {
|
---|
40 | public string Filename { get; set; }
|
---|
41 |
|
---|
42 | public static new Image StaticItemImage {
|
---|
43 | get { return HeuristicLab.Common.Resources.VSImageLibrary.Type; }
|
---|
44 | }
|
---|
45 |
|
---|
46 | public new ParameterCollection Parameters {
|
---|
47 | get { return base.Parameters; }
|
---|
48 | }
|
---|
49 | IKeyedItemCollection<string, IParameter> IParameterizedItem.Parameters {
|
---|
50 | get { return Parameters; }
|
---|
51 | }
|
---|
52 |
|
---|
53 | #region Parameters
|
---|
54 | public IValueParameter<CheckedItemCollection<IEvaluationServiceClient>> ClientsParameter {
|
---|
55 | get { return (IValueParameter<CheckedItemCollection<IEvaluationServiceClient>>)Parameters["Clients"]; }
|
---|
56 | }
|
---|
57 | public IValueParameter<IExternalEvaluationProblemEvaluator> EvaluatorParameter {
|
---|
58 | get { return (IValueParameter<IExternalEvaluationProblemEvaluator>)Parameters["Evaluator"]; }
|
---|
59 | }
|
---|
60 | public ValueParameter<BoolValue> MaximizationParameter {
|
---|
61 | get { return (ValueParameter<BoolValue>)Parameters["Maximization"]; }
|
---|
62 | }
|
---|
63 | IParameter ISingleObjectiveHeuristicOptimizationProblem.MaximizationParameter {
|
---|
64 | get { return MaximizationParameter; }
|
---|
65 | }
|
---|
66 | public ValueParameter<ISolutionCreator> SolutionCreatorParameter {
|
---|
67 | get { return (ValueParameter<ISolutionCreator>)Parameters["SolutionCreator"]; }
|
---|
68 | }
|
---|
69 | IParameter IHeuristicOptimizationProblem.SolutionCreatorParameter {
|
---|
70 | get { return SolutionCreatorParameter; }
|
---|
71 | }
|
---|
72 | IParameter IHeuristicOptimizationProblem.EvaluatorParameter {
|
---|
73 | get { return EvaluatorParameter; }
|
---|
74 | }
|
---|
75 | public OptionalValueParameter<DoubleValue> BestKnownQualityParameter {
|
---|
76 | get { return (OptionalValueParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
|
---|
77 | }
|
---|
78 | IParameter ISingleObjectiveHeuristicOptimizationProblem.BestKnownQualityParameter {
|
---|
79 | get { return BestKnownQualityParameter; }
|
---|
80 | }
|
---|
81 | public OptionalValueParameter<IScope> BestKnownSolutionParameter {
|
---|
82 | get { return (OptionalValueParameter<IScope>)Parameters["BestKnownSolution"]; }
|
---|
83 | }
|
---|
84 | public ValueParameter<ItemList<IItem>> OperatorsParameter {
|
---|
85 | get { return (ValueParameter<ItemList<IItem>>)Parameters["Operators"]; }
|
---|
86 | }
|
---|
87 | public OptionalValueParameter<EvaluationCache> CacheParameter {
|
---|
88 | get { return (OptionalValueParameter<EvaluationCache>)Parameters["Cache"]; }
|
---|
89 | }
|
---|
90 | #endregion
|
---|
91 |
|
---|
92 | #region Properties
|
---|
93 | public BoolValue Maximization {
|
---|
94 | get { return MaximizationParameter.Value; }
|
---|
95 | set { MaximizationParameter.Value = value; }
|
---|
96 | }
|
---|
97 | public ISolutionCreator SolutionCreator {
|
---|
98 | get { return SolutionCreatorParameter.Value; }
|
---|
99 | set { SolutionCreatorParameter.Value = value; }
|
---|
100 | }
|
---|
101 | ISolutionCreator IHeuristicOptimizationProblem.SolutionCreator {
|
---|
102 | get { return SolutionCreatorParameter.Value; }
|
---|
103 | }
|
---|
104 | public IExternalEvaluationProblemEvaluator Evaluator {
|
---|
105 | get { return EvaluatorParameter.Value; }
|
---|
106 | set { EvaluatorParameter.Value = value; }
|
---|
107 | }
|
---|
108 | ISingleObjectiveEvaluator ISingleObjectiveHeuristicOptimizationProblem.Evaluator {
|
---|
109 | get { return EvaluatorParameter.Value; }
|
---|
110 | }
|
---|
111 | IEvaluator IHeuristicOptimizationProblem.Evaluator {
|
---|
112 | get { return EvaluatorParameter.Value; }
|
---|
113 | }
|
---|
114 | public DoubleValue BestKnownQuality {
|
---|
115 | get { return BestKnownQualityParameter.Value; }
|
---|
116 | set { BestKnownQualityParameter.Value = value; }
|
---|
117 | }
|
---|
118 | public IEnumerable<IItem> Operators {
|
---|
119 | get { return OperatorsParameter.Value; }
|
---|
120 | }
|
---|
121 |
|
---|
122 | public IEnumerable<IParameterizedItem> ExecutionContextItems { get { return new[] { this }; } }
|
---|
123 |
|
---|
124 | private BestScopeSolutionAnalyzer BestScopeSolutionAnalyzer {
|
---|
125 | get { return OperatorsParameter.Value.OfType<BestScopeSolutionAnalyzer>().FirstOrDefault(); }
|
---|
126 | }
|
---|
127 | #endregion
|
---|
128 |
|
---|
129 | [StorableConstructor]
|
---|
130 | private ExternalEvaluationProblem(bool deserializing) : base(deserializing) { }
|
---|
131 | private ExternalEvaluationProblem(ExternalEvaluationProblem original, Cloner cloner)
|
---|
132 | : base(original, cloner) {
|
---|
133 | RegisterEventHandlers();
|
---|
134 | }
|
---|
135 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
136 | return new ExternalEvaluationProblem(this, cloner);
|
---|
137 | }
|
---|
138 | public ExternalEvaluationProblem()
|
---|
139 | : base() {
|
---|
140 | ExternalEvaluator evaluator = new ExternalEvaluator();
|
---|
141 | UserDefinedSolutionCreator solutionCreator = new UserDefinedSolutionCreator();
|
---|
142 |
|
---|
143 | Parameters.Add(new ValueParameter<CheckedItemCollection<IEvaluationServiceClient>>("Clients", "The clients that are used to communicate with the external application.", new CheckedItemCollection<IEvaluationServiceClient>() { new EvaluationServiceClient() }));
|
---|
144 | Parameters.Add(new ValueParameter<IExternalEvaluationProblemEvaluator>("Evaluator", "The evaluator that collects the values to exchange.", evaluator));
|
---|
145 | Parameters.Add(new ValueParameter<ISolutionCreator>("SolutionCreator", "An operator to create the solution components.", solutionCreator));
|
---|
146 | Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to false as most test functions are minimization problems.", new BoolValue(false)));
|
---|
147 | Parameters.Add(new OptionalValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this problem."));
|
---|
148 | Parameters.Add(new OptionalValueParameter<IScope>("BestKnownSolution", "The best known solution for this external evaluation problem."));
|
---|
149 | Parameters.Add(new ValueParameter<ItemList<IItem>>("Operators", "The operators and items that the problem provides to the algorithms.", new ItemList<IItem>()));
|
---|
150 | Parameters.Add(new OptionalValueParameter<EvaluationCache>("Cache", "Cache of previously evaluated solutions."));
|
---|
151 |
|
---|
152 | InitializeOperators();
|
---|
153 | RegisterEventHandlers();
|
---|
154 | }
|
---|
155 | [StorableHook(HookType.AfterDeserialization)]
|
---|
156 | private void AfterDeserialization() {
|
---|
157 | // BackwardsCompatibility3.3
|
---|
158 | #region Backwards compatible code, remove with 3.4
|
---|
159 | if (!Parameters.ContainsKey("Clients")) {
|
---|
160 | Parameters.Add(new ValueParameter<CheckedItemCollection<IEvaluationServiceClient>>("Clients", "The clients that are used to communicate with the external application.", new CheckedItemCollection<IEvaluationServiceClient>() { new EvaluationServiceClient() }));
|
---|
161 | if (Parameters.ContainsKey("Client")) {
|
---|
162 | var client = ((IValueParameter<IEvaluationServiceClient>)Parameters["Client"]).Value;
|
---|
163 | if (client != null)
|
---|
164 | ClientsParameter.Value = new CheckedItemCollection<IEvaluationServiceClient>() { client };
|
---|
165 | Parameters.Remove("Client");
|
---|
166 | }
|
---|
167 | }
|
---|
168 |
|
---|
169 | if (Parameters.ContainsKey("Operators") && Parameters["Operators"] is ValueParameter<ItemList<IOperator>>) {
|
---|
170 | ItemList<IOperator> tmp = ((ValueParameter<ItemList<IOperator>>)Parameters["Operators"]).Value;
|
---|
171 | Parameters.Remove("Operators");
|
---|
172 | Parameters.Add(new ValueParameter<ItemList<IItem>>("Operators", "The operators and items that the problem provides to the algorithms.", new ItemList<IItem>(tmp), false));
|
---|
173 | }
|
---|
174 | #endregion
|
---|
175 | RegisterEventHandlers();
|
---|
176 | }
|
---|
177 |
|
---|
178 | #region Events
|
---|
179 | public event EventHandler SolutionCreatorChanged;
|
---|
180 | private void OnSolutionCreatorChanged() {
|
---|
181 | EventHandler handler = SolutionCreatorChanged;
|
---|
182 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
183 | }
|
---|
184 | public event EventHandler EvaluatorChanged;
|
---|
185 | private void OnEvaluatorChanged() {
|
---|
186 | EventHandler handler = EvaluatorChanged;
|
---|
187 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
188 | }
|
---|
189 | public event EventHandler OperatorsChanged;
|
---|
190 | private void OnOperatorsChanged() {
|
---|
191 | EventHandler handler = OperatorsChanged;
|
---|
192 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
193 | }
|
---|
194 | public event EventHandler Reset;
|
---|
195 | private void OnReset() {
|
---|
196 | EventHandler handler = Reset;
|
---|
197 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
198 | }
|
---|
199 |
|
---|
200 | private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
201 | OnSolutionCreatorChanged();
|
---|
202 | }
|
---|
203 | private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
204 | Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
205 | ParameterizeOperators();
|
---|
206 | OnEvaluatorChanged();
|
---|
207 | }
|
---|
208 | private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
209 | ParameterizeOperators();
|
---|
210 | }
|
---|
211 | private void OperatorsParameter_ValueChanged(object sender, EventArgs e) {
|
---|
212 | OnOperatorsChanged();
|
---|
213 | }
|
---|
214 | private void OperatorsParameter_Value_ItemsAdded(object sender, EventArgs e) {
|
---|
215 | OnOperatorsChanged();
|
---|
216 | }
|
---|
217 | private void OperatorsParameter_Value_ItemsRemoved(object sender, EventArgs e) {
|
---|
218 | OnOperatorsChanged();
|
---|
219 | }
|
---|
220 | private void OperatorsParameter_Value_CollectionReset(object sender, EventArgs e) {
|
---|
221 | OnOperatorsChanged();
|
---|
222 | }
|
---|
223 | #endregion
|
---|
224 |
|
---|
225 | #region Helper
|
---|
226 | private void RegisterEventHandlers() {
|
---|
227 | SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
|
---|
228 | EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
|
---|
229 | Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
230 | OperatorsParameter.ValueChanged += new EventHandler(OperatorsParameter_ValueChanged);
|
---|
231 | OperatorsParameter.Value.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<IItem>>(OperatorsParameter_Value_ItemsAdded);
|
---|
232 | OperatorsParameter.Value.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<IItem>>(OperatorsParameter_Value_ItemsRemoved);
|
---|
233 | OperatorsParameter.Value.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<IItem>>(OperatorsParameter_Value_CollectionReset);
|
---|
234 | }
|
---|
235 | private void InitializeOperators() {
|
---|
236 | ItemList<IItem> operators = OperatorsParameter.Value;
|
---|
237 | operators.Add(new BestScopeSolutionAnalyzer());
|
---|
238 | ParameterizeAnalyzers();
|
---|
239 | }
|
---|
240 | private void ParameterizeAnalyzers() {
|
---|
241 | BestScopeSolutionAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
242 | BestScopeSolutionAnalyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
|
---|
243 | BestScopeSolutionAnalyzer.BestKnownQualityParameter.ActualName = BestKnownQualityParameter.Name;
|
---|
244 | BestScopeSolutionAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
|
---|
245 | }
|
---|
246 | private void ParameterizeEvaluator() {
|
---|
247 | Evaluator.ClientsParameter.ActualName = ClientsParameter.Name;
|
---|
248 | }
|
---|
249 | private void ParameterizeOperators() {
|
---|
250 | // This is a best effort approach to wiring
|
---|
251 | string qualityName = Evaluator.QualityParameter.ActualName;
|
---|
252 | foreach (IOperator op in OperatorsParameter.Value) {
|
---|
253 | foreach (ILookupParameter<DoubleValue> param in op.Parameters.OfType<ILookupParameter<DoubleValue>>()) {
|
---|
254 | if (param.Name.Equals("Quality")) param.ActualName = qualityName;
|
---|
255 | }
|
---|
256 | foreach (IScopeTreeLookupParameter<DoubleValue> param in op.Parameters.OfType<IScopeTreeLookupParameter<DoubleValue>>()) {
|
---|
257 | if (param.Name.Equals("Quality")) param.ActualName = qualityName;
|
---|
258 | }
|
---|
259 | }
|
---|
260 | }
|
---|
261 | #endregion
|
---|
262 | }
|
---|
263 | }
|
---|