Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ParameterBinding/HeuristicLab.Optimization/3.3/Problem.cs @ 5533

Last change on this file since 5533 was 4722, checked in by swagner, 14 years ago

Merged cloning refactoring branch back into trunk (#922)

File size: 5.5 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(Problem<T, U> original, Cloner cloner)
47      : base(original, cloner) {
48      operators = cloner.Clone(original.operators);
49      RegisterEventHandlers();
50    }
51
52    protected Problem()
53      : base() {
54      operators = new OperatorCollection();
55      Parameters.Add(new ValueParameter<T>(EvaluatorParameterName, "The operator used to evaluate a solution."));
56      Parameters.Add(new ValueParameter<U>(SolutionCreateParameterName, "The operator to create a solution."));
57      RegisterEventHandlers();
58    }
59
60    [StorableHook(HookType.AfterDeserialization)]
61    private void AfterDeserialization() {
62      RegisterEventHandlers();
63    }
64
65    private void RegisterEventHandlers() {
66      Operators.ItemsAdded += new CollectionItemsChangedEventHandler<IOperator>(Operators_Changed);
67      Operators.ItemsRemoved += new CollectionItemsChangedEventHandler<IOperator>(Operators_Changed);
68      Operators.CollectionReset += new CollectionItemsChangedEventHandler<IOperator>(Operators_Changed);
69
70      EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
71      SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
72    }
73
74    #region properties
75    private OperatorCollection operators;
76    [Storable(Name = "Operators")]
77    private IEnumerable<IOperator> StorableOperators {
78      get { return operators; }
79      set { operators = new OperatorCollection(value); }
80    }
81    protected OperatorCollection Operators {
82      get { return this.operators; }
83    }
84    IEnumerable<IOperator> IProblem.Operators { get { return operators; } }
85
86    public T Evaluator {
87      get { return EvaluatorParameter.Value; }
88      protected set { EvaluatorParameter.Value = value; }
89    }
90    public ValueParameter<T> EvaluatorParameter {
91      get { return (ValueParameter<T>)Parameters[EvaluatorParameterName]; }
92    }
93    IEvaluator IProblem.Evaluator { get { return Evaluator; } }
94    IParameter IProblem.EvaluatorParameter { get { return EvaluatorParameter; } }
95
96    public U SolutionCreator {
97      get { return SolutionCreatorParameter.Value; }
98      protected set { SolutionCreatorParameter.Value = value; }
99    }
100    public ValueParameter<U> SolutionCreatorParameter {
101      get { return (ValueParameter<U>)Parameters[SolutionCreateParameterName]; }
102    }
103    ISolutionCreator IProblem.SolutionCreator { get { return SolutionCreator; } }
104    IParameter IProblem.SolutionCreatorParameter { get { return SolutionCreatorParameter; } }
105    #endregion
106
107    #region events
108    private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
109      OnEvaluatorChanged();
110    }
111    public event EventHandler EvaluatorChanged;
112    protected virtual void OnEvaluatorChanged() {
113      EventHandler handler = EvaluatorChanged;
114      if (handler != null)
115        handler(this, EventArgs.Empty);
116    }
117
118    private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
119      OnSolutionCreatorChanged();
120    }
121    public event EventHandler SolutionCreatorChanged;
122    protected virtual void OnSolutionCreatorChanged() {
123      EventHandler handler = SolutionCreatorChanged;
124      if (handler != null)
125        handler(this, EventArgs.Empty);
126    }
127
128    private void Operators_Changed(object sender, EventArgs e) {
129      OnOperatorsChanged();
130    }
131    public event EventHandler OperatorsChanged;
132    protected virtual void OnOperatorsChanged() {
133      EventHandler handler = OperatorsChanged;
134      if (handler != null)
135        handler(this, EventArgs.Empty);
136    }
137
138    public event EventHandler Reset;
139    protected virtual void OnReset() {
140      EventHandler handler = Reset;
141      if (handler != null)
142        handler(this, EventArgs.Empty);
143    }
144    #endregion
145  }
146}
Note: See TracBrowser for help on using the repository browser.