Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3385 was 3376, checked in by swagner, 14 years ago

Moved interfaces and classes for deep cloning from HeuristicLab.Core to HeuristicLab.Common (#975).

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