1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2016 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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Optimization;
|
---|
28 | using HeuristicLab.Optimization.Algorithms.SingleObjective;
|
---|
29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Algorithms.GeneticAlgorithm {
|
---|
32 | [Item("Evolutionary Algorithm Context", "", ExcludeGenericTypeInfo = true)]
|
---|
33 | [StorableClass]
|
---|
34 | public class EvolutionaryAlgorithmContext<TProblem, TEncoding, TSolution> : HeuristicAlgorithmContext<TProblem, TEncoding, TSolution>,
|
---|
35 | ISingleObjectivePopulationContext<TSolution>, ISingleObjectiveMatingpoolContext<TSolution>, ISingleObjectiveMatingContext<TSolution>,
|
---|
36 | ISingleObjectiveSolutionContext<TSolution>, IIterationsContext
|
---|
37 | where TProblem : class, ISingleObjectiveProblem<TEncoding, TSolution>, ISingleObjectiveProblemDefinition<TEncoding, TSolution>
|
---|
38 | where TEncoding : class, IEncoding<TSolution>
|
---|
39 | where TSolution : class, ISolution {
|
---|
40 |
|
---|
41 | [StorableConstructor]
|
---|
42 | protected EvolutionaryAlgorithmContext(bool deserializing) : base(deserializing) { }
|
---|
43 |
|
---|
44 | protected EvolutionaryAlgorithmContext(EvolutionaryAlgorithmContext<TProblem, TEncoding, TSolution> original, Cloner cloner)
|
---|
45 | : base(original, cloner) {
|
---|
46 | matingPool = original.matingPool != null ? original.matingPool.Select(cloner.Clone).ToList() : null;
|
---|
47 | parents = original.parents != null ? Tuple.Create(cloner.Clone(original.parents.Item1), cloner.Clone(original.parents.Item2)) : null;
|
---|
48 | child = original.child != null ? cloner.Clone(child) : null;
|
---|
49 | }
|
---|
50 | public EvolutionaryAlgorithmContext() {
|
---|
51 | }
|
---|
52 |
|
---|
53 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
54 | return new EvolutionaryAlgorithmContext<TProblem, TEncoding, TSolution>(this, cloner);
|
---|
55 | }
|
---|
56 |
|
---|
57 | IEnumerable<IScope> IPopulationContext.Population {
|
---|
58 | get { return Scope.SubScopes; }
|
---|
59 | }
|
---|
60 |
|
---|
61 | IEnumerable<ISolutionScope<TSolution>> IPopulationContext<TSolution>.Population {
|
---|
62 | get { return Scope.SubScopes.OfType<ISolutionScope<TSolution>>(); }
|
---|
63 | }
|
---|
64 |
|
---|
65 | public IEnumerable<ISingleObjectiveSolutionScope<TSolution>> Population {
|
---|
66 | get { return Scope.SubScopes.OfType<ISingleObjectiveSolutionScope<TSolution>>(); }
|
---|
67 | }
|
---|
68 |
|
---|
69 | [Storable]
|
---|
70 | private IEnumerable<ISingleObjectiveSolutionScope<TSolution>> matingPool;
|
---|
71 | public IEnumerable<ISingleObjectiveSolutionScope<TSolution>> MatingPool {
|
---|
72 | get { return matingPool; }
|
---|
73 | set { matingPool = value; }
|
---|
74 | }
|
---|
75 | IEnumerable<ISolutionScope<TSolution>> IMatingpoolContext<TSolution>.MatingPool {
|
---|
76 | get { return matingPool; }
|
---|
77 | set { matingPool = value.OfType<ISingleObjectiveSolutionScope<TSolution>>(); }
|
---|
78 | }
|
---|
79 |
|
---|
80 | [Storable]
|
---|
81 | private Tuple<ISingleObjectiveSolutionScope<TSolution>, ISingleObjectiveSolutionScope<TSolution>> parents;
|
---|
82 | public Tuple<ISingleObjectiveSolutionScope<TSolution>, ISingleObjectiveSolutionScope<TSolution>> Parents {
|
---|
83 | get { return parents; }
|
---|
84 | set { parents = value; }
|
---|
85 | }
|
---|
86 | Tuple<ISolutionScope<TSolution>, ISolutionScope<TSolution>> IMatingContext<TSolution>.Parents {
|
---|
87 | get { return Tuple.Create((ISolutionScope<TSolution>)Parents.Item1, (ISolutionScope<TSolution>)Parents.Item2); }
|
---|
88 | }
|
---|
89 |
|
---|
90 | [Storable]
|
---|
91 | private ISingleObjectiveSolutionScope<TSolution> child;
|
---|
92 |
|
---|
93 |
|
---|
94 | public ISingleObjectiveSolutionScope<TSolution> Child {
|
---|
95 | get { return child; }
|
---|
96 | set { child = value; }
|
---|
97 | }
|
---|
98 | ISolutionScope<TSolution> IMatingContext<TSolution>.Child {
|
---|
99 | get { return Child; }
|
---|
100 | }
|
---|
101 | public ISingleObjectiveSolutionScope<TSolution> Solution {
|
---|
102 | get { return child; }
|
---|
103 | }
|
---|
104 | ISolutionScope<TSolution> ISolutionContext<TSolution>.Solution { get { return child; } }
|
---|
105 | IScope ISolutionContext.Solution { get { return child; } }
|
---|
106 |
|
---|
107 | [Storable]
|
---|
108 | private int iterations;
|
---|
109 | public int Iterations {
|
---|
110 | get { return iterations; }
|
---|
111 | set { iterations = value; }
|
---|
112 | }
|
---|
113 | }
|
---|
114 | }
|
---|