1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2015 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 | using HeuristicLab.PluginInfrastructure;
|
---|
36 |
|
---|
37 | namespace HeuristicLab.Problems.ExternalEvaluation {
|
---|
38 | [Item("External Evaluation Problem", "A problem that is evaluated in a different process.")]
|
---|
39 | [StorableClass]
|
---|
40 | [NonDiscoverableType]
|
---|
41 | public sealed class ExternalEvaluationProblem : ParameterizedNamedItem, ISingleObjectiveHeuristicOptimizationProblem, IStorableContent {
|
---|
42 | public string Filename { get; set; }
|
---|
43 |
|
---|
44 | public static new Image StaticItemImage {
|
---|
45 | get { return HeuristicLab.Common.Resources.VSImageLibrary.Type; }
|
---|
46 | }
|
---|
47 |
|
---|
48 | public new ParameterCollection Parameters {
|
---|
49 | get { return base.Parameters; }
|
---|
50 | }
|
---|
51 | IKeyedItemCollection<string, IParameter> IParameterizedItem.Parameters {
|
---|
52 | get { return Parameters; }
|
---|
53 | }
|
---|
54 |
|
---|
55 | #region Parameters
|
---|
56 | public IValueParameter<CheckedItemCollection<IEvaluationServiceClient>> ClientsParameter {
|
---|
57 | get { return (IValueParameter<CheckedItemCollection<IEvaluationServiceClient>>)Parameters["Clients"]; }
|
---|
58 | }
|
---|
59 | public IValueParameter<IExternalEvaluationProblemEvaluator> EvaluatorParameter {
|
---|
60 | get { return (IValueParameter<IExternalEvaluationProblemEvaluator>)Parameters["Evaluator"]; }
|
---|
61 | }
|
---|
62 | public ValueParameter<BoolValue> MaximizationParameter {
|
---|
63 | get { return (ValueParameter<BoolValue>)Parameters["Maximization"]; }
|
---|
64 | }
|
---|
65 | IParameter ISingleObjectiveHeuristicOptimizationProblem.MaximizationParameter {
|
---|
66 | get { return MaximizationParameter; }
|
---|
67 | }
|
---|
68 | public ValueParameter<ISolutionCreator> SolutionCreatorParameter {
|
---|
69 | get { return (ValueParameter<ISolutionCreator>)Parameters["SolutionCreator"]; }
|
---|
70 | }
|
---|
71 | IParameter IHeuristicOptimizationProblem.SolutionCreatorParameter {
|
---|
72 | get { return SolutionCreatorParameter; }
|
---|
73 | }
|
---|
74 | IParameter IHeuristicOptimizationProblem.EvaluatorParameter {
|
---|
75 | get { return EvaluatorParameter; }
|
---|
76 | }
|
---|
77 | public OptionalValueParameter<DoubleValue> BestKnownQualityParameter {
|
---|
78 | get { return (OptionalValueParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
|
---|
79 | }
|
---|
80 | IParameter ISingleObjectiveHeuristicOptimizationProblem.BestKnownQualityParameter {
|
---|
81 | get { return BestKnownQualityParameter; }
|
---|
82 | }
|
---|
83 | public OptionalValueParameter<IScope> BestKnownSolutionParameter {
|
---|
84 | get { return (OptionalValueParameter<IScope>)Parameters["BestKnownSolution"]; }
|
---|
85 | }
|
---|
86 | public ValueParameter<ItemList<IItem>> OperatorsParameter {
|
---|
87 | get { return (ValueParameter<ItemList<IItem>>)Parameters["Operators"]; }
|
---|
88 | }
|
---|
89 | public OptionalValueParameter<EvaluationCache> CacheParameter {
|
---|
90 | get { return (OptionalValueParameter<EvaluationCache>)Parameters["Cache"]; }
|
---|
91 | }
|
---|
92 | #endregion
|
---|
93 |
|
---|
94 | #region Properties
|
---|
95 | public BoolValue Maximization {
|
---|
96 | get { return MaximizationParameter.Value; }
|
---|
97 | set { MaximizationParameter.Value = value; }
|
---|
98 | }
|
---|
99 | public ISolutionCreator SolutionCreator {
|
---|
100 | get { return SolutionCreatorParameter.Value; }
|
---|
101 | set { SolutionCreatorParameter.Value = value; }
|
---|
102 | }
|
---|
103 | ISolutionCreator IHeuristicOptimizationProblem.SolutionCreator {
|
---|
104 | get { return SolutionCreatorParameter.Value; }
|
---|
105 | }
|
---|
106 | public IExternalEvaluationProblemEvaluator Evaluator {
|
---|
107 | get { return EvaluatorParameter.Value; }
|
---|
108 | set { EvaluatorParameter.Value = value; }
|
---|
109 | }
|
---|
110 | ISingleObjectiveEvaluator ISingleObjectiveHeuristicOptimizationProblem.Evaluator {
|
---|
111 | get { return EvaluatorParameter.Value; }
|
---|
112 | }
|
---|
113 | IEvaluator IHeuristicOptimizationProblem.Evaluator {
|
---|
114 | get { return EvaluatorParameter.Value; }
|
---|
115 | }
|
---|
116 | public DoubleValue BestKnownQuality {
|
---|
117 | get { return BestKnownQualityParameter.Value; }
|
---|
118 | set { BestKnownQualityParameter.Value = value; }
|
---|
119 | }
|
---|
120 | public IEnumerable<IItem> Operators {
|
---|
121 | get { return OperatorsParameter.Value; }
|
---|
122 | }
|
---|
123 |
|
---|
124 | public IEnumerable<IParameterizedItem> ExecutionContextItems { get { return new[] { this }; } }
|
---|
125 |
|
---|
126 | private BestScopeSolutionAnalyzer BestScopeSolutionAnalyzer {
|
---|
127 | get { return OperatorsParameter.Value.OfType<BestScopeSolutionAnalyzer>().FirstOrDefault(); }
|
---|
128 | }
|
---|
129 | #endregion
|
---|
130 |
|
---|
131 | [StorableConstructor]
|
---|
132 | private ExternalEvaluationProblem(bool deserializing) : base(deserializing) { }
|
---|
133 | private ExternalEvaluationProblem(ExternalEvaluationProblem original, Cloner cloner)
|
---|
134 | : base(original, cloner) {
|
---|
135 | RegisterEventHandlers();
|
---|
136 | }
|
---|
137 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
138 | return new ExternalEvaluationProblem(this, cloner);
|
---|
139 | }
|
---|
140 | public ExternalEvaluationProblem()
|
---|
141 | : base() {
|
---|
142 | ExternalEvaluator evaluator = new ExternalEvaluator();
|
---|
143 | UserDefinedSolutionCreator solutionCreator = new UserDefinedSolutionCreator();
|
---|
144 |
|
---|
145 | Parameters.Add(new ValueParameter<CheckedItemCollection<IEvaluationServiceClient>>("Clients", "The clients that are used to communicate with the external application.", new CheckedItemCollection<IEvaluationServiceClient>() { new EvaluationServiceClient() }));
|
---|
146 | Parameters.Add(new ValueParameter<IExternalEvaluationProblemEvaluator>("Evaluator", "The evaluator that collects the values to exchange.", evaluator));
|
---|
147 | Parameters.Add(new ValueParameter<ISolutionCreator>("SolutionCreator", "An operator to create the solution components.", solutionCreator));
|
---|
148 | Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to false as most test functions are minimization problems.", new BoolValue(false)));
|
---|
149 | Parameters.Add(new OptionalValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this problem."));
|
---|
150 | Parameters.Add(new OptionalValueParameter<IScope>("BestKnownSolution", "The best known solution for this external evaluation problem."));
|
---|
151 | Parameters.Add(new ValueParameter<ItemList<IItem>>("Operators", "The operators and items that the problem provides to the algorithms.", new ItemList<IItem>()));
|
---|
152 | Parameters.Add(new OptionalValueParameter<EvaluationCache>("Cache", "Cache of previously evaluated solutions."));
|
---|
153 |
|
---|
154 | InitializeOperators();
|
---|
155 | RegisterEventHandlers();
|
---|
156 | }
|
---|
157 | [StorableHook(HookType.AfterDeserialization)]
|
---|
158 | private void AfterDeserialization() {
|
---|
159 | // BackwardsCompatibility3.3
|
---|
160 | #region Backwards compatible code, remove with 3.4
|
---|
161 | if (!Parameters.ContainsKey("Clients")) {
|
---|
162 | Parameters.Add(new ValueParameter<CheckedItemCollection<IEvaluationServiceClient>>("Clients", "The clients that are used to communicate with the external application.", new CheckedItemCollection<IEvaluationServiceClient>() { new EvaluationServiceClient() }));
|
---|
163 | if (Parameters.ContainsKey("Client")) {
|
---|
164 | var client = ((IValueParameter<IEvaluationServiceClient>)Parameters["Client"]).Value;
|
---|
165 | if (client != null)
|
---|
166 | ClientsParameter.Value = new CheckedItemCollection<IEvaluationServiceClient>() { client };
|
---|
167 | Parameters.Remove("Client");
|
---|
168 | }
|
---|
169 | }
|
---|
170 |
|
---|
171 | if (Parameters.ContainsKey("Operators") && Parameters["Operators"] is ValueParameter<ItemList<IOperator>>) {
|
---|
172 | ItemList<IOperator> tmp = ((ValueParameter<ItemList<IOperator>>)Parameters["Operators"]).Value;
|
---|
173 | Parameters.Remove("Operators");
|
---|
174 | Parameters.Add(new ValueParameter<ItemList<IItem>>("Operators", "The operators and items that the problem provides to the algorithms.", new ItemList<IItem>(tmp), false));
|
---|
175 | }
|
---|
176 | #endregion
|
---|
177 | RegisterEventHandlers();
|
---|
178 | }
|
---|
179 |
|
---|
180 | #region Events
|
---|
181 | public event EventHandler SolutionCreatorChanged;
|
---|
182 | private void OnSolutionCreatorChanged() {
|
---|
183 | EventHandler handler = SolutionCreatorChanged;
|
---|
184 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
185 | }
|
---|
186 | public event EventHandler EvaluatorChanged;
|
---|
187 | private void OnEvaluatorChanged() {
|
---|
188 | EventHandler handler = EvaluatorChanged;
|
---|
189 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
190 | }
|
---|
191 | public event EventHandler OperatorsChanged;
|
---|
192 | private void OnOperatorsChanged() {
|
---|
193 | EventHandler handler = OperatorsChanged;
|
---|
194 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
195 | }
|
---|
196 | public event EventHandler Reset;
|
---|
197 | private void OnReset() {
|
---|
198 | EventHandler handler = Reset;
|
---|
199 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
200 | }
|
---|
201 |
|
---|
202 | private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
203 | OnSolutionCreatorChanged();
|
---|
204 | }
|
---|
205 | private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
206 | Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
207 | ParameterizeOperators();
|
---|
208 | OnEvaluatorChanged();
|
---|
209 | }
|
---|
210 | private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
211 | ParameterizeOperators();
|
---|
212 | }
|
---|
213 | private void OperatorsParameter_ValueChanged(object sender, EventArgs e) {
|
---|
214 | OnOperatorsChanged();
|
---|
215 | }
|
---|
216 | private void OperatorsParameter_Value_ItemsAdded(object sender, EventArgs e) {
|
---|
217 | OnOperatorsChanged();
|
---|
218 | }
|
---|
219 | private void OperatorsParameter_Value_ItemsRemoved(object sender, EventArgs e) {
|
---|
220 | OnOperatorsChanged();
|
---|
221 | }
|
---|
222 | private void OperatorsParameter_Value_CollectionReset(object sender, EventArgs e) {
|
---|
223 | OnOperatorsChanged();
|
---|
224 | }
|
---|
225 | #endregion
|
---|
226 |
|
---|
227 | #region Helper
|
---|
228 | private void RegisterEventHandlers() {
|
---|
229 | SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
|
---|
230 | EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
|
---|
231 | Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
232 | OperatorsParameter.ValueChanged += new EventHandler(OperatorsParameter_ValueChanged);
|
---|
233 | OperatorsParameter.Value.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<IItem>>(OperatorsParameter_Value_ItemsAdded);
|
---|
234 | OperatorsParameter.Value.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<IItem>>(OperatorsParameter_Value_ItemsRemoved);
|
---|
235 | OperatorsParameter.Value.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<IItem>>(OperatorsParameter_Value_CollectionReset);
|
---|
236 | }
|
---|
237 | private void InitializeOperators() {
|
---|
238 | ItemList<IItem> operators = OperatorsParameter.Value;
|
---|
239 | operators.Add(new BestScopeSolutionAnalyzer());
|
---|
240 | ParameterizeAnalyzers();
|
---|
241 | }
|
---|
242 | private void ParameterizeAnalyzers() {
|
---|
243 | BestScopeSolutionAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
244 | BestScopeSolutionAnalyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
|
---|
245 | BestScopeSolutionAnalyzer.BestKnownQualityParameter.ActualName = BestKnownQualityParameter.Name;
|
---|
246 | BestScopeSolutionAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;
|
---|
247 | }
|
---|
248 | private void ParameterizeEvaluator() {
|
---|
249 | Evaluator.ClientsParameter.ActualName = ClientsParameter.Name;
|
---|
250 | }
|
---|
251 | private void ParameterizeOperators() {
|
---|
252 | // This is a best effort approach to wiring
|
---|
253 | string qualityName = Evaluator.QualityParameter.ActualName;
|
---|
254 | foreach (IOperator op in OperatorsParameter.Value) {
|
---|
255 | foreach (ILookupParameter<DoubleValue> param in op.Parameters.OfType<ILookupParameter<DoubleValue>>()) {
|
---|
256 | if (param.Name.Equals("Quality")) param.ActualName = qualityName;
|
---|
257 | }
|
---|
258 | foreach (IScopeTreeLookupParameter<DoubleValue> param in op.Parameters.OfType<IScopeTreeLookupParameter<DoubleValue>>()) {
|
---|
259 | if (param.Name.Equals("Quality")) param.ActualName = qualityName;
|
---|
260 | }
|
---|
261 | }
|
---|
262 | }
|
---|
263 | #endregion
|
---|
264 | }
|
---|
265 | }
|
---|