Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.ParticleSwarmOptimization/3.3/ParticleSwarmOptimization.cs @ 3352

Last change on this file since 3352 was 3348, checked in by mkofler, 14 years ago

Added project for particle swarm optimization algorithm #852 (WIP)

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