Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CloningRefactoring/HeuristicLab.Optimization/3.3/Problem.cs @ 4656

Last change on this file since 4656 was 4597, checked in by mkommend, 14 years ago

Fixed cloning of Problem (ticket #1232).

File size: 5.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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 HeuristicLab.Collections;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Optimization {
32  [Item("Problem", "Represents the base class for a problem.")]
33  [StorableClass]
34  public abstract class Problem<T, U> : ParameterizedNamedItem, IProblem
35    where T : class,IEvaluator
36    where U : class,ISolutionCreator {
37    private const string EvaluatorParameterName = "Evaluator";
38    private const string SolutionCreateParameterName = "SolutionCreator";
39
40    public override Image ItemImage {
41      get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Type; }
42    }
43
44    [StorableConstructor]
45    protected Problem(bool deserializing) : base(deserializing) { }
46    protected Problem()
47      : base() {
48      operators = new OperatorCollection();
49      Parameters.Add(new ValueParameter<T>(EvaluatorParameterName, "The operator used to evaluate a solution."));
50      Parameters.Add(new ValueParameter<U>(SolutionCreateParameterName, "The operator to create a solution."));
51      RegisterEventHandlers();
52    }
53
54    [StorableHook(HookType.AfterDeserialization)]
55    private void AfterDeserialization() {
56      RegisterEventHandlers();
57    }
58
59    public override IDeepCloneable Clone(Cloner cloner) {
60      Problem<T, U> clone = (Problem<T, U>)base.Clone(cloner);
61      clone.operators = (OperatorCollection)cloner.Clone(operators);
62      clone.RegisterEventHandlers();
63      return clone;
64    }
65
66    private void RegisterEventHandlers() {
67      Operators.ItemsAdded += new CollectionItemsChangedEventHandler<IOperator>(Operators_Changed);
68      Operators.ItemsRemoved += new CollectionItemsChangedEventHandler<IOperator>(Operators_Changed);
69      Operators.CollectionReset += new CollectionItemsChangedEventHandler<IOperator>(Operators_Changed);
70
71      EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
72      SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
73    }
74
75    #region properties
76    private OperatorCollection operators;
77    [Storable(Name = "Operators")]
78    private IEnumerable<IOperator> StorableOperators {
79      get { return operators; }
80      set { operators = new OperatorCollection(value); }
81    }
82    protected OperatorCollection Operators {
83      get { return this.operators; }
84    }
85    IEnumerable<IOperator> IProblem.Operators { get { return operators; } }
86
87    public T Evaluator {
88      get { return EvaluatorParameter.Value; }
89      protected set { EvaluatorParameter.Value = value; }
90    }
91    public ValueParameter<T> EvaluatorParameter {
92      get { return (ValueParameter<T>)Parameters[EvaluatorParameterName]; }
93    }
94    IEvaluator IProblem.Evaluator { get { return Evaluator; } }
95    IParameter IProblem.EvaluatorParameter { get { return EvaluatorParameter; } }
96
97    public U SolutionCreator {
98      get { return SolutionCreatorParameter.Value; }
99      protected set { SolutionCreatorParameter.Value = value; }
100    }
101    public ValueParameter<U> SolutionCreatorParameter {
102      get { return (ValueParameter<U>)Parameters[SolutionCreateParameterName]; }
103    }
104    ISolutionCreator IProblem.SolutionCreator { get { return SolutionCreator; } }
105    IParameter IProblem.SolutionCreatorParameter { get { return SolutionCreatorParameter; } }
106    #endregion
107
108    #region events
109    private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
110      OnEvaluatorChanged();
111    }
112    public event EventHandler EvaluatorChanged;
113    protected virtual void OnEvaluatorChanged() {
114      EventHandler handler = EvaluatorChanged;
115      if (handler != null)
116        handler(this, EventArgs.Empty);
117    }
118
119    private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
120      OnSolutionCreatorChanged();
121    }
122    public event EventHandler SolutionCreatorChanged;
123    protected virtual void OnSolutionCreatorChanged() {
124      EventHandler handler = SolutionCreatorChanged;
125      if (handler != null)
126        handler(this, EventArgs.Empty);
127    }
128
129    private void Operators_Changed(object sender, EventArgs e) {
130      OnOperatorsChanged();
131    }
132    public event EventHandler OperatorsChanged;
133    protected virtual void OnOperatorsChanged() {
134      EventHandler handler = OperatorsChanged;
135      if (handler != null)
136        handler(this, EventArgs.Empty);
137    }
138
139    public event EventHandler Reset;
140    protected virtual void OnReset() {
141      EventHandler handler = Reset;
142      if (handler != null)
143        handler(this, EventArgs.Empty);
144    }
145    #endregion
146  }
147}
Note: See TracBrowser for help on using the repository browser.