Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PSO/HeuristicLab.Algorithms.ParticleSwarmOptimization/3.3/VelocityBoundsModifier.cs @ 5342

Last change on this file since 5342 was 5316, checked in by epitzer, 14 years ago

added item descriptions, implemented IStorableContent interface, added missing StorableClass attributes, and fixed value updater reset (#852)

File size: 6.6 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using HeuristicLab.Operators;
6using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
7using HeuristicLab.Common;
8using HeuristicLab.Core;
9using HeuristicLab.Data;
10using HeuristicLab.Optimization;
11using HeuristicLab.Parameters;
12using HeuristicLab.PluginInfrastructure;
13
14namespace HeuristicLab.Algorithms.ParticleSwarmOptimization {
15
16  [StorableClass]
17  public class VelocityBoundsModifier : SingleSuccessorOperator, IDiscreteDoubleMatrixModifier {
18
19    #region Parameters
20    public ILookupParameter<DoubleMatrix> ValueParameter {
21      get { return (ILookupParameter<DoubleMatrix>)Parameters["Matrix"]; }
22    }
23    public IValueLookupParameter<DoubleValue> ScaleParameter {
24      get { return (IValueLookupParameter<DoubleValue>)Parameters["Scale"]; }
25    }
26    public ConstrainedValueParameter<IDiscreteDoubleValueModifier> ScalingOperatorParameter {
27      get { return (ConstrainedValueParameter<IDiscreteDoubleValueModifier>)Parameters["ScalingOperator"]; }
28    }
29    public IValueLookupParameter<DoubleValue> StartValueParameter {
30      get { return (IValueLookupParameter<DoubleValue>)Parameters["StartValue"]; }
31    }
32    public IValueLookupParameter<DoubleValue> EndValueParameter {
33      get { return (IValueLookupParameter<DoubleValue>)Parameters["EndValue"]; }
34    }
35    public ILookupParameter<IntValue> IndexParameter {
36      get { return (ILookupParameter<IntValue>)Parameters["Index"]; }
37    }
38    public IValueLookupParameter<IntValue> StartIndexParameter {
39      get { return (IValueLookupParameter<IntValue>)Parameters["StartIndex"]; }
40    }
41    public IValueLookupParameter<IntValue> EndIndexParameter {
42      get { return (IValueLookupParameter<IntValue>)Parameters["EndIndex"]; }
43    }
44    #endregion
45
46    #region Construction & Cloning
47
48    [StorableConstructor]
49    protected VelocityBoundsModifier(bool deserializing) : base(deserializing) { }
50    protected VelocityBoundsModifier(VelocityBoundsModifier original, Cloner cloner) : base(original, cloner) {
51      Initialize();
52    }
53    public VelocityBoundsModifier() {
54      Parameters.Add(new LookupParameter<DoubleMatrix>("Matrix", "The double matrix to modify."));
55      Parameters.Add(new ValueLookupParameter<DoubleValue>("Scale", "Scale parameter."));
56      Parameters.Add(new ConstrainedValueParameter<IDiscreteDoubleValueModifier>("ScalingOperator", "Modifies the value"));
57      Parameters.Add(new ValueLookupParameter<DoubleValue>("StartValue", "The start value of 'Value'.", new DoubleValue(1)));
58      Parameters.Add(new ValueLookupParameter<DoubleValue>("EndValue", "The end value of 'Value'."));
59      Parameters.Add(new LookupParameter<IntValue>("Index", "The current index."));
60      Parameters.Add(new ValueLookupParameter<IntValue>("StartIndex", "The start index at which to start modifying 'Value'."));
61      Parameters.Add(new ValueLookupParameter<IntValue>("EndIndex", "The end index by which 'Value' should have reached 'EndValue'."));
62      Initialize();
63    }
64    public override IDeepCloneable Clone(Cloner cloner) {
65      return new VelocityBoundsModifier(this, cloner);
66    }
67
68    private void Initialize() {
69      foreach (IDiscreteDoubleValueModifier op in ApplicationManager.Manager.GetInstances<IDiscreteDoubleValueModifier>()) {
70        ScalingOperatorParameter.ValidValues.Add(op);
71        op.ValueParameter.ActualName = ScaleParameter.ActualName;
72      }
73      ScaleParameter.ActualNameChanged += new EventHandler(ScaleParameter_ActualNameChanged);
74      StartValueParameter.ActualNameChanged += new EventHandler(StartValueParameter_ActualNameChanged);
75      EndValueParameter.ActualNameChanged += new EventHandler(EndValueParameter_ActualNameChanged);
76      IndexParameter.ActualNameChanged += new EventHandler(IndexParameter_ActualNameChanged);
77      EndIndexParameter.ActualNameChanged += new EventHandler(EndIndexParameter_ActualNameChanged);
78      StartIndexParameter.ActualNameChanged += new EventHandler(StartIndexParameter_ActualNameChanged);
79    }
80    #endregion
81
82    #region Events
83    private void ScaleParameter_ActualNameChanged(object sender, EventArgs e) {
84      foreach (IDiscreteDoubleValueModifier modifier in ScalingOperatorParameter.ValidValues) {
85        modifier.ValueParameter.ActualName = ScaleParameter.ActualName;
86      }
87    }
88
89    private void StartValueParameter_ActualNameChanged(object sender, EventArgs e) {
90      foreach (IDiscreteDoubleValueModifier modifier in ScalingOperatorParameter.ValidValues) {
91        modifier.StartValueParameter.ActualName = StartValueParameter.ActualName;
92      }
93    }
94
95    private void EndValueParameter_ActualNameChanged(object sender, EventArgs e) {
96      foreach (IDiscreteDoubleValueModifier modifier in ScalingOperatorParameter.ValidValues) {
97        modifier.EndValueParameter.ActualName = EndValueParameter.ActualName;
98      }
99    }
100
101    private void IndexParameter_ActualNameChanged(object sender, EventArgs e) {
102      foreach (IDiscreteDoubleValueModifier modifier in ScalingOperatorParameter.ValidValues) {
103        modifier.IndexParameter.ActualName = IndexParameter.ActualName;
104      }
105    }
106
107    private void StartIndexParameter_ActualNameChanged(object sender, EventArgs e) {
108      foreach (IDiscreteDoubleValueModifier modifier in ScalingOperatorParameter.ValidValues) {
109        modifier.StartIndexParameter.ActualName = StartIndexParameter.ActualName;
110      }
111    }
112
113    private void EndIndexParameter_ActualNameChanged(object sender, EventArgs e) {
114      foreach (IDiscreteDoubleValueModifier modifier in ScalingOperatorParameter.ValidValues) {
115        modifier.EndIndexParameter.ActualName = EndIndexParameter.ActualName;
116      }
117    }
118    #endregion
119
120    public override IOperation Apply() {
121      OperationCollection next = new OperationCollection();
122      DoubleMatrix matrix = ValueParameter.ActualValue;
123      if (this.ScaleParameter.ActualValue == null && this.StartValueParameter.ActualValue != null) {
124        this.ScaleParameter.ActualValue = new DoubleValue(StartValueParameter.ActualValue.Value);
125      }
126      for (int i = 0; i < matrix.Rows; i++) {
127        for (int j = 0; j < matrix.Columns; j++) {
128          if (matrix[i, j] >= 0) {
129            matrix[i, j] = ScaleParameter.ActualValue.Value;
130          } else {
131            matrix[i, j] = (-1) * ScaleParameter.ActualValue.Value;
132          }
133        }
134      }
135      next.Add(ExecutionContext.CreateChildOperation(this.ScalingOperatorParameter.Value));
136      next.Add(base.Apply());
137      return next;
138    }
139  }
140}
Note: See TracBrowser for help on using the repository browser.