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.Collections;
|
---|
27 | using HeuristicLab.Common;
|
---|
28 | using HeuristicLab.Core;
|
---|
29 | using HeuristicLab.Data;
|
---|
30 | using HeuristicLab.Parameters;
|
---|
31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
32 |
|
---|
33 | namespace HeuristicLab.Optimization {
|
---|
34 | /// <summary>
|
---|
35 | /// A problem which can be defined by the user.
|
---|
36 | /// </summary>
|
---|
37 | [Item("User-Defined Problem", "A problem which can be defined by the user.")]
|
---|
38 | [Creatable("Problems")]
|
---|
39 | [StorableClass]
|
---|
40 | public sealed class UserDefinedProblem : 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 | 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<ISingleObjectiveEvaluator> EvaluatorParameter {
|
---|
55 | get { return (IValueParameter<ISingleObjectiveEvaluator>)Parameters["Evaluator"]; }
|
---|
56 | }
|
---|
57 | public ValueParameter<BoolValue> MaximizationParameter {
|
---|
58 | get { return (ValueParameter<BoolValue>)Parameters["Maximization"]; }
|
---|
59 | }
|
---|
60 | IParameter ISingleObjectiveProblem.MaximizationParameter {
|
---|
61 | get { return MaximizationParameter; }
|
---|
62 | }
|
---|
63 | public ValueParameter<ISolutionCreator> SolutionCreatorParameter {
|
---|
64 | get { return (ValueParameter<ISolutionCreator>)Parameters["SolutionCreator"]; }
|
---|
65 | }
|
---|
66 | IParameter IProblem.SolutionCreatorParameter {
|
---|
67 | get { return SolutionCreatorParameter; }
|
---|
68 | }
|
---|
69 | IParameter IProblem.EvaluatorParameter {
|
---|
70 | get { return EvaluatorParameter; }
|
---|
71 | }
|
---|
72 | public OptionalValueParameter<DoubleValue> BestKnownQualityParameter {
|
---|
73 | get { return (OptionalValueParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
|
---|
74 | }
|
---|
75 | IParameter ISingleObjectiveProblem.BestKnownQualityParameter {
|
---|
76 | get { return BestKnownQualityParameter; }
|
---|
77 | }
|
---|
78 | public OptionalValueParameter<IScope> BestKnownSolutionParameter {
|
---|
79 | get { return (OptionalValueParameter<IScope>)Parameters["BestKnownSolution"]; }
|
---|
80 | }
|
---|
81 | public ValueParameter<ItemList<IOperator>> OperatorsParameter {
|
---|
82 | get { return (ValueParameter<ItemList<IOperator>>)Parameters["Operators"]; }
|
---|
83 | }
|
---|
84 | #endregion
|
---|
85 |
|
---|
86 | #region Properties
|
---|
87 | public BoolValue Maximization {
|
---|
88 | get { return MaximizationParameter.Value; }
|
---|
89 | set { MaximizationParameter.Value = value; }
|
---|
90 | }
|
---|
91 | public ISolutionCreator SolutionCreator {
|
---|
92 | get { return SolutionCreatorParameter.Value; }
|
---|
93 | set { SolutionCreatorParameter.Value = value; }
|
---|
94 | }
|
---|
95 | ISolutionCreator IProblem.SolutionCreator {
|
---|
96 | get { return SolutionCreatorParameter.Value; }
|
---|
97 | }
|
---|
98 | public ISingleObjectiveEvaluator Evaluator {
|
---|
99 | get { return EvaluatorParameter.Value; }
|
---|
100 | set { EvaluatorParameter.Value = value; }
|
---|
101 | }
|
---|
102 | ISingleObjectiveEvaluator ISingleObjectiveProblem.Evaluator {
|
---|
103 | get { return EvaluatorParameter.Value; }
|
---|
104 | }
|
---|
105 | IEvaluator IProblem.Evaluator {
|
---|
106 | get { return EvaluatorParameter.Value; }
|
---|
107 | }
|
---|
108 | public DoubleValue BestKnownQuality {
|
---|
109 | get { return BestKnownQualityParameter.Value; }
|
---|
110 | set { BestKnownQualityParameter.Value = value; }
|
---|
111 | }
|
---|
112 | public IEnumerable<IOperator> Operators {
|
---|
113 | get { return OperatorsParameter.Value; }
|
---|
114 | }
|
---|
115 | #endregion
|
---|
116 |
|
---|
117 | [StorableConstructor]
|
---|
118 | private UserDefinedProblem(bool deserializing) : base(deserializing) { }
|
---|
119 | public UserDefinedProblem()
|
---|
120 | : base() {
|
---|
121 | Parameters.Add(new ValueParameter<ISingleObjectiveEvaluator>("Evaluator", "The evaluator that collects the values to exchange.", new EmptyUserDefinedProblemEvaluator()));
|
---|
122 | Parameters.Add(new ValueParameter<ISolutionCreator>("SolutionCreator", "An operator to create the solution components."));
|
---|
123 | Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to false as most test functions are minimization problems.", new BoolValue(false)));
|
---|
124 | Parameters.Add(new OptionalValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this problem."));
|
---|
125 | Parameters.Add(new OptionalValueParameter<IScope>("BestKnownSolution", "The best known solution for this external evaluation problem."));
|
---|
126 | Parameters.Add(new ValueParameter<ItemList<IOperator>>("Operators", "The operators that are passed to the algorithm.", new ItemList<IOperator>()));
|
---|
127 |
|
---|
128 | AttachEventHandlers();
|
---|
129 | }
|
---|
130 |
|
---|
131 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
132 | UserDefinedProblem clone = (UserDefinedProblem)base.Clone(cloner);
|
---|
133 | clone.AttachEventHandlers();
|
---|
134 | return clone;
|
---|
135 | }
|
---|
136 |
|
---|
137 | #region Events
|
---|
138 | public event EventHandler SolutionCreatorChanged;
|
---|
139 | private void OnSolutionCreatorChanged() {
|
---|
140 | EventHandler handler = SolutionCreatorChanged;
|
---|
141 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
142 | }
|
---|
143 | public event EventHandler EvaluatorChanged;
|
---|
144 | private void OnEvaluatorChanged() {
|
---|
145 | EventHandler handler = EvaluatorChanged;
|
---|
146 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
147 | }
|
---|
148 | public event EventHandler OperatorsChanged;
|
---|
149 | private void OnOperatorsChanged() {
|
---|
150 | EventHandler handler = OperatorsChanged;
|
---|
151 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
152 | }
|
---|
153 | public event EventHandler Reset;
|
---|
154 | private void OnReset() {
|
---|
155 | EventHandler handler = Reset;
|
---|
156 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
157 | }
|
---|
158 | #endregion
|
---|
159 |
|
---|
160 | #region Event handlers
|
---|
161 | private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
162 | OnSolutionCreatorChanged();
|
---|
163 | }
|
---|
164 | private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
165 | if (Evaluator != null)
|
---|
166 | Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
167 | ParameterizeOperators();
|
---|
168 | OnEvaluatorChanged();
|
---|
169 | }
|
---|
170 | private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
171 | ParameterizeOperators();
|
---|
172 | }
|
---|
173 | private void OperatorsParameter_ValueChanged(object sender, EventArgs e) {
|
---|
174 | OnOperatorsChanged();
|
---|
175 | }
|
---|
176 | private void OperatorsParameter_Value_ItemsAdded(object sender, EventArgs e) {
|
---|
177 | OnOperatorsChanged();
|
---|
178 | }
|
---|
179 | private void OperatorsParameter_Value_ItemsRemoved(object sender, EventArgs e) {
|
---|
180 | OnOperatorsChanged();
|
---|
181 | }
|
---|
182 | private void OperatorsParameter_Value_CollectionReset(object sender, EventArgs e) {
|
---|
183 | OnOperatorsChanged();
|
---|
184 | }
|
---|
185 | #endregion
|
---|
186 |
|
---|
187 | #region Helpers
|
---|
188 | [StorableHook(HookType.AfterDeserialization)]
|
---|
189 | private void AttachEventHandlers() {
|
---|
190 | SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
|
---|
191 | EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
|
---|
192 | if (Evaluator != null)
|
---|
193 | Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
194 | OperatorsParameter.ValueChanged += new EventHandler(OperatorsParameter_ValueChanged);
|
---|
195 | OperatorsParameter.Value.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<IOperator>>(OperatorsParameter_Value_ItemsAdded);
|
---|
196 | OperatorsParameter.Value.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<IOperator>>(OperatorsParameter_Value_ItemsRemoved);
|
---|
197 | OperatorsParameter.Value.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<IOperator>>(OperatorsParameter_Value_CollectionReset);
|
---|
198 | }
|
---|
199 |
|
---|
200 | private void ParameterizeOperators() {
|
---|
201 | // A best effort approach to wiring
|
---|
202 | if (Evaluator != null) {
|
---|
203 | string qualityName = Evaluator.QualityParameter.ActualName;
|
---|
204 | foreach (IOperator op in OperatorsParameter.Value) {
|
---|
205 | foreach (ILookupParameter<DoubleValue> param in op.Parameters.OfType<ILookupParameter<DoubleValue>>()) {
|
---|
206 | if (param.Name.Equals("Quality")) param.ActualName = qualityName;
|
---|
207 | }
|
---|
208 | foreach (IScopeTreeLookupParameter<DoubleValue> param in op.Parameters.OfType<IScopeTreeLookupParameter<DoubleValue>>()) {
|
---|
209 | if (param.Name.Equals("Quality")) param.ActualName = qualityName;
|
---|
210 | }
|
---|
211 | }
|
---|
212 | }
|
---|
213 | }
|
---|
214 | #endregion
|
---|
215 |
|
---|
216 | [Item("EmptyUserDefinedProblemEvaluator", "A dummy evaluator that will throw an exception when executed.")]
|
---|
217 | [StorableClass]
|
---|
218 | private class EmptyUserDefinedProblemEvaluator : ParameterizedNamedItem, ISingleObjectiveEvaluator {
|
---|
219 | #region ISingleObjectiveEvaluator Members
|
---|
220 |
|
---|
221 | public ILookupParameter<DoubleValue> QualityParameter {
|
---|
222 | get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
223 | }
|
---|
224 |
|
---|
225 | #endregion
|
---|
226 |
|
---|
227 | public EmptyUserDefinedProblemEvaluator() {
|
---|
228 | Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The solution quality."));
|
---|
229 | }
|
---|
230 |
|
---|
231 | #region IOperator Members
|
---|
232 |
|
---|
233 | public bool Breakpoint {
|
---|
234 | get;
|
---|
235 | set;
|
---|
236 | }
|
---|
237 |
|
---|
238 | public IOperation Execute(IExecutionContext context) {
|
---|
239 | throw new InvalidOperationException("Please choose an appropriate evaluation operator.");
|
---|
240 | }
|
---|
241 |
|
---|
242 | public void Abort() {
|
---|
243 | throw new InvalidOperationException("Please choose an appropriate evaluation operator.");
|
---|
244 | }
|
---|
245 |
|
---|
246 | #pragma warning disable 67
|
---|
247 | public event EventHandler BreakpointChanged;
|
---|
248 |
|
---|
249 | public event EventHandler Executed;
|
---|
250 | #pragma warning restore 67
|
---|
251 |
|
---|
252 | #endregion
|
---|
253 |
|
---|
254 | public override Image ItemImage {
|
---|
255 | get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Method; }
|
---|
256 | }
|
---|
257 | }
|
---|
258 | }
|
---|
259 | }
|
---|