[3871] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[12012] | 3 | * Copyright (C) 2002-2015 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;
|
---|
[5741] | 33 | using HeuristicLab.PluginInfrastructure;
|
---|
[3871] | 34 |
|
---|
| 35 | namespace HeuristicLab.Optimization {
|
---|
| 36 | /// <summary>
|
---|
| 37 | /// A problem which can be defined by the user.
|
---|
| 38 | /// </summary>
|
---|
| 39 | [Item("User-Defined Problem", "A problem which can be defined by the user.")]
|
---|
[12504] | 40 | [Creatable(CreatableAttribute.Categories.Problems, Priority = 120)]
|
---|
[3871] | 41 | [StorableClass]
|
---|
[5809] | 42 | public sealed class UserDefinedProblem : ParameterizedNamedItem, ISingleObjectiveHeuristicOptimizationProblem, IStorableContent {
|
---|
[4419] | 43 | public string Filename { get; set; }
|
---|
| 44 |
|
---|
[13656] | 45 | public static new Image StaticItemImage
|
---|
| 46 | {
|
---|
| 47 | get { return new Bitmap(25, 25); }
|
---|
[3871] | 48 | }
|
---|
[13656] | 49 | public new ParameterCollection Parameters
|
---|
| 50 | {
|
---|
[3871] | 51 | get { return base.Parameters; }
|
---|
| 52 | }
|
---|
[13656] | 53 | IKeyedItemCollection<string, IParameter> IParameterizedItem.Parameters
|
---|
| 54 | {
|
---|
[3871] | 55 | get { return Parameters; }
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | #region Parameters
|
---|
[13656] | 59 | public IValueParameter<ISingleObjectiveEvaluator> EvaluatorParameter
|
---|
| 60 | {
|
---|
[3871] | 61 | get { return (IValueParameter<ISingleObjectiveEvaluator>)Parameters["Evaluator"]; }
|
---|
| 62 | }
|
---|
[13656] | 63 | public ValueParameter<BoolValue> MaximizationParameter
|
---|
| 64 | {
|
---|
[3871] | 65 | get { return (ValueParameter<BoolValue>)Parameters["Maximization"]; }
|
---|
| 66 | }
|
---|
[13656] | 67 | IParameter ISingleObjectiveHeuristicOptimizationProblem.MaximizationParameter
|
---|
| 68 | {
|
---|
[3871] | 69 | get { return MaximizationParameter; }
|
---|
| 70 | }
|
---|
[13656] | 71 | public ValueParameter<ISolutionCreator> SolutionCreatorParameter
|
---|
| 72 | {
|
---|
[3871] | 73 | get { return (ValueParameter<ISolutionCreator>)Parameters["SolutionCreator"]; }
|
---|
| 74 | }
|
---|
[13656] | 75 | IParameter IHeuristicOptimizationProblem.SolutionCreatorParameter
|
---|
| 76 | {
|
---|
[3871] | 77 | get { return SolutionCreatorParameter; }
|
---|
| 78 | }
|
---|
[13656] | 79 | IParameter IHeuristicOptimizationProblem.EvaluatorParameter
|
---|
| 80 | {
|
---|
[3871] | 81 | get { return EvaluatorParameter; }
|
---|
| 82 | }
|
---|
[13656] | 83 | public OptionalValueParameter<DoubleValue> BestKnownQualityParameter
|
---|
| 84 | {
|
---|
[3871] | 85 | get { return (OptionalValueParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
|
---|
| 86 | }
|
---|
[13656] | 87 | IParameter ISingleObjectiveHeuristicOptimizationProblem.BestKnownQualityParameter
|
---|
| 88 | {
|
---|
[3871] | 89 | get { return BestKnownQualityParameter; }
|
---|
| 90 | }
|
---|
[13656] | 91 | public OptionalValueParameter<IScope> BestKnownSolutionParameter
|
---|
| 92 | {
|
---|
[3871] | 93 | get { return (OptionalValueParameter<IScope>)Parameters["BestKnownSolution"]; }
|
---|
| 94 | }
|
---|
[13656] | 95 | public ValueParameter<ItemList<IItem>> OperatorsParameter
|
---|
| 96 | {
|
---|
[7999] | 97 | get { return (ValueParameter<ItemList<IItem>>)Parameters["Operators"]; }
|
---|
[3871] | 98 | }
|
---|
| 99 | #endregion
|
---|
| 100 |
|
---|
| 101 | #region Properties
|
---|
[13656] | 102 | public BoolValue Maximization
|
---|
| 103 | {
|
---|
[3871] | 104 | get { return MaximizationParameter.Value; }
|
---|
| 105 | set { MaximizationParameter.Value = value; }
|
---|
| 106 | }
|
---|
[13656] | 107 | public ISolutionCreator SolutionCreator
|
---|
| 108 | {
|
---|
[3871] | 109 | get { return SolutionCreatorParameter.Value; }
|
---|
| 110 | set { SolutionCreatorParameter.Value = value; }
|
---|
| 111 | }
|
---|
[13656] | 112 | ISolutionCreator IHeuristicOptimizationProblem.SolutionCreator
|
---|
| 113 | {
|
---|
[3871] | 114 | get { return SolutionCreatorParameter.Value; }
|
---|
| 115 | }
|
---|
[13656] | 116 | public ISingleObjectiveEvaluator Evaluator
|
---|
| 117 | {
|
---|
[3871] | 118 | get { return EvaluatorParameter.Value; }
|
---|
| 119 | set { EvaluatorParameter.Value = value; }
|
---|
| 120 | }
|
---|
[13656] | 121 | ISingleObjectiveEvaluator ISingleObjectiveHeuristicOptimizationProblem.Evaluator
|
---|
| 122 | {
|
---|
[3871] | 123 | get { return EvaluatorParameter.Value; }
|
---|
| 124 | }
|
---|
[13656] | 125 | IEvaluator IHeuristicOptimizationProblem.Evaluator
|
---|
| 126 | {
|
---|
[3871] | 127 | get { return EvaluatorParameter.Value; }
|
---|
| 128 | }
|
---|
[13656] | 129 | public DoubleValue BestKnownQuality
|
---|
| 130 | {
|
---|
[3871] | 131 | get { return BestKnownQualityParameter.Value; }
|
---|
| 132 | set { BestKnownQualityParameter.Value = value; }
|
---|
| 133 | }
|
---|
[13656] | 134 | public IEnumerable<IItem> Operators
|
---|
| 135 | {
|
---|
[3871] | 136 | get { return OperatorsParameter.Value; }
|
---|
| 137 | }
|
---|
[11961] | 138 |
|
---|
[13656] | 139 | public IEnumerable<IParameterizedItem> ExecutionContextItems
|
---|
| 140 | {
|
---|
[11961] | 141 | get { yield return this; }
|
---|
| 142 | }
|
---|
[3871] | 143 | #endregion
|
---|
| 144 |
|
---|
| 145 | [StorableConstructor]
|
---|
| 146 | private UserDefinedProblem(bool deserializing) : base(deserializing) { }
|
---|
[4722] | 147 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 148 | private void AfterDeserialization() {
|
---|
[7999] | 149 | // BackwardsCompatibility3.3
|
---|
| 150 | #region Backwards compatible code, remove with 3.4
|
---|
| 151 | if (Parameters.ContainsKey("Operators") && Parameters["Operators"] is ValueParameter<ItemList<IOperator>>) {
|
---|
| 152 | ItemList<IOperator> tmp = ((ValueParameter<ItemList<IOperator>>)Parameters["Operators"]).Value;
|
---|
| 153 | Parameters.Remove("Operators");
|
---|
| 154 | Parameters.Add(new ValueParameter<ItemList<IItem>>("Operators", "The operators and items that the problem provides to the algorithms.", new ItemList<IItem>(tmp), false));
|
---|
| 155 | }
|
---|
| 156 | #endregion
|
---|
| 157 |
|
---|
[7351] | 158 | RegisterEventHandlers();
|
---|
[4722] | 159 | }
|
---|
[3871] | 160 | public UserDefinedProblem()
|
---|
| 161 | : base() {
|
---|
[3880] | 162 | Parameters.Add(new ValueParameter<ISingleObjectiveEvaluator>("Evaluator", "The evaluator that collects the values to exchange.", new EmptyUserDefinedProblemEvaluator()));
|
---|
[3871] | 163 | Parameters.Add(new ValueParameter<ISolutionCreator>("SolutionCreator", "An operator to create the solution components."));
|
---|
| 164 | Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to false as most test functions are minimization problems.", new BoolValue(false)));
|
---|
| 165 | Parameters.Add(new OptionalValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this problem."));
|
---|
| 166 | Parameters.Add(new OptionalValueParameter<IScope>("BestKnownSolution", "The best known solution for this external evaluation problem."));
|
---|
[7999] | 167 | Parameters.Add(new ValueParameter<ItemList<IItem>>("Operators", "The operators and items that the problem provides to the algorithms.", new ItemList<IItem>()));
|
---|
[3871] | 168 |
|
---|
[7351] | 169 | RegisterEventHandlers();
|
---|
[3871] | 170 | }
|
---|
| 171 |
|
---|
[4722] | 172 | private UserDefinedProblem(UserDefinedProblem original, Cloner cloner)
|
---|
| 173 | : base(original, cloner) {
|
---|
[7351] | 174 | RegisterEventHandlers();
|
---|
[4722] | 175 | }
|
---|
[3871] | 176 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[4722] | 177 | return new UserDefinedProblem(this, cloner);
|
---|
[3871] | 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 | #endregion
|
---|
| 202 |
|
---|
| 203 | #region Event handlers
|
---|
| 204 | private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 205 | OnSolutionCreatorChanged();
|
---|
| 206 | }
|
---|
| 207 | private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 208 | if (Evaluator != null)
|
---|
| 209 | Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
| 210 | ParameterizeOperators();
|
---|
| 211 | OnEvaluatorChanged();
|
---|
| 212 | }
|
---|
| 213 | private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
| 214 | ParameterizeOperators();
|
---|
| 215 | }
|
---|
| 216 | private void OperatorsParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 217 | OnOperatorsChanged();
|
---|
| 218 | }
|
---|
| 219 | private void OperatorsParameter_Value_ItemsAdded(object sender, EventArgs e) {
|
---|
| 220 | OnOperatorsChanged();
|
---|
| 221 | }
|
---|
| 222 | private void OperatorsParameter_Value_ItemsRemoved(object sender, EventArgs e) {
|
---|
| 223 | OnOperatorsChanged();
|
---|
| 224 | }
|
---|
| 225 | private void OperatorsParameter_Value_CollectionReset(object sender, EventArgs e) {
|
---|
| 226 | OnOperatorsChanged();
|
---|
| 227 | }
|
---|
| 228 | #endregion
|
---|
| 229 |
|
---|
| 230 | #region Helpers
|
---|
[7351] | 231 | private void RegisterEventHandlers() {
|
---|
[3871] | 232 | SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
|
---|
| 233 | EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
|
---|
| 234 | if (Evaluator != null)
|
---|
| 235 | Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
| 236 | OperatorsParameter.ValueChanged += new EventHandler(OperatorsParameter_ValueChanged);
|
---|
[7999] | 237 | OperatorsParameter.Value.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<IItem>>(OperatorsParameter_Value_ItemsAdded);
|
---|
| 238 | OperatorsParameter.Value.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<IItem>>(OperatorsParameter_Value_ItemsRemoved);
|
---|
| 239 | OperatorsParameter.Value.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<IItem>>(OperatorsParameter_Value_CollectionReset);
|
---|
[3871] | 240 | }
|
---|
| 241 |
|
---|
| 242 | private void ParameterizeOperators() {
|
---|
| 243 | // A best effort approach to wiring
|
---|
| 244 | if (Evaluator != null) {
|
---|
| 245 | string qualityName = Evaluator.QualityParameter.ActualName;
|
---|
[7999] | 246 | foreach (IOperator op in OperatorsParameter.Value.OfType<IOperator>()) {
|
---|
[3871] | 247 | foreach (ILookupParameter<DoubleValue> param in op.Parameters.OfType<ILookupParameter<DoubleValue>>()) {
|
---|
| 248 | if (param.Name.Equals("Quality")) param.ActualName = qualityName;
|
---|
| 249 | }
|
---|
| 250 | foreach (IScopeTreeLookupParameter<DoubleValue> param in op.Parameters.OfType<IScopeTreeLookupParameter<DoubleValue>>()) {
|
---|
| 251 | if (param.Name.Equals("Quality")) param.ActualName = qualityName;
|
---|
| 252 | }
|
---|
| 253 | }
|
---|
| 254 | }
|
---|
| 255 | }
|
---|
| 256 | #endregion
|
---|
[3880] | 257 |
|
---|
| 258 | [Item("EmptyUserDefinedProblemEvaluator", "A dummy evaluator that will throw an exception when executed.")]
|
---|
| 259 | [StorableClass]
|
---|
[5741] | 260 | [NonDiscoverableType]
|
---|
[4722] | 261 | private sealed class EmptyUserDefinedProblemEvaluator : ParameterizedNamedItem, ISingleObjectiveEvaluator {
|
---|
| 262 |
|
---|
| 263 | [StorableConstructor]
|
---|
| 264 | private EmptyUserDefinedProblemEvaluator(bool deserializing) : base(deserializing) { }
|
---|
| 265 | private EmptyUserDefinedProblemEvaluator(EmptyUserDefinedProblemEvaluator original, Cloner cloner)
|
---|
| 266 | : base(original, cloner) {
|
---|
| 267 | }
|
---|
| 268 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 269 | return new EmptyUserDefinedProblemEvaluator(this, cloner);
|
---|
| 270 | }
|
---|
| 271 |
|
---|
[3880] | 272 | #region ISingleObjectiveEvaluator Members
|
---|
| 273 |
|
---|
[13656] | 274 | public ILookupParameter<DoubleValue> QualityParameter
|
---|
| 275 | {
|
---|
[3880] | 276 | get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
| 277 | }
|
---|
| 278 |
|
---|
| 279 | #endregion
|
---|
| 280 |
|
---|
| 281 | public EmptyUserDefinedProblemEvaluator() {
|
---|
| 282 | Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The solution quality."));
|
---|
| 283 | }
|
---|
| 284 |
|
---|
| 285 | #region IOperator Members
|
---|
| 286 |
|
---|
[4722] | 287 | public bool Breakpoint { get; set; }
|
---|
[3880] | 288 |
|
---|
[5193] | 289 | public IOperation Execute(IExecutionContext context, CancellationToken cancellationToken) {
|
---|
[3880] | 290 | throw new InvalidOperationException("Please choose an appropriate evaluation operator.");
|
---|
| 291 | }
|
---|
| 292 |
|
---|
[4722] | 293 | #pragma warning disable 67
|
---|
[3880] | 294 | public event EventHandler BreakpointChanged;
|
---|
| 295 |
|
---|
| 296 | public event EventHandler Executed;
|
---|
[4722] | 297 | #pragma warning restore 67
|
---|
[3880] | 298 |
|
---|
| 299 | #endregion
|
---|
| 300 |
|
---|
[13656] | 301 | public static new Image StaticItemImage
|
---|
| 302 | {
|
---|
| 303 | get { return new Bitmap(25, 25); }
|
---|
[3880] | 304 | }
|
---|
[4722] | 305 | }
|
---|
[3871] | 306 | }
|
---|
| 307 | }
|
---|