[3871] | 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]
|
---|
[4419] | 40 | public sealed class UserDefinedProblem : ParameterizedNamedItem, ISingleObjectiveProblem, IStorableContent {
|
---|
| 41 | public string Filename { get; set; }
|
---|
| 42 |
|
---|
[3871] | 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) { }
|
---|
[4722] | 119 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 120 | private void AfterDeserialization() {
|
---|
| 121 | AttachEventHandlers();
|
---|
| 122 | }
|
---|
[3871] | 123 | public UserDefinedProblem()
|
---|
| 124 | : base() {
|
---|
[3880] | 125 | Parameters.Add(new ValueParameter<ISingleObjectiveEvaluator>("Evaluator", "The evaluator that collects the values to exchange.", new EmptyUserDefinedProblemEvaluator()));
|
---|
[3871] | 126 | Parameters.Add(new ValueParameter<ISolutionCreator>("SolutionCreator", "An operator to create the solution components."));
|
---|
| 127 | Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to false as most test functions are minimization problems.", new BoolValue(false)));
|
---|
| 128 | Parameters.Add(new OptionalValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this problem."));
|
---|
| 129 | Parameters.Add(new OptionalValueParameter<IScope>("BestKnownSolution", "The best known solution for this external evaluation problem."));
|
---|
| 130 | Parameters.Add(new ValueParameter<ItemList<IOperator>>("Operators", "The operators that are passed to the algorithm.", new ItemList<IOperator>()));
|
---|
| 131 |
|
---|
| 132 | AttachEventHandlers();
|
---|
| 133 | }
|
---|
| 134 |
|
---|
[4722] | 135 | private UserDefinedProblem(UserDefinedProblem original, Cloner cloner)
|
---|
| 136 | : base(original, cloner) {
|
---|
| 137 | AttachEventHandlers();
|
---|
| 138 | }
|
---|
[3871] | 139 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[4722] | 140 | return new UserDefinedProblem(this, cloner);
|
---|
[3871] | 141 | }
|
---|
| 142 |
|
---|
| 143 | #region Events
|
---|
| 144 | public event EventHandler SolutionCreatorChanged;
|
---|
| 145 | private void OnSolutionCreatorChanged() {
|
---|
| 146 | EventHandler handler = SolutionCreatorChanged;
|
---|
| 147 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 148 | }
|
---|
| 149 | public event EventHandler EvaluatorChanged;
|
---|
| 150 | private void OnEvaluatorChanged() {
|
---|
| 151 | EventHandler handler = EvaluatorChanged;
|
---|
| 152 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 153 | }
|
---|
| 154 | public event EventHandler OperatorsChanged;
|
---|
| 155 | private void OnOperatorsChanged() {
|
---|
| 156 | EventHandler handler = OperatorsChanged;
|
---|
| 157 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 158 | }
|
---|
| 159 | public event EventHandler Reset;
|
---|
| 160 | private void OnReset() {
|
---|
| 161 | EventHandler handler = Reset;
|
---|
| 162 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 163 | }
|
---|
| 164 | #endregion
|
---|
| 165 |
|
---|
| 166 | #region Event handlers
|
---|
| 167 | private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 168 | OnSolutionCreatorChanged();
|
---|
| 169 | }
|
---|
| 170 | private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 171 | if (Evaluator != null)
|
---|
| 172 | Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
| 173 | ParameterizeOperators();
|
---|
| 174 | OnEvaluatorChanged();
|
---|
| 175 | }
|
---|
| 176 | private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
| 177 | ParameterizeOperators();
|
---|
| 178 | }
|
---|
| 179 | private void OperatorsParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 180 | OnOperatorsChanged();
|
---|
| 181 | }
|
---|
| 182 | private void OperatorsParameter_Value_ItemsAdded(object sender, EventArgs e) {
|
---|
| 183 | OnOperatorsChanged();
|
---|
| 184 | }
|
---|
| 185 | private void OperatorsParameter_Value_ItemsRemoved(object sender, EventArgs e) {
|
---|
| 186 | OnOperatorsChanged();
|
---|
| 187 | }
|
---|
| 188 | private void OperatorsParameter_Value_CollectionReset(object sender, EventArgs e) {
|
---|
| 189 | OnOperatorsChanged();
|
---|
| 190 | }
|
---|
| 191 | #endregion
|
---|
| 192 |
|
---|
| 193 | #region Helpers
|
---|
| 194 | private void AttachEventHandlers() {
|
---|
| 195 | SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
|
---|
| 196 | EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
|
---|
| 197 | if (Evaluator != null)
|
---|
| 198 | Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
| 199 | OperatorsParameter.ValueChanged += new EventHandler(OperatorsParameter_ValueChanged);
|
---|
| 200 | OperatorsParameter.Value.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<IOperator>>(OperatorsParameter_Value_ItemsAdded);
|
---|
| 201 | OperatorsParameter.Value.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<IOperator>>(OperatorsParameter_Value_ItemsRemoved);
|
---|
| 202 | OperatorsParameter.Value.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<IOperator>>(OperatorsParameter_Value_CollectionReset);
|
---|
| 203 | }
|
---|
| 204 |
|
---|
| 205 | private void ParameterizeOperators() {
|
---|
| 206 | // A best effort approach to wiring
|
---|
| 207 | if (Evaluator != null) {
|
---|
| 208 | string qualityName = Evaluator.QualityParameter.ActualName;
|
---|
| 209 | foreach (IOperator op in OperatorsParameter.Value) {
|
---|
| 210 | foreach (ILookupParameter<DoubleValue> param in op.Parameters.OfType<ILookupParameter<DoubleValue>>()) {
|
---|
| 211 | if (param.Name.Equals("Quality")) param.ActualName = qualityName;
|
---|
| 212 | }
|
---|
| 213 | foreach (IScopeTreeLookupParameter<DoubleValue> param in op.Parameters.OfType<IScopeTreeLookupParameter<DoubleValue>>()) {
|
---|
| 214 | if (param.Name.Equals("Quality")) param.ActualName = qualityName;
|
---|
| 215 | }
|
---|
| 216 | }
|
---|
| 217 | }
|
---|
| 218 | }
|
---|
| 219 | #endregion
|
---|
[3880] | 220 |
|
---|
| 221 | [Item("EmptyUserDefinedProblemEvaluator", "A dummy evaluator that will throw an exception when executed.")]
|
---|
| 222 | [StorableClass]
|
---|
[4722] | 223 | private sealed class EmptyUserDefinedProblemEvaluator : ParameterizedNamedItem, ISingleObjectiveEvaluator {
|
---|
| 224 |
|
---|
| 225 | [StorableConstructor]
|
---|
| 226 | private EmptyUserDefinedProblemEvaluator(bool deserializing) : base(deserializing) { }
|
---|
| 227 | private EmptyUserDefinedProblemEvaluator(EmptyUserDefinedProblemEvaluator original, Cloner cloner)
|
---|
| 228 | : base(original, cloner) {
|
---|
| 229 | }
|
---|
| 230 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 231 | return new EmptyUserDefinedProblemEvaluator(this, cloner);
|
---|
| 232 | }
|
---|
| 233 |
|
---|
[3880] | 234 | #region ISingleObjectiveEvaluator Members
|
---|
| 235 |
|
---|
| 236 | public ILookupParameter<DoubleValue> QualityParameter {
|
---|
| 237 | get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
| 238 | }
|
---|
| 239 |
|
---|
| 240 | #endregion
|
---|
| 241 |
|
---|
| 242 | public EmptyUserDefinedProblemEvaluator() {
|
---|
| 243 | Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The solution quality."));
|
---|
| 244 | }
|
---|
| 245 |
|
---|
| 246 | #region IOperator Members
|
---|
| 247 |
|
---|
[4722] | 248 | public bool Breakpoint { get; set; }
|
---|
[3880] | 249 |
|
---|
| 250 | public IOperation Execute(IExecutionContext context) {
|
---|
| 251 | throw new InvalidOperationException("Please choose an appropriate evaluation operator.");
|
---|
| 252 | }
|
---|
| 253 |
|
---|
| 254 | public void Abort() {
|
---|
| 255 | throw new InvalidOperationException("Please choose an appropriate evaluation operator.");
|
---|
| 256 | }
|
---|
| 257 |
|
---|
[4722] | 258 | #pragma warning disable 67
|
---|
[3880] | 259 | public event EventHandler BreakpointChanged;
|
---|
| 260 |
|
---|
| 261 | public event EventHandler Executed;
|
---|
[4722] | 262 | #pragma warning restore 67
|
---|
[3880] | 263 |
|
---|
| 264 | #endregion
|
---|
| 265 |
|
---|
| 266 | public override Image ItemImage {
|
---|
| 267 | get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Method; }
|
---|
| 268 | }
|
---|
[4722] | 269 | }
|
---|
[3871] | 270 | }
|
---|
| 271 | }
|
---|