Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2924-dotnet-core/HeuristicLab.Clients.OKB/3.3/RunCreation/OKBProblem.cs @ 15961

Last change on this file since 15961 was 15583, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers

File size: 10.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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 HeuristicLab.Common;
23using HeuristicLab.Core;
24using HeuristicLab.Optimization;
25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
26using HeuristicLab.Persistence.Default.Xml;
27using System;
28using System.Collections.Generic;
29using System.Drawing;
30using System.IO;
31using System.Linq;
32
33namespace HeuristicLab.Clients.OKB.RunCreation {
34  [Item("OKB Problem", "A base class for problems which are stored in the OKB.")]
35  [StorableClass]
36  public abstract class OKBProblem : Item, IHeuristicOptimizationProblem {
37    public virtual Type ProblemType {
38      get { return typeof(IHeuristicOptimizationProblem); }
39    }
40    private long problemId;
41    public long ProblemId {
42      get { return problemId; }
43    }
44    private IHeuristicOptimizationProblem problem;
45    protected IHeuristicOptimizationProblem Problem {
46      get { return problem; }
47      private set {
48        if (value == null) throw new ArgumentNullException("Problem", "Problem cannot be null.");
49        if (value != problem) {
50          CancelEventArgs<string> e = new CancelEventArgs<string>(value.Name);
51          OnNameChanging(e);
52          if (!e.Cancel) {
53            DeregisterProblemEvents();
54            problem = value;
55            RegisterProblemEvents();
56
57            OnToStringChanged();
58            OnItemImageChanged();
59            OnNameChanged();
60            OnDescriptionChanged();
61            OnProblemChanged();
62            OnSolutionCreatorChanged();
63            OnEvaluatorChanged();
64            OnOperatorsChanged();
65            OnReset();
66
67            solutions.Clear();
68          }
69        }
70      }
71    }
72
73    [Storable]
74    private ItemList<OKBSolution> solutions;
75    public ItemList<OKBSolution> Solutions {
76      get { return solutions; }
77    }
78
79    public override Image ItemImage {
80      get { return Problem.ItemImage; }
81    }
82
83    public static new Image StaticItemImage {
84      get { return HeuristicLab.Common.Resources.VSImageLibrary.Type; }
85    }
86
87    public string Name {
88      get { return Problem.Name; }
89      set { throw new NotSupportedException("Name cannot be changed."); }
90    }
91    public string Description {
92      get { return Problem.Description; }
93      set { throw new NotSupportedException("Description cannot be changed."); }
94    }
95    public bool CanChangeName {
96      get { return false; }
97    }
98    public bool CanChangeDescription {
99      get { return false; }
100    }
101
102    public IKeyedItemCollection<string, IParameter> Parameters {
103      get { return Problem.Parameters; }
104    }
105
106    public IParameter SolutionCreatorParameter {
107      get { return Problem.SolutionCreatorParameter; }
108    }
109    public ISolutionCreator SolutionCreator {
110      get { return Problem.SolutionCreator; }
111    }
112    public IParameter EvaluatorParameter {
113      get { return Problem.EvaluatorParameter; }
114    }
115    public IEvaluator Evaluator {
116      get { return Problem.Evaluator; }
117    }
118    public IEnumerable<IItem> Operators {
119      get { return Problem.Operators; }
120    }
121
122    public IEnumerable<IParameterizedItem> ExecutionContextItems { get { return new[] { this }; } }
123
124    #region Persistence Properties
125    [Storable(Name = "ProblemId")]
126    private long StorableProblemId {
127      get { return problemId; }
128      set { problemId = value; }
129    }
130    [Storable(Name = "Problem")]
131    private IHeuristicOptimizationProblem StorableProblem {
132      get { return problem; }
133      set {
134        problem = value;
135        RegisterProblemEvents();
136      }
137    }
138    #endregion
139
140    [StorableConstructor]
141    protected OKBProblem(bool deserializing) : base(deserializing) { }
142    protected OKBProblem(OKBProblem original, Cloner cloner)
143      : base(original, cloner) {
144      problemId = original.problemId;
145      problem = cloner.Clone(original.problem);
146      solutions = cloner.Clone(original.solutions);
147      RegisterProblemEvents();
148    }
149    protected OKBProblem(IHeuristicOptimizationProblem initialProblem)
150      : base() {
151      if (initialProblem == null) throw new ArgumentNullException("initialProblem", "Initial problem cannot be null.");
152      problemId = -1;
153      problem = initialProblem;
154      solutions = new ItemList<OKBSolution>();
155      RegisterProblemEvents();
156    }
157
158    public void Load(long problemId) {
159      if (this.problemId != problemId) {
160        IHeuristicOptimizationProblem problem;
161        byte[] problemData = RunCreationClient.Instance.GetProblemData(problemId);
162        using (MemoryStream stream = new MemoryStream(problemData)) {
163          problem = XmlParser.Deserialize<IHeuristicOptimizationProblem>(stream);
164        }
165        if (ProblemType.IsAssignableFrom(problem.GetType())) {
166          this.problemId = problemId;
167          Problem = problem;
168        }
169      }
170    }
171
172    public void RefreshSolutions() {
173      if (ProblemId != -1) {
174        var sols = RunCreationClient.Instance.GetSolutions(ProblemId).Select(OKBSolution.Convert).ToList();
175        foreach (var sol in sols) sol.DownloadData();
176        Solutions.Replace(sols);
177      } else Solutions.Clear();
178    }
179
180    public IProblem CloneProblem() {
181      return (IProblem)Problem.Clone();
182    }
183
184    public void CollectParameterValues(IDictionary<string, IItem> values) {
185      Problem.CollectParameterValues(values);
186    }
187
188    #region Events
189    public event EventHandler ProblemChanged;
190    protected virtual void OnProblemChanged() {
191      EventHandler handler = ProblemChanged;
192      if (handler != null) handler(this, EventArgs.Empty);
193    }
194    public event EventHandler<CancelEventArgs<string>> NameChanging;
195    protected virtual void OnNameChanging(CancelEventArgs<string> e) {
196      var handler = NameChanging;
197      if (handler != null) handler(this, e);
198    }
199    public event EventHandler NameChanged;
200    protected virtual void OnNameChanged() {
201      var handler = NameChanged;
202      if (handler != null) handler(this, EventArgs.Empty);
203    }
204    public event EventHandler DescriptionChanged;
205    protected virtual void OnDescriptionChanged() {
206      var handler = DescriptionChanged;
207      if (handler != null) handler(this, EventArgs.Empty);
208    }
209    public event EventHandler SolutionCreatorChanged;
210    protected virtual void OnSolutionCreatorChanged() {
211      var handler = SolutionCreatorChanged;
212      if (handler != null) handler(this, EventArgs.Empty);
213    }
214    public event EventHandler EvaluatorChanged;
215    protected virtual void OnEvaluatorChanged() {
216      var handler = EvaluatorChanged;
217      if (handler != null) handler(this, EventArgs.Empty);
218    }
219    public event EventHandler OperatorsChanged;
220    protected virtual void OnOperatorsChanged() {
221      var handler = OperatorsChanged;
222      if (handler != null) handler(this, EventArgs.Empty);
223    }
224    public event EventHandler Reset;
225    protected virtual void OnReset() {
226      var handler = Reset;
227      if (handler != null) handler(this, EventArgs.Empty);
228    }
229
230    protected virtual void RegisterProblemEvents() {
231      if (Problem != null) {
232        Problem.ToStringChanged += new EventHandler(Problem_ToStringChanged);
233        Problem.ItemImageChanged += new EventHandler(Problem_ItemImageChanged);
234        Problem.NameChanging += new EventHandler<CancelEventArgs<string>>(Problem_NameChanging);
235        Problem.NameChanged += new EventHandler(Problem_NameChanged);
236        Problem.DescriptionChanged += new EventHandler(Problem_DescriptionChanged);
237        Problem.SolutionCreatorChanged += new EventHandler(Problem_SolutionCreatorChanged);
238        Problem.EvaluatorChanged += new EventHandler(Problem_EvaluatorChanged);
239        Problem.OperatorsChanged += new EventHandler(Problem_OperatorsChanged);
240        Problem.Reset += new EventHandler(Problem_Reset);
241      }
242    }
243    protected virtual void DeregisterProblemEvents() {
244      if (Problem != null) {
245        Problem.ToStringChanged -= new EventHandler(Problem_ToStringChanged);
246        Problem.ItemImageChanged -= new EventHandler(Problem_ItemImageChanged);
247        Problem.NameChanging -= new EventHandler<CancelEventArgs<string>>(Problem_NameChanging);
248        Problem.NameChanged -= new EventHandler(Problem_NameChanged);
249        Problem.DescriptionChanged -= new EventHandler(Problem_DescriptionChanged);
250        Problem.SolutionCreatorChanged -= new EventHandler(Problem_SolutionCreatorChanged);
251        Problem.EvaluatorChanged -= new EventHandler(Problem_EvaluatorChanged);
252        Problem.OperatorsChanged -= new EventHandler(Problem_OperatorsChanged);
253        Problem.Reset -= new EventHandler(Problem_Reset);
254      }
255    }
256
257    private void Problem_ToStringChanged(object sender, EventArgs e) {
258      OnToStringChanged();
259    }
260    private void Problem_ItemImageChanged(object sender, EventArgs e) {
261      OnItemImageChanged();
262    }
263    private void Problem_NameChanging(object sender, CancelEventArgs<string> e) {
264      OnNameChanging(e);
265    }
266    private void Problem_NameChanged(object sender, EventArgs e) {
267      OnNameChanged();
268    }
269    private void Problem_DescriptionChanged(object sender, EventArgs e) {
270      OnDescriptionChanged();
271    }
272    private void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
273      OnSolutionCreatorChanged();
274    }
275    private void Problem_EvaluatorChanged(object sender, EventArgs e) {
276      OnEvaluatorChanged();
277    }
278    private void Problem_OperatorsChanged(object sender, EventArgs e) {
279      OnOperatorsChanged();
280    }
281    private void Problem_Reset(object sender, EventArgs e) {
282      OnReset();
283    }
284    #endregion
285  }
286}
Note: See TracBrowser for help on using the repository browser.