Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Clients.OKB/3.3/RunCreation/OKBProblem.cs @ 17612

Last change on this file since 17612 was 17612, checked in by mkommend, 4 years ago

#2521: Added first version of problem results.

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