[4323] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17226] | 3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[4323] | 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;
|
---|
[16946] | 25 | using HEAL.Attic;
|
---|
[4323] | 26 | using HeuristicLab.Collections;
|
---|
| 27 | using HeuristicLab.Common;
|
---|
| 28 | using HeuristicLab.Core;
|
---|
[7579] | 29 | using HeuristicLab.Data;
|
---|
[6938] | 30 | using HeuristicLab.Parameters;
|
---|
[4323] | 31 |
|
---|
[4564] | 32 | namespace HeuristicLab.Optimization {
|
---|
[17513] | 33 | [StorableType("C213CE21-A970-4886-BC4C-9790B9897738")]
|
---|
| 34 | public abstract class Problem : ParameterizedNamedItem, IProblem {
|
---|
| 35 | public string Filename { get; set; }
|
---|
| 36 |
|
---|
| 37 | public static new Image StaticItemImage {
|
---|
| 38 | get { return HeuristicLab.Common.Resources.VSImageLibrary.Type; }
|
---|
| 39 | }
|
---|
| 40 |
|
---|
[17612] | 41 | [Storable]
|
---|
[17613] | 42 | public ResultCollection Results { get; private set; } = new ResultCollection();
|
---|
[17513] | 43 |
|
---|
| 44 | [StorableConstructor]
|
---|
| 45 | protected Problem(StorableConstructorFlag _) : base(_) { }
|
---|
[17612] | 46 | protected Problem(Problem original, Cloner cloner) : base(original, cloner) {
|
---|
| 47 | Results = cloner.Clone(original.Results);
|
---|
| 48 | }
|
---|
[17513] | 49 | public Problem() : base() { }
|
---|
| 50 |
|
---|
| 51 | protected override IEnumerable<KeyValuePair<string, IItem>> GetCollectedValues(IValueParameter param) {
|
---|
| 52 | var children = base.GetCollectedValues(param);
|
---|
| 53 | foreach (var child in children) {
|
---|
| 54 | if (child.Value is IOperator)
|
---|
| 55 | yield return new KeyValuePair<string, IItem>(child.Key, new StringValue(((IOperator)child.Value).Name));
|
---|
| 56 | else yield return child;
|
---|
| 57 | }
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | public event EventHandler Reset;
|
---|
| 61 | protected virtual void OnReset() {
|
---|
| 62 | EventHandler handler = Reset;
|
---|
| 63 | if (handler != null)
|
---|
| 64 | handler(this, EventArgs.Empty);
|
---|
| 65 | }
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 |
|
---|
| 69 |
|
---|
[4596] | 70 | [Item("Problem", "Represents the base class for a problem.")]
|
---|
[16723] | 71 | [StorableType("6DC97432-9BD1-4304-802A-1FC48A0E0468")]
|
---|
[17513] | 72 | public abstract class EncodedProblem : Problem, IEncodedProblem {
|
---|
[17334] | 73 |
|
---|
[8004] | 74 | private const string OperatorsParameterName = "Operators";
|
---|
[7999] | 75 | public IFixedValueParameter<ItemCollection<IItem>> OperatorsParameter {
|
---|
| 76 | get { return (IFixedValueParameter<ItemCollection<IItem>>)Parameters[OperatorsParameterName]; }
|
---|
[6938] | 77 | }
|
---|
| 78 |
|
---|
[4323] | 79 | [StorableConstructor]
|
---|
[17513] | 80 | protected EncodedProblem(StorableConstructorFlag _) : base(_) { }
|
---|
| 81 | protected EncodedProblem(EncodedProblem original, Cloner cloner)
|
---|
[4722] | 82 | : base(original, cloner) {
|
---|
| 83 | RegisterEventHandlers();
|
---|
| 84 | }
|
---|
| 85 |
|
---|
[17513] | 86 | protected EncodedProblem()
|
---|
[4323] | 87 | : base() {
|
---|
[16946] | 88 | Parameters.Add(new FixedValueParameter<ItemCollection<IItem>>(OperatorsParameterName, "The operators and items that the problem provides to the algorithms.", new ItemCollection<IItem>()) { GetsCollected = false });
|
---|
[6938] | 89 | OperatorsParameter.Hidden = true;
|
---|
[4563] | 90 | RegisterEventHandlers();
|
---|
[4323] | 91 | }
|
---|
| 92 |
|
---|
| 93 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 94 | private void AfterDeserialization() {
|
---|
[7999] | 95 | // BackwardsCompatibility3.3
|
---|
| 96 | #region Backwards compatible code, remove with 3.4
|
---|
[8004] | 97 | IParameter operatorsParam;
|
---|
| 98 | if (Parameters.TryGetValue(OperatorsParameterName, out operatorsParam)) {
|
---|
| 99 | var operators = operatorsParam.ActualValue as OperatorCollection;
|
---|
| 100 | if (operators != null) {
|
---|
| 101 | Parameters.Remove(OperatorsParameterName);
|
---|
[16946] | 102 | Parameters.Add(new FixedValueParameter<ItemCollection<IItem>>(OperatorsParameterName, "The operators and items that the problem provides to the algorithms.", new ItemCollection<IItem>(operators)) { GetsCollected = false });
|
---|
[8004] | 103 | OperatorsParameter.Hidden = true;
|
---|
| 104 | }
|
---|
[7999] | 105 | }
|
---|
| 106 | #endregion
|
---|
| 107 |
|
---|
[4563] | 108 | RegisterEventHandlers();
|
---|
[4323] | 109 | }
|
---|
| 110 |
|
---|
[4563] | 111 | private void RegisterEventHandlers() {
|
---|
[7999] | 112 | Operators.ItemsAdded += new CollectionItemsChangedEventHandler<IItem>(Operators_Changed);
|
---|
| 113 | Operators.ItemsRemoved += new CollectionItemsChangedEventHandler<IItem>(Operators_Changed);
|
---|
| 114 | Operators.CollectionReset += new CollectionItemsChangedEventHandler<IItem>(Operators_Changed);
|
---|
[4323] | 115 | }
|
---|
| 116 |
|
---|
| 117 | #region properties
|
---|
[7431] | 118 | // BackwardsCompatibility3.3
|
---|
| 119 | #region Backwards compatible code, remove with 3.4
|
---|
[16801] | 120 | [Storable(OldName = "Operators")]
|
---|
[4323] | 121 | private IEnumerable<IOperator> StorableOperators {
|
---|
[7431] | 122 | set {
|
---|
[8004] | 123 | IParameter operatorsParam;
|
---|
| 124 | if (Parameters.TryGetValue(OperatorsParameterName, out operatorsParam)) {
|
---|
| 125 | var items = operatorsParam.ActualValue as ItemCollection<IItem>;
|
---|
| 126 | if (items == null) Parameters.Remove(operatorsParam);
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | //necessary to convert old experiments files where no parameter was used for saving the operators
|
---|
[7431] | 130 | if (!Parameters.ContainsKey(OperatorsParameterName)) {
|
---|
[16946] | 131 | Parameters.Add(new FixedValueParameter<ItemCollection<IItem>>(OperatorsParameterName, "The operators and items that the problem provides to the algorithms.", new ItemCollection<IItem>()) { GetsCollected = false });
|
---|
[7431] | 132 | OperatorsParameter.Hidden = true;
|
---|
| 133 | }
|
---|
[8004] | 134 | if (value != null) Operators.AddRange(value);
|
---|
[7431] | 135 | }
|
---|
[4323] | 136 | }
|
---|
[7431] | 137 | #endregion
|
---|
[7999] | 138 | protected ItemCollection<IItem> Operators {
|
---|
[7431] | 139 | get {
|
---|
| 140 | // BackwardsCompatibility3.3
|
---|
| 141 | #region Backwards compatible code, remove with 3.4
|
---|
| 142 | if (!Parameters.ContainsKey(OperatorsParameterName)) {
|
---|
[16946] | 143 | Parameters.Add(new FixedValueParameter<ItemCollection<IItem>>(OperatorsParameterName, "The operators and items that the problem provides to the algorithms.", new ItemCollection<IItem>()) { GetsCollected = false });
|
---|
[7431] | 144 | OperatorsParameter.Hidden = true;
|
---|
| 145 | }
|
---|
| 146 | #endregion
|
---|
| 147 | return OperatorsParameter.Value;
|
---|
| 148 | }
|
---|
[4323] | 149 | }
|
---|
[17513] | 150 | IEnumerable<IItem> IEncodedProblem.Operators { get { return GetOperators(); } }
|
---|
[11961] | 151 |
|
---|
| 152 | protected virtual IEnumerable<IItem> GetOperators() {
|
---|
| 153 | return Operators;
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | public virtual IEnumerable<IParameterizedItem> ExecutionContextItems {
|
---|
| 157 | get { yield return this; }
|
---|
| 158 | }
|
---|
[4323] | 159 | #endregion
|
---|
| 160 |
|
---|
[7579] | 161 |
|
---|
[17612] | 162 | protected virtual void ParameterizeOperators() { }
|
---|
[17513] | 163 |
|
---|
[17570] | 164 |
|
---|
[4323] | 165 | #region events
|
---|
| 166 | private void Operators_Changed(object sender, EventArgs e) {
|
---|
| 167 | OnOperatorsChanged();
|
---|
[17570] | 168 | ParameterizeOperators();
|
---|
[4323] | 169 | }
|
---|
| 170 | public event EventHandler OperatorsChanged;
|
---|
| 171 | protected virtual void OnOperatorsChanged() {
|
---|
| 172 | EventHandler handler = OperatorsChanged;
|
---|
| 173 | if (handler != null)
|
---|
| 174 | handler(this, EventArgs.Empty);
|
---|
| 175 | }
|
---|
| 176 | #endregion
|
---|
| 177 | }
|
---|
| 178 | }
|
---|