Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis Refactoring/HeuristicLab.Algorithms.ParticleSwarmOptimization/3.3/VelocityBoundsModifier.cs @ 5484

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

#852

  • Fixed wiring of VelocityBoundsModifier again (on second thought a "real" wiring is not really necessary)
  • Added PSO as dependency to the main project
File size: 5.4 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 ILookupParameter<DoubleValue> ScaleParameter {
40      get { return (ILookupParameter<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) : base(original, cloner) { }
67    public VelocityBoundsModifier() {
68      Parameters.Add(new LookupParameter<DoubleMatrix>("Matrix", "The double matrix to modify."));
69      Parameters.Add(new LookupParameter<DoubleValue>("Scale", "Scale parameter."));
70      Parameters.Add(new ConstrainedValueParameter<IDiscreteDoubleValueModifier>("ScalingOperator", "Modifies the value"));
71      Parameters.Add(new ValueLookupParameter<DoubleValue>("StartValue", "The start value of 'Value'.", new DoubleValue(1)));
72      Parameters.Add(new ValueLookupParameter<DoubleValue>("EndValue", "The end value of 'Value'."));
73      Parameters.Add(new LookupParameter<IntValue>("Index", "The current index."));
74      Parameters.Add(new ValueLookupParameter<IntValue>("StartIndex", "The start index at which to start modifying 'Value'."));
75      Parameters.Add(new ValueLookupParameter<IntValue>("EndIndex", "The end index by which 'Value' should have reached 'EndValue'."));
76
77      Initialize();
78    }
79
80    public override IDeepCloneable Clone(Cloner cloner) {
81      return new VelocityBoundsModifier(this, cloner);
82    }
83    #endregion
84
85    private void Initialize() {
86      foreach (IDiscreteDoubleValueModifier op in ApplicationManager.Manager.GetInstances<IDiscreteDoubleValueModifier>()) {
87        ScalingOperatorParameter.ValidValues.Add(op);
88        op.ValueParameter.ActualName = ScaleParameter.Name;
89        op.StartValueParameter.ActualName = StartValueParameter.Name;
90        op.EndValueParameter.ActualName = EndValueParameter.Name;
91        op.IndexParameter.ActualName = IndexParameter.Name;
92        op.StartIndexParameter.ActualName = StartIndexParameter.Name;
93        op.EndIndexParameter.ActualName = EndIndexParameter.Name;
94      }
95    }
96
97    public override IOperation Apply() {
98      OperationCollection next = new OperationCollection();
99      DoubleMatrix matrix = ValueParameter.ActualValue;
100      if (this.ScaleParameter.ActualValue == null && this.StartValueParameter.ActualValue != null) {
101        this.ScaleParameter.ActualValue = new DoubleValue(StartValueParameter.ActualValue.Value);
102      }
103      for (int i = 0; i < matrix.Rows; i++) {
104        for (int j = 0; j < matrix.Columns; j++) {
105          if (matrix[i, j] >= 0) {
106            matrix[i, j] = ScaleParameter.ActualValue.Value;
107          } else {
108            matrix[i, j] = (-1) * ScaleParameter.ActualValue.Value;
109          }
110        }
111      }
112      next.Add(ExecutionContext.CreateChildOperation(this.ScalingOperatorParameter.Value));
113      next.Add(base.Apply());
114      return next;
115    }
116  }
117}
Note: See TracBrowser for help on using the repository browser.