Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2936_GQAPIntegration/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms/3.3/GRASP/GRASPContext.cs @ 16749

Last change on this file since 16749 was 16712, checked in by gkronber, 6 years ago

#2936: adapted branch to new persistence (works with HL trunk r16711)

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.Linq;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Parameters;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27using HEAL.Attic;
28
29namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms {
30  [Item("GRASP+PR (GQAP) Context", "Context for GRASP+PR (GQAP).")]
31  [StorableType("40C0A979-FFDB-43E0-89A0-6553BD3EB9DB")]
32  public sealed class GRASPContext : 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    public void SortPopulation() {
49      Scope.SubScopes.Replace(Scope.SubScopes.OfType<ISingleObjectiveSolutionScope<GQAPSolution>>().OrderBy(x => Problem.Maximization ? -x.Fitness : x.Fitness).ToList());
50    }
51   
52    [StorableConstructor]
53    private GRASPContext(StorableConstructorFlag _) : base(_) { }
54    private GRASPContext(GRASPContext original, Cloner cloner)
55      : base(original, cloner) {
56      problem = cloner.Clone(original.problem);
57      bestSolution = cloner.Clone(original.bestSolution);
58    }
59    public GRASPContext() : this("GRASP+PR (GQAP) Context") { }
60    public GRASPContext(string name) : base(name) {
61      Parameters.Add(problem = new ValueParameter<GQAP>("Problem"));
62      Parameters.Add(bestSolution = new ValueParameter<GQAPSolution>("BestFoundSolution"));
63    }
64
65    public override IDeepCloneable Clone(Cloner cloner) {
66      return new GRASPContext(this, cloner);
67    }
68
69    public ISingleObjectiveSolutionScope<GQAPSolution> ToScope(GQAPSolution code, double fitness = double.NaN) {
70      var name = Problem.Encoding.Name;
71      var scope = new SingleObjectiveSolutionScope<GQAPSolution>(code,
72        name + "Solution", fitness, Problem.Evaluator.QualityParameter.ActualName) {
73        Parent = Scope
74      };
75      scope.Variables.Add(new Variable(name, code.Assignment));
76      scope.Variables.Add(new Variable("Evaluation", code.Evaluation));
77      return scope;
78    }
79  }
80}
Note: See TracBrowser for help on using the repository browser.