[4323] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[5445] | 3 | * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[4323] | 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 HeuristicLab.Collections;
|
---|
| 26 | using HeuristicLab.Common;
|
---|
| 27 | using HeuristicLab.Core;
|
---|
[6938] | 28 | using HeuristicLab.Parameters;
|
---|
[4323] | 29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 30 |
|
---|
[4564] | 31 | namespace HeuristicLab.Optimization {
|
---|
[4596] | 32 | [Item("Problem", "Represents the base class for a problem.")]
|
---|
[4323] | 33 | [StorableClass]
|
---|
[5809] | 34 | public abstract class Problem : ParameterizedNamedItem, IProblem {
|
---|
[6938] | 35 | private static readonly string OperatorsParameterName = "Operators";
|
---|
| 36 |
|
---|
| 37 | public IFixedValueParameter<OperatorCollection> OperatorsParameter {
|
---|
| 38 | get { return (IFixedValueParameter<OperatorCollection>)Parameters[OperatorsParameterName]; }
|
---|
| 39 | }
|
---|
| 40 |
|
---|
[7201] | 41 | public static new Image StaticItemImage {
|
---|
[5287] | 42 | get { return HeuristicLab.Common.Resources.VSImageLibrary.Type; }
|
---|
[4323] | 43 | }
|
---|
| 44 |
|
---|
| 45 | [StorableConstructor]
|
---|
[6938] | 46 | protected Problem(bool deserializing)
|
---|
| 47 | : base(deserializing) {
|
---|
| 48 | operators = new OperatorCollection(); // operators must never be null
|
---|
| 49 | }
|
---|
[5809] | 50 | protected Problem(Problem original, Cloner cloner)
|
---|
[4722] | 51 | : base(original, cloner) {
|
---|
| 52 | operators = cloner.Clone(original.operators);
|
---|
| 53 | RegisterEventHandlers();
|
---|
| 54 | }
|
---|
| 55 |
|
---|
[4323] | 56 | protected Problem()
|
---|
| 57 | : base() {
|
---|
[4564] | 58 | operators = new OperatorCollection();
|
---|
[6938] | 59 | Parameters.Add(new FixedValueParameter<OperatorCollection>(OperatorsParameterName, "The operators that the problem provides to the algorithms.", operators, false));
|
---|
| 60 | OperatorsParameter.Hidden = true;
|
---|
[4563] | 61 | RegisterEventHandlers();
|
---|
[4323] | 62 | }
|
---|
| 63 |
|
---|
| 64 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 65 | private void AfterDeserialization() {
|
---|
[6938] | 66 | // BackwardsCompatibility3.3
|
---|
| 67 | #region Backwards compatible code, remove with 3.4
|
---|
| 68 | if (!Parameters.ContainsKey(OperatorsParameterName)) {
|
---|
| 69 | Parameters.Add(new FixedValueParameter<OperatorCollection>(OperatorsParameterName, "The operators that the problem provides to the algorithms.", operators, false));
|
---|
| 70 | OperatorsParameter.Hidden = true;
|
---|
| 71 | }
|
---|
| 72 | #endregion
|
---|
[4563] | 73 | RegisterEventHandlers();
|
---|
[4323] | 74 | }
|
---|
| 75 |
|
---|
[4563] | 76 | private void RegisterEventHandlers() {
|
---|
[4323] | 77 | Operators.ItemsAdded += new CollectionItemsChangedEventHandler<IOperator>(Operators_Changed);
|
---|
| 78 | Operators.ItemsRemoved += new CollectionItemsChangedEventHandler<IOperator>(Operators_Changed);
|
---|
| 79 | Operators.CollectionReset += new CollectionItemsChangedEventHandler<IOperator>(Operators_Changed);
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | #region properties
|
---|
[4564] | 83 | private OperatorCollection operators;
|
---|
[4563] | 84 | [Storable(Name = "Operators")]
|
---|
[4323] | 85 | private IEnumerable<IOperator> StorableOperators {
|
---|
| 86 | get { return operators; }
|
---|
[4564] | 87 | set { operators = new OperatorCollection(value); }
|
---|
[4323] | 88 | }
|
---|
[4564] | 89 | protected OperatorCollection Operators {
|
---|
[4323] | 90 | get { return this.operators; }
|
---|
| 91 | }
|
---|
| 92 | IEnumerable<IOperator> IProblem.Operators { get { return operators; } }
|
---|
| 93 | #endregion
|
---|
| 94 |
|
---|
| 95 | #region events
|
---|
| 96 | private void Operators_Changed(object sender, EventArgs e) {
|
---|
| 97 | OnOperatorsChanged();
|
---|
| 98 | }
|
---|
| 99 | public event EventHandler OperatorsChanged;
|
---|
| 100 | protected virtual void OnOperatorsChanged() {
|
---|
| 101 | EventHandler handler = OperatorsChanged;
|
---|
| 102 | if (handler != null)
|
---|
| 103 | handler(this, EventArgs.Empty);
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | public event EventHandler Reset;
|
---|
| 107 | protected virtual void OnReset() {
|
---|
| 108 | EventHandler handler = Reset;
|
---|
| 109 | if (handler != null)
|
---|
| 110 | handler(this, EventArgs.Empty);
|
---|
| 111 | }
|
---|
| 112 | #endregion
|
---|
| 113 | }
|
---|
| 114 | }
|
---|