Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2645_ContextAlgorithms/HeuristicLab.Optimization/3.3/Model2/Contexts/PopulationContext.cs @ 17456

Last change on this file since 17456 was 15625, checked in by abeham, 7 years ago

#2654: implemented generic context-based genetic algorithm

File size: 3.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2017 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.Collections.Generic;
23using System.Linq;
24using System.Threading;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Optimization.Model2 {
30  [Item("Population Context", "A context for population-based algorithms.")]
31  [StorableClass]
32  public class PopulationContext<TSolutionScope> : StochasticContext
33    where TSolutionScope: class, IScope {
34
35    public IEnumerable<TSolutionScope> Population {
36      get { return Scope.SubScopes.OfType<TSolutionScope>(); }
37    }
38    public void AddToPopulation(TSolutionScope solScope) {
39      Scope.SubScopes.Add(solScope);
40    }
41    public void ReplaceAtPopulation(int index, TSolutionScope solScope) {
42      Scope.SubScopes[index] = solScope;
43    }
44    public void ReplacePopulation(IEnumerable<TSolutionScope> replacement) {
45      Scope.SubScopes.Replace(replacement);
46    }
47    public TSolutionScope AtPopulation(int index) {
48      return Scope.SubScopes[index] as TSolutionScope;
49    }
50    public TSolutionScope AtRandomPopulation() {
51      return Scope.SubScopes[Random.Next(PopulationCount)] as TSolutionScope;
52    }
53    public int PopulationCount {
54      get { return Scope.SubScopes.Count; }
55    }
56
57
58    [StorableConstructor]
59    protected PopulationContext(bool deserializing) : base(deserializing) { }
60    protected PopulationContext(PopulationContext<TSolutionScope> original, Cloner cloner)
61      : base(original, cloner) {
62    }
63    public PopulationContext() : base() { }
64    public PopulationContext(string name) : base(name) { }
65    public PopulationContext(string name, ParameterCollection parameters) : base(name, parameters) { }
66    public PopulationContext(string name, string description) : base(name, description) { }
67    public PopulationContext(string name, string description, ParameterCollection parameters) : base(name, description, parameters) { }
68
69    public override IDeepCloneable Clone(Cloner cloner) {
70      return new PopulationContext<TSolutionScope>(this, cloner);
71    }
72
73    public void RunOperator(IOperator op, int individual, CancellationToken cancellationToken) {
74      RunOperator(op, AtPopulation(individual), cancellationToken);
75    }
76  }
77}
Note: See TracBrowser for help on using the repository browser.