Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.2/sources/HeuristicLab.Algorithms.ParticleSwarmOptimization/3.3/ParticleSwarmOptimization.cs @ 3580

Last change on this file since 3580 was 3415, checked in by mkofler, 14 years ago

Worked on particle swarm optimization algorithm #852 (WIP)

File size: 7.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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 System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.RealVectorEncoding;
29using HeuristicLab.Optimization;
30using HeuristicLab.Operators;
31using HeuristicLab.Optimization.Operators;
32using HeuristicLab.Parameters;
33using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
34using HeuristicLab.PluginInfrastructure;
35using HeuristicLab.Random;
36
37namespace HeuristicLab.Algorithms.ParticleSwarmOptimization {
38  [Item("Particle Swarm Optimization", "A particle swarm optimization algorithm.")]
39  [Creatable("Algorithms")]
40  [StorableClass]
41  public sealed class ParticleSwarmOptimization : EngineAlgorithm {
42    #region Problem Properties
43    public override Type ProblemType {
44      get { return typeof(ISingleObjectiveProblem); }
45    }
46    public new ISingleObjectiveProblem Problem {
47      get { return (ISingleObjectiveProblem)base.Problem; }
48      set { base.Problem = value; }
49    }
50    public IRealVectorEncoder Encoder {
51      get { return EncoderParameter.Value; }
52      set { EncoderParameter.Value = value; }
53    }
54    #endregion
55
56    #region Parameter Properties
57    private ValueParameter<IntValue> SeedParameter {
58      get { return (ValueParameter<IntValue>)Parameters["Seed"]; }
59    }
60    private ValueParameter<BoolValue> SetSeedRandomlyParameter {
61      get { return (ValueParameter<BoolValue>)Parameters["SetSeedRandomly"]; }
62    }
63    private ValueParameter<IntValue> SwarmSizeParameter {
64      get { return (ValueParameter<IntValue>)Parameters["SwarmSize"]; }
65    }
66    private ValueParameter<IntValue> MaxIterationsParameter {
67      get { return (ValueParameter<IntValue>)Parameters["MaxIterations"]; }
68    }
69    private OptionalConstrainedValueParameter<IRealVectorEncoder> EncoderParameter {
70      get { return (OptionalConstrainedValueParameter<IRealVectorEncoder>)Parameters["Encoder"]; }
71    }
72    #endregion
73
74    #region Properties
75    [StorableAttribute]
76    private ParticleSwarmOptimizationMainLoop mainLoop; // Check this !
77    private ParticleSwarmOptimizationMainLoop MainLoop {
78      get { return mainLoop; }
79    }
80    #endregion
81
82    public ParticleSwarmOptimization()
83      : base() {
84      Parameters.Add(new ValueParameter<IntValue>("Seed", "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
85      Parameters.Add(new ValueParameter<BoolValue>("SetSeedRandomly", "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
86      Parameters.Add(new ValueParameter<IntValue>("SwarmSize", "Size of the particle swarm.", new IntValue(1)));
87      Parameters.Add(new ValueParameter<IntValue>("MaxIterations", "Maximal number of iterations.", new IntValue(1000)));
88      Parameters.Add(new OptionalConstrainedValueParameter<IRealVectorEncoder>("Encoder", "The operator used to encode solutions as position vector."));
89      //Parameters.Add(new ConstrainedValueParameter<IManipulator>("Mutator", "The operator used to mutate solutions."));
90      RandomCreator randomCreator = new RandomCreator();
91      SolutionsCreator solutionsCreator = new SolutionsCreator();
92      UniformSubScopesProcessor uniformSubScopesProcessor = new UniformSubScopesProcessor();
93      VariableCreator variableCreator = new VariableCreator();
94      variableCreator.CollectedValues.Add(new ValueParameter<RealVector>("Velocity", new RealVector()));
95      //solutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreator.Name;
96      //mainLoop = new ParticleSwarmOptimizationMainLoop();
97      OperatorGraph.InitialOperator = randomCreator;
98
99      randomCreator.RandomParameter.ActualName = "Random";
100      randomCreator.SeedParameter.ActualName = SeedParameter.Name;
101      randomCreator.SeedParameter.Value = null;
102      randomCreator.SetSeedRandomlyParameter.ActualName = SetSeedRandomlyParameter.Name;
103      randomCreator.SetSeedRandomlyParameter.Value = null;
104      randomCreator.Successor = solutionsCreator;
105
106      solutionsCreator.NumberOfSolutionsParameter.ActualName = SwarmSizeParameter.Name;
107      solutionsCreator.Successor = variableCreator;
108      variableCreator.Successor = uniformSubScopesProcessor;
109     
110
111      //evaluator.Successor = mainLoop;
112      //mainLoop.EncoderParameter.ActualName = EncoderParameter.Name;
113
114      Initialize();
115    }
116
117    [StorableHook(HookType.AfterDeserialization)]
118    private void Initialize() {
119      EncoderParameter.ValueChanged += new EventHandler(EncoderParameter_ValueChanged);
120    }
121
122    [StorableConstructor]
123    private ParticleSwarmOptimization(bool deserializing) : base(deserializing) { }
124
125    public override IDeepCloneable Clone(Cloner cloner) {
126      ParticleSwarmOptimization clone = (ParticleSwarmOptimization)base.Clone(cloner);
127      clone.Initialize();
128      return clone;
129    }
130
131    public override void Prepare() {
132      if (Problem != null) base.Prepare();
133    }
134
135    #region Events
136    protected override void OnProblemChanged() {
137      //ParameterizeStochasticOperator(Problem.SolutionCreator);
138      //ParameterizeStochasticOperator(Problem.Evaluator);
139      //ParameterizeStochasticOperator(Problem.Visualizer);
140      //foreach (IOperator op in Problem.Operators) ParameterizeStochasticOperator(op);
141      //ParameterizeSolutionsCreator();
142      //ParameterizeMainLoop();
143      UpdateEncoders();
144      //UpdateMutators();
145      //Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
146      base.OnProblemChanged();
147    }
148
149    private void EncoderParameter_ValueChanged(object sender, EventArgs e) {
150      ((UniformSubScopesProcessor)((SolutionsCreator)((RandomCreator)OperatorGraph.InitialOperator).Successor).Successor).Operator = EncoderParameter.Value;
151    }
152    #endregion
153
154    #region Helpers
155
156    private void UpdateEncoders() {
157      IRealVectorEncoder oldEncoder = EncoderParameter.Value;
158      EncoderParameter.ValidValues.Clear();
159      foreach (IRealVectorEncoder encoder in Problem.Operators.OfType<IRealVectorEncoder>().OrderBy(x => x.Name)) {
160        EncoderParameter.ValidValues.Add(encoder);
161        encoder.RealVectorParameter.ActualName = "Position";
162      }
163      if (oldEncoder != null) {
164        IRealVectorEncoder encoder = EncoderParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldEncoder.GetType());
165        if (encoder != null) EncoderParameter.Value = encoder;
166      }
167    }
168
169    #endregion
170  }
171}
Note: See TracBrowser for help on using the repository browser.