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