[5311] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Linq;
|
---|
| 4 | using System.Text;
|
---|
| 5 | using HeuristicLab.Operators;
|
---|
| 6 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 7 | using HeuristicLab.Common;
|
---|
| 8 | using HeuristicLab.Core;
|
---|
| 9 | using HeuristicLab.Data;
|
---|
| 10 | using HeuristicLab.Optimization;
|
---|
| 11 | using HeuristicLab.Parameters;
|
---|
| 12 | using HeuristicLab.PluginInfrastructure;
|
---|
| 13 |
|
---|
| 14 | namespace HeuristicLab.Algorithms.ParticleSwarmOptimization {
|
---|
[5316] | 15 |
|
---|
| 16 | [StorableClass]
|
---|
[5311] | 17 | public class VelocityBoundsModifier : SingleSuccessorOperator, IDiscreteDoubleMatrixModifier {
|
---|
[5316] | 18 |
|
---|
| 19 | #region Parameters
|
---|
[5311] | 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 | }
|
---|
[5316] | 44 | #endregion
|
---|
[5311] | 45 |
|
---|
[5316] | 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() {
|
---|
[5311] | 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 | }
|
---|
[5316] | 64 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 65 | return new VelocityBoundsModifier(this, cloner);
|
---|
| 66 | }
|
---|
[5311] | 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 | }
|
---|
[5316] | 80 | #endregion
|
---|
[5311] | 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 | }
|
---|