Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Evolutionary/3.3/PopulationCreator.cs @ 2830

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

Operator architecture refactoring (#95)

  • worked on operators and SGA
  • improved performance
File size: 3.6 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 HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Operators;
26using HeuristicLab.Parameters;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Evolutionary {
30  /// <summary>
31  /// An operator which creates a new population of solutions.
32  /// </summary>
33  [Item("PopulationCreator", "An operator which creates a new population of solutions.")]
34  [EmptyStorableClass]
35  [Creatable("Test")]
36  public sealed class PopulationCreator : SingleSuccessorOperator {
37    public ValueLookupParameter<IntData> PopulationSizeParameter {
38      get { return (ValueLookupParameter<IntData>)Parameters["PopulationSize"]; }
39    }
40    public ValueLookupParameter<IOperator> SolutionCreatorParameter {
41      get { return (ValueLookupParameter<IOperator>)Parameters["SolutionCreator"]; }
42    }
43    public ValueLookupParameter<IOperator> SolutionEvaluatorParameter {
44      get { return (ValueLookupParameter<IOperator>)Parameters["SolutionEvaluator"]; }
45    }
46    private ScopeParameter CurrentScopeParameter {
47      get { return (ScopeParameter)Parameters["CurrentScope"]; }
48    }
49    public IScope CurrentScope {
50      get { return CurrentScopeParameter.ActualValue; }
51    }
52
53    public PopulationCreator()
54      : base() {
55      Parameters.Add(new ValueLookupParameter<IntData>("PopulationSize", "The number of individuals that should be created."));
56      Parameters.Add(new ValueLookupParameter<IOperator>("SolutionCreator", "The operator which is used to create new solutions."));
57      Parameters.Add(new ValueLookupParameter<IOperator>("SolutionEvaluator", "The operator which is used to evaluate new solutions."));
58      Parameters.Add(new ScopeParameter("CurrentScope", "The current scope which represents the population."));
59    }
60
61    public override IExecutionSequence Apply() {
62      int size = PopulationSizeParameter.ActualValue.Value;
63      IOperator creator = SolutionCreatorParameter.ActualValue;
64      IOperator evaluator = SolutionEvaluatorParameter.ActualValue;
65
66      if (CurrentScope.SubScopes.Count > 0) throw new InvalidOperationException("Population is not empty. PopulationCreator cannot be applied on scopes which already contain sub-scopes.");
67
68      for (int i = 0; i < size; i++)
69        CurrentScope.SubScopes.Add(new Scope(i.ToString()));
70
71      ExecutionContextCollection next = new ExecutionContextCollection();
72      for (int i = 0; i < CurrentScope.SubScopes.Count; i++) {
73        if (creator != null) next.Add(ExecutionContext.CreateContext(creator, CurrentScope.SubScopes[i]));
74        if (evaluator != null) next.Add(ExecutionContext.CreateContext(evaluator, CurrentScope.SubScopes[i]));
75      }
76      next.Add(base.Apply());
77      return next;
78    }
79  }
80}
Note: See TracBrowser for help on using the repository browser.