Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
06/29/17 15:26:16 (7 years ago)
Author:
abeham
Message:

#2797:

  • Updated PSO to make it more compatible with SPSO 2011
  • Removed truncation of velocity vector and instead rescaled it given the maximum velocity
  • Added non-zero initial velocity according to SPSO 2011
  • Removed complicated bouncing code due to box constraints and instead implemented as described in SPSO 2011
  • Calculating neighbor best has been changed to use personal best
  • Avoiding local and global particle update and instead relying on neighborbest
  • More randomization during velocity update by using a separate random numbers per dimension
  • Reusing problem specific solution creator in RealVectorParticleCreator instead of always using UniformRandomRealVectorCreator
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/RealVectorSwarmUpdater.cs

    r15071 r15091  
    7676      get { return (IScopeTreeLookupParameter<IntArray>)Parameters["Neighbors"]; }
    7777    }
    78     public IValueLookupParameter<DoubleMatrix> VelocityBoundsParameter {
    79       get { return (ValueLookupParameter<DoubleMatrix>)Parameters["VelocityBounds"]; }
    80     }
    81     public ILookupParameter<DoubleMatrix> CurrentVelocityBoundsParameter {
    82       get { return (ILookupParameter<DoubleMatrix>)Parameters["CurrentVelocityBounds"]; }
     78    public IValueLookupParameter<DoubleValue> MaxVelocityParameter {
     79      get { return (ValueLookupParameter<DoubleValue>)Parameters["MaxVelocity"]; }
     80    }
     81    public ILookupParameter<DoubleValue> CurrentMaxVelocityParameter {
     82      get { return (ILookupParameter<DoubleValue>)Parameters["CurrentMaxVelocity"]; }
    8383    }
    8484    public LookupParameter<ResultCollection> ResultsParameter {
     
    8686    }
    8787
    88     #region Velocity Bounds Updating
    89     public ILookupParameter<DoubleValue> VelocityBoundsScaleParameter {
    90       get { return (ILookupParameter<DoubleValue>)Parameters["VelocityBoundsScale"]; }
    91     }
    92     public IConstrainedValueParameter<IDiscreteDoubleValueModifier> VelocityBoundsScalingOperatorParameter {
    93       get { return (IConstrainedValueParameter<IDiscreteDoubleValueModifier>)Parameters["VelocityBoundsScalingOperator"]; }
    94     }
    95     public IValueLookupParameter<DoubleValue> VelocityBoundsStartValueParameter {
    96       get { return (IValueLookupParameter<DoubleValue>)Parameters["VelocityBoundsStartValue"]; }
    97     }
    98     public IValueLookupParameter<DoubleValue> VelocityBoundsEndValueParameter {
    99       get { return (IValueLookupParameter<DoubleValue>)Parameters["VelocityBoundsEndValue"]; }
    100     }
    101     public ILookupParameter<IntValue> VelocityBoundsIndexParameter {
    102       get { return (ILookupParameter<IntValue>)Parameters["VelocityBoundsIndex"]; }
    103     }
    104     public IValueLookupParameter<IntValue> VelocityBoundsStartIndexParameter {
    105       get { return (IValueLookupParameter<IntValue>)Parameters["VelocityBoundsStartIndex"]; }
    106     }
    107     public IValueLookupParameter<IntValue> VelocityBoundsEndIndexParameter {
    108       get { return (IValueLookupParameter<IntValue>)Parameters["VelocityBoundsEndIndex"]; }
     88    #region Max Velocity Updating
     89    public IConstrainedValueParameter<IDiscreteDoubleValueModifier> MaxVelocityScalingOperatorParameter {
     90      get { return (IConstrainedValueParameter<IDiscreteDoubleValueModifier>)Parameters["MaxVelocityScalingOperator"]; }
     91    }
     92    public IValueLookupParameter<DoubleValue> FinalMaxVelocityParameter {
     93      get { return (IValueLookupParameter<DoubleValue>)Parameters["FinalMaxVelocity"]; }
     94    }
     95    public ILookupParameter<IntValue> MaxVelocityIndexParameter {
     96      get { return (ILookupParameter<IntValue>)Parameters["MaxVelocityIndex"]; }
     97    }
     98    public IValueLookupParameter<IntValue> MaxVelocityStartIndexParameter {
     99      get { return (IValueLookupParameter<IntValue>)Parameters["MaxVelocityStartIndex"]; }
     100    }
     101    public IValueLookupParameter<IntValue> MaxVelocityEndIndexParameter {
     102      get { return (IValueLookupParameter<IntValue>)Parameters["MaxVelocityEndIndex"]; }
    109103    }
    110104    #endregion
     
    132126      Parameters.Add(new ScopeTreeLookupParameter<IntArray>("Neighbors", "The list of neighbors for each particle."));
    133127      Parameters.Add(new LookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
    134       Parameters.Add(new ValueLookupParameter<DoubleMatrix>("VelocityBounds", "Maximum velocity for each dimension.", new DoubleMatrix(new double[,] { { -1, 1 } })));
    135       Parameters.Add(new LookupParameter<DoubleMatrix>("CurrentVelocityBounds", "Current value of velocity bounds."));
     128      Parameters.Add(new ValueLookupParameter<DoubleValue>("MaxVelocity", "Speed limit for each particle.", new DoubleValue(1)));
     129      Parameters.Add(new LookupParameter<DoubleValue>("CurrentMaxVelocity", "Current value of the speed limit."));
    136130      Parameters.Add(new LookupParameter<ResultCollection>("Results", "Results"));
    137131
    138132      #region Velocity Bounds Updating
    139       Parameters.Add(new LookupParameter<DoubleValue>("VelocityBoundsScale", "Scale parameter."));
    140       Parameters.Add(new OptionalConstrainedValueParameter<IDiscreteDoubleValueModifier>("VelocityBoundsScalingOperator", "Modifies the value"));
    141       Parameters.Add(new ValueLookupParameter<DoubleValue>("VelocityBoundsStartValue", "The start value of 'Value'.", new DoubleValue(1)));
    142       Parameters.Add(new ValueLookupParameter<DoubleValue>("VelocityBoundsEndValue", "The end value of 'Value'.", new DoubleValue(1E-10)));
    143       Parameters.Add(new LookupParameter<IntValue>("VelocityBoundsIndex", "The current index.", "Iterations"));
    144       Parameters.Add(new ValueLookupParameter<IntValue>("VelocityBoundsStartIndex", "The start index at which to start modifying 'Value'.", new IntValue(0)));
    145       Parameters.Add(new ValueLookupParameter<IntValue>("VelocityBoundsEndIndex", "The end index by which 'Value' should have reached 'EndValue'.", "MaxIterations"));
    146       VelocityBoundsStartIndexParameter.Hidden = true;
    147       VelocityBoundsEndIndexParameter.Hidden = true;
     133      Parameters.Add(new OptionalConstrainedValueParameter<IDiscreteDoubleValueModifier>("MaxVelocityScalingOperator", "Modifies the value"));
     134      Parameters.Add(new ValueLookupParameter<DoubleValue>("FinalMaxVelocity", "The value of maximum velocity if PSO has reached maximum iterations.", new DoubleValue(1E-10)));
     135      Parameters.Add(new LookupParameter<IntValue>("MaxVelocityIndex", "The current index.", "Iterations"));
     136      Parameters.Add(new ValueLookupParameter<IntValue>("MaxVelocityStartIndex", "The start index at which to start modifying 'Value'.", new IntValue(0)));
     137      Parameters.Add(new ValueLookupParameter<IntValue>("MaxVelocityEndIndex", "The end index by which 'Value' should have reached 'EndValue'.", "MaxIterations"));
     138      MaxVelocityStartIndexParameter.Hidden = true;
     139      MaxVelocityEndIndexParameter.Hidden = true;
    148140      #endregion
    149141
    150142      Initialize();
    151       RegisterEvents();
    152143    }
    153144
     
    167158        Parameters.Remove("BestQuality");
    168159      }
    169       RegisterEvents();
    170     }
    171 
    172     private void RegisterEvents() {
    173       VelocityBoundsStartValueParameter.ValueChanged += new EventHandler(VelocityBoundsStartValueParameter_ValueChanged);
    174       VelocityBoundsStartValueParameter.Value.ValueChanged += new EventHandler(VelocityBoundsStartValueParameter_Value_ValueChanged);
    175     }
    176 
    177     void VelocityBoundsStartValueParameter_Value_ValueChanged(object sender, EventArgs e) {
    178       UpdateVelocityBoundsParamater();
    179     }
    180 
    181     void UpdateVelocityBoundsParamater() {
    182       if (VelocityBoundsParameter.Value == null) {
    183         VelocityBoundsParameter.Value = new DoubleMatrix(1, 2);
    184       } else if (VelocityBoundsParameter.Value.Columns != 2) {
    185         VelocityBoundsParameter.Value = new DoubleMatrix(VelocityBoundsParameter.Value.Rows, 2);
    186       }
    187       if (VelocityBoundsStartValueParameter.Value != null) {
    188         DoubleMatrix matrix = VelocityBoundsParameter.Value;
    189         for (int i = 0; i < matrix.Rows; i++) {
    190           matrix[i, 0] = (-1) * VelocityBoundsStartValueParameter.Value.Value;
    191           matrix[i, 1] = VelocityBoundsStartValueParameter.Value.Value;
    192         }
    193       }
    194     }
    195 
    196     void VelocityBoundsStartValueParameter_ValueChanged(object sender, EventArgs e) {
    197       if (VelocityBoundsStartValueParameter.Value != null) {
    198         VelocityBoundsStartValueParameter.Value.ValueChanged += new EventHandler(VelocityBoundsStartValueParameter_Value_ValueChanged);
    199       }
    200       UpdateVelocityBoundsParamater();
    201     }
    202 
     160    }
    203161    private void Initialize() {
    204162      ResultsCollector = new ResultsCollector();
    205       ResultsCollector.CollectedValues.Add(CurrentVelocityBoundsParameter);
    206       ResultsCollector.CollectedValues.Add(VelocityBoundsParameter);
     163      ResultsCollector.CollectedValues.Add(CurrentMaxVelocityParameter);
     164      ResultsCollector.CollectedValues.Add(MaxVelocityParameter);
    207165
    208166      foreach (IDiscreteDoubleValueModifier op in ApplicationManager.Manager.GetInstances<IDiscreteDoubleValueModifier>()) {
    209         VelocityBoundsScalingOperatorParameter.ValidValues.Add(op);
    210         op.ValueParameter.ActualName = VelocityBoundsScaleParameter.Name;
    211         op.StartValueParameter.ActualName = VelocityBoundsStartValueParameter.Name;
    212         op.EndValueParameter.ActualName = VelocityBoundsEndValueParameter.Name;
    213         op.IndexParameter.ActualName = VelocityBoundsIndexParameter.Name;
    214         op.StartIndexParameter.ActualName = VelocityBoundsStartIndexParameter.Name;
    215         op.EndIndexParameter.ActualName = VelocityBoundsEndIndexParameter.Name;
    216       }
    217       VelocityBoundsScalingOperatorParameter.Value = null;
     167        MaxVelocityScalingOperatorParameter.ValidValues.Add(op);
     168        op.ValueParameter.ActualName = CurrentMaxVelocityParameter.Name;
     169        op.StartValueParameter.ActualName = MaxVelocityParameter.Name;
     170        op.EndValueParameter.ActualName = FinalMaxVelocityParameter.Name;
     171        op.IndexParameter.ActualName = MaxVelocityIndexParameter.Name;
     172        op.StartIndexParameter.ActualName = MaxVelocityStartIndexParameter.Name;
     173        op.EndIndexParameter.ActualName = MaxVelocityEndIndexParameter.Name;
     174      }
     175      MaxVelocityScalingOperatorParameter.Value = null;
    218176    }
    219177
    220178    public override IOperation Apply() {
    221179      var max = MaximizationParameter.ActualValue.Value;
     180      // Update of the personal bests
    222181      var points = RealVectorParameter.ActualValue;
    223182      var qualities = QualityParameter.ActualValue;
    224183      var particles = points.Select((p, i) => new { Particle = p, Index = i })
    225184        .Zip(qualities, (p, q) => Tuple.Create(p.Index, p.Particle, q.Value)).ToList();
    226       UpdateGlobalBest(max, particles);
     185      UpdatePersonalBest(max, particles);
     186
     187      // SPSO: update of the neighbor bests from the personal bests
     188      var personalBestPoints = PersonalBestParameter.ActualValue;
     189      var personalBestQualities = PersonalBestQualityParameter.ActualValue;
     190      particles = personalBestPoints.Select((p, i) => new { Particle = p, Index = i })
     191        .Zip(personalBestQualities, (p, q) => Tuple.Create(p.Index, p.Particle, q.Value)).ToList();
    227192      UpdateNeighborBest(max, particles);
    228       UpdatePersonalBest(max, particles);
    229       return UpdateVelocityBounds();
    230     }
    231 
    232     private void UpdateGlobalBest(bool maximization, IList<Tuple<int, RealVector, double>> particles) {
    233       var best = maximization ? particles.MaxItems(x => x.Item3).First() : particles.MinItems(x => x.Item3).First();
    234       var bestQuality = SwarmBestQualityParameter.ActualValue;
    235       if (bestQuality == null) {
    236         SwarmBestQualityParameter.ActualValue = new DoubleValue(best.Item3);
    237       } else bestQuality.Value = best.Item3;
    238       BestRealVectorParameter.ActualValue = (RealVector)best.Item2.Clone();
     193
     194      var next = new OperationCollection() { base.Apply() };
     195      next.Insert(0, ExecutionContext.CreateChildOperation(ResultsCollector));
     196      if (MaxVelocityScalingOperatorParameter.Value != null) {
     197        next.Insert(0, ExecutionContext.CreateChildOperation(MaxVelocityScalingOperatorParameter.Value));
     198      } else CurrentMaxVelocityParameter.ActualValue = new DoubleValue(MaxVelocityParameter.ActualValue.Value);
     199      return next;
    239200    }
    240201
     
    253214        NeighborBestParameter.ActualValue = neighborBest;
    254215        NeighborBestQualityParameter.ActualValue = neighborBestQuality;
     216      } else {
     217        // Neighbor best = Global best
     218        var best = maximization ? particles.MaxItems(x => x.Item3).First() : particles.MinItems(x => x.Item3).First();
     219        NeighborBestParameter.ActualValue = new ItemArray<RealVector>(particles.Select(x => best.Item2));
     220        NeighborBestQualityParameter.ActualValue = new ItemArray<DoubleValue>(particles.Select(x => new DoubleValue(best.Item3)));
    255221      }
    256222    }
     
    268234          !maximization && p.Item3 < personalBestQuality[p.Item1].Value) {
    269235          personalBestQuality[p.Item1].Value = p.Item3;
    270           personalBest[p.Item1] = p.Item2;
     236          personalBest[p.Item1] = (RealVector)p.Item2.Clone();
    271237        }
    272238      }
    273239      PersonalBestParameter.ActualValue = personalBest;
    274     }
    275 
    276     private IOperation UpdateVelocityBounds() {
    277       var currentVelocityBounds = CurrentVelocityBoundsParameter.ActualValue;
    278 
    279       if (currentVelocityBounds == null) {
    280         currentVelocityBounds = (DoubleMatrix)VelocityBoundsParameter.ActualValue.Clone();
    281         CurrentVelocityBoundsParameter.ActualValue = currentVelocityBounds;
    282       }
    283       if (VelocityBoundsScalingOperatorParameter.Value == null)
    284         return new OperationCollection() {
    285           ExecutionContext.CreateChildOperation(ResultsCollector),       
    286           base.Apply()
    287         };
    288 
    289       var velocityBoundsScale = VelocityBoundsScaleParameter.ActualValue;
    290       var velocityBoundsStartValue = VelocityBoundsStartValueParameter.ActualValue;
    291 
    292       if (velocityBoundsScale == null && velocityBoundsStartValue != null) {
    293         velocityBoundsScale = new DoubleValue(velocityBoundsStartValue.Value);
    294         VelocityBoundsScaleParameter.ActualValue = velocityBoundsScale;
    295       }
    296       for (int i = 0; i < currentVelocityBounds.Rows; i++) {
    297         for (int j = 0; j < currentVelocityBounds.Columns; j++) {
    298           if (currentVelocityBounds[i, j] >= 0) {
    299             currentVelocityBounds[i, j] = velocityBoundsScale.Value;
    300           } else {
    301             currentVelocityBounds[i, j] = (-1) * velocityBoundsScale.Value;
    302           }
    303         }
    304       }
    305 
    306       return new OperationCollection() {
    307         ExecutionContext.CreateChildOperation(ResultsCollector),
    308         ExecutionContext.CreateChildOperation(VelocityBoundsScalingOperatorParameter.Value),
    309         base.Apply()
    310       };
    311240    }
    312241  }
Note: See TracChangeset for help on using the changeset viewer.