Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2457_ExpertSystem/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms/3.3/GRASP/GRASPContext.cs @ 17717

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

#1614: updated to new persistence and .NET 4.6.1

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 HEAL.Attic;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Parameters;
27
28namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms.GRASP {
29  [Item("GRASP+PR (GQAP) Context", "Context for GRASP+PR (GQAP).")]
30  [StorableType("64A7F8C0-8A38-4293-AC84-CE241EBF6F2C")]
31  public sealed class GRASPContext : PopulationContext<ISingleObjectiveSolutionScope<GQAPSolution>> {
32   
33    [Storable]
34    private IValueParameter<GQAP> problem;
35    public GQAP Problem {
36      get { return problem.Value; }
37      set { problem.Value = value; }
38    }
39
40    [Storable]
41    private IValueParameter<GQAPSolution> bestSolution;
42    public GQAPSolution BestSolution {
43      get { return bestSolution.Value; }
44      set { bestSolution.Value = value; }
45    }
46   
47    public void SortPopulation() {
48      Scope.SubScopes.Replace(Scope.SubScopes.OfType<ISingleObjectiveSolutionScope<GQAPSolution>>().OrderBy(x => Problem.Maximization ? -x.Fitness : x.Fitness).ToList());
49    }
50   
51    [StorableConstructor]
52    private GRASPContext(StorableConstructorFlag _) : base(_) { }
53    private GRASPContext(GRASPContext original, Cloner cloner)
54      : base(original, cloner) {
55      problem = cloner.Clone(original.problem);
56      bestSolution = cloner.Clone(original.bestSolution);
57    }
58    public GRASPContext() : this("GRASP+PR (GQAP) Context") { }
59    public GRASPContext(string name) : base(name) {
60      Parameters.Add(problem = new ValueParameter<GQAP>("Problem"));
61      Parameters.Add(bestSolution = new ValueParameter<GQAPSolution>("BestFoundSolution"));
62    }
63
64    public override IDeepCloneable Clone(Cloner cloner) {
65      return new GRASPContext(this, cloner);
66    }
67
68    public ISingleObjectiveSolutionScope<GQAPSolution> ToScope(GQAPSolution code, double fitness = double.NaN) {
69      var name = Problem.Encoding.Name;
70      var scope = new SingleObjectiveSolutionScope<GQAPSolution>(code,
71        name + "Solution", fitness, Problem.Evaluator.QualityParameter.ActualName) {
72        Parent = Scope
73      };
74      scope.Variables.Add(new Variable(name, code.Assignment));
75      scope.Variables.Add(new Variable("Evaluation", code.Evaluation));
76      return scope;
77    }
78  }
79}
Note: See TracBrowser for help on using the repository browser.