Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2817-BinPackingSpeedup/HeuristicLab.Encodings.RealVectorEncoding/3.3/ParticleOperators/RealVectorParticleCreator.cs @ 16048

Last change on this file since 16048 was 15102, checked in by abeham, 7 years ago

#2797:

  • Recreated backwards compatibility by readding old operators and renaming new operators to SPSO*
    • If a previously configured algorithm is run again, the same results should be obtained
  • Set all old operators to internal, NonDiscoverableType, and Obsolete (they are also not fixed, e.g. PersonalBest update remains flawed)
  • Added SPSO 2007 velocity initializer and let users choose in SPSOParticleCreator
  • Changed description of PSO
  • Updated sample
File size: 5.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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 System;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Operators;
27using HeuristicLab.Optimization;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.PluginInfrastructure;
31
32namespace HeuristicLab.Encodings.RealVectorEncoding {
33  [Item("RealVectorParticleCreator", "Creates a particle with position, zero velocity vector and personal best.")]
34  [StorableClass]
35  [NonDiscoverableType]
36  [Obsolete("Use SPSOParticleCreator")]
37  internal class RealVectorParticleCreator : AlgorithmOperator, IRealVectorParticleCreator {
38
39    #region Parameters
40    public ILookupParameter<IntValue> ProblemSizeParameter {
41      get { return (ILookupParameter<IntValue>)Parameters["ProblemSize"]; }
42    }
43    public IValueLookupParameter<DoubleMatrix> BoundsParameter {
44      get { return (IValueLookupParameter<DoubleMatrix>)Parameters["Bounds"]; }
45    }
46    public ILookupParameter<RealVector> RealVectorParameter {
47      get { return (ILookupParameter<RealVector>)Parameters["RealVector"]; }
48    }
49    public ILookupParameter<RealVector> PersonalBestParameter {
50      get { return (ILookupParameter<RealVector>)Parameters["PersonalBest"]; }
51    }
52    public ILookupParameter<RealVector> VelocityParameter {
53      get { return (ILookupParameter<RealVector>)Parameters["Velocity"]; }
54    }
55    public ILookupParameter<ISolutionCreator> SolutionCreatorParameter {
56      get { return (ILookupParameter<ISolutionCreator>)Parameters["SolutionCreator"]; }
57    }
58    #endregion
59
60    #region Parameter Values
61    protected int ProblemSize {
62      get { return ProblemSizeParameter.ActualValue.Value; }
63    }
64    protected RealVector Velocity {
65      set { VelocityParameter.ActualValue = value; }
66    }
67    #endregion
68
69    #region Construction & Cloning
70    [StorableConstructor]
71    protected RealVectorParticleCreator(bool deserializing) : base(deserializing) { }
72    protected RealVectorParticleCreator(RealVectorParticleCreator original, Cloner cloner) : base(original, cloner) { }
73    public RealVectorParticleCreator()
74      : base() {
75      Parameters.Add(new LookupParameter<IntValue>("ProblemSize", "The dimension of the problem."));
76      Parameters.Add(new ValueLookupParameter<DoubleMatrix>("Bounds", "The lower and upper bounds in each dimension."));
77      Parameters.Add(new LookupParameter<RealVector>("RealVector", "Particle's current solution"));
78      Parameters.Add(new LookupParameter<RealVector>("PersonalBest", "Particle's personal best solution."));
79      Parameters.Add(new LookupParameter<RealVector>("Velocity", "Particle's current velocity."));
80      Parameters.Add(new LookupParameter<ISolutionCreator>("SolutionCreator", "The operator that creates the initial position."));
81
82      UniformRandomRealVectorCreator realVectorCreater = new UniformRandomRealVectorCreator();
83      Assigner personalBestPositionAssigner = new Assigner();
84
85      OperatorGraph.InitialOperator = realVectorCreater;
86
87      realVectorCreater.RealVectorParameter.ActualName = RealVectorParameter.Name;
88      realVectorCreater.LengthParameter.ActualName = ProblemSizeParameter.Name;
89      realVectorCreater.BoundsParameter.ActualName = BoundsParameter.Name;
90      realVectorCreater.Successor = personalBestPositionAssigner;
91
92      personalBestPositionAssigner.LeftSideParameter.ActualName = PersonalBestParameter.Name;
93      personalBestPositionAssigner.RightSideParameter.ActualName = RealVectorParameter.Name;
94      personalBestPositionAssigner.Successor = null;
95    }
96    public override IDeepCloneable Clone(Cloner cloner) {
97      return new RealVectorParticleCreator(this, cloner);
98    }
99    #endregion
100
101    public override IOperation Apply() {
102      Velocity = new RealVector(ProblemSize);
103      return base.Apply();
104    }
105   
106    [StorableHook(HookType.AfterDeserialization)]
107    private void AfterDeserialization() {
108      if (!Parameters.ContainsKey("SolutionCreator"))
109        Parameters.Add(new LookupParameter<ISolutionCreator>("SolutionCreator", "The operator that creates the initial position."));
110    }
111  }
112}
Note: See TracBrowser for help on using the repository browser.