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