[15553] | 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 |
|
---|
| 22 | using System.Linq;
|
---|
[16728] | 23 | using HEAL.Attic;
|
---|
[15553] | 24 | using HeuristicLab.Common;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Parameters;
|
---|
| 27 |
|
---|
| 28 | namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms.GRASP {
|
---|
| 29 | [Item("GRASP+PR (GQAP) Context", "Context for GRASP+PR (GQAP).")]
|
---|
[16728] | 30 | [StorableType("64A7F8C0-8A38-4293-AC84-CE241EBF6F2C")]
|
---|
[15572] | 31 | public sealed class GRASPContext : PopulationContext<ISingleObjectiveSolutionScope<GQAPSolution>> {
|
---|
[15553] | 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() {
|
---|
[15562] | 48 | Scope.SubScopes.Replace(Scope.SubScopes.OfType<ISingleObjectiveSolutionScope<GQAPSolution>>().OrderBy(x => Problem.Maximization ? -x.Fitness : x.Fitness).ToList());
|
---|
[15553] | 49 | }
|
---|
| 50 |
|
---|
| 51 | [StorableConstructor]
|
---|
[16728] | 52 | private GRASPContext(StorableConstructorFlag _) : base(_) { }
|
---|
[15553] | 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 | }
|
---|