Free cookie consent management tool by TermsFeed Policy Generator

source: branches/1614_GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms/3.3/Evolutionary/OSGAContext.cs @ 17328

Last change on this file since 17328 was 16728, checked in by abeham, 6 years ago

#1614: updated to new persistence and .NET 4.6.1

File size: 4.3 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.Linq;
23using HEAL.Attic;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Parameters;
28
29namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms.Evolutionary {
30  [Item("OSGA (GQAP) Context", "Context for OSGA (GQAP).")]
31  [StorableType("65ADDF53-91A6-4C8C-9DDE-B330E87AB32B")]
32  public sealed class OSGAContext : PopulationContext<ISingleObjectiveSolutionScope<GQAPSolution>> {
33   
34    [Storable]
35    private IValueParameter<GQAP> problem;
36    public GQAP Problem {
37      get { return problem.Value; }
38      set { problem.Value = value; }
39    }
40
41    [Storable]
42    private IValueParameter<GQAPSolution> bestSolution;
43    public GQAPSolution BestSolution {
44      get { return bestSolution.Value; }
45      set { bestSolution.Value = value; }
46    }
47
48    [Storable]
49    private IValueParameter<ItemList<ISingleObjectiveSolutionScope<GQAPSolution>>> nextGeneration;
50    public ItemList<ISingleObjectiveSolutionScope<GQAPSolution>> NextGeneration {
51      get { return nextGeneration.Value; }
52      set { nextGeneration.Value = value; }
53    }
54
55    [Storable]
56    private IValueParameter<DoubleValue> selectionPressure;
57    public double SelectionPressure {
58      get { return selectionPressure.Value.Value; }
59      set { selectionPressure.Value.Value = value; }
60    }
61
62    [Storable]
63    private IValueParameter<IntValue> attempts;
64    public int Attempts {
65      get { return attempts.Value.Value; }
66      set { attempts.Value.Value = value; }
67    }
68
69    public void SortPopulation() {
70      Scope.SubScopes.Replace(Scope.SubScopes.OfType<ISingleObjectiveSolutionScope<GQAPSolution>>().OrderBy(x => Problem.Maximization ? -x.Fitness : x.Fitness).ToList());
71    }
72   
73    [StorableConstructor]
74    private OSGAContext(StorableConstructorFlag _) : base(_) { }
75    private OSGAContext(OSGAContext original, Cloner cloner)
76      : base(original, cloner) {
77      problem = cloner.Clone(original.problem);
78      bestSolution = cloner.Clone(original.bestSolution);
79      nextGeneration = cloner.Clone(original.nextGeneration);
80      selectionPressure = cloner.Clone(original.selectionPressure);
81      attempts = cloner.Clone(original.attempts);
82    }
83    public OSGAContext() : this("OSGA (GQAP) Context") { }
84    public OSGAContext(string name) : base(name) {
85      Parameters.Add(problem = new ValueParameter<GQAP>("Problem"));
86      Parameters.Add(bestSolution = new ValueParameter<GQAPSolution>("BestFoundSolution"));
87      Parameters.Add(nextGeneration = new ValueParameter<ItemList<ISingleObjectiveSolutionScope<GQAPSolution>>>("NextGeneration"));
88      Parameters.Add(selectionPressure = new ValueParameter<DoubleValue>("SelectionPressure", new DoubleValue(0)));
89      Parameters.Add(attempts = new ValueParameter<IntValue>("Attempts", new IntValue(0)));
90    }
91
92    public override IDeepCloneable Clone(Cloner cloner) {
93      return new OSGAContext(this, cloner);
94    }
95
96    public ISingleObjectiveSolutionScope<GQAPSolution> ToScope(GQAPSolution code, double fitness = double.NaN) {
97      var name = Problem.Encoding.Name;
98      var scope = new SingleObjectiveSolutionScope<GQAPSolution>(code,
99        name + "Solution", fitness, Problem.Evaluator.QualityParameter.ActualName) {
100        Parent = Scope
101      };
102      scope.Variables.Add(new Variable(name, code.Assignment));
103      scope.Variables.Add(new Variable("Evaluation", code.Evaluation));
104      return scope;
105    }
106  }
107}
Note: See TracBrowser for help on using the repository browser.