[3871] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[16956] | 3 | * Copyright (C) 2002-2019 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;
|
---|
[16956] | 27 | using HEAL.Attic;
|
---|
[3871] | 28 | using HeuristicLab.Collections;
|
---|
| 29 | using HeuristicLab.Common;
|
---|
| 30 | using HeuristicLab.Core;
|
---|
| 31 | using HeuristicLab.Data;
|
---|
| 32 | using HeuristicLab.Parameters;
|
---|
[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)]
|
---|
[16956] | 41 | [StorableType("9F18A098-A8B8-4F70-93CF-79FF1496AC8A")]
|
---|
[5809] | 42 | public sealed class UserDefinedProblem : ParameterizedNamedItem, ISingleObjectiveHeuristicOptimizationProblem, IStorableContent {
|
---|
[4419] | 43 | public string Filename { get; set; }
|
---|
| 44 |
|
---|
[7201] | 45 | public static new Image StaticItemImage {
|
---|
[5287] | 46 | get { return HeuristicLab.Common.Resources.VSImageLibrary.Type; }
|
---|
[3871] | 47 | }
|
---|
| 48 | public new ParameterCollection Parameters {
|
---|
| 49 | get { return base.Parameters; }
|
---|
| 50 | }
|
---|
| 51 | IKeyedItemCollection<string, IParameter> IParameterizedItem.Parameters {
|
---|
| 52 | get { return Parameters; }
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | #region Parameters
|
---|
| 56 | public IValueParameter<ISingleObjectiveEvaluator> EvaluatorParameter {
|
---|
| 57 | get { return (IValueParameter<ISingleObjectiveEvaluator>)Parameters["Evaluator"]; }
|
---|
| 58 | }
|
---|
| 59 | public ValueParameter<BoolValue> MaximizationParameter {
|
---|
| 60 | get { return (ValueParameter<BoolValue>)Parameters["Maximization"]; }
|
---|
| 61 | }
|
---|
[5809] | 62 | IParameter ISingleObjectiveHeuristicOptimizationProblem.MaximizationParameter {
|
---|
[3871] | 63 | get { return MaximizationParameter; }
|
---|
| 64 | }
|
---|
| 65 | public ValueParameter<ISolutionCreator> SolutionCreatorParameter {
|
---|
| 66 | get { return (ValueParameter<ISolutionCreator>)Parameters["SolutionCreator"]; }
|
---|
| 67 | }
|
---|
[5809] | 68 | IParameter IHeuristicOptimizationProblem.SolutionCreatorParameter {
|
---|
[3871] | 69 | get { return SolutionCreatorParameter; }
|
---|
| 70 | }
|
---|
[5809] | 71 | IParameter IHeuristicOptimizationProblem.EvaluatorParameter {
|
---|
[3871] | 72 | get { return EvaluatorParameter; }
|
---|
| 73 | }
|
---|
| 74 | public OptionalValueParameter<DoubleValue> BestKnownQualityParameter {
|
---|
| 75 | get { return (OptionalValueParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
|
---|
| 76 | }
|
---|
[5809] | 77 | IParameter ISingleObjectiveHeuristicOptimizationProblem.BestKnownQualityParameter {
|
---|
[3871] | 78 | get { return BestKnownQualityParameter; }
|
---|
| 79 | }
|
---|
| 80 | public OptionalValueParameter<IScope> BestKnownSolutionParameter {
|
---|
| 81 | get { return (OptionalValueParameter<IScope>)Parameters["BestKnownSolution"]; }
|
---|
| 82 | }
|
---|
[7999] | 83 | public ValueParameter<ItemList<IItem>> OperatorsParameter {
|
---|
| 84 | get { return (ValueParameter<ItemList<IItem>>)Parameters["Operators"]; }
|
---|
[3871] | 85 | }
|
---|
| 86 | #endregion
|
---|
| 87 |
|
---|
| 88 | #region Properties
|
---|
| 89 | public BoolValue Maximization {
|
---|
| 90 | get { return MaximizationParameter.Value; }
|
---|
| 91 | set { MaximizationParameter.Value = value; }
|
---|
| 92 | }
|
---|
| 93 | public ISolutionCreator SolutionCreator {
|
---|
| 94 | get { return SolutionCreatorParameter.Value; }
|
---|
| 95 | set { SolutionCreatorParameter.Value = value; }
|
---|
| 96 | }
|
---|
[5809] | 97 | ISolutionCreator IHeuristicOptimizationProblem.SolutionCreator {
|
---|
[3871] | 98 | get { return SolutionCreatorParameter.Value; }
|
---|
| 99 | }
|
---|
| 100 | public ISingleObjectiveEvaluator Evaluator {
|
---|
| 101 | get { return EvaluatorParameter.Value; }
|
---|
| 102 | set { EvaluatorParameter.Value = value; }
|
---|
| 103 | }
|
---|
[5809] | 104 | ISingleObjectiveEvaluator ISingleObjectiveHeuristicOptimizationProblem.Evaluator {
|
---|
[3871] | 105 | get { return EvaluatorParameter.Value; }
|
---|
| 106 | }
|
---|
[5809] | 107 | IEvaluator IHeuristicOptimizationProblem.Evaluator {
|
---|
[3871] | 108 | get { return EvaluatorParameter.Value; }
|
---|
| 109 | }
|
---|
| 110 | public DoubleValue BestKnownQuality {
|
---|
| 111 | get { return BestKnownQualityParameter.Value; }
|
---|
| 112 | set { BestKnownQualityParameter.Value = value; }
|
---|
| 113 | }
|
---|
[7999] | 114 | public IEnumerable<IItem> Operators {
|
---|
[3871] | 115 | get { return OperatorsParameter.Value; }
|
---|
| 116 | }
|
---|
[11961] | 117 |
|
---|
| 118 | public IEnumerable<IParameterizedItem> ExecutionContextItems {
|
---|
| 119 | get { yield return this; }
|
---|
| 120 | }
|
---|
[3871] | 121 | #endregion
|
---|
| 122 |
|
---|
| 123 | [StorableConstructor]
|
---|
[16956] | 124 | private UserDefinedProblem(StorableConstructorFlag _) : base(_) { }
|
---|
[4722] | 125 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 126 | private void AfterDeserialization() {
|
---|
[7999] | 127 | // BackwardsCompatibility3.3
|
---|
| 128 | #region Backwards compatible code, remove with 3.4
|
---|
| 129 | if (Parameters.ContainsKey("Operators") && Parameters["Operators"] is ValueParameter<ItemList<IOperator>>) {
|
---|
| 130 | ItemList<IOperator> tmp = ((ValueParameter<ItemList<IOperator>>)Parameters["Operators"]).Value;
|
---|
| 131 | Parameters.Remove("Operators");
|
---|
[16956] | 132 | Parameters.Add(new ValueParameter<ItemList<IItem>>("Operators", "The operators and items that the problem provides to the algorithms.", new ItemList<IItem>(tmp)) { GetsCollected = false });
|
---|
[7999] | 133 | }
|
---|
| 134 | #endregion
|
---|
| 135 |
|
---|
[7351] | 136 | RegisterEventHandlers();
|
---|
[4722] | 137 | }
|
---|
[3871] | 138 | public UserDefinedProblem()
|
---|
| 139 | : base() {
|
---|
[3880] | 140 | Parameters.Add(new ValueParameter<ISingleObjectiveEvaluator>("Evaluator", "The evaluator that collects the values to exchange.", new EmptyUserDefinedProblemEvaluator()));
|
---|
[3871] | 141 | Parameters.Add(new ValueParameter<ISolutionCreator>("SolutionCreator", "An operator to create the solution components."));
|
---|
| 142 | Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to false as most test functions are minimization problems.", new BoolValue(false)));
|
---|
| 143 | Parameters.Add(new OptionalValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this problem."));
|
---|
| 144 | Parameters.Add(new OptionalValueParameter<IScope>("BestKnownSolution", "The best known solution for this external evaluation problem."));
|
---|
[7999] | 145 | Parameters.Add(new ValueParameter<ItemList<IItem>>("Operators", "The operators and items that the problem provides to the algorithms.", new ItemList<IItem>()));
|
---|
[3871] | 146 |
|
---|
[7351] | 147 | RegisterEventHandlers();
|
---|
[3871] | 148 | }
|
---|
| 149 |
|
---|
[4722] | 150 | private UserDefinedProblem(UserDefinedProblem original, Cloner cloner)
|
---|
| 151 | : base(original, cloner) {
|
---|
[7351] | 152 | RegisterEventHandlers();
|
---|
[4722] | 153 | }
|
---|
[3871] | 154 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[4722] | 155 | return new UserDefinedProblem(this, cloner);
|
---|
[3871] | 156 | }
|
---|
| 157 |
|
---|
| 158 | #region Events
|
---|
| 159 | public event EventHandler SolutionCreatorChanged;
|
---|
| 160 | private void OnSolutionCreatorChanged() {
|
---|
| 161 | EventHandler handler = SolutionCreatorChanged;
|
---|
| 162 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 163 | }
|
---|
| 164 | public event EventHandler EvaluatorChanged;
|
---|
| 165 | private void OnEvaluatorChanged() {
|
---|
| 166 | EventHandler handler = EvaluatorChanged;
|
---|
| 167 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 168 | }
|
---|
| 169 | public event EventHandler OperatorsChanged;
|
---|
| 170 | private void OnOperatorsChanged() {
|
---|
| 171 | EventHandler handler = OperatorsChanged;
|
---|
| 172 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 173 | }
|
---|
| 174 | public event EventHandler Reset;
|
---|
| 175 | private void OnReset() {
|
---|
| 176 | EventHandler handler = Reset;
|
---|
| 177 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 178 | }
|
---|
| 179 | #endregion
|
---|
| 180 |
|
---|
| 181 | #region Event handlers
|
---|
| 182 | private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 183 | OnSolutionCreatorChanged();
|
---|
| 184 | }
|
---|
| 185 | private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 186 | if (Evaluator != null)
|
---|
| 187 | Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
| 188 | ParameterizeOperators();
|
---|
| 189 | OnEvaluatorChanged();
|
---|
| 190 | }
|
---|
| 191 | private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
| 192 | ParameterizeOperators();
|
---|
| 193 | }
|
---|
| 194 | private void OperatorsParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 195 | OnOperatorsChanged();
|
---|
| 196 | }
|
---|
| 197 | private void OperatorsParameter_Value_ItemsAdded(object sender, EventArgs e) {
|
---|
| 198 | OnOperatorsChanged();
|
---|
| 199 | }
|
---|
| 200 | private void OperatorsParameter_Value_ItemsRemoved(object sender, EventArgs e) {
|
---|
| 201 | OnOperatorsChanged();
|
---|
| 202 | }
|
---|
| 203 | private void OperatorsParameter_Value_CollectionReset(object sender, EventArgs e) {
|
---|
| 204 | OnOperatorsChanged();
|
---|
| 205 | }
|
---|
| 206 | #endregion
|
---|
| 207 |
|
---|
| 208 | #region Helpers
|
---|
[7351] | 209 | private void RegisterEventHandlers() {
|
---|
[3871] | 210 | SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
|
---|
| 211 | EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
|
---|
| 212 | if (Evaluator != null)
|
---|
| 213 | Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
| 214 | OperatorsParameter.ValueChanged += new EventHandler(OperatorsParameter_ValueChanged);
|
---|
[7999] | 215 | OperatorsParameter.Value.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<IItem>>(OperatorsParameter_Value_ItemsAdded);
|
---|
| 216 | OperatorsParameter.Value.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<IItem>>(OperatorsParameter_Value_ItemsRemoved);
|
---|
| 217 | OperatorsParameter.Value.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<IItem>>(OperatorsParameter_Value_CollectionReset);
|
---|
[3871] | 218 | }
|
---|
| 219 |
|
---|
| 220 | private void ParameterizeOperators() {
|
---|
| 221 | // A best effort approach to wiring
|
---|
| 222 | if (Evaluator != null) {
|
---|
| 223 | string qualityName = Evaluator.QualityParameter.ActualName;
|
---|
[7999] | 224 | foreach (IOperator op in OperatorsParameter.Value.OfType<IOperator>()) {
|
---|
[3871] | 225 | foreach (ILookupParameter<DoubleValue> param in op.Parameters.OfType<ILookupParameter<DoubleValue>>()) {
|
---|
| 226 | if (param.Name.Equals("Quality")) param.ActualName = qualityName;
|
---|
| 227 | }
|
---|
| 228 | foreach (IScopeTreeLookupParameter<DoubleValue> param in op.Parameters.OfType<IScopeTreeLookupParameter<DoubleValue>>()) {
|
---|
| 229 | if (param.Name.Equals("Quality")) param.ActualName = qualityName;
|
---|
| 230 | }
|
---|
| 231 | }
|
---|
| 232 | }
|
---|
| 233 | }
|
---|
| 234 | #endregion
|
---|
[3880] | 235 |
|
---|
| 236 | [Item("EmptyUserDefinedProblemEvaluator", "A dummy evaluator that will throw an exception when executed.")]
|
---|
[16956] | 237 | [StorableType("E27E4145-6D44-4A9D-B15A-B0E0528ECD0D")]
|
---|
[5741] | 238 | [NonDiscoverableType]
|
---|
[4722] | 239 | private sealed class EmptyUserDefinedProblemEvaluator : ParameterizedNamedItem, ISingleObjectiveEvaluator {
|
---|
| 240 |
|
---|
| 241 | [StorableConstructor]
|
---|
[16956] | 242 | private EmptyUserDefinedProblemEvaluator(StorableConstructorFlag _) : base(_) { }
|
---|
[4722] | 243 | private EmptyUserDefinedProblemEvaluator(EmptyUserDefinedProblemEvaluator original, Cloner cloner)
|
---|
| 244 | : base(original, cloner) {
|
---|
| 245 | }
|
---|
| 246 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 247 | return new EmptyUserDefinedProblemEvaluator(this, cloner);
|
---|
| 248 | }
|
---|
| 249 |
|
---|
[3880] | 250 | #region ISingleObjectiveEvaluator Members
|
---|
| 251 |
|
---|
| 252 | public ILookupParameter<DoubleValue> QualityParameter {
|
---|
| 253 | get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
| 254 | }
|
---|
| 255 |
|
---|
| 256 | #endregion
|
---|
| 257 |
|
---|
| 258 | public EmptyUserDefinedProblemEvaluator() {
|
---|
| 259 | Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The solution quality."));
|
---|
| 260 | }
|
---|
| 261 |
|
---|
| 262 | #region IOperator Members
|
---|
| 263 |
|
---|
[4722] | 264 | public bool Breakpoint { get; set; }
|
---|
[3880] | 265 |
|
---|
[5193] | 266 | public IOperation Execute(IExecutionContext context, CancellationToken cancellationToken) {
|
---|
[3880] | 267 | throw new InvalidOperationException("Please choose an appropriate evaluation operator.");
|
---|
| 268 | }
|
---|
| 269 |
|
---|
[4722] | 270 | #pragma warning disable 67
|
---|
[3880] | 271 | public event EventHandler BreakpointChanged;
|
---|
| 272 |
|
---|
| 273 | public event EventHandler Executed;
|
---|
[4722] | 274 | #pragma warning restore 67
|
---|
[3880] | 275 |
|
---|
| 276 | #endregion
|
---|
| 277 |
|
---|
[7201] | 278 | public static new Image StaticItemImage {
|
---|
[5287] | 279 | get { return HeuristicLab.Common.Resources.VSImageLibrary.Method; }
|
---|
[3880] | 280 | }
|
---|
[4722] | 281 | }
|
---|
[3871] | 282 | }
|
---|
| 283 | }
|
---|