Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.ParticleSwarmOptimization/3.3/VelocityBoundsModifier.cs @ 5435

Last change on this file since 5435 was 5435, checked in by abeham, 14 years ago

#852

  • Made public properties that redirect to ActualValue.Value private or protected
  • Sealed all of the specific operators
  • Removed files that are present in the repository, but are not included in the project
  • Removed .sln file (is this still needed?)
  • Added license headers to some files
  • Unified the pattern for writing the constructors similar to other files in the trunk
  • Corrected assembly and plugin version from 3.3.0.x to 3.3.2.x
  • Fixed the wiring in the VelocityBoundsModifier
File size: 5.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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 HeuristicLab.Common;
23using HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Operators;
26using HeuristicLab.Optimization;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29using HeuristicLab.PluginInfrastructure;
30
31namespace HeuristicLab.Algorithms.ParticleSwarmOptimization {
32  [Item("VelocityBoundsModifier", "Modifies the velocity bounds.")]
33  [StorableClass]
34  public sealed class VelocityBoundsModifier : SingleSuccessorOperator, IDiscreteDoubleMatrixModifier {
35    #region Parameters
36    public ILookupParameter<DoubleMatrix> ValueParameter {
37      get { return (ILookupParameter<DoubleMatrix>)Parameters["Matrix"]; }
38    }
39    public IValueLookupParameter<DoubleValue> ScaleParameter {
40      get { return (IValueLookupParameter<DoubleValue>)Parameters["Scale"]; }
41    }
42    public ConstrainedValueParameter<IDiscreteDoubleValueModifier> ScalingOperatorParameter {
43      get { return (ConstrainedValueParameter<IDiscreteDoubleValueModifier>)Parameters["ScalingOperator"]; }
44    }
45    public IValueLookupParameter<DoubleValue> StartValueParameter {
46      get { return (IValueLookupParameter<DoubleValue>)Parameters["StartValue"]; }
47    }
48    public IValueLookupParameter<DoubleValue> EndValueParameter {
49      get { return (IValueLookupParameter<DoubleValue>)Parameters["EndValue"]; }
50    }
51    public ILookupParameter<IntValue> IndexParameter {
52      get { return (ILookupParameter<IntValue>)Parameters["Index"]; }
53    }
54    public IValueLookupParameter<IntValue> StartIndexParameter {
55      get { return (IValueLookupParameter<IntValue>)Parameters["StartIndex"]; }
56    }
57    public IValueLookupParameter<IntValue> EndIndexParameter {
58      get { return (IValueLookupParameter<IntValue>)Parameters["EndIndex"]; }
59    }
60    #endregion
61
62    #region Construction & Cloning
63
64    [StorableConstructor]
65    private VelocityBoundsModifier(bool deserializing) : base(deserializing) { }
66    private VelocityBoundsModifier(VelocityBoundsModifier original, Cloner cloner)
67      : base(original, cloner) {
68      ParameterizeModifiers();
69    }
70    public VelocityBoundsModifier() {
71      Parameters.Add(new LookupParameter<DoubleMatrix>("Matrix", "The double matrix to modify."));
72      Parameters.Add(new ValueLookupParameter<DoubleValue>("Scale", "Scale parameter."));
73      Parameters.Add(new ConstrainedValueParameter<IDiscreteDoubleValueModifier>("ScalingOperator", "Modifies the value"));
74      Parameters.Add(new ValueLookupParameter<DoubleValue>("StartValue", "The start value of 'Value'.", new DoubleValue(1)));
75      Parameters.Add(new ValueLookupParameter<DoubleValue>("EndValue", "The end value of 'Value'."));
76      Parameters.Add(new LookupParameter<IntValue>("Index", "The current index."));
77      Parameters.Add(new ValueLookupParameter<IntValue>("StartIndex", "The start index at which to start modifying 'Value'."));
78      Parameters.Add(new ValueLookupParameter<IntValue>("EndIndex", "The end index by which 'Value' should have reached 'EndValue'."));
79
80      Initialize();
81      ParameterizeModifiers();
82    }
83
84    public override IDeepCloneable Clone(Cloner cloner) {
85      return new VelocityBoundsModifier(this, cloner);
86    }
87    #endregion
88
89    [StorableHook(HookType.AfterDeserialization)]
90    private void AfterDeserialization() {
91      ParameterizeModifiers();
92    }
93
94    private void Initialize() {
95      foreach (IDiscreteDoubleValueModifier op in ApplicationManager.Manager.GetInstances<IDiscreteDoubleValueModifier>()) {
96        ScalingOperatorParameter.ValidValues.Add(op);
97      }
98    }
99
100    private void ParameterizeModifiers() {
101      foreach (IDiscreteDoubleValueModifier op in ScalingOperatorParameter.ValidValues) {
102        op.ValueParameter.ActualName = ScaleParameter.Name;
103        op.StartValueParameter.ActualName = StartValueParameter.Name;
104        op.EndValueParameter.ActualName = EndValueParameter.Name;
105        op.IndexParameter.ActualName = IndexParameter.Name;
106        op.StartIndexParameter.ActualName = StartIndexParameter.Name;
107        op.EndIndexParameter.ActualName = EndIndexParameter.Name;
108      }
109    }
110
111    public override IOperation Apply() {
112      OperationCollection next = new OperationCollection();
113      DoubleMatrix matrix = ValueParameter.ActualValue;
114      if (this.ScaleParameter.ActualValue == null && this.StartValueParameter.ActualValue != null) {
115        this.ScaleParameter.ActualValue = new DoubleValue(StartValueParameter.ActualValue.Value);
116      }
117      for (int i = 0; i < matrix.Rows; i++) {
118        for (int j = 0; j < matrix.Columns; j++) {
119          if (matrix[i, j] >= 0) {
120            matrix[i, j] = ScaleParameter.ActualValue.Value;
121          } else {
122            matrix[i, j] = (-1) * ScaleParameter.ActualValue.Value;
123          }
124        }
125      }
126      next.Add(ExecutionContext.CreateChildOperation(this.ScalingOperatorParameter.Value));
127      next.Add(base.Apply());
128      return next;
129    }
130  }
131}
Note: See TracBrowser for help on using the repository browser.