Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5728 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
RevLine 
[5435]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
[5311]22using HeuristicLab.Common;
23using HeuristicLab.Core;
24using HeuristicLab.Data;
[5435]25using HeuristicLab.Operators;
[5311]26using HeuristicLab.Optimization;
27using HeuristicLab.Parameters;
[5435]28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[5311]29using HeuristicLab.PluginInfrastructure;
30
31namespace HeuristicLab.Algorithms.ParticleSwarmOptimization {
[5435]32  [Item("VelocityBoundsModifier", "Modifies the velocity bounds.")]
[5316]33  [StorableClass]
[5435]34  public sealed class VelocityBoundsModifier : SingleSuccessorOperator, IDiscreteDoubleMatrixModifier {
[5316]35    #region Parameters
[5311]36    public ILookupParameter<DoubleMatrix> ValueParameter {
37      get { return (ILookupParameter<DoubleMatrix>)Parameters["Matrix"]; }
38    }
[5440]39    public ILookupParameter<DoubleValue> ScaleParameter {
40      get { return (ILookupParameter<DoubleValue>)Parameters["Scale"]; }
[5311]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    }
[5316]60    #endregion
[5311]61
[5316]62    #region Construction & Cloning
63
64    [StorableConstructor]
[5435]65    private VelocityBoundsModifier(bool deserializing) : base(deserializing) { }
[5440]66    private VelocityBoundsModifier(VelocityBoundsModifier original, Cloner cloner) : base(original, cloner) { }
[5316]67    public VelocityBoundsModifier() {
[5311]68      Parameters.Add(new LookupParameter<DoubleMatrix>("Matrix", "The double matrix to modify."));
[5440]69      Parameters.Add(new LookupParameter<DoubleValue>("Scale", "Scale parameter."));
[5311]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'."));
[5435]76
[5311]77      Initialize();
78    }
[5435]79
[5316]80    public override IDeepCloneable Clone(Cloner cloner) {
81      return new VelocityBoundsModifier(this, cloner);
82    }
[5435]83    #endregion
[5311]84
85    private void Initialize() {
86      foreach (IDiscreteDoubleValueModifier op in ApplicationManager.Manager.GetInstances<IDiscreteDoubleValueModifier>()) {
87        ScalingOperatorParameter.ValidValues.Add(op);
[5435]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;
[5311]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.