Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/Problems/Problem.cs @ 17578

Last change on this file since 17578 was 17570, checked in by abeham, 5 years ago

#2521: adapted knapsack, added base call to symbolic expression tree encoding, changed parameterization operators in base classes

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