Changeset 5796 for branches/DataAnalysis Refactoring/HeuristicLab.Algorithms.ParticleSwarmOptimization
- Timestamp:
- 03/22/11 16:45:46 (14 years ago)
- Location:
- branches/DataAnalysis Refactoring/HeuristicLab.Algorithms.ParticleSwarmOptimization/3.3
- Files:
-
- 8 edited
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
branches/DataAnalysis Refactoring/HeuristicLab.Algorithms.ParticleSwarmOptimization/3.3/HeuristicLab.Algorithms.ParticleSwarmOptimization-3.3.csproj
r5426 r5796 106 106 </ItemGroup> 107 107 <ItemGroup> 108 <Compile Include="BestPointInitializer.cs" />109 <Compile Include="IDiscreteDoubleMatrixModifier.cs" />110 <Compile Include="IGlobalParticleUpdater.cs" />111 <Compile Include="ILocalParticleUpdater.cs" />112 108 <Compile Include="MultiPSOTopologyUpdater.cs" /> 109 <Compile Include="ParticleSwarmOptimizationMainLoop.cs" /> 113 110 <Compile Include="VelocityBoundsModifier.cs" /> 114 111 <Compile Include="RandomTopologyInitializer.cs" /> 115 112 <Compile Include="VonNeumannTopologyInitializer.cs" /> 116 113 <Compile Include="RingTopologyInitializer.cs" /> 117 <Compile Include="ITopologyUpdater.cs" />118 <Compile Include="NeighborUpdater.cs" />119 114 <Compile Include="TopologyInitializer.cs" /> 120 <Compile Include="ITopologyInitializer.cs" />121 <Compile Include="NeighborhoodParticleUpdater.cs" />122 <Compile Include="IParticleUpdater.cs" />123 <Compile Include="TotallyConnectedParticleUpdater.cs" />124 115 <Compile Include="HeuristicLabAlgorithmsParticleSwarmOptimizationPlugin.cs" /> 125 116 <Compile Include="ParticleSwarmOptimization.cs" /> 126 117 <Compile Include="Properties\AssemblyInfo.cs" /> 127 <Compile Include="ParticleUpdater.cs" />128 <Compile Include="SwarmUpdater.cs" />129 118 </ItemGroup> 130 119 <ItemGroup> … … 170 159 <Project>{BBAB9DF5-5EF3-4BA8-ADE9-B36E82114937}</Project> 171 160 <Name>HeuristicLab.Data-3.3</Name> 172 </ProjectReference>173 <ProjectReference Include="..\..\HeuristicLab.Encodings.IntegerVectorEncoding\3.3\HeuristicLab.Encodings.IntegerVectorEncoding-3.3.csproj">174 <Project>{DDFB14DD-2A85-493C-A52D-E69729BBAEB0}</Project>175 <Name>HeuristicLab.Encodings.IntegerVectorEncoding-3.3</Name>176 </ProjectReference>177 <ProjectReference Include="..\..\HeuristicLab.Encodings.RealVectorEncoding\3.3\HeuristicLab.Encodings.RealVectorEncoding-3.3.csproj">178 <Project>{BB6D334A-4BB6-4674-9883-31A6EBB32CAB}</Project>179 <Name>HeuristicLab.Encodings.RealVectorEncoding-3.3</Name>180 161 </ProjectReference> 181 162 <ProjectReference Include="..\..\HeuristicLab.Operators\3.3\HeuristicLab.Operators-3.3.csproj"> -
branches/DataAnalysis Refactoring/HeuristicLab.Algorithms.ParticleSwarmOptimization/3.3/HeuristicLabAlgorithmsParticleSwarmOptimizationPlugin.cs.frame
r5446 r5796 33 33 [PluginDependency("HeuristicLab.Core", "3.3")] 34 34 [PluginDependency("HeuristicLab.Data", "3.3")] 35 [PluginDependency("HeuristicLab.Encodings.RealVectorEncoding", "3.3")]36 [PluginDependency("HeuristicLab.Encodings.IntegerVectorEncoding", "3.3")]37 35 [PluginDependency("HeuristicLab.Operators", "3.3")] 38 36 [PluginDependency("HeuristicLab.Optimization", "3.3")] -
branches/DataAnalysis Refactoring/HeuristicLab.Algorithms.ParticleSwarmOptimization/3.3/MultiPSOTopologyUpdater.cs
r5435 r5796 25 25 using HeuristicLab.Core; 26 26 using HeuristicLab.Data; 27 using HeuristicLab.Encodings.IntegerVectorEncoding;28 27 using HeuristicLab.Operators; 28 using HeuristicLab.Optimization; 29 29 using HeuristicLab.Parameters; 30 30 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 31 31 32 32 namespace HeuristicLab.Algorithms.ParticleSwarmOptimization { 33 [Item("Multi PSO Topology Initializer/Updater", "Splits swarm into swarmsize / (nrOfConnections + 1)non-overlapping sub-swarms. Swarms are re-grouped every regroupingPeriod iteration. The operator is implemented as described in Liang, J.J. and Suganthan, P.N 2005. Dynamic multi-swarm particle swarm optimizer. IEEE Swarm Intelligence Symposium, pp. 124-129.")]33 [Item("Multi PSO Topology Updater", "Splits swarm into NrOfSwarms non-overlapping sub-swarms. Swarms are re-grouped every regroupingPeriod iteration. The operator is implemented as described in Liang, J.J. and Suganthan, P.N 2005. Dynamic multi-swarm particle swarm optimizer. IEEE Swarm Intelligence Symposium, pp. 124-129.")] 34 34 [StorableClass] 35 public sealed class MultiPSOTopologyUpdater : SingleSuccessorOperator, ITopologyUpdater, ITopologyInitializer { 35 public sealed class MultiPSOTopologyUpdater : SingleSuccessorOperator, ITopologyUpdater { 36 36 37 public override bool CanChangeName { 37 38 get { return false; } … … 42 43 get { return (ILookupParameter<IRandom>)Parameters["Random"]; } 43 44 } 44 public IValueLookupParameter<IntValue> NrOf ConnectionsParameter {45 get { return (IValueLookupParameter<IntValue>)Parameters["NrOf Connections"]; }45 public IValueLookupParameter<IntValue> NrOfSwarmsParameter { 46 get { return (IValueLookupParameter<IntValue>)Parameters["NrOfSwarms"]; } 46 47 } 47 48 public ILookupParameter<IntValue> SwarmSizeParameter { 48 49 get { return (ILookupParameter<IntValue>)Parameters["SwarmSize"]; } 49 50 } 50 public IScopeTreeLookupParameter<Int egerVector> NeighborsParameter {51 get { return (IScopeTreeLookupParameter<Int egerVector>)Parameters["Neighbors"]; }51 public IScopeTreeLookupParameter<IntArray> NeighborsParameter { 52 get { return (IScopeTreeLookupParameter<IntArray>)Parameters["Neighbors"]; } 52 53 } 53 54 public ILookupParameter<IntValue> CurrentIterationParameter { … … 63 64 get { return RandomParameter.ActualValue; } 64 65 } 65 private int NrOf Connections {66 get { return NrOf ConnectionsParameter.ActualValue.Value; }66 private int NrOfSwarms { 67 get { return NrOfSwarmsParameter.ActualValue.Value; } 67 68 } 68 69 private int SwarmSize { 69 70 get { return SwarmSizeParameter.ActualValue.Value; } 70 71 } 71 private ItemArray<Int egerVector> Neighbors {72 private ItemArray<IntArray> Neighbors { 72 73 get { return NeighborsParameter.ActualValue; } 73 74 set { NeighborsParameter.ActualValue = value; } … … 81 82 #endregion 82 83 84 #region Construction & Cloning 83 85 [StorableConstructor] 84 86 private MultiPSOTopologyUpdater(bool deserializing) : base(deserializing) { } … … 87 89 : base() { 88 90 Parameters.Add(new LookupParameter<IRandom>("Random", "A random number generator.")); 89 Parameters.Add(new ValueLookupParameter<IntValue>("NrOf Connections", "Nr of connected neighbors.", new IntValue(3)));91 Parameters.Add(new ValueLookupParameter<IntValue>("NrOfSwarms", "Nr of connected sub-swarms.", new IntValue(3))); 90 92 Parameters.Add(new LookupParameter<IntValue>("SwarmSize", "Number of particles in the swarm.")); 91 Parameters.Add(new ScopeTreeLookupParameter<Int egerVector>("Neighbors", "The list of neighbors for each particle."));93 Parameters.Add(new ScopeTreeLookupParameter<IntArray>("Neighbors", "The list of neighbors for each particle.")); 92 94 Parameters.Add(new LookupParameter<IntValue>("CurrentIteration", "The current iteration of the algorithm.")); 93 95 Parameters.Add(new ValueLookupParameter<IntValue>("RegroupingPeriod", "Update interval (=iterations) for regrouping of neighborhoods.", new IntValue(5))); 94 96 } 95 96 97 public override IDeepCloneable Clone(Cloner cloner) { 97 98 return new MultiPSOTopologyUpdater(this, cloner); 98 99 } 100 #endregion 99 101 100 // Splits the swarm into non-overlapping sub swarms101 102 public override IOperation Apply() { 102 if (CurrentIteration % RegroupingPeriod == 0) { 103 ItemArray<IntegerVector> neighbors = new ItemArray<IntegerVector>(SwarmSize); 104 Dictionary<int, List<int>> neighborsPerParticle = new Dictionary<int, List<int>>(); 105 for (int i = 0; i < SwarmSize; i++) { 106 neighborsPerParticle.Add(i, new List<int>()); 103 if (CurrentIteration > 0 && CurrentIteration % RegroupingPeriod == 0) { 104 ItemArray<IntArray> neighbors = new ItemArray<IntArray>(SwarmSize); 105 106 var particles = Enumerable.Range(0, SwarmSize).ToList(); 107 for (int i = SwarmSize-1; i>0; i--) { 108 int j = Random.Next(i+1); 109 int t = particles[j]; 110 particles[j] = particles[i]; 111 particles[i] = t; 107 112 } 108 113 109 // partition swarm into groups 110 Dictionary<int, List<int>> groups = new Dictionary<int, List<int>>(); 111 int groupId = 0; 112 var numbers = Enumerable.Range(0, SwarmSize).ToList(); 113 for (int i = 0; i < SwarmSize; i++) { 114 int nextParticle = numbers[Random.Next(0, numbers.Count)]; 115 if (!groups.ContainsKey(groupId)) { 116 groups.Add(groupId, new List<int>()); 117 } 118 groups[groupId].Add(nextParticle); 119 if (groups[groupId].Count - 1 == NrOfConnections) { 120 groupId++; 121 } 122 numbers.Remove(nextParticle); 114 for (int partitionNr = 0; partitionNr<NrOfSwarms; partitionNr++) { 115 int start = partitionNr*SwarmSize/NrOfSwarms; 116 int end = (partitionNr+1)*SwarmSize/NrOfSwarms; 117 for (int i = start; i<end; i++) 118 neighbors[particles[i]] = GetSegment(particles, start, end, i); 123 119 } 124 120 125 // add neighbors to each particle126 foreach (List<int> group in groups.Values) {127 foreach (int sib1 in group) {128 foreach (int sib2 in group) {129 if (sib1 != sib2 && !neighborsPerParticle[sib1].Contains(sib2)) {130 neighborsPerParticle[sib1].Add(sib2);131 }132 }133 }134 }135 136 for (int particle = 0; particle < neighborsPerParticle.Count; particle++) {137 neighbors[particle] = new IntegerVector(neighborsPerParticle[particle].ToArray());138 }139 121 Neighbors = neighbors; 140 122 } 141 123 return base.Apply(); 142 124 } 125 126 public static IntArray GetSegment(IEnumerable<int> list, int start, int end, int excludedIndex) { 127 return new IntArray(list 128 .Skip(start) 129 .Take(end-start) 130 .Where((p, j) => start+j != excludedIndex) 131 .ToArray()); 132 } 143 133 } 144 134 } -
branches/DataAnalysis Refactoring/HeuristicLab.Algorithms.ParticleSwarmOptimization/3.3/ParticleSwarmOptimization.cs
r5644 r5796 26 26 using HeuristicLab.Core; 27 27 using HeuristicLab.Data; 28 using HeuristicLab.Encodings.RealVectorEncoding;29 28 using HeuristicLab.Operators; 30 29 using HeuristicLab.Optimization; … … 53 52 set { AnalyzerParameter.Value = value; } 54 53 } 55 public IDiscreteDoubleValueModifier OmegaUpdater { 56 get { return OmegaUpdaterParameter.Value; } 57 set { OmegaUpdaterParameter.Value = value; } 58 } 59 public IDiscreteDoubleMatrixModifier VelocityBoundsUpdater { 60 get { return VelocityBoundsUpdaterParameter.Value; } 61 set { VelocityBoundsUpdaterParameter.Value = value; } 54 public IDiscreteDoubleValueModifier InertiaUpdater { 55 get { return InertiaUpdaterParameter.Value; } 56 set { InertiaUpdaterParameter.Value = value; } 62 57 } 63 58 #endregion … … 76 71 get { return (IValueParameter<IntValue>)Parameters["MaxIterations"]; } 77 72 } 78 public IValueParameter<DoubleValue> OmegaParameter {79 get { return (IValueParameter<DoubleValue>)Parameters[" Omega"]; }80 } 81 public IValueParameter<DoubleValue> P hi_PParameter {82 get { return (IValueParameter<DoubleValue>)Parameters["P hi_P"]; }83 } 84 public IValueParameter<DoubleValue> Phi_GParameter {85 get { return (IValueParameter<DoubleValue>)Parameters[" Phi_G"]; }73 public IValueParameter<DoubleValue> InertiaParameter { 74 get { return (IValueParameter<DoubleValue>)Parameters["Inertia"]; } 75 } 76 public IValueParameter<DoubleValue> PersonalBestAttractionParameter { 77 get { return (IValueParameter<DoubleValue>)Parameters["PersonalBestAttraction"]; } 78 } 79 public IValueParameter<DoubleValue> NeighborBestAttractionParameter { 80 get { return (IValueParameter<DoubleValue>)Parameters["NeighborBestAttraction"]; } 86 81 } 87 82 public IValueParameter<MultiAnalyzer> AnalyzerParameter { 88 83 get { return (IValueParameter<MultiAnalyzer>)Parameters["Analyzer"]; } 89 84 } 90 public IValueLookupParameter<DoubleMatrix> VelocityBoundsParameter {91 get { return ( IValueLookupParameter<DoubleMatrix>)Parameters["VelocityBounds"]; }85 public ConstrainedValueParameter<IParticleCreator> ParticleCreatorParameter { 86 get { return (ConstrainedValueParameter<IParticleCreator>)Parameters["ParticleCreator"]; } 92 87 } 93 88 public ConstrainedValueParameter<IParticleUpdater> ParticleUpdaterParameter { … … 100 95 get { return (OptionalConstrainedValueParameter<ITopologyUpdater>)Parameters["TopologyUpdater"]; } 101 96 } 102 public OptionalConstrainedValueParameter<IDiscreteDoubleMatrixModifier> VelocityBoundsUpdaterParameter { 103 get { return (OptionalConstrainedValueParameter<IDiscreteDoubleMatrixModifier>)Parameters["VelocityBoundsUpdater"]; } 104 } 105 public OptionalConstrainedValueParameter<IDiscreteDoubleValueModifier> OmegaUpdaterParameter { 106 get { return (OptionalConstrainedValueParameter<IDiscreteDoubleValueModifier>)Parameters["OmegaUpdater"]; } 97 public OptionalConstrainedValueParameter<IDiscreteDoubleValueModifier> InertiaUpdaterParameter { 98 get { return (OptionalConstrainedValueParameter<IDiscreteDoubleValueModifier>)Parameters["InertiaUpdater"]; } 99 } 100 public ConstrainedValueParameter<ISwarmUpdater> SwarmUpdaterParameter { 101 get { return (ConstrainedValueParameter<ISwarmUpdater>)Parameters["SwarmUpdater"]; } 102 107 103 } 108 104 #endregion … … 114 110 [Storable] 115 111 private BestAverageWorstQualityAnalyzer qualityAnalyzer; 112 113 [Storable] 114 private SolutionsCreator solutionsCreator; 115 116 [Storable] 117 private ParticleSwarmOptimizationMainLoop mainLoop; 116 118 117 119 public ITopologyInitializer TopologyInitializer { … … 125 127 } 126 128 129 public IParticleCreator ParticleCreator { 130 get { return ParticleCreatorParameter.Value; } 131 set { ParticleCreatorParameter.Value = value; } 132 } 133 127 134 public IParticleUpdater ParticleUpdater { 128 135 get { return ParticleUpdaterParameter.Value; } 129 136 set { ParticleUpdaterParameter.Value = value; } 130 137 } 131 132 138 #endregion 133 139 … … 137 143 : base(original, cloner) { 138 144 qualityAnalyzer = cloner.Clone(original.qualityAnalyzer); 145 solutionsCreator = cloner.Clone(original.solutionsCreator); 146 mainLoop = cloner.Clone(original.mainLoop); 139 147 Initialize(); 140 148 } … … 146 154 Parameters.Add(new ValueParameter<IntValue>("MaxIterations", "Maximal number of iterations.", new IntValue(1000))); 147 155 Parameters.Add(new ValueParameter<MultiAnalyzer>("Analyzer", "The operator used to analyze each generation.", new MultiAnalyzer())); 148 Parameters.Add(new ValueParameter<DoubleValue>("Omega", "Weight for particle's velocity vector.", new DoubleValue(-0.2))); 149 Parameters.Add(new ValueParameter<DoubleValue>("Phi_P", "Weight for particle's personal best position.", new DoubleValue(-0.01))); 150 Parameters.Add(new ValueParameter<DoubleValue>("Phi_G", "Weight for global best position.", new DoubleValue(3.7))); 151 Parameters.Add(new ValueLookupParameter<DoubleMatrix>("VelocityBounds", "Maximum Velocity in every dimension", new DoubleMatrix(new double[,] { { -1, 1 } }))); 152 Parameters.Add(new ConstrainedValueParameter<IParticleUpdater>("ParticleUpdater", "Operator that calculates new position and velocity of a particle")); 153 Parameters.Add(new OptionalConstrainedValueParameter<ITopologyInitializer>("TopologyInitializer", "Creates neighborhood description vectors")); 154 Parameters.Add(new OptionalConstrainedValueParameter<ITopologyUpdater>("TopologyUpdater", "Updates the neighborhood description vectors")); 155 Parameters.Add(new OptionalConstrainedValueParameter<IDiscreteDoubleValueModifier>("OmegaUpdater", "Updates the omega parameter")); 156 Parameters.Add(new OptionalConstrainedValueParameter<IDiscreteDoubleMatrixModifier>("VelocityBoundsUpdater", "Adjusts the velocity bounds.")); 157 ParticleUpdaterParameter.ActualValue = ParticleUpdaterParameter.ValidValues.SingleOrDefault(v => v.GetType() == typeof(TotallyConnectedParticleUpdater)); 156 Parameters.Add(new ValueParameter<DoubleValue>("Inertia", "Inertia weight on a particle's movement (omega).", new DoubleValue(-0.2))); 157 Parameters.Add(new ValueParameter<DoubleValue>("PersonalBestAttraction", "Weight for particle's pull towards its personal best soution (phi_p).", new DoubleValue(-0.01))); 158 Parameters.Add(new ValueParameter<DoubleValue>("NeighborBestAttraction", "Weight for pull towards the neighborhood best solution or global best solution in case of a totally connected topology (phi_g).", new DoubleValue(3.7))); 159 Parameters.Add(new ConstrainedValueParameter<IParticleCreator>("ParticleCreator", "Operator creates a new particle.")); 160 Parameters.Add(new ConstrainedValueParameter<IParticleUpdater>("ParticleUpdater", "Operator that updates a particle.")); 161 Parameters.Add(new OptionalConstrainedValueParameter<ITopologyInitializer>("TopologyInitializer", "Creates neighborhood description vectors.")); 162 Parameters.Add(new OptionalConstrainedValueParameter<ITopologyUpdater>("TopologyUpdater", "Updates the neighborhood description vectors.")); 163 Parameters.Add(new OptionalConstrainedValueParameter<IDiscreteDoubleValueModifier>("InertiaUpdater", "Updates the omega parameter.")); 164 Parameters.Add(new ConstrainedValueParameter<ISwarmUpdater>("SwarmUpdater", "Encoding-specific parameter which is provided by the problem. May provide additional encoding-specific parameters, such as velocity bounds for real valued problems")); 158 165 159 166 RandomCreator randomCreator = new RandomCreator(); 160 167 VariableCreator variableCreator = new VariableCreator(); 161 SolutionsCreator solutionsCreator = new SolutionsCreator(); 162 CombinedOperator particleCreator = new CombinedOperator(); 163 Placeholder evaluatorPlaceholder = new Placeholder(); 164 Assigner bestPersonalQualityAssigner = new Assigner(); 165 BestPointInitializer bestPositionInitializer = new BestPointInitializer(); 168 Assigner assigner = new Assigner(); 169 solutionsCreator = new SolutionsCreator(); 170 SubScopesCounter subScopesCounter = new SubScopesCounter(); 166 171 Placeholder topologyInitializerPlaceholder = new Placeholder(); 167 NeighborUpdater neighborUpdater = new NeighborUpdater();168 172 Placeholder analyzerPlaceholder = new Placeholder(); 169 UniformSubScopesProcessor uniformSubScopeProcessor = new UniformSubScopesProcessor(); 170 Placeholder particleUpdaterPlaceholder = new Placeholder(); 171 Placeholder topologyUpdaterPlaceholder = new Placeholder(); 172 UniformSubScopesProcessor uniformSubscopesProcessor2 = new UniformSubScopesProcessor(); 173 UniformSubScopesProcessor evaluationProcessor = new UniformSubScopesProcessor(); 174 NeighborUpdater neighborUpdater2 = new NeighborUpdater(); 175 Placeholder evaluatorPlaceholder2 = new Placeholder(); 176 SwarmUpdater swarmUpdater = new SwarmUpdater(); 177 Placeholder analyzerPlaceholder2 = new Placeholder(); 178 IntCounter currentIterationCounter = new IntCounter(); 179 Comparator currentIterationComparator = new Comparator(); 180 ConditionalBranch conditionalBranch = new ConditionalBranch(); 181 Placeholder velocityBoundsUpdaterPlaceholder = new Placeholder(); 182 Placeholder omegaUpdaterPlaceholder = new Placeholder(); 173 mainLoop = new ParticleSwarmOptimizationMainLoop(); 183 174 184 175 OperatorGraph.InitialOperator = randomCreator; … … 189 180 190 181 variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("CurrentIteration", new IntValue(0))); 191 variableCreator.Successor = solutionsCreator; 182 variableCreator.CollectedValues.Add(new ValueParameter<DoubleValue>("CurrentVelocityBounds", new DoubleValue(0))); 183 variableCreator.Successor = assigner; 184 185 assigner.Name = "CurrentInertia := Inertia"; 186 assigner.LeftSideParameter.ActualName = "CurrentInertia"; 187 assigner.RightSideParameter.ActualName = "Inertia"; 188 assigner.Successor = solutionsCreator; 192 189 193 190 solutionsCreator.NumberOfSolutionsParameter.ActualName = "SwarmSize"; 194 solutionsCreator.EvaluatorParameter.Value = evaluatorPlaceholder; 195 solutionsCreator.SolutionCreatorParameter.Value = particleCreator; 196 solutionsCreator.Successor = bestPositionInitializer; 197 198 InitializeParticleCreator(particleCreator); 199 200 evaluatorPlaceholder.Name = "(Evaluator)"; 201 evaluatorPlaceholder.OperatorParameter.ActualName = "Evaluator"; 202 evaluatorPlaceholder.Successor = bestPersonalQualityAssigner; 203 204 bestPersonalQualityAssigner.LeftSideParameter.ActualName = "PersonalBestQuality"; 205 bestPersonalQualityAssigner.RightSideParameter.ActualName = "Quality"; 206 207 bestPositionInitializer.Successor = topologyInitializerPlaceholder; 191 ParameterizeSolutionsCreator(); 192 solutionsCreator.Successor = subScopesCounter; 193 194 subScopesCounter.Name = "Initialize EvaluatedSolutions"; 195 subScopesCounter.ValueParameter.ActualName = "EvaluatedSolutions"; 196 subScopesCounter.Successor = topologyInitializerPlaceholder; 208 197 209 198 topologyInitializerPlaceholder.Name = "(TopologyInitializer)"; 210 199 topologyInitializerPlaceholder.OperatorParameter.ActualName = "TopologyInitializer"; 211 topologyInitializerPlaceholder.Successor = neighborUpdater; 212 213 neighborUpdater.Successor = analyzerPlaceholder; 214 215 analyzerPlaceholder.Name = "(Analyzer)"; 216 analyzerPlaceholder.OperatorParameter.ActualName = "Analyzer"; 217 analyzerPlaceholder.Successor = uniformSubScopeProcessor; 218 219 uniformSubScopeProcessor.Operator = particleUpdaterPlaceholder; 220 uniformSubScopeProcessor.Successor = evaluationProcessor; 221 222 particleUpdaterPlaceholder.Name = "(ParticleUpdater)"; 223 particleUpdaterPlaceholder.OperatorParameter.ActualName = "ParticleUpdater"; 224 225 evaluationProcessor.Parallel = new BoolValue(true); 226 evaluationProcessor.Operator = evaluatorPlaceholder2; 227 evaluationProcessor.Successor = topologyUpdaterPlaceholder; 228 229 evaluatorPlaceholder2.Name = "(Evaluator)"; 230 evaluatorPlaceholder2.OperatorParameter.ActualName = "Evaluator"; 231 232 topologyUpdaterPlaceholder.Name = "(TopologyUpdater)"; 233 topologyUpdaterPlaceholder.OperatorParameter.ActualName = "TopologyUpdater"; 234 topologyUpdaterPlaceholder.Successor = neighborUpdater2; 235 236 neighborUpdater2.Successor = uniformSubscopesProcessor2; 237 238 uniformSubscopesProcessor2.Operator = swarmUpdater; 239 uniformSubscopesProcessor2.Successor = analyzerPlaceholder2; 240 241 analyzerPlaceholder2.Name = "(Analyzer)"; 242 analyzerPlaceholder2.OperatorParameter.ActualName = "Analyzer"; 243 analyzerPlaceholder2.Successor = currentIterationCounter; 244 245 currentIterationCounter.Name = "CurrentIteration++"; 246 currentIterationCounter.ValueParameter.ActualName = "CurrentIteration"; 247 currentIterationCounter.Successor = omegaUpdaterPlaceholder; 248 249 omegaUpdaterPlaceholder.Name = "(Omega Updater)"; 250 omegaUpdaterPlaceholder.OperatorParameter.ActualName = "OmegaUpdater"; 251 omegaUpdaterPlaceholder.Successor = velocityBoundsUpdaterPlaceholder; 252 253 velocityBoundsUpdaterPlaceholder.Name = "(Velocity Bounds Updater)"; 254 velocityBoundsUpdaterPlaceholder.OperatorParameter.ActualName = "VelocityBoundsUpdater"; 255 velocityBoundsUpdaterPlaceholder.Successor = currentIterationComparator; 256 257 currentIterationComparator.LeftSideParameter.ActualName = "CurrentIteration"; 258 currentIterationComparator.Comparison = new Comparison(ComparisonType.Less); 259 currentIterationComparator.RightSideParameter.ActualName = "MaxIterations"; 260 currentIterationComparator.ResultParameter.ActualName = "ContinueIteration"; 261 currentIterationComparator.Successor = conditionalBranch; 262 263 conditionalBranch.Name = "ContinueIteration?"; 264 conditionalBranch.ConditionParameter.ActualName = "ContinueIteration"; 265 conditionalBranch.TrueBranch = uniformSubScopeProcessor; 200 topologyInitializerPlaceholder.Successor = mainLoop; 201 202 mainLoop.AnalyzerParameter.ActualName = AnalyzerParameter.Name; 203 mainLoop.InertiaParameter.ActualName = "CurrentInertia"; 204 mainLoop.MaxIterationsParameter.ActualName = MaxIterationsParameter.Name; 205 mainLoop.NeighborBestAttractionParameter.ActualName = NeighborBestAttractionParameter.Name; 206 mainLoop.InertiaUpdaterParameter.ActualName = InertiaUpdaterParameter.Name; 207 mainLoop.ParticleUpdaterParameter.ActualName = ParticleUpdaterParameter.Name; 208 mainLoop.PersonalBestAttractionParameter.ActualName = PersonalBestAttractionParameter.Name; 209 mainLoop.RandomParameter.ActualName = randomCreator.RandomParameter.ActualName; 210 mainLoop.SwarmSizeParameter.ActualName = SwarmSizeParameter.Name; 211 mainLoop.TopologyUpdaterParameter.ActualName = TopologyUpdaterParameter.Name; 212 mainLoop.RandomParameter.ActualName = randomCreator.RandomParameter.ActualName; 213 mainLoop.ResultsParameter.ActualName = "Results"; 266 214 267 215 InitializeAnalyzers(); 268 InitVelocityBoundsUpdater(); 216 InitializeParticleCreator(); 217 InitializeSwarmUpdater(); 218 ParameterizeSolutionsCreator(); 269 219 UpdateAnalyzers(); 270 Update OmegaUpdater();271 Init OmegaUpdater();220 UpdateInertiaUpdater(); 221 InitInertiaUpdater(); 272 222 UpdateTopologyInitializer(); 273 223 Initialize(); 224 ParameterizeMainLoop(); 274 225 } 275 226 … … 284 235 285 236 public override void Prepare() { 286 if (Problem != null ) {237 if (Problem != null && ParticleCreator != null && ParticleUpdater != null) { 287 238 base.Prepare(); 288 if (OmegaUpdater != null && OmegaUpdater.StartValueParameter.Value != null) {289 this.OmegaParameter.ActualValue = new DoubleValue(OmegaUpdaterParameter.Value.StartValueParameter.Value.Value);290 }291 if (VelocityBoundsUpdater != null && VelocityBoundsUpdater.StartValueParameter.Value != null && VelocityBoundsParameter.Value != null) {292 DoubleMatrix matrix = VelocityBoundsParameter.Value;293 for (int i = 0; i < matrix.Rows; i++) {294 matrix[i, 0] = -VelocityBoundsUpdater.StartValueParameter.Value.Value;295 matrix[i, 1] = VelocityBoundsUpdater.StartValueParameter.Value.Value;296 }297 }298 239 } 299 240 } … … 303 244 UpdateAnalyzers(); 304 245 ParameterizeAnalyzers(); 246 UpdateTopologyParameters(); 247 InitializeParticleCreator(); 248 InitializeSwarmUpdater(); 249 ParameterizeSolutionsCreator(); 305 250 base.OnProblemChanged(); 306 251 } … … 308 253 void TopologyInitializerParameter_ValueChanged(object sender, EventArgs e) { 309 254 this.UpdateTopologyParameters(); 310 }311 312 void VelocityBoundsUpdaterParameter_ValueChanged(object sender, EventArgs e) {313 if (VelocityBoundsParameter.Value != null) {314 foreach (IDiscreteDoubleMatrixModifier matrixOp in VelocityBoundsUpdaterParameter.Value.ScalingOperatorParameter.ValidValues) {315 matrixOp.ValueParameter.ActualName = VelocityBoundsUpdater.ScaleParameter.Name;316 matrixOp.StartValueParameter.Value = new DoubleValue(VelocityBoundsUpdater.ScaleParameter.ActualValue.Value);317 }318 }319 255 } 320 256 #endregion … … 325 261 } 326 262 327 private static void InitializeParticleCreator(CombinedOperator particleCreator) { 328 Placeholder positionCreator = new Placeholder(); 329 Assigner personalBestPositionAssigner = new Assigner(); 330 UniformRandomRealVectorCreator velocityCreator = new UniformRandomRealVectorCreator(); 331 332 particleCreator.Name = "Particle Creator"; 333 particleCreator.OperatorGraph.InitialOperator = positionCreator; 334 335 positionCreator.Name = "(SolutionCreator)"; 336 positionCreator.OperatorParameter.ActualName = "SolutionCreator"; 337 positionCreator.Successor = personalBestPositionAssigner; 338 339 personalBestPositionAssigner.LeftSideParameter.ActualName = "PersonalBestPoint"; 340 personalBestPositionAssigner.RightSideParameter.ActualName = "Point"; 341 personalBestPositionAssigner.Successor = velocityCreator; 342 343 velocityCreator.LengthParameter.ActualName = "ProblemSize"; 344 velocityCreator.BoundsParameter.ActualName = "VelocityBounds"; 345 velocityCreator.RealVectorParameter.ActualName = "Velocity"; 263 private void InitializeParticleCreator() { 264 if (Problem != null) { 265 IParticleCreator oldParticleCreator = ParticleCreator; 266 ParticleCreatorParameter.ValidValues.Clear(); 267 foreach (IParticleCreator Creator in Problem.Operators.OfType<IParticleCreator>().OrderBy(x => x.Name)) { 268 ParticleCreatorParameter.ValidValues.Add(Creator); 269 } 270 if (oldParticleCreator != null) { 271 IParticleCreator creator = ParticleCreatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldParticleCreator.GetType()); 272 if (creator != null) ParticleCreator = creator; 273 } 274 } 346 275 } 347 276 348 277 private void InitializeAnalyzers() { 349 278 qualityAnalyzer = new BestAverageWorstQualityAnalyzer(); 279 qualityAnalyzer.ResultsParameter.ActualName = "Results"; 350 280 ParameterizeAnalyzers(); 351 281 } … … 368 298 } 369 299 370 private void InitVelocityBoundsUpdater() { 371 foreach (IDiscreteDoubleMatrixModifier matrixOp in ApplicationManager.Manager.GetInstances<IDiscreteDoubleMatrixModifier>()) { 372 VelocityBoundsUpdaterParameter.ValidValues.Add(matrixOp); 373 matrixOp.ValueParameter.ActualName = VelocityBoundsParameter.Name; 374 matrixOp.EndIndexParameter.ActualName = MaxIterationsParameter.Name; 375 matrixOp.StartIndexParameter.Value = new IntValue(0); 376 matrixOp.IndexParameter.ActualName = "CurrentIteration"; 377 matrixOp.EndValueParameter.Value = new DoubleValue(0); 378 } 379 VelocityBoundsUpdaterParameter.ValueChanged += new EventHandler(VelocityBoundsUpdaterParameter_ValueChanged); 380 } 381 382 private void InitOmegaUpdater() { 383 foreach (IDiscreteDoubleValueModifier updater in OmegaUpdaterParameter.ValidValues) { 300 private void InitInertiaUpdater() { 301 foreach (IDiscreteDoubleValueModifier updater in InertiaUpdaterParameter.ValidValues) { 384 302 updater.EndIndexParameter.ActualName = MaxIterationsParameter.Name; 385 303 updater.StartIndexParameter.Value = new IntValue(0); 386 304 updater.IndexParameter.ActualName = "CurrentIteration"; 387 updater.ValueParameter.ActualName = OmegaParameter.Name;305 updater.ValueParameter.ActualName = "CurrentInertia"; 388 306 updater.StartValueParameter.Value = new DoubleValue(1); 389 updater.EndValueParameter.Value = new DoubleValue( 0);390 } 391 } 392 393 private void Update OmegaUpdater() {394 IDiscreteDoubleValueModifier old OmegaUpdater = OmegaUpdater;395 OmegaUpdaterParameter.ValidValues.Clear();307 updater.EndValueParameter.Value = new DoubleValue(double.Epsilon); 308 } 309 } 310 311 private void UpdateInertiaUpdater() { 312 IDiscreteDoubleValueModifier oldInertiaUpdater = InertiaUpdater; 313 InertiaUpdaterParameter.ValidValues.Clear(); 396 314 foreach (IDiscreteDoubleValueModifier updater in ApplicationManager.Manager.GetInstances<IDiscreteDoubleValueModifier>().OrderBy(x => x.Name)) { 397 OmegaUpdaterParameter.ValidValues.Add(updater);398 } 399 if (old OmegaUpdater != null) {400 IDiscreteDoubleValueModifier updater = OmegaUpdaterParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldOmegaUpdater.GetType());401 if (updater != null) OmegaUpdaterParameter.Value = updater;315 InertiaUpdaterParameter.ValidValues.Add(updater); 316 } 317 if (oldInertiaUpdater != null) { 318 IDiscreteDoubleValueModifier updater = InertiaUpdaterParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldInertiaUpdater.GetType()); 319 if (updater != null) InertiaUpdaterParameter.Value = updater; 402 320 } 403 321 } … … 418 336 IParticleUpdater oldParticleUpdater = ParticleUpdater; 419 337 ClearTopologyParameters(); 420 if (TopologyInitializer != null) { 421 foreach (ITopologyUpdater topologyUpdater in ApplicationManager.Manager.GetInstances<ITopologyUpdater>()) 422 TopologyUpdaterParameter.ValidValues.Add(topologyUpdater); 423 foreach (IParticleUpdater particleUpdater in ApplicationManager.Manager.GetInstances<ILocalParticleUpdater>()) 424 ParticleUpdaterParameter.ValidValues.Add(particleUpdater); 425 } else { 426 foreach (IParticleUpdater particleUpdater in ApplicationManager.Manager.GetInstances<IGlobalParticleUpdater>()) 427 ParticleUpdaterParameter.ValidValues.Add(particleUpdater); 428 } 429 if (oldTopologyUpdater != null) { 430 ITopologyUpdater newTopologyUpdater = TopologyUpdaterParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldParticleUpdater.GetType()); 431 if (newTopologyUpdater != null) TopologyUpdater = newTopologyUpdater; 432 } 433 if (oldParticleUpdater != null) { 434 IParticleUpdater newParticleUpdater = ParticleUpdaterParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldParticleUpdater.GetType()); 435 if (newParticleUpdater != null) ParticleUpdater = newParticleUpdater; 338 if (Problem != null) { 339 if (TopologyInitializer != null) { 340 foreach (ITopologyUpdater topologyUpdater in ApplicationManager.Manager.GetInstances<ITopologyUpdater>()) 341 TopologyUpdaterParameter.ValidValues.Add(topologyUpdater); 342 foreach (IParticleUpdater particleUpdater in Problem.Operators.OfType<ILocalParticleUpdater>().OrderBy(x => x.Name)) 343 ParticleUpdaterParameter.ValidValues.Add(particleUpdater); 344 } else { 345 foreach (IParticleUpdater particleUpdater in Problem.Operators.OfType<IGlobalParticleUpdater>().OrderBy(x => x.Name)) 346 ParticleUpdaterParameter.ValidValues.Add(particleUpdater); 347 } 348 if (oldTopologyUpdater != null) { 349 ITopologyUpdater newTopologyUpdater = TopologyUpdaterParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldParticleUpdater.GetType()); 350 if (newTopologyUpdater != null) TopologyUpdater = newTopologyUpdater; 351 } 352 if (oldParticleUpdater != null) { 353 IParticleUpdater newParticleUpdater = ParticleUpdaterParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldParticleUpdater.GetType()); 354 if (newParticleUpdater != null) ParticleUpdater = newParticleUpdater; 355 } 436 356 } 437 357 } … … 441 361 ParticleUpdaterParameter.ValidValues.Clear(); 442 362 } 363 364 private void ParameterizeSolutionsCreator() { 365 if (Problem != null) { 366 solutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name; 367 solutionsCreator.SolutionCreatorParameter.ActualName = ParticleCreatorParameter.Name; 368 } 369 } 370 371 private void ParameterizeMainLoop() { 372 if (Problem != null) { 373 mainLoop.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name; 374 } 375 } 376 377 private void InitializeSwarmUpdater() { 378 if (Problem != null) { 379 ISwarmUpdater updater = Problem.Operators.OfType<ISwarmUpdater>().FirstOrDefault(); 380 SwarmUpdaterParameter.ValidValues.Clear(); 381 SwarmUpdaterParameter.ValidValues.Add(updater); 382 SwarmUpdaterParameter.Value = updater; 383 } 384 } 443 385 #endregion 444 386 -
branches/DataAnalysis Refactoring/HeuristicLab.Algorithms.ParticleSwarmOptimization/3.3/RandomTopologyInitializer.cs
r5445 r5796 25 25 using HeuristicLab.Core; 26 26 using HeuristicLab.Data; 27 using HeuristicLab.Encodings.IntegerVectorEncoding;28 27 using HeuristicLab.Parameters; 29 28 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; … … 66 65 67 66 public override IOperation Apply() { 68 ItemArray<Int egerVector> neighbors = new ItemArray<IntegerVector>(SwarmSize);67 ItemArray<IntArray> neighbors = new ItemArray<IntArray>(SwarmSize); 69 68 for (int i = 0; i < SwarmSize; i++) { 70 69 var numbers = Enumerable.Range(0, SwarmSize).ToList(); … … 76 75 numbers.RemoveAt(index); 77 76 } 78 neighbors[i] = new Int egerVector(selectedNumbers.ToArray());77 neighbors[i] = new IntArray(selectedNumbers.ToArray()); 79 78 } 80 79 Neighbors = neighbors; -
branches/DataAnalysis Refactoring/HeuristicLab.Algorithms.ParticleSwarmOptimization/3.3/RingTopologyInitializer.cs
r5445 r5796 22 22 using HeuristicLab.Common; 23 23 using HeuristicLab.Core; 24 using HeuristicLab.Encodings.IntegerVectorEncoding;25 24 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 25 using HeuristicLab.Data; 26 26 27 27 namespace HeuristicLab.Algorithms.ParticleSwarmOptimization { … … 43 43 44 44 public override IOperation Apply() { 45 ItemArray<Int egerVector> neighbors = new ItemArray<IntegerVector>(SwarmSize);45 ItemArray<IntArray> neighbors = new ItemArray<IntArray>(SwarmSize); 46 46 for (int i = 0; i < SwarmSize; i++) { 47 neighbors[i] = new Int egerVector(new[] { (SwarmSize + i - 1) % SwarmSize, (i + 1) % SwarmSize });47 neighbors[i] = new IntArray(new[] { (SwarmSize + i - 1) % SwarmSize, (i + 1) % SwarmSize }); 48 48 } 49 49 Neighbors = neighbors; -
branches/DataAnalysis Refactoring/HeuristicLab.Algorithms.ParticleSwarmOptimization/3.3/TopologyInitializer.cs
r5445 r5796 23 23 using HeuristicLab.Core; 24 24 using HeuristicLab.Data; 25 using HeuristicLab.Encodings.IntegerVectorEncoding;26 25 using HeuristicLab.Operators; 26 using HeuristicLab.Optimization; 27 27 using HeuristicLab.Parameters; 28 28 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; … … 37 37 38 38 #region Parameters 39 public IScopeTreeLookupParameter<Int egerVector> NeighborsParameter {40 get { return (IScopeTreeLookupParameter<Int egerVector>)Parameters["Neighbors"]; }39 public IScopeTreeLookupParameter<IntArray> NeighborsParameter { 40 get { return (IScopeTreeLookupParameter<IntArray>)Parameters["Neighbors"]; } 41 41 } 42 42 43 public ILookupParameter<IntValue> SwarmSizeParameter { 43 44 get { return (ILookupParameter<IntValue>)Parameters["SwarmSize"]; } … … 47 48 48 49 #region Parameter Values 49 protected ItemArray<Int egerVector> Neighbors {50 protected ItemArray<IntArray> Neighbors { 50 51 get { return NeighborsParameter.ActualValue; } 51 52 set { NeighborsParameter.ActualValue = value; } … … 60 61 protected TopologyInitializer(bool deserializing) : base(deserializing) { } 61 62 protected TopologyInitializer(TopologyInitializer original, Cloner cloner) : base(original, cloner) { } 63 62 64 public TopologyInitializer() { 63 Parameters.Add(new ScopeTreeLookupParameter<Int egerVector>("Neighbors", "The list of neighbors for each particle."));65 Parameters.Add(new ScopeTreeLookupParameter<IntArray>("Neighbors", "The list of neighbors for each particle.")); 64 66 Parameters.Add(new LookupParameter<IntValue>("SwarmSize", "Number of particles in the swarm.")); 65 67 } 66 68 #endregion 67 68 69 } 69 70 -
branches/DataAnalysis Refactoring/HeuristicLab.Algorithms.ParticleSwarmOptimization/3.3/VonNeumannTopologyInitializer.cs
r5445 r5796 22 22 using HeuristicLab.Common; 23 23 using HeuristicLab.Core; 24 using HeuristicLab.Encodings.IntegerVectorEncoding;25 24 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 25 using HeuristicLab.Data; 26 26 27 27 namespace HeuristicLab.Algorithms.ParticleSwarmOptimization { … … 44 44 45 45 public override IOperation Apply() { 46 ItemArray<Int egerVector> neighbors = new ItemArray<IntegerVector>(SwarmSize);46 ItemArray<IntArray> neighbors = new ItemArray<IntArray>(SwarmSize); 47 47 for (int i = 0; i < SwarmSize; i++) { 48 neighbors[i] = new Int egerVector(new[] {48 neighbors[i] = new IntArray(new[] { 49 49 (SwarmSize + i-2) % SwarmSize, 50 50 (SwarmSize + i-1) % SwarmSize,
Note: See TracChangeset
for help on using the changeset viewer.