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